0

I have a fabric script called fwp.py that I run without calling it throug fab by using:

if __name__ == '__main__':
    # imports for standalone mode only
    import sys
    import fabric.main

    fabric.main.main(fabfile_locations=[__file__])

The thing is then have to call the script by calling fwp.py. I'd like to rename it as fwp to be able to call it as fwp. But doing that would result in

Fatal error: Couldn't find any fabfiles!

Is there a way to make Python/Fabric import this file, despite the lack of a ".py" extension?

To reiterate and clarify:

I'm not using the "fab" utility (e.g. as fab task task:parameter); just calling my script as fwp.py task task:parameter, and would like to be able to call it as fwp task task:parameter.

Update

It's not a duplicate of this question. The question is not "How to run a stand-alone fabric script?", but "How to do so, while having a script without a .py" extension.

yivi
  • 42,438
  • 18
  • 116
  • 138
  • should rename it as `fabfile.py` – itzMEonTV Feb 15 '15 at 21:27
  • No, that's not the issue. I'm running the script as an standalone process, without using running "fab task", just "fwp.py task"; would like it to be "fwp task". – yivi Feb 15 '15 at 21:27
  • "fwp.py task" works perfectly. – yivi Feb 15 '15 at 21:28
  • possible duplicate of [Stand-alone fabfile for fabric?](http://stackoverflow.com/questions/3278880/stand-alone-fabfile-for-fabric) – Thomas Orozco Feb 15 '15 at 21:44
  • No, it's not a duplicate of that at all. I'm already running an stand-alone fabric. My question is if it is possible to have the fabric script file called "script" instead of "script.py" – yivi Feb 15 '15 at 21:46

1 Answers1

1

EDIT: Original answer corrected

The fabric.main.main() function automatically adds .py to the end of supplied fabfile locations (see https://github.com/fabric/fabric/blob/master/fabric/main.py#L93). Unfortunately that function also uses Python's import machinery to load the file so it has to look like a module or package. Without reimplementing much of the fabric.main module I don't think it will be possible. You could try monkey-patching both fabric.main.find_fabfiles and fabric.main.load_fabfiles to make it work.

Origininal answer (wrong)

I can get this to work unaltered on a freshly installed fabric package. The following will execute with a filename fwp and executable permission on version 1.10.1, Python2.7. I would just try upgrading fabric.

#!/usr/bin/env python

from fabric.api import *
import fabric.main

def do():
    local('echo "Hello World"')

if __name__ == '__main__':
    fabric.main.main(fabfile_locations=[__file__])

Output:

$ ./fwp do
Hello World

Done
yivi
  • 42,438
  • 18
  • 116
  • 138
Stephen Pascoe
  • 468
  • 2
  • 11
  • Thanks. Obviously there is something wrong in my environment, since I have Fabric 1.10.1 and Python 2.7.6. If I run the script as is, it'll work. If instead I use /usr/bin/env python, it'll complain about not finding Fabric. Then again, if I go into the interpreter calling it by "/usr/bin/python", attempts to import fabric.api will fail as well... I guess I'll accept your answer as valid. Syntax is ok, problem is on my end... I'll have to keep looking. Thanks! – yivi Feb 16 '15 at 09:41
  • Out of curiosity: Is your "fwp" file inside a "package"? (e.g., a dir with `__init__.py` in it? – yivi Feb 16 '15 at 10:09
  • Answer corrected. Sorry, I had left a valid module in the same directory (`fwp.py`) -- it was dumb. – Stephen Pascoe Feb 16 '15 at 10:59
  • Thanks for your correction. I was going through fabric/main.py and reaching the same conclusion, but since it was working for you I was ready to think there was some python dark magic going on beyond my limited understanding. :P – yivi Feb 16 '15 at 11:01