Exemplo n.º 1
0
def parse(self, in_args):
    params = DefaultUpdate()
    # add default values
    for opt in self.options:
        params.set_default(opt.name, opt.default)
        
        # parse options using getopt
    try:
        opts, args = getopt.gnu_getopt(in_args, self.get_short(),
                                   self.get_long())
    except Exception as error:
        msg = "Error parsing %s: %s (parsing options: %s)"
        raise CmdParseError(msg % (self._type, str(error), in_args))
        
    # update params with values from command line
    for opt, val in opts:
        this, inverse = self.get_option(opt)
        if this.type is bool:
            params[this.name] = not inverse
        elif this.type is list:
            params[this.name].append(val)
        else:
            try:
                params[this.name] = this.type(val)
            except ValueError as exception:
                msg = "Error parsing parameter '%s' %s.\n%s\n"
                raise CmdParseError(msg % (this.name, this.type,
                                           str(exception)))
                
    return params, args
Exemplo n.º 2
0
 def test_add_defaults(self):
     du = DefaultUpdate()
     du.add_defaults({'a': 0, 'b':1})
     du['c'] = 5
     du.add_defaults({'a':2, 'c':2})
     assert 0 == du['a']
     assert 1 == du['b']
     assert 5 == du['c']
Exemplo n.º 3
0
    def test(self):
        du = DefaultUpdate()

        du.set_default('a', 0)
        du.set_default('b', 0)

        assert 0 == du['a']
        assert 0 == du['b']

        du['b'] = 1
        du.update_defaults({'a':2, 'b':2})
        assert 2 == du['a']
        assert 1 == du['b']
Exemplo n.º 4
0
 def test_add_defaults(self):
     du = DefaultUpdate()
     du.add_defaults({'a': 0, 'b': 1})
     du['c'] = 5
     du.add_defaults({'a': 2, 'c': 2})
     assert 0 == du['a']
     assert 1 == du['b']
     assert 5 == du['c']
Exemplo n.º 5
0
 def test_opt_show_all(self, dependency1, depfile_name):
     output = StringIO()
     task = Task("tt", ["cat %(dependencies)s"],
                 file_dep=['tests/data/dependency1'])
     cmd = Strace(outstream=output)
     cmd._loader.load_tasks = mock.Mock(return_value=([task], {}))
     params = DefaultUpdate(dep_file=depfile_name,
                            show_all=True,
                            keep_trace=False,
                            backend='dbm')
     result = cmd.execute(params, ['tt'])
     assert 0 == result
     got = output.getvalue().split("\n")
     assert "cat" in got[0]
Exemplo n.º 6
0
 def test_opt_show_all(self, dependency1, depfile_name):
     output = StringIO()
     task = Task("tt", ["cat %(dependencies)s"],
                 file_dep=['tests/data/dependency1'])
     cmd = CmdFactory(Strace, outstream=output)
     cmd.loader = self.loader_for_task(task)
     params = DefaultUpdate(dep_file=depfile_name,
                            show_all=True,
                            keep_trace=False,
                            backend='dbm',
                            check_file_uptodate='md5')
     result = cmd.execute(params, ['tt'])
     assert 0 == result
     got = output.getvalue().split("\n")
     assert "cat" in got[0]
Exemplo n.º 7
0
 def test_target(self, dependency1, depfile_name):
     output = StringIO()
     task = Task("tt", ["touch %(targets)s"],
                 targets=['tests/data/dependency1'])
     cmd = Strace(outstream=output)
     cmd._loader.load_tasks = mock.Mock(return_value=([task], {}))
     params = DefaultUpdate(dep_file=depfile_name,
                            show_all=False,
                            keep_trace=False,
                            backend='dbm')
     result = cmd.execute(params, ['tt'])
     assert 0 == result
     got = output.getvalue().split("\n")
     tgt_path = os.path.abspath("tests/data/dependency1")
     assert "W %s" % tgt_path in got[0]
Exemplo n.º 8
0
 def test_dep(self, dependency1, depfile_name):
     output = StringIO()
     task = Task("tt", ["cat %(dependencies)s"],
                 file_dep=['tests/data/dependency1'])
     cmd = CmdFactory(Strace, outstream=output)
     cmd.loader.load_tasks = mock.Mock(return_value=([task], {}))
     params = DefaultUpdate(dep_file=depfile_name,
                            show_all=False,
                            keep_trace=False,
                            backend='dbm',
                            check_file_uptodate='md5')
     result = cmd.execute(params, ['tt'])
     assert 0 == result
     got = output.getvalue().split("\n")
     dep_path = os.path.abspath("tests/data/dependency1")
     assert "R %s" % dep_path in got[0]
Exemplo n.º 9
0
    def test_ignore_python_actions(self, dependency1, depfile_name):
        output = StringIO()

        def py_open():
            with open(dependency1) as ignore:
                ignore

        task = Task("tt", [py_open])
        cmd = Strace(outstream=output)
        cmd._loader.load_tasks = mock.Mock(return_value=([task], {}))
        params = DefaultUpdate(dep_file=depfile_name,
                               show_all=False,
                               keep_trace=False,
                               backend='dbm')
        result = cmd.execute(params, ['tt'])
        assert 0 == result
