From b97c83f6a608cd50834b94a67c36e8610d294974 Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Mon, 14 Nov 2016 06:09:41 -0600 Subject: [PATCH] Add tasks_from_directory function --- dotfiles/lib/python/imalison.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/dotfiles/lib/python/imalison.py b/dotfiles/lib/python/imalison.py index 1ca9ab0e..3ea633e8 100644 --- a/dotfiles/lib/python/imalison.py +++ b/dotfiles/lib/python/imalison.py @@ -38,3 +38,27 @@ def build_task_factory(ns): def namespace_and_factory(): ns = Collection() return ns, build_task_factory(ns) + + +def extension_checker(extension): + extension_suffix = ".{}".format(extension) + def ends_with(string): + return string.endswith(extension_suffix) + return ends_with + + +def tasks_from_directory(directory_path, file_predicate=extension_checker("sh")): + ns, make_task = namespace_and_factory() + + def task_from_file(filepath): + @make_task() + def run_script(ctx): + ctx.run(filepath) + return run_script + + filepaths = filter(os.path.isfile, + [os.path.join(directory_path, filename) + for filename in os.listdir(directory_path)]) + + map(task_from_file, filepaths) + return ns