From 8a77e386f6ea0753d90f3bf4cde97483386eb622 Mon Sep 17 00:00:00 2001
From: Amir MOHAMMADI <amir.mohammadi@idiap.ch>
Date: Wed, 4 Apr 2018 14:09:23 +0200
Subject: [PATCH] Minor fixes to the conda-bootstrap script

---
 conda/conda-bootstrap.py | 91 +++++++++++++++++++++++-----------------
 1 file changed, 53 insertions(+), 38 deletions(-)

diff --git a/conda/conda-bootstrap.py b/conda/conda-bootstrap.py
index 85e6e40..a7d1668 100755
--- a/conda/conda-bootstrap.py
+++ b/conda/conda-bootstrap.py
@@ -82,11 +82,13 @@ def which(env, program):
 
   fpath, fname = os.path.split(program)
   if fpath:
-    if is_exe(program): return program
+    if is_exe(program):
+      return program
   else:
     for path in os.environ["PATH"].split(os.pathsep):
       exe_file = os.path.join(path, program)
-      if is_exe(exe_file): return exe_file
+      if is_exe(exe_file):
+        return exe_file
 
   return None
 
@@ -106,13 +108,14 @@ def get_rendered_recipe(args):
     destfile = os.path.join(d, 'meta.yaml')
     print('$ cp %s -> %s' % (orig, destfile))
     print('$ edit %s # add bob-devel to host section' % (destfile,))
-    with open(destfile, 'wt') as f: f.write(contents)
+    with open(destfile, 'wt') as f:
+      f.write(contents)
 
     cmd = [
         args.conda, 'render',
         '--variant-config-files', args.config,
         '--python', args.python, d
-        ]
+    ]
     print(('$ CONDARC=%s ' % args.condarc) + ' '.join(cmd))
     output = subprocess.check_output(cmd, env=args.env)
     print('$ rm -rf %s' % (d,))
@@ -126,7 +129,7 @@ def parse_dependencies(args):
       recipe['requirements'].get('build', []) + \
       recipe['requirements'].get('run', []) + \
       recipe.get('test', {}).get('requires', []) + \
-      ['bob.buildout', 'mr.developer', 'ipdb'] #packages required for local dev
+      ['bob.buildout', 'mr.developer', 'ipdb']  # packages required for local dev
 
 
 def get_env_directory(args):
@@ -135,7 +138,8 @@ def get_env_directory(args):
   output = subprocess.check_output(cmd, env=args.env)
   data = json.loads(output)
   retval = [k for k in data.get('envs', []) if k.endswith(os.sep + args.name)]
-  if retval: return retval[0]
+  if retval:
+    return retval[0]
   return None
 
 
@@ -143,31 +147,35 @@ def conda_create(args, packages):
 
   specs = []
   for k in packages:
-    k = ' '.join(k.split()[:2]) #remove eventual build string
+    k = ' '.join(k.split()[:2])  # remove eventual build string
     if not k.startswith('bob'):
-      k = k.split()[0] #get whatever is compatible for those
-    if any(elem in k for elem in '><|'): specs.append(k.replace(' ', ''))
-    else: specs.append(k.replace(' ', '='))
+      k = k.split()[0]  # get whatever is compatible for those
+    if any(elem in k for elem in '><|'):
+      specs.append(k.replace(' ', ''))
+    else:
+      specs.append(k.replace(' ', '='))
 
-  #if the current environment exists, delete it first
+  # if the current environment exists, delete it first
   envdir = get_env_directory(args)
   if envdir is not None:
     if args.overwrite:
       cmd = [args.conda, 'env', 'remove', '--yes', '--name', args.name]
       print(('$ CONDARC=%s ' % args.condarc) + ' '.join(cmd))
       status = subprocess.call(cmd, env=args.env)
-      if status != 0: return status
+      if status != 0:
+        return status
     else:
-      raise RuntimeError('environment `%s\' exists in `%s\' - use ' \
-          '--overwrite to overwrite' % (args.name, envdir))
+      raise RuntimeError('environment `%s\' exists in `%s\' - use '
+                         '--overwrite to overwrite' % (args.name, envdir))
 
   cmd = [args.conda, 'create', '--yes', '--name', args.name] + specs
   print(('$ CONDARC=%s ' % args.condarc) + ' '.join(cmd))
   status = subprocess.call(cmd, env=args.env)
 
