Skip to content

Commit

Permalink
Issue cylc#2991 define cylc git directory via parameter for profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno P. Kinoshita committed Mar 14, 2019
1 parent c3d669e commit 99609b7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
22 changes: 16 additions & 6 deletions bin/cylc-profile-battery
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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):
Expand All @@ -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():
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 2 additions & 18 deletions lib/cylc/profiling/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 9 additions & 2 deletions lib/cylc/profiling/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 2 additions & 3 deletions lib/cylc/profiling/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=''):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 99609b7

Please sign in to comment.