def run(argv, show_reports=False): linter = lint.PyLinter() checkers.initialize(linter) AstCheckers.register(linter) for warning in DISABLED_WARNINGS: linter.disable(warning) linter.set_option('ignore', ['local_settings.py', 'migrations']) linter.set_option('include-ids', True) if not show_reports: linter.set_option('reports', False) if not argv: # django lint uses deprecated style of pylint warning and we are not # interested in seeing warnings about this with warnings.catch_warnings(): targets = [x for x in os.listdir('.') if (os.path.isdir(x) and os.path.exists('%s/__init__.py' % (x,)))] warnings.simplefilter('ignore') linter.check(targets) else: files = linter.load_command_line_configuration(argv) linter.check(files)
def main(): usage = """ %prog [options] target Django Lint is a tool that statically analyses Django projects and applications, checking for programming errors and bad code smells. For example, it reports nullable "CharField" fields, as well as reporting for unspecified options in settings.py. The `target` argument is mandatory and can specify either a directory containing a Django project, a single application or a single file. """.rstrip() parser = OptionParser(usage=usage) parser.add_option( '-r', '--reports', dest='report', action='store_true', default=False, help='generate report', ) parser.add_option( '-p', '--pylint', dest='pylint', action='store_true', default=False, help='run normal PyLint checks', ) parser.add_option( '-e', '--errors', dest='errors', action='store_true', default=False, help='only show errors', ) parser.add_option( '-f', '--format', dest='outputformat', metavar='OUTPUT', default='text', help='Set the output format. Available formats are text,' 'parseable, colorized, msvs (visual studio) and html', ) options, args = parser.parse_args() try: args[0] except IndexError: args = ['.'] targets = [os.path.abspath(arg) for arg in args] for target in targets: if not os.path.exists(target): try: # Is target a module? x = __import__(args[0], locals(), globals(), [], -1) target = sys.modules[args[0]].__path__[0] except: pass if not os.path.exists(target): raise parser.error( "The specified target (%r) does not exist" \ % target ) path = target while True: flag = False for django_file in ('manage.py', 'models.py', 'urls.py'): if os.path.exists(os.path.join(path, django_file)): sys.path.insert(0, os.path.dirname(path)) flag = True break if flag: break path = os.path.dirname(path) if path == '/': raise parser.error( "The specified target (%r) does not appear to be part of a " \ "Django application" % target ) try: import django except ImportError: print >>sys.stderr, "E: Cannot import `django' module, exiting.." return 1 linter = lint.PyLinter() linter.set_option('reports', options.report) linter.set_option('output-format', options.outputformat) if options.pylint: checkers.initialize(linter) for msg in ('C0111', 'C0301'): linter.disable(msg) AstCheckers.register(linter) if options.errors: linter.set_option('disable-msg-cat', 'WCRI') linter.set_option('reports', False) linter.set_option('persistent', False) linter.check(targets) return linter.msg_status
def main(): usage = """ %prog [options] target Django Lint is a tool that statically analyses Django projects and applications, checking for programming errors and bad code smells. For example, it reports nullable "CharField" fields, as well as reporting for unspecified options in settings.py. The `target` argument is mandatory and can specify either a directory containing a Django project, a single application or a single file. """.rstrip() parser = OptionParser(usage=usage) parser.add_option( '-r', '--reports', dest='report', action='store_true', default=False, help='generate report', ) parser.add_option( '-p', '--pylint', dest='pylint', action='store_true', default=False, help='run normal PyLint checks', ) parser.add_option( '-e', '--errors', dest='errors', action='store_true', default=False, help='only show errors', ) options, args = parser.parse_args() try: target = args[0] except IndexError: target = '.' target = os.path.abspath(target) if not os.path.exists(target): raise parser.error( "The specified target (%r) does not exist" \ % target ) path = target while True: flag = False for django_file in ('manage.py', 'models.py', 'urls.py'): if os.path.exists(os.path.join(path, django_file)): sys.path.insert(0, os.path.dirname(path)) flag = True break if flag: break path = os.path.dirname(path) if path == '/': raise parser.error( "The specified target (%r) does not appear to be part of a " \ "Django application" % target ) try: import django except ImportError: print >>sys.stderr, "E: Cannot import `django' module, exiting.." return 1 linter = lint.PyLinter() linter.set_option('reports', options.report) if options.errors: linter.set_option('disable-msg-cat', 'WCRI') linter.set_option('reports', False) linter.set_option('persistent', False) if options.pylint: checkers.initialize(linter) for msg in ('C0111', 'C0301'): linter.disable_message(msg) AstCheckers.register(linter) linter.check([target]) return linter.msg_status