class URLLoader(object): """Load URLs from apps.""" def __init__(self): """Load URLs.""" self.apps = [] self.database = Database() def get_urls(self): """Get the urls.""" apps = self.database.get_app_list() if not apps: return URLS urls = URLS for app in apps: url_path = '%s.urls' % app try: url_module = importlib.import_module(url_path) except: print("Cannot import: %s" % url_path) print("Skipping app: %s" % app) else: urls.extend(getattr(url_module, 'URLS', [])) return urls
def handle(self, *args, **kwargs): """ Run the unit tests for all the test labels in the provided list. Labels must be of the form: - app,TestClass,test_method Run a single specific test method - app,TestClass Run all the test methods in a given class - app Search for doctests and unittests in the named application. When looking for tests, the test runner will look in the models and tests modules for the application. A list of 'extra' tests may also be provided; these tests will be added to the test suite. Returns the number of tests that failed. """ if args: labels = args else: database = Database() labels = database.get_app_list() suite = self.get_suite(labels) verbosity = int(kwargs['verbosity']) failures = unittest.TextTestRunner(verbosity=verbosity).run(suite) if failures: sys.exit(bool(failures))
def handle(self, *args, **kwargs): """Get the Urls.""" database = Database() apps = database.get_app_list() urls = URLS if apps: for app in apps: url_path = '%s.urls' % app try: url_module = importlib.import_module(url_path) except: pass else: urls.extend(getattr(url_module, 'URLS', [])) for url in urls: print(url)
def handle(self, *args, **options): """Collect the static files of an application.""" database = Database() # 0. Get the target directory target = database.get_setting('static', 'root') if not target: raise CommandError( u'No static root setting. Set static root with:\n' u'mag.py add_setting static root <directory-name>\n' u'E.g.\n' u'mag.py add_setting static root /var/www/static') # 1. Get the list of applications apps = database.get_app_list() if apps: apps.append('magpy') else: apps = ['magpy'] # 2. Build a dictionary of static file names static_files = [] for app in apps: print("Looking for static files in ", app) path = self.get_path(app) if path: static_files.extend( self.get_filenames( path, target, app, options["appify"])) # 3. Check if the timestamps on the static files are newer than the # the saved files, and cut out any that are not if options['check_time']: static_files = filter(self.check_newer, static_files) # 4. Copy the files for source, target in static_files: # Make sure the target directory exist target_dir = os.path.dirname(target) if not os.path.exists(target_dir): os.makedirs(target_dir) # Copy the file print("Copying %s to %s" % (source, target)) copy(source, target)
def handle(self, *args, **options): """Collect the static files of an application.""" database = Database() # 0. Get the target directory target = database.get_setting('static', 'root') if not target: raise CommandError( u'No static root setting. Set static root with:\n' u'mag.py add_setting static root <directory-name>\n' u'E.g.\n' u'mag.py add_setting static root /var/www/static') # 1. Get the list of applications apps = database.get_app_list() if apps: apps.append('magpy') else: apps = ['magpy'] # 2. Build a dictionary of static file names static_files = [] for app in apps: print("Looking for static files in ", app) path = self.get_path(app) if path: static_files.extend( self.get_filenames(path, target, app, options["appify"])) # 3. Check if the timestamps on the static files are newer than the # the saved files, and cut out any that are not if options['check_time']: static_files = filter(self.check_newer, static_files) # 4. Copy the files for source, target in static_files: # Make sure the target directory exist target_dir = os.path.dirname(target) if not os.path.exists(target_dir): os.makedirs(target_dir) # Copy the file print("Copying %s to %s" % (source, target)) copy(source, target)
def get_commands(): """ Returns a dictionary mapping command names to their callback applications. This works by looking for a management.commands package in magpy, and in each installed application -- if a commands package exists, all commands in that package are registered. The dictionary is in the format {command_name: app_name}. Key-value pairs from this dictionary can then be used in calls to load_command_class(app_name, command_name) If a specific version of a command must be loaded (e.g., with the startapp command), the instantiated module can be placed in the dictionary in place of the application name. The dictionary is cached on the first call and reused on subsequent calls. """ global _COMMANDS # pylint: disable-msg=W0603 if _COMMANDS is None: # Find the builtin commands magpy_path = find_management_module('magpy') _COMMANDS = dict([(name, 'magpy') for \ name in find_commands(magpy_path)]) # Find the installed apps database = Database() apps = database.get_app_list() if apps == None: apps = [] # Find and load the management module for each installed app. for app_name in apps: try: path = find_management_module(app_name) _COMMANDS.update(dict([(name, app_name) for name in find_commands(path)])) except ImportError: pass # No management module - ignore this app return _COMMANDS
def autocomplete(self): # pylint: disable-msg=R0914 """ Output completion suggestions for BASH. The output of this function is passed to BASH's `COMREPLY` variable and treated as completion suggestions. `COMREPLY` expects a space separated string as the result. The `COMP_WORDS` and `COMP_CWORD` BASH environment variables are used to get information about the cli input. Please refer to the BASH man-page for more information about this variables. Subcommand options are saved as pairs. A pair consists of the long option string (e.g. '--exclude') and a boolean value indicating if the option requires arguments. When printing to stdout, a equal sign is appended to options which require arguments. Note: If debugging this function, it is recommended to write the debug output in a separate file. Otherwise the debug output will be treated and formatted as potential completion suggestions. """ # Don't complete if user hasn't sourced bash_completion file. # This is found in django-trunk/extras/django_bash_completion if 'DJANGO_AUTO_COMPLETE' not in os.environ: return cwords = os.environ['COMP_WORDS'].split()[1:] cword = int(os.environ['COMP_CWORD']) try: curr = cwords[cword - 1] except IndexError: curr = '' subcommands = get_commands().keys() + ['help'] options = [('--help', None)] # subcommand if cword == 1: debug_text = ' '.join(sorted(filter(lambda x: x.startswith(curr), subcommands))) print(debug_text) # subcommand options # special case: the 'help' subcommand has no options elif cwords[0] in subcommands and cwords[0] != 'help': subcommand_cls = self.fetch_command(cwords[0]) # special case: add the names of installed apps to options if cwords[0] in ('dumpdata', 'sql', 'sqlall', 'sqlclear', 'sqlcustom', 'sqlindexes', 'sqlsequencereset', 'test'): try: database = Database() # Get the last part of the dotted path as the app name. options += [(a.split('.')[-1], 0) for \ a in database.get_app_list()] except ImportError: # Fail silently if DJANGO_SETTINGS_MODULE isn't set. The # user will find out once they execute the command. pass options += [(s_opt.get_opt_string(), s_opt.nargs) for s_opt in subcommand_cls.option_list] # filter out previously specified options from available options prev_opts = [x.split('=')[0] for x in cwords[1:cword - 1]] # Original Python 2 version #options = filter(lambda (x, v): x not in prev_opts, options) # Python 3 version? #options = filter(lambda x_v: x_v[0] not in prev_opts, options) options = [opt for opt in options if opt[0] not in prev_opts] # filter options by current input options = sorted([(k, v) for k, v in \ options if k.startswith(curr)]) for option in options: opt_label = option[0] # append '=' to options which require args if option[1]: opt_label += '=' print(opt_label) sys.exit(1)