Skip to content

Commit

Permalink
finaly code setup is working!!
Browse files Browse the repository at this point in the history
  • Loading branch information
unkcpz committed Aug 13, 2024
1 parent 41111bd commit f4c248d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ RUN --mount=from=qe_conda_env,source=${QE_DIR},target=${QE_DIR} \
bash /usr/local/bin/before-notebook.d/40_prepare-aiida.sh && \
bash /usr/local/bin/before-notebook.d/41_setup-hq-computer.sh && \
python -m aiidalab_qe install-qe --computer local-hq && \
python -m aiidalab_qe install-pseudos --source ${PSEUDO_FOLDER} && \
# python -m aiidalab_qe install-pseudos --source ${PSEUDO_FOLDER} && \
verdi daemon stop && \
mamba run -n aiida-core-services pg_ctl stop && \
cd /home/${NB_USER} && tar -cf /opt/conda/home.tar .
Expand Down
6 changes: 3 additions & 3 deletions src/aiidalab_qe/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ def cli():
def install_qe(force, profile, computer):
load_profile(profile)
try:
for msg in install_and_setup_qe_codes(target_computer=computer, force=force):
for msg in install_and_setup_qe_codes(computer=computer, force=force):
click.echo(msg)
assert codes_are_setup()
assert codes_are_setup(computer=computer)
click.secho("Codes are setup!", fg="green")
except Exception as error:
raise click.ClickException(f"Failed to set up QE failed: {error}") from error
raise click.ClickException(f"Failed to set up QE: {error}") from error


@cli.command()
Expand Down
36 changes: 18 additions & 18 deletions src/aiidalab_qe/common/setup_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ def install_qe():
)


def _code_is_setup(name):
def _code_is_setup(name, computer):
try:
load_code(f"{name}-{QE_VERSION}@localhost")
load_code(f"{name}-{QE_VERSION}@{computer}")
except NotExistent:
return False
else:
return True


def codes_are_setup():
return all(_code_is_setup(code_name) for code_name in CODE_NAMES)
def codes_are_setup(computer):
return all(_code_is_setup(code_name, computer) for code_name in CODE_NAMES)


def _generate_header_to_setup_code():
Expand All @@ -106,7 +106,7 @@ def _generate_header_to_setup_code():
return header_code


def _generate_string_to_setup_code(code_name, computer="localhost"):
def _generate_string_to_setup_code(code_name, computer):
"""Generate the Python string to setup an AiiDA code for a given computer.
Tries to load an existing code and if not existent,
Expand All @@ -118,7 +118,7 @@ def _generate_string_to_setup_code(code_name, computer="localhost"):
description = f"{code_name}.x ({QE_VERSION}) setup by AiiDAlab."
filepath_executable = get_qe_env().joinpath("bin", f"{code_name}.x")
default_calc_job_plugin = f"quantumespresso.{code_name}"
prepend_text = f'eval "$(conda shell.posix hook)"\\nconda activate {get_qe_env()}\\nexport OMP_NUM_THREADS=1'
prepend_text = f'eval "$(conda shell.posix hook)"\\nconda acitvate {get_qe_env()}\\nexport OMP_NUM_THREADS=1'
python_code = """
computer = load_computer('{}')
code = InstalledCode(computer=computer,
Expand All @@ -144,22 +144,22 @@ def _generate_string_to_setup_code(code_name, computer="localhost"):
return ""


def setup_codes():
def setup_codes(computer):
python_code = _generate_header_to_setup_code()
for code_name in CODE_NAMES:
python_code += _generate_string_to_setup_code(code_name)
python_code += _generate_string_to_setup_code(code_name, computer)
try:
subprocess.run(["python", "-c", python_code], capture_output=True, check=True)
except subprocess.CalledProcessError as error:
raise RuntimeError(f"Failed to setup codes: {error}") from None
except subprocess.CalledProcessError as err:
raise RuntimeError(f"Failed to setup codes, exit_code={err.returncode}, {err.stderr}") from None


def install_and_setup(target_computer, force=False):
def install_and_setup(computer="localhost", force=False):
"""Install Quantum ESPRESSO and the corresponding AiiDA codes.
Args:
force: Ignore previously failed attempts and install anyways.
target_computer: computer label in AiiDA where the code is setup for
computer: computer label in AiiDA where the code is setup for
"""
# Check for "do not install file" and skip actual check. The purpose of
# this file is to not re-try this process on every app start in case that
Expand All @@ -169,7 +169,7 @@ def install_and_setup(target_computer, force=False):
raise RuntimeError("Installation failed in previous attempt.")

yield from _install()
yield from _setup(target_computer)
yield from _setup(computer)


def _install():
Expand Down Expand Up @@ -219,14 +219,14 @@ def _setup(computer):
# present (`which conda`). If that is not the case then we assume
# that this is a custom user environment in which case we also take
# no further action.
if codes_are_setup():
if codes_are_setup(computer=computer):
return # Already setup

# After installing QE, we install the corresponding
# AiiDA codes:
python_code = _generate_header_to_setup_code()
for code_name in CODE_NAMES:
if not _code_is_setup(code_name):
if not _code_is_setup(code_name, computer=computer):
yield f"Preparing setup script for ({code_name}) on ({computer})..."
code_string = _generate_string_to_setup_code(code_name, computer)
python_code += code_string
Expand All @@ -235,14 +235,14 @@ def _setup(computer):
subprocess.run(
["python", "-c", python_code], capture_output=True, check=True
)
except subprocess.CalledProcessError as error:
raise RuntimeError(f"Failed to setup codes: {error}") from None
except subprocess.CalledProcessError as err:
raise RuntimeError(f"Failed to setup codes, exit_code={err.returncode}, {err.stderr}") from None

except Timeout:
# Assume that the installation was triggered by a different process.
yield "Installation was already started, waiting for it to finish..."
with FileLock(FN_SETUP_LOCKFILE, timeout=120):
if not codes_are_setup():
if not codes_are_setup(computer=computer):
raise RuntimeError(
"Installation process did not finish in the expected time."
) from None
Expand Down

0 comments on commit f4c248d

Please sign in to comment.