def run_load_fabfile(path, sys_path): # Module-esque object fake_module = Fake().has_attr(__dict__={}) # Fake __import__ importer = Fake(callable=True).returns(fake_module) # Snapshot sys.path for restore orig_path = copy.copy(sys.path) # Update with fake path sys.path = sys_path # Test for side effects load_fabfile(path, importer=importer) eq_(sys.path, sys_path) # Restore sys.path = orig_path
def load_tests(loader, suite, patterns): """ Custom test loader for functional tests """ # Try to add vagrant functional tests from .vagrant import base_boxes, VagrantFunctionTestCase, VagrantTestSuite boxes = base_boxes() if boxes: vagrant_suite = VagrantTestSuite(boxes) # Add a test case for each task in each fabfile fabfiles = os.path.join(os.path.dirname(__file__), 'fabfiles') for filename in sorted(os.listdir(fabfiles)): if fnmatch.fnmatch(filename, '[!_]*.py'): fabfile = os.path.join(fabfiles, filename) _, tasks, _ = load_fabfile(fabfile) for task in tasks.values(): test = VagrantFunctionTestCase(task, description=short_doc(task)) vagrant_suite.addTest(test) suite.addTest(vagrant_suite) return suite
def ec2_fab(service, args): """ Run Fabric commands against EC2 instances """ instance_ids = args.instances instances = service.list(elb=args.elb, instance_ids=instance_ids) hosts = service.resolve_hosts(instances) fab.env.hosts = hosts fab.env.key_filename = settings.get('SSH', 'KEY_FILE') fab.env.user = settings.get('SSH', 'USER', getpass.getuser()) fab.env.parallel = True fabfile = find_fabfile(args.file) if not fabfile: print 'Couldn\'t find any fabfiles!' return fab.env.real_fabile = fabfile docstring, callables, default = load_fabfile(fabfile) fab_state.commands.update(callables) commands_to_run = parse_arguments(args.methods) for name, args, kwargs, arg_hosts, arg_roles, arg_exclude_hosts in commands_to_run: fab.execute(name, hosts=arg_hosts, roles=arg_roles, exclude_hosts=arg_exclude_hosts, *args, **kwargs)
def cli_command_list(args): """ Runs task from deploy file """ filepath = args.get('filename') task_name = args.get('task') if filepath: fullpath = os.path.join(os.path.realpath(os.curdir), filepath) docstring, callables, default = load_fabfile(fullpath) if not docstring == None: print(docstring.rstrip('\n')) print('\nAvailable commands:\n') for task_name in callables: task_desc = callables.get(task_name).__doc__ if not task_desc == None: description = task_desc.lstrip('\n').rstrip('\n') else: description = '\n' print('%s %s' % (green(task_name), description))
def load_tests(loader, suite, patterns): """ Custom test loader for functional tests """ # Optional include/exclude list of fabfiles include_files = os.environ.get('FABTOOLS_TEST_INCLUDE', '').split() exclude_files = os.environ.get('FABTOOLS_TEST_EXCLUDE', '').split() # Try to add vagrant functional tests from .vagrant import test_boxes, VagrantTestCase, VagrantTestSuite boxes = test_boxes() if boxes: vagrant_suite = VagrantTestSuite(boxes) # Add a test case for each task in each fabfile fabfiles = os.path.join(os.path.dirname(__file__), 'fabfiles') for filename in sorted(os.listdir(fabfiles)): if fnmatch.fnmatch(filename, '[!_]*.py'): # Skip file if in exclude list if filename in exclude_files: continue # Skip file if it's not in an explicit include list if include_files and filename not in include_files: continue fabfile = os.path.join(fabfiles, filename) _, tasks, _ = load_fabfile(fabfile) for name, callable in tasks.iteritems(): test = VagrantTestCase(name, callable) vagrant_suite.addTest(test) suite.addTest(vagrant_suite) return suite
def load_tests(loader, suite, patterns): """ Custom test loader for functional tests """ # Optional include/exclude list of fabfiles include_files = os.environ.get('FABTOOLS_TEST_INCLUDE', '').split() exclude_files = os.environ.get('FABTOOLS_TEST_EXCLUDE', '').split() # Try to add vagrant functional tests from .vagrant import base_boxes, VagrantTestCase, VagrantTestSuite boxes = base_boxes() if boxes: vagrant_suite = VagrantTestSuite(boxes) # Add a test case for each task in each fabfile fabfiles = os.path.join(os.path.dirname(__file__), 'fabfiles') for filename in sorted(os.listdir(fabfiles)): if fnmatch.fnmatch(filename, '[!_]*.py'): # Skip file if in exclude list if filename in exclude_files: continue # Skip file if it's not in an explicit include list if include_files and filename not in include_files: continue fabfile = os.path.join(fabfiles, filename) _, tasks, _ = load_fabfile(fabfile) for name, callable in tasks.iteritems(): test = VagrantTestCase(name, callable) vagrant_suite.addTest(test) suite.addTest(vagrant_suite) return suite
def visit_fabfile(path=None): """Load and process a fabfile from the current working directory.""" path = path or find_fabfile() if not path: raise ValueError('No fabfile detected') callables = load_fabfile(path)[1] return visit(callables)
def get_task_list(parts): fabfile = '%s.py' % os.path.join(satellite.commands.__path__[0], *parts) if not os.path.exists(fabfile): return [] docstring, callables, default = load_fabfile(fabfile) callables = dict((k, v) for k, v in callables.iteritems() if v.__module__ == parts[-1]) return _task_names(callables)
def display_fabric_tasks(): paths = find_fabfile() docstring, callables, default = load_fabfile(paths) callables.pop('setup', None) state.commands.update(callables) commands = list_commands(docstring, 'normal') print("\n".join(commands))
def setup(self): conf = self._service.config env.user = conf["user"] env.key_filename = conf["identity_file"] if "fabric" in conf: env.update(conf["fabric"]) for name, func in load_fabfile(conf["fabfile"])[1].items(): setattr(self, name, lambda *a, **kw: execute(func, hosts=self._service.hosts, *a, **kw))
def test_recursion_steps_into_nontask_modules(self): """ Recursive loading will continue through modules with no tasks """ module = fabfile('deep') with path_prefix(module): docs, funcs = load_fabfile(module) eq_(len(funcs), 1) ok_('submodule.subsubmodule.deeptask' in _task_names(funcs))
def test_newstyle_task_presence_skips_classic_task_modules(self): """ Classic-task-only modules shouldn't add tasks if any new-style tasks exist """ module = fabfile('deep') with path_prefix(module): docs, funcs = load_fabfile(module) eq_(len(funcs), 1) ok_('submodule.classic_task' not in _task_names(funcs))
def test_should_load_decorated_tasks_only_if_one_is_found(self): """ If any new-style tasks are found, *only* new-style tasks should load """ module = fabfile('decorated_fabfile.py') with path_prefix(module): docs, funcs = load_fabfile(module) eq_(len(funcs), 1) ok_('foo' in funcs)
def import_fabfile(fabfile='fabfile.py'): """ you have to call this first to enable fabric tasks""" from fabric.main import find_fabfile, load_fabfile from fabric.network import normalize from fabric import state state.env.fabfile = fabfile _, fabfile = load_fabfile(find_fabfile()) return fabfile
def test_implicit_discovery(self): """ Default to automatically collecting all tasks in a fabfile module """ implicit = fabfile("implicit_fabfile.py") with path_prefix(implicit): docs, funcs = load_fabfile(implicit) eq_(len(funcs), 2) ok_("foo" in funcs) ok_("bar" in funcs)
def test_explicit_discovery(self): """ If __all__ is present, only collect the tasks it specifies """ explicit = fabfile("explicit_fabfile.py") with path_prefix(explicit): docs, funcs = load_fabfile(explicit) eq_(len(funcs), 1) ok_("foo" in funcs) ok_("bar" not in funcs)
def test_class_based_tasks_are_found_with_proper_name(self): """ Wrapped new-style tasks should preserve their function names """ module = fabfile('decorated_fabfile_with_classbased_task.py') from fabric.state import env with path_prefix(module): docs, funcs = load_fabfile(module) eq_(len(funcs), 1) ok_('foo' in funcs)
def _load_fabfile(self): app.logger.info("loading fabfile %s" % self.fabfile_path) if not state.commands: docstring, callables, default = load_fabfile(self.fabfile_path) state.commands.update(callables) app.logger.info("loaded {0} tasks from fabfile".format( len(state.commands))) # Don't prompt me bro state.env.abort_on_prompts = True # Let us capture exceptions state.env.abort_exception = FabricException
def get_fabric_tasks(request): """ Generate a list of fabric tasks that are available """ try: docstring, callables, default = load_fabfile(find_fabfile(None)) all_tasks = _task_names(callables) dict_with_docs = {task: callables[task].__doc__ for task in all_tasks} except Exception as e: messages.error(request, 'Error loading fabfile: ' + e.message) dict_with_docs = {} return dict_with_docs
def get_fab_tasks(): fab_task_list = {} if type(settings.fabfile).__name__ == 'dict': for alias, fabfile in settings.fabfile.items(): (docstring, tasks, default) = main.load_fabfile(fabfile) fab_task_list[alias] = {'tasks': tasks, 'doc': docstring, 'default': default, 'alias': alias, } return fab_task_list
def __init__(self, path): _path = path if not path.endswith('fabric'): _path = os.path.join(path, 'fabfile') if 'fabfile' in sys.modules.keys(): del sys.modules['fabfile'] self.doc, _dict = load_fabfile(_path)[:2] self.task_dict = dict((x.name, FabricTask(x)) for x in _dict.values() if hasattr(x, 'name')) _modules = [k for k, m in _dict.items() if not hasattr(m, 'name')] for _m in _modules: self.task_dict.update(dict(('{0}.{1}'.format(_m, x.name), FabricTask(x, m=_m)) for x in _dict[_m].values() if hasattr(x, 'name'))) self.directory = os.path.split(path)[0]
def get_fabric_tasks(request): """ Generate a list of fabric tasks that are available """ try: docstring, callables, default = load_fabfile(settings.FABFILE_PATH) all_tasks = _task_names(callables) dict_with_docs = {task: callables[task].__doc__ for task in all_tasks} except Exception as e: messages.error(request, "Error loading fabfile: " + str(e)) dict_with_docs = {} return dict_with_docs
def get_fab_tasks(fabfile_dir): fab_task_list = dict() if os.path.isdir(fabfile_dir): for fabfile in os.listdir(fabfile_dir): if fabfile.endswith('.py') and fabfile != '__init__.py': (docstring, task, default) = load_fabfile(fabfile) task_name = fabfile.replace('.py', '') fab_task_list[task_name] = task[task_name] return fab_task_list
def get_fab_tasks(): fab_task_list = {} if type(settings.fabfile).__name__ == 'dict': for alias, fabfile in list(settings.fabfile.items()): (docstring, tasks, default) = main.load_fabfile(fabfile) fab_task_list[alias] = { 'tasks': tasks, 'doc': docstring, 'default': default, 'alias': alias, } return fab_task_list
def load_tests(loader, tests, patterns): """ Custom test loader """ suite = VagrantTestSuite(BASE_BOXES) # Add a test case for each fabric task path = os.path.join(os.path.dirname(__file__), "fabfile.py") _, tasks, _ = load_fabfile(path) for name, task in tasks.items(): suite.addTest(VagrantFunctionTestCase(task)) return suite
def load_tests(loader, tests, patterns): """ Custom test loader """ suite = VagrantTestSuite(BASE_BOXES) # Add a test case for each fabric task path = os.path.join(os.path.dirname(__file__), 'fabfile.py') _, tasks, _ = load_fabfile(path) for name, task in tasks.items(): suite.addTest(VagrantFunctionTestCase(task)) return suite
def allcmds(self): if self.sets: return self._allcmds self.sets.extend( findfabfiles() ) for fabfile in self.fabfiles: #fabric._load_default_settings() commands = load_fabfile(fabfile) self.sets.append((commands,fabfile)) self._allcmds = {} for commands,fabfile in self.sets: self._allcmds.update(commands) return self._allcmds
def cli_command_run(args): """ Runs task from deploy file """ filepath = args.get('filename') task_name = args.get('task') if filepath: fullpath = os.path.join(os.path.realpath(os.curdir), filepath) docstring, callables, default = load_fabfile(fullpath) if task_name in callables: execute(callables[task_name])
def build_deploy_docs(docs_path): try: from fabric.main import load_fabfile except ImportError: warn("Couldn't build fabfile.rst, fabric not installed") return project_template_path = path_for_import("mezzanine.project_template") commands = load_fabfile(os.path.join(project_template_path, "fabfile"))[1] lines = [] for name in sorted(commands.keys()): doc = commands[name].__doc__.strip().split("\n")[0] lines.append(" * ``fab %s`` - %s" % (name, doc)) with open(os.path.join(docs_path, "fabfile.rst"), "w") as f: f.write("\n".join(lines))
def __init__(self, path): _path = path if not path.endswith('fabric'): _path = os.path.join(path, 'fabfile') if 'fabfile' in sys.modules.keys(): del sys.modules['fabfile'] self.doc, _dict = load_fabfile(_path)[:2] self.task_dict = dict((x.name, FabricTask(x)) for x in _dict.values() if hasattr(x, 'name')) _modules = [k for k, m in _dict.items() if not hasattr(m, 'name')] for _m in _modules: self.task_dict.update( dict(('{0}.{1}'.format(_m, x.name), FabricTask(x, m=_m)) for x in _dict[_m].values() if hasattr(x, 'name'))) self.directory = os.path.split(path)[0]
def main(): """Load our default fabfile, then attempt to load any local fabfiles.""" our_fab = os.path.join(os.path.dirname(__file__), 'default_tasks.py') docstring, callables, default = fab.load_fabfile(our_fab) fab.state.commands.update(callables) fabfiles = [] other_fab = fab.find_fabfile() if other_fab: fabfiles.append(other_fab) fabfiles.append(our_fab) fab.main(fabfiles)
def schema(request): """ Parses the project fabfile and returns API listing the available commands. Commands are made available by adding the @tailored decorator to a fabric function. """ if request.REQUEST.get('key'): if request.REQUEST.get('key') in djangosettings.TAILOR_API_KEYS.values(): fabfile = djangosettings.TAILOR_FABFILE_PATH fabric_dir = os.path.dirname(fabfile) # Custom importer importer = lambda _: imp.load_source('fabfile', fabfile) sys.path.insert(0, fabric_dir) os.chdir(fabric_dir) try: _, task_map, _ = load_fabfile('/fabfile.py', importer) task_list = _task_names(task_map) fab_dict = { 'tasks' : { 'map' : task_map, 'folded' : task_list } } del sys.path[0] response = json.dumps(fab_dict) return HttpResponse(response, mimetype='application/json', status=200) except: return HttpResponse("Failed to build schema", status=500) finally: else: return HttpResponse("API Key Not Recognized", status=403) else: return HttpResponse("API Key Required", status=403)
def load_tests(loader, tests, patterns): """ Custom test loader """ suite = unittest.TestSuite() # Add unit tests (import here to avoid circular import) suite.addTest(loader.loadTestsFromModule(unit)) # Try to add vagrant functional tests boxes = base_boxes() if boxes: vagrant_suite = VagrantTestSuite(boxes) # Add a test case for each fabric task path = os.path.join(os.path.dirname(__file__), 'fabfile.py') _, tasks, _ = load_fabfile(path) for name, task in tasks.items(): vagrant_suite.addTest(VagrantFunctionTestCase(task)) suite.addTest(vagrant_suite) return suite
def run(): try: # Parse command line options parser, options, arguments = parse_options() # Handle regular args vs -- args arguments = parser.largs if len(arguments) > 1: abort("You can call only one command!") if arguments: tmp_parts = arguments[0].split(':') parts = tmp_parts[0].split('.') if not command_exists(parts): print print 'Warning: command not found!' print return fabfile_locations = ['%s.py' % os.path.join(satellite.commands.__path__[0], *parts[:-1])] arguments[0] = parts[-1] + ''.join(":%s" % i for i in tmp_parts[1:]) else: print "Command tree:" print_tree_commands() return # Update env with any overridden option values # NOTE: This needs to remain the first thing that occurs # post-parsing, since so many things hinge on the values in env. for option in env_options: state.env[option.dest] = option.default # Handle --hosts, --roles, --exclude-hosts (comma separated string => # list) for key in ['hosts', 'roles', 'exclude_hosts']: if key in state.env and isinstance(state.env[key], basestring): state.env[key] = state.env[key].split(',') # Load settings from user settings file, into shared env dict. settings.update(load_settings(options.configfile)) state.env.update(settings.fabric) # Find local fabfile path or abort fabfile = find_fabfile(fabfile_locations) if not fabfile: abort("""Couldn't find any fabfiles! Use -h for help.""") # Store absolute path to fabfile in case anyone needs it state.env.real_fabfile = fabfile # Load fabfile (which calls its module-level code, including # tweaks to env values) and put its commands in the shared commands # dict default = None if fabfile: docstring, callables, default = load_fabfile(fabfile) state.commands.update(callables) # Abort if no commands found if not state.commands: abort("Fabfile didn't contain any commands!") # Now that we're settled on a fabfile, inform user. if state.output.debug: if fabfile: print("Using fabfile '%s'" % fabfile) else: print("No fabfile loaded -- remainder command only") # Parse arguments into commands to run (plus args/kwargs/hosts) commands_to_run = parse_arguments(arguments) if state.output.debug: names = ", ".join(x[0] for x in commands_to_run) print("Commands to run: %s" % names) # At this point all commands must exist, so execute them in order. for name, args, kwargs, arg_hosts, arg_roles, arg_exclude_hosts in commands_to_run: execute( name, hosts=arg_hosts, roles=arg_roles, exclude_hosts=arg_exclude_hosts, *args, **kwargs ) # If we got here, no errors occurred, so print a final note. if state.output.status: print("\nDone.") except SystemExit: # a number of internal functions might raise this one. raise except KeyboardInterrupt: if state.output.status: sys.stderr.write("\nStopped.\n") sys.exit(1) except: sys.excepthook(*sys.exc_info()) # we might leave stale threads if we don't explicitly exit() sys.exit(1) finally: disconnect_all() sys.exit(0)
from datetime import date from random import shuffle ''' GameQ Function Import ''' from gameq import gameq ''' Py-TS3 Imports ''' from tsstatus import clientdict, gen_privilegekey, get_privilegekeys, rem_privilegekey, tsviewer import ts3.query as tsquery import ts3.commands, ts3.response from ts3.common import TS3Error from socket import error as socket_error ''' Fabric3 Imports and Setup ''' import fabfile from fabric import state from fabric.api import execute from fabric.main import load_fabfile docstring, callables, default = load_fabfile('fabfile.py') state.commands.update(callables) ''' Config.json Import ''' with open('../data/config.json') as cfg: config = json.load(cfg) ''' Initialize Flask App ''' app = Flask(__name__) app.config.update(SQLALCHEMY_DATABASE_URI='sqlite:///../data/' + config['sqlite']['db'], SQLALCHEMY_TRACK_MODIFICATIONS=False, SECRET_KEY=config['steam-api']['key'], DEBUG=config['flask']['debug'], HOST=config['flask']['host'], PORT=config['flask']['port']) ''' Database and Open ID Instantiation '''
from fabric.state import env from fabric.main import load_fabfile from fabmagic.utils import _rel from fabmagic.constants import COOKBOOKS_NAME, COOKBOOKS_LIBRARY_NAME, ROLES_NAME __all__ = 'BaseTestCase', logger = logging.getLogger("fabmagic.test") env_copy = deepcopy(env) current_dir = os.path.dirname(__file__) fabfile_path = _rel(current_dir, "fabfile.py") env.real_fabfile = fabfile_path loading_result = load_fabfile(fabfile_path) class BaseTestCase(unittest.TestCase): """Base test case """ def setUp(self): self.logger = logger self.current_dir = current_dir self.fabfile_path = fabfile_path self.locading_result = loading_result self.env_copy = env_copy self.env = env self.config_file = _rel(os.path.dirname(__file__), ".config") self.new_config_file = _rel(os.path.dirname(__file__), ".new_config")
def list_output(module, format_, expected): module = fabfile(module) with path_prefix(module): docstring, tasks = load_fabfile(module) with patched_context(fabric.state, 'commands', tasks): eq_output(docstring, format_, expected)
def print_task_list(path): tasks = load_fabfile(path) task_dict = tasks[1] for task in task_dict.values(): print(u'{0:10}: {1}'.format(task.name, task.__doc__))
def import_fabfile(fabfile='fabfile.py'): "you have to call this first to enable fabric tasks" state.env.fabfile = fabfile _, fabfile = load_fabfile(find_fabfile()) return fabfile
def gentleman(): args_dict = docopt(__doc__, version=__version__) arguments = args_dict['COMMAND'] if not os.path.exists(os.path.join(os.getcwd(), '.gentle.yaml')): if arguments != ['init']: print(red('This is not a gentle directory, Make sure this is the correct' # noqa 'directory, and use `gt init` Initialization')) return sys.exit(1) else: init() arguments = args_dict['COMMAND'] try: # state.env.update(load_settings(state.env.rcfile)) fabfile = os.path.join(here, 'gt') state.env.real_fabfile = fabfile default = None if fabfile: docstring, callables, default = load_fabfile(fabfile) state.commands.update(callables) commands_to_run = parse_arguments(arguments) unknown_commands = [] for tup in commands_to_run: if crawl(tup[0], state.commands) is None: unknown_commands.append(tup[0]) if args_dict['--list']: show_commands(docstring, 'normal', 1) for type in ['show', 'hide']: t = args_dict['--' + type] if t is not None: hide = True if type == 'hide' else False set_output(t, hide=hide, only=args_dict['--only']) # Abort if any unknown commands were specified if unknown_commands: warn("Command(s) not found:\n%s" % indent(unknown_commands)) show_commands(None, 'normal', 1) for name, args, kwargs, arg_hosts, arg_roles, arg_exclude_hosts in \ commands_to_run: execute( name, hosts=arg_hosts, roles=arg_roles, exclude_hosts=arg_exclude_hosts, *args, **kwargs) if state.output.status: print("\nDone.") except SystemExit: raise except KeyboardInterrupt: if state.output.status: sys.stderr.write("\nStopped.\n") sys.exit(1) except: sys.excepthook(*sys.exc_info()) sys.exit(1) finally: disconnect_all() sys.exit(0)