Exemplo n.º 10
0
 def test_target(self, dependency1, depfile_name):
     output = StringIO()
     task = Task("tt", ["touch %(targets)s"],
                 targets=['tests/data/dependency1'])
     cmd = CmdFactory(Strace, outstream=output)
     cmd.loader = self.loader_for_task(task)
     params = DefaultUpdate(dep_file=depfile_name,
                            show_all=False,
                            keep_trace=False,
                            backend='dbm',
                            check_file_uptodate='md5')
     result = cmd.execute(params, ['tt'])
     assert 0 == result
     got = output.getvalue().split("\n")
     tgt_path = os.path.abspath("tests/data/dependency1")
     assert "W %s" % tgt_path in got[0]
Exemplo n.º 11
0
    def test_ignore_python_actions(self, dependency1, depfile_name):
        output = StringIO()

        def py_open():
            with open(dependency1) as ignore:
                ignore

        task = Task("tt", [py_open])
        cmd = CmdFactory(Strace, outstream=output)
        cmd.loader = self.loader_for_task(task)
        params = DefaultUpdate(dep_file=depfile_name,
                               show_all=False,
                               keep_trace=False,
                               backend='dbm',
                               check_file_uptodate='md5')
        result = cmd.execute(params, ['tt'])
        assert 0 == result
Exemplo n.º 12
0
 def test_opt_keep_trace(self, dependency1, depfile_name):
     output = StringIO()
     task = Task("tt", ["cat %(dependencies)s"],
                 file_dep=['tests/data/dependency1'])
     cmd = CmdFactory(Strace, outstream=output)
     cmd.loader.load_tasks = mock.Mock(return_value=([task], {}))
     params = DefaultUpdate(dep_file=depfile_name,
                            show_all=True,
                            keep_trace=True,
                            backend='dbm',
                            check_file_uptodate='md5')
     result = cmd.execute(params, ['tt'])
     assert 0 == result
     got = output.getvalue().split("\n")
     assert "cat" in got[0]
     assert os.path.exists(cmd.TRACE_OUT)
     os.unlink(cmd.TRACE_OUT)
Exemplo n.º 13
0
    def test(self):
        du = DefaultUpdate()

        du.set_default('a', 0)
        du.set_default('b', 0)

        assert 0 == du['a']
        assert 0 == du['b']

        # set b with non-default value
        du['b'] = 1
        # only a is update
        du.update_defaults({'a': 2, 'b': 2})
        assert 2 == du['a']
        assert 1 == du['b']

        # default for `a` can be updated again
        du.update_defaults({'a': 3})
        assert 3 == du['a']
Exemplo n.º 14
0
    def test(self):
        du = DefaultUpdate()

        du.set_default('a', 0)
        du.set_default('b', 0)

        assert 0 == du['a']
        assert 0 == du['b']

        du['b'] = 1
        du.update_defaults({'a': 2, 'b': 2})
        assert 2 == du['a']
        assert 1 == du['b']
Exemplo n.º 15
0
    def test_run_wait(self, dependency1, target1, depfile_name):
        def ok():
            with open(target1, 'w') as fp:
                fp.write('ok')

        t1 = Task("t1", [ok], file_dep=[dependency1])
        cmd = CmdFactory(cmd_auto.Auto,
                         task_loader=FakeLoader([t1], depfile_name))

        run_wait_proc = Process(target=cmd.run_watch,
                                args=(DefaultUpdate(), []))
        run_wait_proc.start()

        # wait until task is executed
        for x in range(5):
            try:
                got = open(target1, 'r').read()
                print(got)
                if got == 'ok':
                    break
            except:
                print('busy')
            time.sleep(0.1)
        else:  # pragma: no cover
            raise Exception("target not created")

        # write on file to terminate process
        fd = open(dependency1, 'w')
        fd.write("hi" + str(time.asctime()))
        fd.close()

        run_wait_proc.join(.5)
        if run_wait_proc.is_alive():  # pragma: no cover
            # this test is very flaky so we give it one more chance...
            # write on file to terminate process
            fd = open(dependency1, 'w')
            fd.write("hi" + str(time.asctime()))
            fd.close()

            run_wait_proc.join(1)
            if run_wait_proc.is_alive():  # pragma: no cover
                run_wait_proc.terminate()
                raise Exception("process not terminated")
        assert 0 == run_wait_proc.exitcode
Exemplo n.º 16
0
    def test(self):
        du = DefaultUpdate()

        du.set_default('a', 0)
        du.set_default('b', 0)

        assert 0 == du['a']
        assert 0 == du['b']

        # set b with non-default value
        du['b'] = 1
        # only a is update
        du.update_defaults({'a':2, 'b':2})
        assert 2 == du['a']
        assert 1 == du['b']

        # default for `a` can be updated again
        du.update_defaults({'a':3})
        assert 3 == du['a']
Exemplo n.º 17
0
 def test_pickle(self):
     du = DefaultUpdate()
     du.set_default('x', 0)
     dump = pickle.dumps(du, 2)
     pickle.loads(dump)
Exemplo n.º 18
0
 def test_pickle(self):
     du = DefaultUpdate()
     du.set_default('x', 0)
     dump = pickle.dumps(du,2)
     pickle.loads(dump)