Skip to content
Snippets Groups Projects
Commit 15e436de authored by Ashwin Kumar Karnad's avatar Ashwin Kumar Karnad
Browse files

first attempt at get_root_dir

parent a8e16cc9
Branches
No related tags found
1 merge request!89Implement the init command
Pipeline #374347 passed
...@@ -1115,6 +1115,49 @@ def initialize_environment(root_dir: Path) -> None: ...@@ -1115,6 +1115,49 @@ def initialize_environment(root_dir: Path) -> None:
) )
def get_root_dir() -> Path:
"""Get the root directory of the installation.
Look for the hidden file ``.mpsd-software-root``
(defined in config_vars["init_file"])
in the current directory, or any parent directory.
If found, return the path to the root directory
of the MPSD software instance.
If not found, exit with an error message.
Returns
-------
root_dir : pathlib.Path
A Path object pointing to the root directory of the installation.
This folder contains the hidden file ``.mpsd-software-root``,
``mpsd_releases`` ( for eg ``dev-23a``) and ``mpsd-spack-cache``.
"""
# check if the root_dir is not already initialized
script_call_dir = Path.cwd()
init_file = script_call_dir / config_vars["init_file"]
if not init_file.exists():
for parent_folder in script_call_dir.parents:
init_file = parent_folder / config_vars["init_file"]
if init_file.exists():
script_call_dir = parent_folder
return script_call_dir
logging.getLogger("print").info(
"Error: Couldnt find MPSD software instance"
"in the current directory or any parent directory. \n"
f"Directory {str(script_call_dir)} is not a MPSD software instance. \n"
"Please run 'mpsd-software init' to "
"initialise the software instance here, \n"
"or switch to a directory which is already initialised.\n \n"
f"Hint: Look for the hidden file {config_vars['init_file']}"
" to check if a directory is initialised"
)
sys.exit(1)
else:
return script_call_dir
def main(): def main():
"""Execute main entry point.""" """Execute main entry point."""
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
...@@ -1200,32 +1243,41 @@ def main(): ...@@ -1200,32 +1243,41 @@ def main():
# Carry out the action # Carry out the action
args = parser.parse_args() args = parser.parse_args()
# Set up logging without file handle:
# this is used in the init action and for logging the
# get_root_dir() function
set_up_logging(args.loglevel)
# Check if the action is init
# if so, call the init function and exit
if args.action == "init":
initialize_environment(Path(os.getcwd()))
sys.exit(0)
# root dir is the place where this script is called from # root dir is the place where this script is called from
root_dir = Path(os.getcwd()) root_dir = get_root_dir()
# set up logging for all actions except init # set up logging ( with file handler) for all actions (except init)
if args.action != "init": log_file = get_installer_log_file_path(
log_file = get_installer_log_file_path(args.release, args.action, root_dir) args.release,
else: args.action,
log_file = None root_dir,
)
set_up_logging( set_up_logging(
args.loglevel, args.loglevel,
log_file, log_file,
) )
# sanity check for common mistakes in command line arguments # sanity check for common mistakes in command line arguments
if args.action != "init": if args.release.endswith("/"): # happens easily with autocompletion
if args.release.endswith("/"): # happens easily with autocompletion logging.error(
logging.error( f"You provided mpsd-release='{args.release}'. "
f"You provided mpsd-release='{args.release}'. " f"Did you mean '{args.release.removesuffix('/')}'?"
f"Did you mean '{args.release.removesuffix('/')}'?" )
) sys.exit(1)
sys.exit(1)
# Check the command and run related function # Check the command and run related function
if args.action == "init": if args.action == "remove":
initialize_environment(root_dir)
elif args.action == "remove":
remove_environment(args.release, root_dir, args.package_set) remove_environment(args.release, root_dir, args.package_set)
elif args.action == "start-new": elif args.action == "start-new":
start_new_environment(args.from_release, args.to_release, root_dir) start_new_environment(args.from_release, args.to_release, root_dir)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment