From 99609b7e5e621a863558c07b50c04bcca5ad1db9 Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Wed, 13 Mar 2019 15:37:37 +1300 Subject: [PATCH] Issue #2991 define cylc git directory via parameter for profiling --- bin/cylc-profile-battery | 22 ++++++++++++++++------ lib/cylc/profiling/__init__.py | 20 ++------------------ lib/cylc/profiling/git.py | 11 +++++++++-- lib/cylc/profiling/profile.py | 5 ++--- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/bin/cylc-profile-battery b/bin/cylc-profile-battery index 22ffc2753b2..fcba3614709 100755 --- a/bin/cylc-profile-battery +++ b/bin/cylc-profile-battery @@ -38,12 +38,14 @@ import cylc.profiling as prof from cylc.profiling.analysis import (make_table, print_table, plot_results) import cylc.profiling.git as git -RUN_DOC = r"""cylc profile-battery [-e [EXPERIMENT ...]] [-v [VERSION ...]] +RUN_DOC = r"""cylc profile-battery [-e [EXPERIMENT ...]] [-v [VERSION ...]] GIT_DIRECTORY Run profiling experiments against different versions of cylc. A list of experiments can be specified after the -e flag, if not provided the experiment "complex" will be chosen. A list of versions to profile against can be specified after the -v flag, if not provided the current version will be used. +The last argument is the git directory of Cylc, and if not provided the current +directory will be used (i.e. '.'). Experiments are stored in etc/profile-experiments, user experiments can be stored in .profiling/experiments. Experiments are specified without the file @@ -109,7 +111,7 @@ def parse_args(): assert value is None value = [] for arg in parser.rargs: - if arg[0] == '-': + if arg[0] == '-' or arg[0] == '/': break value.append(arg) del parser.rargs[:len(value)] @@ -153,7 +155,7 @@ def parse_args(): parser.add_option('--test', action='store_true', default=False, help='For development purposes, run experiment without ' 'saving results and regardless of any prior runs.') - opts = parser.parse_args()[0] + opts, args = parser.parse_args() # Defaults for experiments and versions if we are not in list mode. if not (opts.ls or opts.delete): @@ -167,7 +169,10 @@ def parse_args(): if not opts.versions: opts.versions = [] - return opts + if not args: + args.append(".") + + return opts, args def get_results(): @@ -942,13 +947,18 @@ def promote(experiment_id, yes=False): def main(): """cylc profile-battery""" - opts = parse_args() + opts, args = parse_args() + + cylc_directory = os.path.abspath(args[0]) - if not prof.IS_GIT_REPO: + if not git.is_git_repo(cylc_directory): print('ERROR: profiling requires cylc to be a git repository.', file=sys.stderr) sys.exit(2) + # TODO: revisit it later and review module/objects in profiling + prof.CYLC_DIR = cylc_directory + # Promote mode. if opts.promote: promote(opts.promote, opts.yes) diff --git a/lib/cylc/profiling/__init__.py b/lib/cylc/profiling/__init__.py index 10e5143efb9..020a9164205 100644 --- a/lib/cylc/profiling/__init__.py +++ b/lib/cylc/profiling/__init__.py @@ -17,27 +17,11 @@ import os import re -from subprocess import Popen, PIPE -import sys - -from .git import is_git_repo - - -def get_cylc_directory(): - """Returns the location of cylc's working copy.""" - ver_str = Popen( - ['cylc', 'version', '--long'], - stdout=PIPE, stdin=open(os.devnull)).communicate()[0].decode() - try: - return os.path.realpath(re.search(r'\((.*)\)', ver_str).groups()[0]) - except IndexError: - sys.exit('Could not locate local git repository for cylc.') +# TODO: we mustn't have constants changing values # Ensure that the cylc directory is a git repository. -CYLC_DIR = get_cylc_directory() -os.chdir(CYLC_DIR) -IS_GIT_REPO = is_git_repo() +CYLC_DIR = None # Files and directories PROFILE_DIR_NAME = '.profiling' # Path to profiling directory. diff --git a/lib/cylc/profiling/git.py b/lib/cylc/profiling/git.py index 926cb64088f..916bedcb329 100644 --- a/lib/cylc/profiling/git.py +++ b/lib/cylc/profiling/git.py @@ -101,7 +101,14 @@ def has_changes_to_be_committed(): return False -def is_git_repo(): - """Returns true if we are currently within a git repository.""" +def is_git_repo(directory): + """Returns true if we are currently within a git repository. + + Args: + directory (str): the git directory. + Returns: + bool: whether the directory is a git repository or not. + """ + os.chdir(directory) proc = Popen(['git', 'rev-parse', '--git-dir'], stdout=PIPE, stderr=PIPE,) return proc.wait() == 0 diff --git a/lib/cylc/profiling/profile.py b/lib/cylc/profiling/profile.py index d42ce5396fe..933187d0643 100644 --- a/lib/cylc/profiling/profile.py +++ b/lib/cylc/profiling/profile.py @@ -31,6 +31,7 @@ PROFILE_FILES, SUITE_STARTUP_STRING) from .analysis import extract_results from .git import (checkout, describe, GitCheckoutError,) +from cylc import __version__ as CYLC_VERSION def cylc_env(cylc_conf_path=''): @@ -69,9 +70,7 @@ class ProfilingKilledException(SuiteFailedException): def cylc_major_version(): """Return the first character of the cylc version e.g. '7'.""" - return Popen( - ['cylc', '--version'], env=CLEAN_ENV, stdin=open(os.devnull), - stdout=PIPE).communicate()[0].decode().strip()[0] + return CYLC_VERSION.strip()[0] def register_suite(reg, sdir):