def check(self, app_configs=None, tags=None, display_num_errors=False, include_deployment_checks=False): """ Uses the system check framework to validate entire Django project. Raises CommandError for any serious message (error or critical errors). If there are only light messages (like warnings), they are printed to stderr and no exception is raised. """ all_issues = checks.run_checks( app_configs=app_configs, tags=tags, include_deployment_checks=include_deployment_checks, ) msg = "" visible_issue_count = 0 # excludes silenced warnings if all_issues: debugs = [e for e in all_issues if e.level < checks.INFO and not e.is_silenced()] infos = [e for e in all_issues if checks.INFO <= e.level < checks.WARNING and not e.is_silenced()] warnings = [e for e in all_issues if checks.WARNING <= e.level < checks.ERROR and not e.is_silenced()] errors = [e for e in all_issues if checks.ERROR <= e.level < checks.CRITICAL] criticals = [e for e in all_issues if checks.CRITICAL <= e.level] sorted_issues = [ (criticals, 'CRITICALS'), (errors, 'ERRORS'), (warnings, 'WARNINGS'), (infos, 'INFOS'), (debugs, 'DEBUGS'), ] for issues, group_name in sorted_issues: if issues: visible_issue_count += len(issues) formatted = ( color_style().ERROR(force_str(e)) if e.is_serious() else color_style().WARNING(force_str(e)) for e in issues) formatted = "\n".join(sorted(formatted)) msg += '\n%s:\n%s\n' % (group_name, formatted) if msg: msg = "System check identified some issues:\n%s" % msg if display_num_errors: if msg: msg += '\n' msg += "System check identified %s (%s silenced)." % ( "no issues" if visible_issue_count == 0 else "1 issue" if visible_issue_count == 1 else "%s issues" % visible_issue_count, len(all_issues) - visible_issue_count, ) if any(e.is_serious() and not e.is_silenced() for e in all_issues): raise CommandError(msg) elif msg and visible_issue_count: self.stderr.write(msg) elif msg: self.stdout.write(msg)
def fixture(*names): files = [] for name in names: filename = os.path.join(settings.ROOT, 'mkt/site/fixtures/data', name) if not os.path.splitext(filename)[1]: filename += '.json' if not os.path.exists(filename): print color_style().ERROR('No fixture: %s, skipping.' % filename) continue files.append(filename) return files
def handle(self, *prefixes, **options): groups = [ (u'CRITICALS', datacheck.CRITICAL, float(u'inf'), color_style().ERROR), (u'ERRORS', datacheck.ERROR, datacheck.CRITICAL, color_style().ERROR), (u'WARNINGS', datacheck.WARNING, datacheck.ERROR, color_style().WARNING), (u'INFOS', datacheck.INFO, datacheck.WARNING, color_style().NOTICE), (u'DEBUGS', 0, datacheck.INFO, color_style().NOTICE), ] output = [] if options[u'list']: output.append(u'Available data checks:') output.extend(u' -- {}'.format(c) for c in datacheck.registry) self.stdout.write(u'\n'.join(output)) return if prefixes: registry = datacheck.registry.filtered(prefixes) output.append(u'Running {} data checks:'.format(len(registry))) output.extend(u' -- {}'.format(c) for c in registry) output.append(u'') else: registry = datacheck.registry output.append(u'Running all registered data checks.') output.append(u'') issues = registry.run_checks( superficial=options[u'superficial'], autofix=options[u'autofix']) autofixable = len([s for s in issues if s.autofixable]) if autofixable: if options[u'autofix']: output.append(squeeze(u""" Data checks identified {} issues, {} of them were autofixed. """).format(len(issues), autofixable)) else: output.append(squeeze(u""" Data checks identified {} issues, {} of them can be autofixed (use --autofix). """).format(len(issues), autofixable)) else: output.append(u'Data checks identified {} issues.'.format(len(issues))) for group, level_min, level_max, style in groups: filtered = [a for a in issues if level_min <= a.level < level_max] if filtered: output.append(u'') output.append(u'{}:'.format(group)) output.extend(style(format(a)) for a in filtered) self.stdout.write(u'\n'.join(output))
def create_empty_source_table(self, clazz, source_table_name, source_db_connection, extra_fields={}): project = Project(key='sutter_county', id=0) db_entity = DbEntity(key=Keys.DB_ABSTRACT_BASE_AGRICULTURE_FEATURE, feature_class_configuration=dict( abstract_class=full_module_path(clazz) )) SourceClass = FeatureClassCreator(project, db_entity, no_ensure=True).dynamic_model_class(base_only=True, schema='public', table=source_table_name) # class SourceClass(clazz): # class Meta(clazz.Meta): # db_table = source_table_name create_tables_for_dynamic_classes(SourceClass) for field in SourceClass._meta.fields[1:]: setattr(field, 'null', True) drop_table = "DROP TABLE IF EXISTS {final_table} CASCADE;".format(final_table=source_table_name) sql, refs = source_db_connection.creation.sql_create_model(SourceClass, color_style()) add_geometry_fields = ''' ALTER TABLE {final_table} ADD COLUMN geography_id VARCHAR; ALTER TABLE {final_table} ADD COLUMN wkb_geometry GEOMETRY;'''.format(final_table=source_table_name) sql = drop_table + sql[0] + add_geometry_fields for dbfield, fieldtype in extra_fields.items(): sql += 'ALTER TABLE {final_table} ADD COLUMN {field} {type}'.format( final_table=source_table_name, field=dbfield, type=fieldtype) source_db_connection.cursor().execute(sql)
def __init__(self, stdout=None, stderr=None, no_color=False): self.stdout = OutputWrapper(stdout or sys.stdout) self.stderr = OutputWrapper(stderr or sys.stderr) if no_color: self.style = no_style() else: self.style = color_style() self.stderr.style_func = self.style.ERROR # `requires_model_validation` is deprecated in favor of # `requires_system_checks`. If both options are present, an error is # raised. Otherwise the present option is used. If none of them is # defined, the default value (True) is used. has_old_option = hasattr(self, 'requires_model_validation') has_new_option = hasattr(self, 'requires_system_checks') if has_old_option: warnings.warn( '"requires_model_validation" is deprecated ' 'in favor of "requires_system_checks".', RemovedInDjango19Warning) if has_old_option and has_new_option: raise ImproperlyConfigured( 'Command %s defines both "requires_model_validation" ' 'and "requires_system_checks", which is illegal. Use only ' '"requires_system_checks".' % self.__class__.__name__) self.requires_system_checks = ( self.requires_system_checks if has_new_option else self.requires_model_validation if has_old_option else True)
def main_help_text() -> str: """ Returns the script's main help text, as a string. """ usage = [ "", "Type 'otree help <subcommand>' for help on a specific subcommand.", "", "Available subcommands:", ] commands_dict = defaultdict(lambda: []) for name, app in six.iteritems(get_commands()): if app == 'django.core': app = 'django' else: app = app.rpartition('.')[-1] commands_dict[app].append(name) style = color_style() for app in sorted(commands_dict.keys()): usage.append("") usage.append(style.NOTICE("[%s]" % app)) for name in sorted(commands_dict[app]): usage.append(" %s" % name) return '\n'.join(usage)
def link_app_media(sender, verbosity, **kwargs): """Symlink 'media' folder for this app to settings.MEDIA_ROOT'/<app_name>' after syncdb has been run on a project that list this app in settings.INTALLED_APPS """ app_name = sender.__name__.split('.')[-2] # GUARD: In principal, this function can actually create a symling for # *all* apps containing a 'media' folder. But we will limit it to only # create symlinks for this particular app: if app_name != 'jquery_widgets': return app_dir = os.path.dirname(sender.__file__) app_media_dir = os.path.join(app_dir, 'media') if os.path.exists(app_media_dir): dest = os.path.join(settings.MEDIA_ROOT, app_name) if not os.path.exists(dest): try: os.symlink(app_media_dir, dest) # will not work on windows. if verbosity > 1: print "symlinked app_media dir for app: %s" % app_name except: # Windows users should get a note, that they should copy the # media files to the destination. error_msg = "Failed to link media for '%s'\n" % app_name instruction = ("Please copy the media files to the MEDIA_ROOT", "manually\n") sys.stderr.write(color_style().ERROR(str(error_msg))) sys.stderr.write(" ".join(instruction))
def autodiscover(self, module_name=None, verbose=True): """Autodiscovers consent classes in the consents.py file of any INSTALLED_APP. """ module_name = module_name or 'consents' writer = sys.stdout.write if verbose else lambda x: x style = color_style() writer(f' * checking for site {module_name} ...\n') for app in django_apps.app_configs: writer(f' * searching {app} \r') try: mod = import_module(app) try: before_import_registry = deepcopy(site_consents.registry) import_module(f'{app}.{module_name}') writer( f' * registered consents \'{module_name}\' from \'{app}\'\n') except ConsentError as e: writer(f' - loading {app}.consents ... ') writer(style.ERROR(f'ERROR! {e}\n')) except ImportError as e: site_consents.registry = before_import_registry if module_has_submodule(mod, module_name): raise SiteConsentError(str(e)) except ImportError: pass
def populate_exam_run_fk(apps, schema_editor): ProctoredExamGrade = apps.get_model('grades', 'ProctoredExamGrade') ExamAuthorization = apps.get_model('exams', 'ExamAuthorization') style = color_style() fake_exam_grades = [] exam_grades = ProctoredExamGrade.objects.filter(exam_run__isnull=True) for exam_grade in exam_grades.iterator(): try: exam_grade.exam_run = ExamAuthorization.objects.get( id=exam_grade.client_authorization_id ).exam_run exam_grade.save() except ValueError: fake_exam_grades.append(exam_grade) continue for fake_exam_grade in fake_exam_grades: sys.stdout.write( style.ERROR( '\nInvalid client_authorization_id {0} for ProctoredExamGrade {1}'.format( fake_exam_grade.client_authorization_id, fake_exam_grade.id ) ) )
def color_style(): style = color.color_style() style.FILTER = termcolors.make_style(fg='yellow', opts=('bold',)) style.MODULE_NAME = termcolors.make_style(fg='green', opts=('bold',)) style.TAG = termcolors.make_style(fg='red', opts=('bold',)) style.TAGLIB = termcolors.make_style(fg='blue', opts=('bold',)) return style
def execute(self, *args, **options): """ Try to execute this command, performing system checks if needed (as controlled by the ``requires_system_checks`` attribute, except if force-skipped). """ if options['force_color'] and options['no_color']: raise CommandError("The --no-color and --force-color options can't be used together.") if options['force_color']: self.style = color_style(force_color=True) elif options['no_color']: self.style = no_style() self.stderr.style_func = None if options.get('stdout'): self.stdout = OutputWrapper(options['stdout']) if options.get('stderr'): self.stderr = OutputWrapper(options['stderr'], self.stderr.style_func) if self.requires_system_checks and not options.get('skip_checks'): self.check() if self.requires_migrations_checks: self.check_migrations() output = self.handle(*args, **options) if output: if self.output_transaction: connection = connections[options.get('database', DEFAULT_DB_ALIAS)] output = '%s\n%s\n%s' % ( self.style.SQL_KEYWORD(connection.ops.start_transaction_sql()), output, self.style.SQL_KEYWORD(connection.ops.end_transaction_sql()), ) self.stdout.write(output) return output
def __init__(self): self.style = color_style() # `requires_model_validation` is deprecated in favor of # `requires_system_checks`. If both options are present, an error is # raised. Otherwise the present option is used. If none of them is # defined, the default value (True) is used. has_old_option = hasattr(self, 'requires_model_validation') has_new_option = hasattr(self, 'requires_system_checks') if has_old_option: warnings.warn( '"requires_model_validation" is deprecated ' 'in favor of "requires_system_checks".', PendingDeprecationWarning) if has_old_option and has_new_option: raise ImproperlyConfigured( 'Command %s defines both "requires_model_validation" ' 'and "requires_system_checks", which is illegal. Use only ' '"requires_system_checks".' % self.__class__.__name__) self.requires_system_checks = ( self.requires_system_checks if has_new_option else self.requires_model_validation if has_old_option else True)
def color_style(): style = color.color_style() style.SECTION = termcolors.make_style(fg="yellow", opts=("bold",)) style.SUCCESS = termcolors.make_style(fg="green", opts=("bold",)) style.ERROR = termcolors.make_style(fg="red", opts=("bold",)) style.INFO = termcolors.make_style(fg="blue", opts=("bold",)) return style
def handle(self, *directories, **options): from django.db import transaction self.style = color_style() verbose = options.get('verbose') import_picture = options.get('import_picture') wait_until = None if options.get('wait_until'): wait_until = time.mktime(time.strptime(options.get('wait_until'), '%Y-%m-%d %H:%M:%S')) if verbose > 0: print "Will wait until %s; it's %f seconds from now" % ( time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(wait_until)), wait_until - time.time()) index = None if options.get('search_index') and not settings.NO_SEARCH_INDEX: index = Index() try: index.index_tags() index.index.commit() except Exception, e: index.index.rollback() raise e
def color_style(): style = color.color_style() style.PROVIDER = termcolors.make_style(fg='green', opts=('bold',)) style.COMMAND = termcolors.make_style(opts=('bold',)) style.UNKNOWN = termcolors.make_style(fg='red') style.HELP = termcolors.make_style() return style
def otree_heroku_cli(): """ This function is the entry point for the ``otree-heroku`` console script. """ # We need to add the current directory to the python path as this is not # set by default when no using "python <script>" but a standalone script # like ``otree``. if os.getcwd() not in sys.path: sys.path.insert(0, os.getcwd()) os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings') style = color_style() try: from django.conf import settings settings.INSTALLED_APPS except ImportError: print(style.ERROR( "Cannot import otree settings. Please make sure that you are " "in the base directory of your oTree library checkout. " "This directory contains a settings.py and a manage.py file.")) sys.exit(1) otree.management.deploy.heroku.execute_from_command_line(sys.argv)
def _fn(sender, **kwargs): if sender.__name__ != module_name: return cursor = connection.cursor() existing_columns = [row[0] for row in \ connection.introspection.get_table_description(cursor, cls._meta.db_table)] missing_columns = [] for field in cls._meta.fields: if field.column not in existing_columns: missing_columns.append(field) if not missing_columns: return style = color_style() print style.ERROR('The following columns seem to be missing in the database table %s:' % cls._meta.db_table) for field in missing_columns: print u'%s:%s%s' % ( style.SQL_KEYWORD(field.column), ' ' * (25 - len(field.column)), u'%s.%s' % (field.__class__.__module__, field.__class__.__name__), ) print style.NOTICE('\nPlease consult the output of `python manage.py sql %s` to' ' find out what the correct column types are. (Or use south, which is what' ' you should be doing anyway.)\n' % ( cls._meta.app_label, ))
def handle(self, *args, **options): from django.conf import settings style = color_style() template_dirs = set(settings.TEMPLATE_DIRS) template_dirs |= set(options.get('includes', [])) template_dirs |= set(getattr(settings, 'VALIDATE_TEMPLATES_EXTRA_TEMPLATE_DIRS', [])) settings.TEMPLATE_DIRS = list(template_dirs) verbosity = int(options.get('verbosity', 1)) errors = 0 for template_dir in template_dirs: for root, dirs, filenames in os.walk(template_dir): for filename in filenames: if filename.endswith(".swp"): continue if filename.endswith("~"): continue filepath = os.path.join(root, filename) if verbosity>1: print filepath try: tmpl = Template(open(filepath).read()) except Exception, e: errors += 1 if options.get('break', False): raise CommandError("%s: %s %s" % (filepath, e.__class__.__name__, str(e))) else: print "%s:" % filepath print style.ERROR("%s %s" % (e.__class__.__name__, str(e))) print
def otree_cli(): """ This function is the entry point for the ``otree`` console script. """ try: subcommand = sys.argv[1] except IndexError: subcommand = 'help' # default # We need to add the current directory to the python path as this is not # set by default when no using "python <script>" but a standalone script # like ``otree``. if os.getcwd() not in sys.path: sys.path.insert(0, os.getcwd()) argv = sys.argv os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings') # some commands don't need the setings.INSTALLED_APPS # see: https://github.com/oTree-org/otree-core/issues/388 try: settings.INSTALLED_APPS except ImportError: if subcommand in NO_SETTINGS_COMMANDS: settings.configure(**get_default_settings()) else: style = color_style() msg = style.ERROR(SETTINGS_NOT_FOUND_MESSAGE) print(msg) sys.exit(1) execute_from_command_line(argv, 'otree')
def main_help_text(self, commands_only=False): """ Returns the script's main help text, as a string. """ if commands_only: usage = sorted(get_commands().keys()) else: usage = [ "", "Type '%s help <subcommand>' for help on a specific subcommand." % self.prog_name, "", "Available subcommands:", ] commands_dict = collections.defaultdict(lambda: []) for name, app in get_commands().iteritems(): if app == 'django.core': app = 'django' else: app = app.rpartition('.')[-1] commands_dict[app].append(name) style = color_style() for app in sorted(commands_dict.keys()): usage.append("") usage.append(style.NOTICE("[%s]" % app)) for name in sorted(commands_dict[app]): usage.append(" %s" % name) return '\n'.join(usage)
def run_migrations(args, options, executor_codename, schema_name, allow_atomic=True): from django.core.management import color from django.core.management.base import OutputWrapper from django.db import connection style = color.color_style() def style_func(msg): return '[%s:%s] %s' % ( style.NOTICE(executor_codename), style.NOTICE(schema_name), msg ) connection.set_schema(schema_name) stdout = OutputWrapper(sys.stdout) stdout.style_func = style_func stderr = OutputWrapper(sys.stderr) stderr.style_func = style_func if int(options.get('verbosity', 1)) >= 1: stdout.write(style.NOTICE("=== Starting migration")) MigrateCommand(stdout=stdout, stderr=stderr).execute(*args, **options) try: transaction.commit() connection.close() connection.connection = None except transaction.TransactionManagementError: if not allow_atomic: raise # We are in atomic transaction, don't close connections pass connection.set_schema_to_public()
def create_default_locations(app, created_models, verbosity, **kwargs): from geo.models import Location from django.core.management import call_command if Location in created_models and kwargs.get('interactive', True): msg = "\nYou just installed the geo app, would you like to install the " \ "standard set of Locations? (yes/no): " confirm = raw_input(msg) while 1: if confirm not in ('yes', 'no'): confirm = raw_input('Please enter either "yes" or "no": ') continue if confirm == 'yes': gunzip_location() connection = connections[DEFAULT_DB_ALIAS] cursor = connection.cursor() custom_sql = custom_sql_for_model(Location, color_style(), connection) if custom_sql: if verbosity >= 1: print "Installing custom SQL for geo.Location model" try: for sql in custom_sql: cursor.execute(sql) except Exception, e: sys.stderr.write("Failed to install custom SQL for geo.Location model: %s\n" % e) transaction.rollback_unless_managed(using=DEFAULT_DB_ALIAS) else: transaction.commit_unless_managed(using=DEFAULT_DB_ALIAS) else: if verbosity >= 2: print "No custom SQL for geo.Location model" gzip_location() break
def main_help_text(self, commands_only=False): """ Returns the script's main help text, as a string. """ if commands_only: usage = sorted(get_commands().keys()) else: usage = [ "", "Type '%s help <subcommand>' for help on a specific subcommand." % self.prog_name, "", "Available subcommands:", ] commands_dict = defaultdict(lambda: []) for name, app in six.iteritems(get_commands()): if app == 'django.core': app = 'django' else: app = app.rpartition('.')[-1] commands_dict[app].append(name) style = color_style() for app in sorted(commands_dict.keys()): usage.append("") usage.append(style.NOTICE("[%s]" % app)) for name in sorted(commands_dict[app]): usage.append(" %s" % name) # Output an extra note if settings are not properly configured if self.settings_exception is not None: usage.append(style.NOTICE( "Note that only Django core commands are listed " "as settings are not properly configured (error: %s)." % self.settings_exception)) return '\n'.join(usage)
def run_chekers(): is_ok = True style = color_style() for app in settings.INSTALLED_APPS: if app=='apps.tools': continue; try: klass = get_class_by_string("%s.check_settings.Check" % app) except ImportError: continue except Exception, e: sys.stderr.write(style.ERROR("%s\n" % unicode(e))) from apps.ice_logger.global_log import log log.exception(e) continue if klass: try: obj = klass() obj.run() except Exception, e: is_ok = False try: sys.stderr.write(style.ERROR("%s\n" % force_unicode(e))) from apps.ice_logger.global_log import log log.exception(e) except Exception, ee: sys.stderr.write(style.ERROR("Exception in show excaption '%s'\n" % type(e))) sys.stderr.write(style.ERROR("%s" % ee))
def color_style(): style = color.color_style() style.ALREADY = termcolors.make_style(fg='yellow') style.OK = termcolors.make_style(fg='green', opts=('bold'),) style.REGULAR = termcolors.make_style() return style
def color_style(): style = color.color_style() style.FILTER = termcolors.make_style(fg="yellow", opts=("bold",)) style.MODULE_NAME = termcolors.make_style(fg="green", opts=("bold",)) style.TAG = termcolors.make_style(fg="red", opts=("bold",)) style.TAGLIB = termcolors.make_style(fg="blue", opts=("bold",)) return style
def otree_cli(): """ This function is the entry point for the ``otree`` console script. """ # We need to add the current directory to the python path as this is not # set by default when no using "python <script>" but a standalone script # like ``otree``. if os.getcwd() not in sys.path: sys.path.insert(0, os.getcwd()) os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings') try: from django.conf import settings settings.INSTALLED_APPS except ImportError: style = color_style() msg = style.ERROR( "Cannot import otree settings.\n" "Please make sure that you are in the base directory of your " "oTree library checkout. This directory contains a settings.py " "and a manage.py file.") print(msg) sys.exit(1) # some commands don't need the setings.INSTALLED_APPS # see: https://github.com/oTree-org/otree-core/issues/388 ignore_apps = IGNORE_APPS_COMMANDS.intersection(sys.argv) if ignore_apps: settings.INSTALLED_APPS = tuple(settings.NO_EXPERIMENT_APPS) execute_from_command_line(sys.argv, 'otree')
def color_style(): style = color.color_style() style.URL = termcolors.make_style(fg='green', opts=('bold',)) style.MODULE = termcolors.make_style(fg='yellow') style.MODULE_NAME = termcolors.make_style(opts=('bold',)) style.URL_NAME = termcolors.make_style(fg='red') return style
def __init__(self, *args, **kwargs): from django.conf import settings self.admin_media_prefix = settings.ADMIN_MEDIA_PREFIX # We set self.path to avoid crashes in log_message() on unsupported # requests (like "OPTIONS"). self.path = '' self.style = color_style() BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
def __init__(self, stdout=None, stderr=None, no_color=False): self.stdout = OutputWrapper(stdout or sys.stdout) self.stderr = OutputWrapper(stderr or sys.stderr) if no_color: self.style = no_style() else: self.style = color_style() self.stderr.style_func = self.style.ERROR
def update_proxy_model_permissions(apps, schema_editor, reverse=False): """ Update the content_type of proxy model permissions to use the ContentType of the proxy model. """ style = color_style() Permission = apps.get_model('auth', 'Permission') ContentType = apps.get_model('contenttypes', 'ContentType') alias = schema_editor.connection.alias for Model in apps.get_models(): opts = Model._meta if not opts.proxy: continue proxy_default_permissions_codenames = [ '%s_%s' % (action, opts.model_name) for action in opts.default_permissions ] permissions_query = Q(codename__in=proxy_default_permissions_codenames) for codename, name in opts.permissions: permissions_query = permissions_query | Q(codename=codename, name=name) content_type_manager = ContentType.objects.db_manager(alias) concrete_content_type = content_type_manager.get_for_model( Model, for_concrete_model=True) proxy_content_type = content_type_manager.get_for_model( Model, for_concrete_model=False) old_content_type = proxy_content_type if reverse else concrete_content_type new_content_type = concrete_content_type if reverse else proxy_content_type try: with transaction.atomic(using=alias): Permission.objects.using(alias).filter( permissions_query, content_type=old_content_type, ).update(content_type=new_content_type) except IntegrityError: old = '{}_{}'.format(old_content_type.app_label, old_content_type.model) new = '{}_{}'.format(new_content_type.app_label, new_content_type.model) sys.stdout.write( style.WARNING( WARNING.format(old=old, new=new, query=permissions_query)))
def main_help_text(self, commands_only=False): """ Returns the script's main help text, as a string. """ if commands_only: usage = sorted(get_commands().keys()) else: usage = [ "", "Type '%s help <subcommand>' for " "help on a specific subcommand." % self.prog_name, "", "Available subcommands:", ] commands = {} for app_config in reversed(list(apps.get_app_configs())): path = os.path.join(app_config.path, 'management') if app_config.name.startswith('lico'): commands.update( {name: app_config for name in find_commands(path)}) commands_dict = defaultdict(lambda: []) for name, app_config in six.iteritems(commands): commands_dict[app_config.verbose_name].append(name) style = color_style() for app in sorted(commands_dict.keys()): usage.append("") usage.append(style.NOTICE("[%s]" % app)) for name in sorted(commands_dict[app]): usage.append(" %s" % name) # Output an extra note if settings are not properly configured if self.settings_exception is not None: usage.append( style.NOTICE( "Note that only Django core commands are listed " "as settings are not properly configured (error: %s)." % self.settings_exception)) return '\n'.join(usage)
def run_migrations(args, options, executor_codename, schema_name, allow_atomic=True, idx=None, count=None): from django.core.management import color from django.core.management.base import OutputWrapper from django.db import connection style = color.color_style() def style_func(msg): percent_str = '' if idx is not None and count is not None and count > 0: percent_str = '%d/%d (%s%%) ' % (idx + 1, count, int(100 * (idx + 1) / count)) return '[%s%s:%s] %s' % (percent_str, style.NOTICE(executor_codename), style.NOTICE(schema_name), msg) connection.set_schema(schema_name) stdout = OutputWrapper(sys.stdout) stdout.style_func = style_func stderr = OutputWrapper(sys.stderr) stderr.style_func = style_func if int(options.get('verbosity', 1)) >= 1: stdout.write(style.NOTICE("=== Starting migration")) MigrateCommand(stdout=stdout, stderr=stderr).execute(*args, **options) try: transaction.commit() connection.close() connection.connection = None except transaction.TransactionManagementError: if not allow_atomic: raise # We are in atomic transaction, don't close connections pass connection.set_schema_to_public()
def set_werkzeug_log_color(): """Try to set color to the werkzeug log. """ from django.core.management.color import color_style from werkzeug.serving import WSGIRequestHandler from werkzeug._internal import _log _style = color_style() _orig_log = WSGIRequestHandler.log def werk_log(self, type, message, *args): try: msg = '%s - - [%s] %s' % ( self.address_string(), self.log_date_time_string(), message % args, ) http_code = str(args[1]) except: return _orig_log(type, message, *args) # Utilize terminal colors, if available if http_code[0] == '2': # Put 2XX first, since it should be the common case msg = _style.HTTP_SUCCESS(msg) elif http_code[0] == '1': msg = _style.HTTP_INFO(msg) elif http_code == '304': msg = _style.HTTP_NOT_MODIFIED(msg) elif http_code[0] == '3': msg = _style.HTTP_REDIRECT(msg) elif http_code == '404': msg = _style.HTTP_NOT_FOUND(msg) elif http_code[0] == '4': msg = _style.HTTP_BAD_REQUEST(msg) else: # Any 5XX, or any other response msg = _style.HTTP_SERVER_ERROR(msg) _log(type, msg) WSGIRequestHandler.log = werk_log
def handle(self, *args, **options): from django.conf import settings style = color_style() template_dirs = set(get_template_setting('DIRS')) template_dirs |= set(options.get('includes', [])) template_dirs |= set( getattr(settings, 'VALIDATE_TEMPLATES_EXTRA_TEMPLATE_DIRS', [])) # This is unsafe: # https://docs.djangoproject.com/en/1.10/topics/settings/#altering-settings-at-runtime if hasattr(settings, 'TEMPLATES'): settings.TEMPLATES[0]['DIRS'] = list(template_dirs) else: settings.TEMPLATE_DIRS = list(template_dirs) settings.TEMPLATE_DEBUG = True verbosity = int(options.get('verbosity', 1)) errors = 0 for template_dir in template_dirs: for root, dirs, filenames in os.walk(template_dir): for filename in filenames: if filename.endswith(".swp"): continue if filename.endswith("~"): continue filepath = os.path.join(root, filename) if verbosity > 1: print(filepath) try: get_template(filepath) except Exception as e: errors += 1 print("%s: %s" % (filepath, style.ERROR("%s %s" % (e.__class__.__name__, str(e))))) if errors and options.get('break', False): raise CommandError("Errors found") if errors: raise CommandError("%s errors found" % errors) print("%s errors found" % errors)
def validate_tag_fields(sender, **kwargs): ''' Validates ``TagField``s on models. ''' namespaces = set() for field in sender._meta.fields: if isinstance(field, TagField): if field.namespace in namespaces: import sys from django.core.management.color import color_style style = color_style() e = (u"You specified more than one tag field with the " u"namespace '%s' on the model '%s.%s'. Please make " u"sure that there are only tag fields with different " u"namespaces on the same model." % (field.namespace, sender._meta.app_label, sender._meta.object_name)) sys.stderr.write(style.ERROR(str('Error: %s\n' % e))) sys.exit(1) namespaces.add(field.namespace)
def execute(self, *args, **options): """ Try to execute this command, performing system checks if needed (as controlled by the ``requires_system_checks`` attribute, except if force-skipped). """ if options["force_color"] and options["no_color"]: raise CommandError( "The --no-color and --force-color options can't be used together." ) if options["force_color"]: self.style = color_style(force_color=True) elif options["no_color"]: self.style = no_style() self.stderr.style_func = None if options.get("stdout"): self.stdout = OutputWrapper(options["stdout"]) if options.get("stderr"): self.stderr = OutputWrapper(options["stderr"]) if self.requires_system_checks and not options["skip_checks"]: if self.requires_system_checks == ALL_CHECKS: self.check() else: self.check(tags=self.requires_system_checks) if self.requires_migrations_checks: self.check_migrations() output = self.handle(*args, **options) if output: if self.output_transaction: connection = connections[options.get("database", DEFAULT_DB_ALIAS)] output = "%s\n%s\n%s" % ( self.style.SQL_KEYWORD( connection.ops.start_transaction_sql()), output, self.style.SQL_KEYWORD( connection.ops.end_transaction_sql()), ) self.stdout.write(output) return output
def _fn(sender, **kwargs): if sender.__name__ != module_name: return cursor = connection.cursor() existing_columns = [ row[0] for row in connection.introspection.get_table_description( cursor, cls._meta.db_table) ] missing_columns = [] for field in cls._meta.fields: if field.column not in existing_columns: missing_columns.append(field) if not missing_columns: return style = color_style() print( style.ERROR( 'The following columns seem to be missing in the database table' ' %s:' % cls._meta.db_table)) for field in missing_columns: print('%s:%s%s' % ( style.SQL_KEYWORD(field.column), ' ' * (25 - len(field.column)), '%s.%s' % (field.__class__.__module__, field.__class__.__name__), )) print( style.NOTICE( '\nPlease consult the output of `python manage.py sql %s` to' ' find out what the correct column types are. (Or use south,' ' which is what you should be doing anyway.)\n' % (cls._meta.app_label)))
def populate_exam_run_fk(apps, schema_editor): ProctoredExamGrade = apps.get_model('grades', 'ProctoredExamGrade') ExamAuthorization = apps.get_model('exams', 'ExamAuthorization') style = color_style() fake_exam_grades = [] exam_grades = ProctoredExamGrade.objects.filter(exam_run__isnull=True) for exam_grade in exam_grades.iterator(): try: exam_grade.exam_run = ExamAuthorization.objects.get( id=exam_grade.client_authorization_id).exam_run exam_grade.save() except ValueError: fake_exam_grades.append(exam_grade) continue for fake_exam_grade in fake_exam_grades: sys.stdout.write( style.ERROR( '\nInvalid client_authorization_id {0} for ProctoredExamGrade {1}' .format(fake_exam_grade.client_authorization_id, fake_exam_grade.id)))
def update_proxy_model_permissions(apps, schema_editor, reverse=False): """ Update the content_type of proxy model permissions to use the ContentType of the proxy model. """ style = color_style() Permission = apps.get_model("auth", "Permission") ContentType = apps.get_model("contenttypes", "ContentType") for Model in apps.get_models(): opts = Model._meta if not opts.proxy: continue proxy_default_permissions_codenames = [ "%s_%s" % (action, opts.model_name) for action in opts.default_permissions ] permissions_query = Q(codename__in=proxy_default_permissions_codenames) for codename, name in opts.permissions: permissions_query = permissions_query | Q(codename=codename, name=name) concrete_content_type = ContentType.objects.get_for_model( Model, for_concrete_model=True) proxy_content_type = ContentType.objects.get_for_model( Model, for_concrete_model=False) old_content_type = proxy_content_type if reverse else concrete_content_type new_content_type = concrete_content_type if reverse else proxy_content_type try: with transaction.atomic(): Permission.objects.filter( permissions_query, content_type=old_content_type).update( content_type=new_content_type) except IntegrityError: old = "{}_{}".format(old_content_type.app_label, old_content_type.model) new = "{}_{}".format(new_content_type.app_label, new_content_type.model) sys.stdout.write( style.WARNING( WARNING.format(old=old, new=new, query=permissions_query)))
def autodiscover(self, module_name=None, verbose=True): if 'migrate' not in sys.argv and 'makemigrations' not in sys.argv: module_name = module_name or 'list_data' writer = sys.stdout.write if verbose else lambda x: x style = color_style() writer(f' * checking for site {module_name} ...\n') for app in django_apps.app_configs: writer(f' * searching {app} \r') try: mod = import_module(app) try: import_module(f'{app}.{module_name}') writer( f' * loading \'{module_name}\' from \'{app}\'\n') except PreloadDataError as e: writer(f' - loading {app}.{module_name} ... ') writer(style.ERROR(f'ERROR! {e}\n')) except ImportError as e: if module_has_submodule(mod, module_name): raise SiteListDataError(e) except ImportError: pass
def otree_heroku_cli(): """ This function is the entry point for the ``otree-heroku`` console script. """ # We need to add the current directory to the python path as this is not # set by default when no using "python <script>" but a standalone script # like ``otree``. if os.getcwd() not in sys.path: sys.path.insert(0, os.getcwd()) os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings') style = color_style() try: from django.conf import settings settings.INSTALLED_APPS except ImportError: print(style.ERROR(SETTINGS_NOT_FOUND_MESSAGE)) sys.exit(1) otree.management.deploy.heroku.execute_from_command_line(sys.argv)
def copy_template(template_name, copy_to, **options): """copies the specified template directory to the copy_to location""" import django_extensions style = color_style() ERROR = getattr(style, 'ERROR', lambda x: x) SUCCESS = getattr(style, 'SUCCESS', lambda x: x) template_dir = os.path.join(django_extensions.__path__[0], 'conf', template_name) # walks the template structure and copies it for d, subdirs, files in os.walk(template_dir): relative_dir = d[len(template_dir) + 1:] if relative_dir and not os.path.exists(os.path.join(copy_to, relative_dir)): os.mkdir(os.path.join(copy_to, relative_dir)) for i, subdir in enumerate(subdirs): if subdir.startswith('.'): del subdirs[i] for f in files: if f.endswith('.pyc') or f.startswith('.DS_Store'): continue path_old = os.path.join(d, f) path_new = os.path.join(copy_to, relative_dir, f).rstrip(".tmpl") if os.path.exists(path_new): if options.get('verbosity', 1) > 1: print(ERROR("%s already exists" % path_new)) continue if options.get('verbosity', 1) > 1: print(SUCCESS("%s" % path_new)) with open(path_old, 'r') as fp_orig: with open(path_new, 'w') as fp_new: fp_new.write(fp_orig.read()) try: shutil.copymode(path_old, path_new) _make_writeable(path_new) except OSError: sys.stderr.write("Notice: Couldn't set permission bits on %s. You're probably using an uncommon filesystem setup. No problem.\n" % path_new)
def main_help_text(self, commands_only=False): """ Returns the script's main help text, as a string. """ if commands_only: usage = sorted(get_commands().keys()) else: usage = [ "", "Type '%s help <subcommand>' for help on a specific subcommand." % self.prog_name, "", "Available subcommands:", ] commands_dict = collections.defaultdict(lambda: []) for name, app in six.iteritems(get_commands()): if app == 'django.core': app = 'django' else: app = app.rpartition('.')[-1] commands_dict[app].append(name) style = color_style() for app in sorted(commands_dict.keys()): usage.append("") usage.append(style.NOTICE("[%s]" % app)) for name in sorted(commands_dict[app]): usage.append(" %s" % name) # Output an extra note if settings are not properly configured try: from django.conf import settings settings.INSTALLED_APPS except ImproperlyConfigured as e: usage.append( style.NOTICE( "Note that only Django core commands are listed as settings " "are not properly configured (error: %s)." % e)) return '\n'.join(usage)
def run_migrations(args, options, executor_codename, schema_name, allow_atomic=True): from django.core.management import color from django.core.management.base import OutputWrapper from django.db import connection style = color.color_style() def style_func(msg): return '[%s:%s] %s' % (style.NOTICE(executor_codename), style.NOTICE(schema_name), msg) stdout = OutputWrapper(sys.stdout) stdout.style_func = style_func stderr = OutputWrapper(sys.stderr) stderr.style_func = style_func if int(options.get('verbosity', 1)) >= 1: stdout.write( style.NOTICE("=== Running migrate for schema %s" % schema_name)) connection.set_schema(schema_name) MigrateCommand(stdout=stdout, stderr=stderr).execute(*args, **options) try: transaction.commit() connection.close() connection.connection = None except transaction.TransactionManagementError: if not allow_atomic: raise # We are in atomic transaction, don't close connections pass connection.set_schema_to_public()
def setUpClass(cls): super().setUpClass() style = color_style() sys.stdout.write( style.ERROR(f'{cls.__name__}. Overwriting survey dates.\n')) for survey_schedule in site_surveys.get_survey_schedules( group_name=cls.site_survey_group_name): for survey in survey_schedule.surveys: survey.start = survey.start - relativedelta( days=cls.study_tdelta.days) survey.end = survey.end - relativedelta( days=cls.study_tdelta.days) survey_schedule.start = (survey_schedule.start - relativedelta(days=cls.study_tdelta.days)) survey_schedule.end = (survey_schedule.end - relativedelta(days=cls.study_tdelta.days)) for survey_schedule in site_surveys.get_survey_schedules( group_name=cls.site_survey_group_name): sys.stdout.write( f' * {survey_schedule.name}: {survey_schedule.start} - ' f'{survey_schedule.end}\n')
def autodiscover(self, module_name=None, verbose=True): module_name = module_name or 'navbars' writer = sys.stdout.write if verbose else lambda x: x style = color_style() writer(f' * checking for site {module_name} ...\n') for app in django_apps.app_configs: writer(f' * searching {app} \r') try: mod = import_module(app) try: before_import_registry = copy.copy(site_navbars.registry) import_module(f'{app}.{module_name}') writer( f' * registered navbars \'{module_name}\' from \'{app}\'\n') except NavbarError as e: writer(f' - loading {app}.navbars ... ') writer(style.ERROR(f'ERROR! {e}\n')) except ImportError as e: site_navbars.registry = before_import_registry if module_has_submodule(mod, module_name): raise except ImportError: pass
def __init__(self, stdout=None, stderr=None, no_color=False, force_color=False): self.stdout = OutputWrapper(stdout or sys.stdout) self.stderr = OutputWrapper(stderr or sys.stderr) if no_color and force_color: raise CommandError("'no_color' and 'force_color' can't be used together.") if no_color: self.style = no_style() else: self.style = color_style(force_color) self.stderr.style_func = self.style.ERROR if self.requires_system_checks in [False, True]: warnings.warn( "Using a boolean value for requires_system_checks is " "deprecated. Use '__all__' instead of True, and [] (an empty " "list) instead of False.", RemovedInDjango41Warning, ) self.requires_system_checks = ALL_CHECKS if self.requires_system_checks else [] if ( not isinstance(self.requires_system_checks, (list, tuple)) and self.requires_system_checks != ALL_CHECKS ): raise TypeError('requires_system_checks must be a list or tuple.')
def main_help_text(self, commands_only=False): """ Returns the script's main help text, as a string. """ if commands_only: usage = sorted(self.get_commands().keys()) else: second_line = ("Type {} help <subcommand>' for help on a specific " "subcommand.").format(self.prog_name) usage = ["", second_line, "", "Available subcommands:"] commands_dict = defaultdict(lambda: []) for name, app in six.iteritems(self.get_commands()): if app == 'django.core': app = 'django' else: app = app.rpartition('.')[-1] commands_dict[app].append(name) style = color_style() for app in sorted(commands_dict.keys()): usage.append("") usage.append(style.NOTICE("[%s]" % app)) for name in sorted(commands_dict[app]): helptext = " ".join( self.fetch_command(name).help.splitlines()) helptext = self.limit_text(helptext, 80) usage.append(" {} - {}".format(name, helptext)) # Output an extra note if settings are not properly configured if self.settings_exception is not None: usage.append( style.NOTICE( "Note that only Django core commands are listed " "as settings are not properly configured (error: %s)." % self.settings_exception)) return '\n'.join(usage)
def __init__(self): self.style = color_style() # `requires_model_validation` is deprecated in favor of # `requires_system_checks`. If both options are present, an error is # raised. Otherwise the present option is used. If none of them is # defined, the default value (True) is used. has_old_option = hasattr(self, 'requires_model_validation') has_new_option = hasattr(self, 'requires_system_checks') if has_old_option: warnings.warn( '"requires_model_validation" is deprecated ' 'in favor of "requires_system_checks".', RemovedInDjango19Warning) if has_old_option and has_new_option: raise ImproperlyConfigured( 'Command %s defines both "requires_model_validation" ' 'and "requires_system_checks", which is illegal. Use only ' '"requires_system_checks".' % self.__class__.__name__) self.requires_system_checks = ( self.requires_system_checks if has_new_option else self.requires_model_validation if has_old_option else True)
def bsync_request(self, **options): """ Performs the bsync request. """ style = color_style() verbosity = int(options['verbosity']) bsync_port = options['bsync_port'] if options['bsync_port']: sub_cmd = " --port {}".format(bsync_port) else: sub_cmd = "" cmd = "browser-sync reload{}".format(sub_cmd) # host = 'localhost:%s' % options['bsync_port'] try: os.system(cmd) #urlopen('http://%s/changed?files=.' % host) self.message('bsync request emitted. cmd ={}\n'.format(cmd), verbosity, style.HTTP_INFO) except IOError: pass
def create_empty_source_table(self, clazz, source_table_name, source_db_connection, extra_fields={}): project = Project(key='sutter_county', id=0) db_entity = DbEntity(key=Keys.DB_ABSTRACT_BASE_AGRICULTURE_FEATURE, feature_class_configuration=dict( abstract_class=full_module_path(clazz))) SourceClass = FeatureClassCreator(project, db_entity, no_ensure=True).dynamic_model_class( base_only=True, schema='public', table=source_table_name) # class SourceClass(clazz): # class Meta(clazz.Meta): # db_table = source_table_name create_tables_for_dynamic_classes(SourceClass) for field in SourceClass._meta.fields[1:]: setattr(field, 'null', True) drop_table = "DROP TABLE IF EXISTS {final_table} CASCADE;".format( final_table=source_table_name) sql, refs = source_db_connection.creation.sql_create_model( SourceClass, color_style()) add_geometry_fields = ''' ALTER TABLE {final_table} ADD COLUMN geography_id VARCHAR; ALTER TABLE {final_table} ADD COLUMN wkb_geometry GEOMETRY;'''.format( final_table=source_table_name) sql = drop_table + sql[0] + add_geometry_fields for dbfield, fieldtype in extra_fields.items(): sql += 'ALTER TABLE {final_table} ADD COLUMN {field} {type}'.format( final_table=source_table_name, field=dbfield, type=fieldtype) source_db_connection.cursor().execute(sql)
def main_help_text(self, commands_only=False): """Return the script's main help text, as a string.""" if commands_only: usage = sorted(get_commands()) else: usage = [ "", "Type '%s help <subcommand>' for help on a specific subcommand." % self.prog_name, "", "Available subcommands:", ] commands_dict = defaultdict(lambda: []) for name, app in get_commands().items(): if app == "django.core": app = "django" else: app = app.rpartition(".")[-1] commands_dict[app].append(name) style = color_style() for app in sorted(commands_dict): usage.append("") usage.append(style.NOTICE("[%s]" % app)) for name in sorted(commands_dict[app]): usage.append(" %s" % name) # Output an extra note if settings are not properly configured if self.settings_exception is not None: usage.append( style.NOTICE( "Note that only Django core commands are listed " "as settings are not properly configured (error: %s)." % self.settings_exception ) ) return "\n".join(usage)
class Progress(BaseCommand, ABC): stdout = OutputWrapper(sys.stdout) style = color_style() @classmethod def show_progress(cls, progress, instance_name): width = 40 points = int(width * progress) backspaces = width - points bar = ('[' + '.' * points + ' ' * backspaces + '] ' + str( int(progress * 100)) + ' %') text = f'Populating {instance_name} '.ljust(25) cls.stdout.write( cls.style.SUCCESS(text + bar), ending='\r' ) @classmethod def report_success(cls, number, instance_name): cls.stdout.write(' ' * 100, ending='\r') cls.stdout.write( cls.style.SUCCESS( f'Successfully created {number} {instance_name}' ))
def __init__(self): self.style = color_style()
def __init__(self, *args, **kwargs): self.style = color_style() super(WSGIRequestHandler, self).__init__(*args, **kwargs)
def __init__(self, *args, **kwargs): super(DjangoColorsFormatter, self).__init__(*args, **kwargs) self.style = self.configure_style(color_style())
def __init__(self, outfile=sys.stdout): self.errors = [] self.outfile = outfile self.style = color_style()
import sys from django.apps import AppConfig as DjangoAppConfig from django.core.management.color import color_style from .site_list_data import site_list_data from edc_list_data.site_list_data import SiteListDataError style = color_style() """ If you need list data in your tests add to your test case: @classmethod def setUpClass(cls): site_list_data.autodiscover() super().setUpClass() @classmethod def tearDownClass(cls): super().tearDownClass() """ class AppConfig(DjangoAppConfig): name = 'edc_list_data' verbose_name = 'Edc List Data' def ready(self): sys.stdout.write(f'Loading {self.verbose_name} ...\n')
def __init__(self, *args, **kwargs): self.style = color_style() super(ServerFormatter, self).__init__(*args, **kwargs)