From a89b55936aff0bdd9f69414fbbe16bacf50954cf Mon Sep 17 00:00:00 2001 From: Eduardo Schettino Date: Thu, 1 Apr 2010 23:22:05 +0800 Subject: [PATCH] sub-command "clean" option "clean-dep" to follow dependencies (#444247) --HG-- extra : convert_revision : eduardo%40eduardo-laptop-20100401152205-wa3lklyc79iqly0h --- CHANGES | 2 +- TODO.txt | 1 - doit/cmds.py | 30 ++++++++++++++++++++++-------- doit/doit_cmd.py | 13 ++++++++++--- doit/task.py | 6 +++++- tests/test_cmds.py | 30 ++++++++++++++++++++---------- tests/test_task.py | 2 +- 7 files changed, 59 insertions(+), 25 deletions(-) diff --git a/CHANGES b/CHANGES index fd36fcbd..d98a2b7a 100644 --- a/CHANGES +++ b/CHANGES @@ -6,7 +6,7 @@ Changes 0.7.0 ======= -- +- sub-command "clean" option "clean-dep" to follow dependencies (#444247) - task dependency "False" means never up-to-date, "None" ignored - sub-command "list" by default do not show tasks starting with an underscore, added option (-p/--private) - new sub-coomand "auto" diff --git a/TODO.txt b/TODO.txt index 16d54cf7..cd16f96c 100644 --- a/TODO.txt +++ b/TODO.txt @@ -2,7 +2,6 @@ ---------- . 524387 configure options on dodo file -. 444247 command clean follow dependencies . 444243 clean and forget acts on all tasks, they should act only on default tasks . 441084 tests for cmdline diff --git a/doit/cmds.py b/doit/cmds.py index ce0d2876..b19f1fa3 100644 --- a/doit/cmds.py +++ b/doit/cmds.py @@ -44,20 +44,34 @@ def doit_run(dependencyFile, task_list, output, options=None, -def doit_clean(task_list, outstream, dryrun, clean_tasks): +def doit_clean(task_list, outstream, dryrun, clean_dep, clean_tasks): """Clean tasks @param task_list (list - L{Task}): list of all tasks from dodo file + @ivar dryrun (bool): if True clean tasks are not executed + (just print out what would be executed) @param clean_tasks (list - string): tasks bo be clean. clean all if empty list. + @param clean_dep (bool): execute clean from task_dep + """ + tasks = dict([(t.name, t) for t in task_list]) + cleaned = set() + + def clean_task(task_name): + if task_name not in cleaned: + cleaned.add(task_name) + tasks[task_name].clean(outstream, dryrun) + + # clean all tasks if none specified if not clean_tasks: - # clean all tasks - for task_ in task_list: - task_.clean(outstream, dryrun) - else: - tasks = dict([(t.name, t) for t in task_list]) - for name in clean_tasks: - tasks[name].clean(outstream, dryrun) + clean_tasks = [t.name for t in task_list] + + for name in clean_tasks: + if clean_dep: + for td in tasks[name].task_dep: + clean_task(td) + clean_task(name) + diff --git a/doit/doit_cmd.py b/doit/doit_cmd.py index d81a4577..6713ad98 100644 --- a/doit/doit_cmd.py +++ b/doit/doit_cmd.py @@ -212,7 +212,7 @@ def cmd_run(params, args): 'default': False, 'help': "print private tasks (start with '_')"} -# TODO list should support "args" as a filter. + def cmd_list(params, args): dodo_module = main.get_module(params['dodoFile'], params['cwdPath']) command_names = params['sub'].keys() @@ -236,12 +236,19 @@ def cmd_list(params, args): 'default': False, 'help': 'print actions without really executing them'} +opt_clean_cleandep = {'name': 'cleandep', + 'short': 'c', # clean + 'long': 'clean-dep', + 'type': bool, + 'default': False, + 'help': 'clean task dependencies too'} + def cmd_clean(params, args): dodo_module = main.get_module(params['dodoFile'], params['cwdPath']) command_names = params['sub'].keys() dodo_tasks = main.load_task_generators(dodo_module, command_names) return doit_clean(dodo_tasks['task_list'], sys.stdout, params['dryrun'], - args) + params['cleandep'], args) ########################################################## @@ -384,7 +391,7 @@ def cmd_main(cmd_args): subCmd['run'] = cmdparse.Command('run', run_options, cmd_run, run_doc) # clean command - clean_options = (opt_dodo, opt_cwd, opt_clean_dryrun) + clean_options = (opt_dodo, opt_cwd, opt_clean_cleandep, opt_clean_dryrun) subCmd['clean'] = cmdparse.Command('clean', clean_options, cmd_clean, clean_doc) diff --git a/doit/task.py b/doit/task.py index 1c0089ff..f3ce2528 100644 --- a/doit/task.py +++ b/doit/task.py @@ -548,7 +548,11 @@ def execute(self, out=None, err=None, verbosity=None): def clean(self, outstream, dryrun): - """Execute task's clean""" + """Execute task's clean + @ivar outstream: 'write' output into this stream + @ivar dryrun (bool): if True clean tasks are not executed + (just print out what would be executed) + """ # if clean is True remove all targets if self._remove_targets is True: files = filter(os.path.isfile, self.targets) diff --git a/tests/test_cmds.py b/tests/test_cmds.py index 38068dce..b764b90f 100644 --- a/tests/test_cmds.py +++ b/tests/test_cmds.py @@ -218,25 +218,35 @@ class TestCmdClean(object): def pytest_funcarg__tasks(self, request): def create_tasks(): - self.count = 0 - self.tasks = [Task("t1", None, clean=[(self.increment,)]), - Task("t2", None, clean=[(self.increment,)]),] + self.cleaned = [] + def myclean(name): + self.cleaned.append(name) + self.tasks = [Task("t1", None, dependencies=[':t2'], + clean=[(myclean,('t1',))]), + Task("t2", None, clean=[(myclean,('t2',))]),] return request.cached_setup( setup=create_tasks, scope="function") - def increment(self): - self.count += 1 - return True - def test_clean_all(self, tasks): output = StringIO.StringIO() - cmds.doit_clean(self.tasks, output, False, []) - assert 2 == self.count + cmds.doit_clean(self.tasks, output, False, False, []) + assert ['t1','t2'] == self.cleaned def test_clean_selected(self, tasks): output = StringIO.StringIO() - cmds.doit_clean(self.tasks, output, False, ['t2']) + cmds.doit_clean(self.tasks, output, False, False, ['t2']) + assert ['t2'] == self.cleaned + + def test_clean_taskdep(self, tasks): + output = StringIO.StringIO() + cmds.doit_clean(self.tasks, output, False, True, ['t1']) + assert ['t2', 't1'] == self.cleaned + + def test_clean_taskdep_once(self, tasks): + output = StringIO.StringIO() + cmds.doit_clean(self.tasks, output, False, True, ['t1', 't2']) + assert ['t2', 't1'] == self.cleaned class TestCmdIgnore(object): diff --git a/tests/test_task.py b/tests/test_task.py index b1be962f..8b31055d 100644 --- a/tests/test_task.py +++ b/tests/test_task.py @@ -362,7 +362,7 @@ def test_ruOnce_or_fileDependency(self): def test_title(self): t = task.Task("MyName",["MyAction"]) - assert "MyName" == t.name, t.name + assert "MyName" == t.title() def test_custom_title(self):