-  if status != 0: return status
+  if status != 0:
+    return status
 
-  #copy the used condarc file to the just created environment
+  # copy the used condarc file to the just created environment
   envdir = get_env_directory(args)
   destrc = os.path.join(envdir, '.condarc')
   print('$ cp %s -> %s' % (args.condarc, destrc))
@@ -179,41 +187,48 @@ def main():
 
   subst = {'prog': os.path.basename(sys.argv[0])}
 
-  parser = argparse.ArgumentParser(description='Creates a conda ' \
+  parser = argparse.ArgumentParser(
+      description='Creates a conda '
       'environment with the build/run/test dependencies of a package',
       formatter_class=argparse.RawDescriptionHelpFormatter,
       epilog=__doc__ % subst)
-  parser.add_argument('name', help='name of the target environment to create')
-  parser.add_argument('recipe', help='path to the **directory** ' \
+  parser.add_argument(
+      'name', help='name of the target environment to create')
+  parser.add_argument(
+      'recipe', help='path to the **directory** '
       'containing the conda recipe [default: %(default)s]', nargs='?',
       default=os.path.join(os.path.realpath('.'), 'conda'))
 
-
-  parser.add_argument('-p', '--python', help='version of python to build the ' \
+  parser.add_argument(
+      '-p', '--python', help='version of python to build the '
       'environment for [default: %(default)s]', nargs='?',
       default=('%d.%d' % sys.version_info[:2]))
-  parser.add_argument('-c', '--conda', default=which(os.environ, 'conda'),
-      help='path leading to the conda executable to use if not available on ' \
-          '$PATH [default: %(default)s]')
-  parser.add_argument('-o', '--overwrite', action='store_true', default=False,
-      help='if set and an environment with the same name exists, ' \
-          'deletes it first before creating the new environment')
-
-  BASEDIR=os.path.realpath(os.path.dirname(sys.argv[0]))
+  parser.add_argument(
+      '-c', '--conda', default=which(os.environ, 'conda'),
+      help='path leading to the conda executable to use if not available on '
+      '$PATH [default: %(default)s]')
+  parser.add_argument(
+      '-o', '--overwrite', action='store_true', default=False,
+      help='if set and an environment with the same name exists, '
+      'deletes it first before creating the new environment')
+
+  BASEDIR = os.path.realpath(os.path.dirname(sys.argv[0]))
   default_condarc = os.path.join(BASEDIR, 'build-condarc')
   default_condarc = os.environ.get('CONDARC', default_condarc)
 
-  parser.add_argument('-r', '--condarc', default=default_condarc,
-      help='overwrites the path leading to the condarc file to use ' \
-          '[default: %(default)s]')
+  parser.add_argument(
+      '-r', '--condarc', default=default_condarc,
+      help='overwrites the path leading to the condarc file to use '
+      '[default: %(default)s]')
 
   default_variant = os.path.join(os.path.dirname(BASEDIR), 'gitlab',
-      'conda_build_config.yaml')
+                                 'conda_build_config.yaml')
 
-  parser.add_argument('-m', '--config', '--variant-config-files',
+  parser.add_argument(
+      '-m', '--config', '--variant-config-files',
       default=default_variant,
-      help='overwrites the path leading to variant configuration file to ' \
-          'use [default: %(default)s]')
+      help='overwrites the path leading to variant configuration file to '
+      'use [default: %(default)s]')
 
   # no surprises, no globals - all configuration comes from the cmdline
   args = parser.parse_args()
@@ -230,12 +245,12 @@ def main():
     raise RuntimeError("The directory %s does not exist" % args.recipe)
 
   # use this environment for all conda-related commands
-  args.env = env = copy.copy(os.environ)
+  args.env = copy.copy(os.environ)
   args.env['CONDARC'] = args.condarc
 
   deps = parse_dependencies(args)
   status = conda_create(args, deps)
-  print('$ #execute on your shell: source activate %s' % args.name)
+  print('$ #execute on your shell: conda activate %s' % args.name)
   sys.exit(status)
 
 
-- 
GitLab