Is there a pie equivalent for Python?

I have earned a lot of "make for Python" projects, but I cannot find anything with the simplicity of a cake file . I am looking for a Python equivalent that will allow me:

  • Save build commands in one file in the project root directory
  • Define each task as a simple function with a description that will be automatically displayed when the "make" file is run without arguments
  • Import My Python Modules

I present something like this:

from pymake import task, main @task('reset_tables', 'Drop and recreate all MySQL tables') def reset_tables(): # ... @task('build_stylus', 'Build the stylus files to public/css/*') def build_stylus(): from myproject import stylus_builder # ... @task('build_cscript', 'Build the coffee-script files to public/js/*') def build_cscript(): # ... @task('build', 'Build everything buildable') def build(): build_cscript() build_stylus() # etc... # Function that parses command line args etc... main() 

I searched and searched, but found nothing. If this does not exist, I will do it myself and will probably answer this question.

Thank you for your help!

+7
source share
4 answers

It is not so difficult to build a simple solution:

 import sys tasks = {} def task (f): tasks[f.__name__] = f return f def showHelp (): print('Available tasks:') for name, task in tasks.items(): print(' {0}: {1}'.format(name, task.__doc__)) def main (): if len(sys.argv) < 2 or sys.argv[1] not in tasks: showHelp() return print('Executing task {0}.'.format(sys.argv[1])) tasks[sys.argv[1]]() 

And then a small sample:

 from pymake import task, main @task def print_foo(): '''Prints foo''' print('foo') @task def print_hello_world(): '''Prints hello world''' print('Hello World!') @task def print_both(): '''Prints both''' print_foo() print_hello_world() if __name__ == '__main__': main() 

And what it will look like when using:

  >. \ test.py
 Available tasks:
   print_hello_world: Prints hello world
   print_foo: Prints foo
   print_both: Prints both
 >. \ test.py print_hello_world
 Executing task print_hello_world.
 Hello World!
+5
source

Have you studied the fabric ?

To implement your example using it, you just need to add it to a file called fabfile.py :

 def reset_tables(): ''' Drop and recreate all MySQL tables ''' # ... def build_stylus(): ''' Build the stylus files to public/css/ ''' from myproject import stylus_builder # ... def build_cscript(): ''' Build the coffee-script files to public/js/* ''' # ... def build(): ''' Build everything buildable ''' build_cscript() build_stylus() 

Then you just need to run fab build to build. And you can run fab -l to see the available commands along with their descriptions.

Guess it's also worth mentioning that the fabric provides some other features that you may (or may not) find useful. Among other things, he received some functions that help deploy files to remote servers and some others that allow you to run remote commands through ssh. Since it looks like you are developing a web project, this may be useful for creating deployment scripts or the like.

+4
source

Funny, there is a Python creation tool called Cake that uses almost the same syntax as you. See here .

+2
source

I would just create a standard Makefile, and not find something specific to the language. In several of my projects, make db , make test , etc. Compared to scripts written in Python, but can also be easily accessed in any language whose scripts are executed from the command line.

0
source

All Articles