示例#1
0
 def test_quote(self):
     """Test quote/unquote"""
     value = 'value with whitespace'
     txt = '--option=%s' % value
     # this looks strange, but is correct
     self.assertEqual(str(txt), '--option=value with whitespace')
     self.assertEqual(txt, shell_unquote(shell_quote(txt)))
示例#2
0
 def test_quote(self):
     """Test quote/unquote"""
     value = 'value with whitespace'
     txt = '--option=%s' % value
     # this looks strange, but is correct
     self.assertEqual(str(txt), '--option=value with whitespace')
     self.assertEqual(txt , shell_unquote(shell_quote(txt)))
示例#3
0
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(shell_quote(sys.executable))

    # add script
    env.append('"%s"' % cmd_list[0])

    return " ".join(env)
示例#4
0
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, err:
        if script is not None:
            script.close()
        raise EasyBuildError("Failed to write to %s: %s", filename, err)
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, err:
        if script is not None:
            script.close()
        raise EasyBuildError("Failed to write to %s: %s", filename, err)
示例#6
0
    def test_noshell_executable(self):
        ec, output = run("echo '(foo bar)'")
        self.assertEqual(ec, 0)
        self.assertTrue('(foo bar)' in output)

        ec, output = run(['echo', "(foo bar)"])
        self.assertEqual(ec, 0)
        self.assertTrue('(foo bar)' in output)

        # to run Python command, it's required to use the right executable (Python shell rather than default)
        python_cmd = shell_quote(sys.executable)
        ec, output = run("""%s -c 'print ("foo")'""" % python_cmd)
        self.assertEqual(ec, 0)
        self.assertTrue('foo' in output)

        ec, output = run([sys.executable, '-c', 'print ("foo")'])
        self.assertEqual(ec, 0)
        self.assertTrue('foo' in output)
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 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'))
示例#9
0
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)
    def generate_cmd_line(self, ignore=None, add_default=None):
        """Create the commandline options that would create the current self.options
            opt_name is destination

            @param ignore : regex on destination
            @param add_default : print value that are equal to default
        """
        if ignore is not None:
            self.log.debug("generate_cmd_line ignore %s" % ignore)
            ignore = re.compile(ignore)
        else:
            self.log.debug("generate_cmd_line no ignore")

        args = []
        opt_dests = self.options.__dict__.keys()
        opt_dests.sort()

        for opt_dest in opt_dests:
            opt_value = self.options.__dict__[opt_dest]
            # this is the action as parsed by the class, not the actual action set in option
            # (eg action store_or_None is shown here as store_or_None, not as callback)
            default = self.processed_options[opt_dest][self.PROCESSED_OPTIONS_PROPERTIES.index('default')]
            action = self.processed_options[opt_dest][self.PROCESSED_OPTIONS_PROPERTIES.index('action')]
            opt_name = self.processed_options[opt_dest][self.PROCESSED_OPTIONS_PROPERTIES.index('opt_name')]

            if ignore is not None and ignore.search(opt_dest):
                self.log.debug("generate_cmd_line adding %s value %s matches ignore. Not adding to args." %
                               (opt_name, opt_value))
                continue

            if opt_value == default:
                # do nothing
                # except for store_or_None and friends
                msg = ''
                if not (add_default or action in ('store_or_None',)):
                    msg = ' Not adding to args.'
                self.log.debug("generate_cmd_line adding %s value %s default found.%s" %
                               (opt_name, opt_value, msg))
                if not (add_default or action in ('store_or_None',)):
                    continue

            if opt_value is None:
                # do nothing
                self.log.debug("generate_cmd_line adding %s value %s. None found. not adding to args." %
                               (opt_name, opt_value))
                continue

            if action in ('store_or_None',):
                if opt_value == default:
                    self.log.debug("generate_cmd_line %s adding %s (value is default value %s)" %
                                   (action, opt_name, opt_value))
                    args.append("--%s" % (opt_name))
                else:
                    self.log.debug("generate_cmd_line %s adding %s non-default value %s" %
                                   (action, opt_name, opt_value))
                    args.append("--%s=%s" % (opt_name, shell_quote(opt_value)))
            elif action in ("store_true", "store_false", 'store_debuglog'):
                # not default!
                self.log.debug("generate_cmd_line adding %s value %s. store action found" %
                               (opt_name, opt_value))
                if (action in ('store_true', 'store_debuglog',)  and default == True and opt_value == False) or \
                    (action in ('store_false',) and default == False and opt_value == True):
                    if hasattr(self.parser.option_class, 'ENABLE') and hasattr(self.parser.option_class, 'DISABLE'):
                        args.append("--%s-%s" % (self.parser.option_class.DISABLE, opt_name))
                    else:
                        self.log.error(("generate_cmd_line: %s : can't set inverse of default %s with action %s "
                                        "with missing ENABLE/DISABLE in option_class") %
                                       (opt_name, default, action))
                else:
                    if opt_value == default and ((action in ('store_true', 'store_debuglog',) and default == False) \
                                                 or (action in ('store_false',) and default == True)):
                        if hasattr(self.parser.option_class, 'ENABLE') and \
                            hasattr(self.parser.option_class, 'DISABLE'):
                            args.append("--%s-%s" % (self.parser.option_class.DISABLE, opt_name))
                        else:
                            self.log.debug(("generate_cmd_line: %s : action %s can only set to inverse of default %s "
                                            "and current value is default. Not adding to args.") %
                                           (opt_name, action, default))
                    else:
                        args.append("--%s" % opt_name)
            elif action in ("extend",):
                # comma separated
                self.log.debug("generate_cmd_line adding %s value %s. extend action, return as comma-separated list" %
                               (opt_name, opt_value))

                if default is not None:
                    # remove these. if default is set, extend extends the default!
                    for def_el in default:
                        opt_value.remove(def_el)

                if len(opt_value) == 0:
                    self.log.debug('generate_cmd_line skipping.')
                    continue

                args.append("--%s=%s" % (opt_name, shell_quote(",".join(opt_value))))
            elif action in ("append",):
                # add multiple times
                self.log.debug("generate_cmd_line adding %s value %s. append action, return as multiple args" %
                               (opt_name, opt_value))
                args.extend(["--%s=%s" % (opt_name, shell_quote(v)) for v in opt_value])
            else:
                self.log.debug("generate_cmd_line adding %s value %s" % (opt_name, opt_value))
                args.append("--%s=%s" % (opt_name, shell_quote(opt_value)))

        self.log.debug("commandline args %s" % args)
        return args