def write_changes(filename): """ Write current changes to filename and reset environment afterwards """ try: with open(filename, 'w') as script: for key in _changes: script.write('export %s=%s\n' % (key, shell_quote(_changes[key]))) except IOError as err: raise EasyBuildError("Failed to write to %s: %s", filename, err) reset_changes()
def write_changes(filename): """ Write current changes to filename and reset environment afterwards """ script = None try: script = open(filename, 'w') for key in _changes: script.write('export %s=%s\n' % (key, shell_quote(_changes[key]))) script.close() except IOError as err: if script is not None: script.close() raise EasyBuildError("Failed to write to %s: %s", filename, err) reset_changes()
def setvar(key, value, verbose=True): """ put key in the environment with value tracks added keys until write_changes has been called :param verbose: include message in dry run output for defining this environment variable """ if key in os.environ: oldval_info = "previous value: '%s'" % os.environ[key] else: oldval_info = "previously undefined" # os.putenv() is not necessary. os.environ will call this. os.environ[key] = value _changes[key] = value _log.info("Environment variable %s set to %s (%s)", key, value, oldval_info) if verbose and build_option('extended_dry_run'): quoted_value = shell_quote(value) if quoted_value[0] not in ['"', "'"]: quoted_value = '"%s"' % quoted_value dry_run_msg(" export %s=%s" % (key, quoted_value), silent=build_option('silent'))
def gen_cmdline(cmd_list, partial, shebang=True): """Create the commandline to generate simulated tabcompletion output :param cmd_list: command to execute as list of strings :param partial: the string to autocomplete (typically, partial is an element of the cmd_list) :param shebang: script has python shebang (if not, add sys.executable) """ cmdline = ' '.join([shell_quote(cmd) for cmd in cmd_list]) env = [] env.append("%s=1" % OPTCOMPLETE_ENVIRONMENT) env.append('COMP_LINE="%s"' % cmdline) env.append('COMP_WORDS="(%s)"' % cmdline) env.append("COMP_POINT=%s" % len(cmdline)) env.append("COMP_CWORD=%s" % cmd_list.index(partial)) if not shebang: env.append(sys.executable) # add script env.append('"%s"' % cmd_list[0]) return " ".join(env)