#!/usr/bin/env python # -*- coding: UTF-8 -*- """ Test executing fabric tasks wrapped on the command-line vs. programmatically. """ import sys from subprocess import PIPE, Popen from fabric.decorators import task from fabric.api import local, execute from fabric.contrib.console import confirm from fabric.main import parse_arguments @task def date(): "Print the current date." local('date') @task def date_confirmed(): "Also print the current date, but now with some interactive confirmation and long docstring." if confirm("Really?", default=False): local('date') if __name__ == "__main__": try: argv = sys.argv[1:] except IndexError: sys.exit() # check --exec option and remove it if given do_exec = '--exec' in argv if '--exec' in argv: argv.remove('--exec') if not do_exec: cmd = ['fab', '-f', __file__] + argv print('running: %s' % ' '.join(cmd)) p = Popen(cmd, shell=False, stdout=PIPE, stderr=PIPE) print(p.stdout.read().strip()) else: args = parse_arguments(argv) for a in args: name, args, kwargs = a[:3] print('executing: %s' % name) execute(eval(name), *args, **kwargs)