diff --git a/fre/__init__.py b/fre/__init__.py index e69de29b..4711e312 100644 --- a/fre/__init__.py +++ b/fre/__init__.py @@ -0,0 +1 @@ +__version__ = '2024.01' diff --git a/fre/pp/checkoutScript.py b/fre/pp/checkoutScript.py index 57036634..3ded8384 100644 --- a/fre/pp/checkoutScript.py +++ b/fre/pp/checkoutScript.py @@ -4,19 +4,20 @@ # Description: import os +import sys import subprocess from subprocess import PIPE from subprocess import STDOUT import re import click +from fre import fre +from click.testing import CliRunner -############################################# - -package_dir = os.path.dirname(os.path.abspath(__file__)) ############################################# -def _checkoutTemplate(experiment, platform, target, branch='main'): + +def _checkoutTemplate(experiment, platform, target, branch=None): """ Checkout the workflow template files from the repo """ @@ -30,39 +31,49 @@ def _checkoutTemplate(experiment, platform, target, branch='main'): # Set the name of the directory name = f"{experiment}__{platform}__{target}" - # Clone the repository with depth=1; check for errors - click.echo("cloning experiment into directory " + directory + "/" + name) - clonecmd = ( - f"git clone -b {branch} --single-branch --depth=1 --recursive " - f"https://github.com/NOAA-GFDL/fre-workflows.git {name}" ) - preexist_error = f"fatal: destination path '{name}' exists and is not an empty directory." - click.echo(clonecmd) - cloneproc = subprocess.run(clonecmd, shell=True, check=False, stdout=PIPE, stderr=STDOUT) - if not cloneproc.returncode == 0: - if re.search(preexist_error.encode('ASCII'),cloneproc.stdout) is not None: - argstring = f" -e {experiment} -p {platform} -t {target}" - stop_report = ( - "Error in checkoutTemplate: the workflow definition specified by -e/-p/-t already" - f" exists at the location ~/cylc-src/{name}!\n" - f"In the future, we will confirm that ~/cylc-src/{name} is usable and will check " - "whether it is up-to-date.\n" - "But for now, if you wish to proceed, you must delete the workflow definition.\n" - "To start over, try:\n" - f"\t cylc stop {name}\n" - f"\t cylc clean {name}\n" - f"\t rm -r ~/cylc-src/{name}" ) - click.echo(stop_report) - return 1 - else: - #if not identified, just print the error - click.echo(clonecmd) - click.echo(cloneproc.stdout) - return 1 + # branch and version parameters + default_tag = subprocess.run(["fre","--version"],capture_output=True, text=True).stdout.split()[2] + if default_tag == '2024.1': #hard coded solution to current discrepencies with fre --version + default_tag = '2024.01' + print('the default tag for directory ',directory,'/',name, ' is ', default_tag) + if branch != None: + print(branch) + if os.path.isdir(name): #scenario 4 + os.chdir(name) + name_path_tag=subprocess.run(["git","describe","--tags"],capture_output=True, text=True).stdout.split('*') + name_path_branch=subprocess.run(["git","branch"],capture_output=True, text=True).stdout + name_path_branch = name_path_branch.split('*')[1].split()[0] + os.chdir(directory) + if default_tag not in name_path_tag and name_path_branch != branch: + stop_report = f"Tag and branch of prexisting directory {directory}/{name} does not match fre --version or branch requested" + sys.exit(stop_report) + return 1 + print('scenario 4: directory exists, and branch requested matches branch in use') + + else: #scenario 2 + clone_output = subprocess.run(['git', 'clone','--recursive', f'--branch={branch}', 'https://github.com/NOAA-GFDL/fre-workflows.git', f'{name}'], capture_output=True, text=True) + print('scenario 2: output of fre pp checkouts git clone command is as follows:',clone_output) + else: + + + if os.path.isdir(name): #scenario 3 + os.chdir(name) + name_path_tag=subprocess.run(["git","describe","--tags"],capture_output=True, text=True).stdout.split()[0] + os.chdir(directory) + if not default_tag in name_path_tag: + stop_report = f"Tag of prexisting directory {diretory}/{name} does not match fre --version" + sys.exit(stop_report) + return 1 + print('scenario 3: directory exists, and its branch matches default tag') + + else: #scenario 1 + clone_output = subprocess.run(['git', 'clone', '--recursive', '-b',f'{default_tag}', 'https://github.com/NOAA-GFDL/fre-workflows.git', f'{name}'], capture_output=True, text=True) + print('scenario 1: output of fre pp checkouts git clone command is as follows:',clone_output) ############################################# @click.command() -def checkoutTemplate(experiment, platform, target, branch="main"): +def checkoutTemplate(experiment, platform, target, branch = None): ''' Wrapper script for calling checkoutTemplate - allows the decorated version of the function to be separate from the undecorated version