Discussion:
[Fab-user] How to dynamically define tasks in different modules?
Chris Spencer
2015-11-18 05:39:38 UTC
Permalink
Is it possible to dynamically expose tasks in a namespace other than the
module in which they appear?

Say I have the file `myfabtasks.py` containing the tasks
`configure_apache`. By default, Fabric exposes this as
`myfabtasks.configure_apache`, which is a little clunky. How would I expose
it as `apache.configure` without renaming my module filename and task?

My goal is to make tasks class-based, and I'm currently able to dynamically
create tasks with arbitrary names, but they're still restricted to the
namespace of their module's filename, which causes conflicts if I have two
classes in the same file that have the same method name.

I spent some time digging through the code, specifically in main.py and the
`extract_tasks` function, but this behavior unfortunately seems to be
hardcoded. Is there any workaround?

I tried dynamically creating a module with an arbitrary name and
reassigning my tasks there, as explained at
http://stackoverflow.com/questions/3799545/dynamically-importing-python-module/3799609#3799609,
but Fab never seems to find the dynamic module.
Brandon Whaley
2015-11-18 14:08:34 UTC
Permalink
Hi Chris,

If I'm not mistaken, extract_tasks is only looking for modules in the
fabfile.py namespace and only runs once at import time. Have you tried
executing your code at the end of your fabfile so the arbitrarily named
module is in that namespace at import time?
Post by Chris Spencer
Is it possible to dynamically expose tasks in a namespace other than the
module in which they appear?
Say I have the file `myfabtasks.py` containing the tasks
`configure_apache`. By default, Fabric exposes this as
`myfabtasks.configure_apache`, which is a little clunky. How would I expose
it as `apache.configure` without renaming my module filename and task?
My goal is to make tasks class-based, and I'm currently able to
dynamically create tasks with arbitrary names, but they're still restricted
to the namespace of their module's filename, which causes conflicts if I
have two classes in the same file that have the same method name.
I spent some time digging through the code, specifically in main.py and
the `extract_tasks` function, but this behavior unfortunately seems to be
hardcoded. Is there any workaround?
I tried dynamically creating a module with an arbitrary name and
reassigning my tasks there, as explained at
http://stackoverflow.com/questions/3799545/dynamically-importing-python-module/3799609#3799609,
but Fab never seems to find the dynamic module.
_______________________________________________
Fab-user mailing list
https://lists.nongnu.org/mailman/listinfo/fab-user
Loading...