def run(paths, linters, fmt, outgoing, workdir, edit, list_linters=None, **lintargs): from mozlint import LintRoller, formatters from mozlint.editor import edit_results if list_linters: lint_paths = find_linters(linters) print("Available linters: {}".format( [os.path.splitext(os.path.basename(l))[0] for l in lint_paths] )) return 0 lint = LintRoller(**lintargs) lint.read(find_linters(linters)) # run all linters results = lint.roll(paths, outgoing=outgoing, workdir=workdir) if edit and results: edit_results(results) results = lint.roll(results.keys()) formatter = formatters.get(fmt) # Encode output with 'replace' to avoid UnicodeEncodeErrors on # environments that aren't using utf-8. out = formatter(results, failed=lint.failed).encode( sys.stdout.encoding or 'ascii', 'replace') if out: print(out) return 1 if results or lint.failed else 0
def run(paths, linters, formats, outgoing, workdir, edit, setup=False, list_linters=False, num_procs=None, **lintargs): from mozlint import LintRoller, formatters from mozlint.editor import edit_issues if list_linters: lint_paths = find_linters(linters) print("Available linters: {}".format( [os.path.splitext(os.path.basename(l))[0] for l in lint_paths])) return 0 lint = LintRoller(**lintargs) lint.read(find_linters(linters)) # Always run bootstrapping, but return early if --setup was passed in. ret = lint.setup() if setup: return ret # run all linters result = lint.roll(paths, outgoing=outgoing, workdir=workdir, num_procs=num_procs) if edit and result.issues: edit_issues(result) result = lint.roll(result.issues.keys(), num_procs=num_procs) for formatter_name, path in formats: formatter = formatters.get(formatter_name) out = formatter(result) if out: fh = open(path, 'w') if path else sys.stdout if not path and fh.encoding == 'ascii': # If sys.stdout.encoding is ascii, printing output will fail # due to the stylish formatter's use of unicode characters. # Ideally the user should fix their environment by setting # `LC_ALL=C.UTF-8` or similar. But this is a common enough # problem that we help them out a little here by manually # encoding and writing to the stdout buffer directly. out += '\n' fh.buffer.write(out.encode('utf-8', errors='replace')) fh.buffer.flush() else: print(out, file=fh) return result.returncode
def lint_setup(self, linters=None, **lintargs): from mozlint import LintRoller lint_files = self.find_linters(linters) lint = LintRoller(lintargs=lintargs) lint.read(lint_files) for l in lint.linters: if 'setup' in l: l['setup']()
def run(paths, linters, fmt, rev, workdir, **lintargs): from mozlint import LintRoller, formatters lint = LintRoller(**lintargs) lint.read(find_linters(linters)) # run all linters results = lint.roll(paths, rev=rev, workdir=workdir) formatter = formatters.get(fmt) print(formatter(results)) return 1 if results else 0
class TestLintRoller(TestCase): def __init__(self, *args, **kwargs): TestCase.__init__(self, *args, **kwargs) self.filedir = os.path.join(here, 'files') self.files = [ os.path.join(self.filedir, f) for f in os.listdir(self.filedir) ] self.lintdir = os.path.join(here, 'linters') names = ('string.lint', 'regex.lint', 'external.lint') self.linters = [os.path.join(self.lintdir, n) for n in names] def setUp(self): TestCase.setUp(self) self.lint = LintRoller(root=here) def test_roll_no_linters_configured(self): with self.assertRaises(LintersNotConfigured): self.lint.roll(self.files) def test_roll_successful(self): self.lint.read(self.linters) result = self.lint.roll(self.files) self.assertEqual(len(result), 1) path = result.keys()[0] self.assertEqual(os.path.basename(path), 'foobar.js') errors = result[path] self.assertIsInstance(errors, list) self.assertEqual(len(errors), 6) container = errors[0] self.assertIsInstance(container, ResultContainer) self.assertEqual(container.rule, 'no-foobar') def test_roll_catch_exception(self): self.lint.read(os.path.join(self.lintdir, 'raises.lint')) # suppress printed traceback from test output old_stderr = sys.stderr sys.stderr = open(os.devnull, 'w') with self.assertRaises(LintException): self.lint.roll(self.files) sys.stderr = old_stderr def test_roll_with_excluded_path(self): self.lint.lintargs.update({'exclude': ['**/foobar.js']}) self.lint.read(self.linters) result = self.lint.roll(self.files) self.assertEqual(len(result), 0) def test_roll_with_invalid_extension(self): self.lint.read(os.path.join(self.lintdir, 'external.lint')) result = self.lint.roll(os.path.join(self.filedir, 'foobar.py')) self.assertEqual(len(result), 0)
class TestLintRoller(TestCase): def __init__(self, *args, **kwargs): TestCase.__init__(self, *args, **kwargs) self.filedir = os.path.join(here, 'files') self.files = [os.path.join(self.filedir, f) for f in os.listdir(self.filedir)] self.lintdir = os.path.join(here, 'linters') names = ('string.lint', 'regex.lint', 'external.lint') self.linters = [os.path.join(self.lintdir, n) for n in names] def setUp(self): TestCase.setUp(self) self.lint = LintRoller(root=here) def test_roll_no_linters_configured(self): with self.assertRaises(LintersNotConfigured): self.lint.roll(self.files) def test_roll_successful(self): self.lint.read(self.linters) result = self.lint.roll(self.files) self.assertEqual(len(result), 1) path = result.keys()[0] self.assertEqual(os.path.basename(path), 'foobar.js') errors = result[path] self.assertIsInstance(errors, list) self.assertEqual(len(errors), 6) container = errors[0] self.assertIsInstance(container, ResultContainer) self.assertEqual(container.rule, 'no-foobar') def test_roll_catch_exception(self): self.lint.read(os.path.join(self.lintdir, 'raises.lint')) # suppress printed traceback from test output old_stderr = sys.stderr sys.stderr = open(os.devnull, 'w') with self.assertRaises(LintException): self.lint.roll(self.files) sys.stderr = old_stderr def test_roll_with_excluded_path(self): self.lint.lintargs.update({'exclude': ['**/foobar.js']}) self.lint.read(self.linters) result = self.lint.roll(self.files) self.assertEqual(len(result), 0) def test_roll_with_invalid_extension(self): self.lint.read(os.path.join(self.lintdir, 'external.lint')) result = self.lint.roll(os.path.join(self.filedir, 'foobar.py')) self.assertEqual(len(result), 0)
class TestLinterTypes(TestCase): def __init__(self, *args, **kwargs): TestCase.__init__(self, *args, **kwargs) self.lintdir = os.path.join(here, 'linters') self.filedir = os.path.join(here, 'files') self.files = [ os.path.join(self.filedir, f) for f in os.listdir(self.filedir) ] def setUp(self): TestCase.setUp(self) self.lint = LintRoller(root=here) def path(self, name): return os.path.join(self.filedir, name) def test_string_linter(self): self.lint.read(os.path.join(self.lintdir, 'string.lint')) result = self.lint.roll(self.files) self.assertIsInstance(result, dict) self.assertIn(self.path('foobar.js'), result.keys()) self.assertNotIn(self.path('no_foobar.js'), result.keys()) result = result[self.path('foobar.js')][0] self.assertIsInstance(result, ResultContainer) self.assertEqual(result.linter, 'StringLinter') def test_regex_linter(self): self.lint.read(os.path.join(self.lintdir, 'regex.lint')) result = self.lint.roll(self.files) self.assertIsInstance(result, dict) self.assertIn(self.path('foobar.js'), result.keys()) self.assertNotIn(self.path('no_foobar.js'), result.keys()) result = result[self.path('foobar.js')][0] self.assertIsInstance(result, ResultContainer) self.assertEqual(result.linter, 'RegexLinter') def test_external_linter(self): self.lint.read(os.path.join(self.lintdir, 'external.lint')) result = self.lint.roll(self.files) self.assertIsInstance(result, dict) self.assertIn(self.path('foobar.js'), result.keys()) self.assertNotIn(self.path('no_foobar.js'), result.keys()) result = result[self.path('foobar.js')][0] self.assertIsInstance(result, ResultContainer) self.assertEqual(result.linter, 'ExternalLinter') def test_no_filter(self): self.lint.read(os.path.join(self.lintdir, 'explicit_path.lint')) result = self.lint.roll(self.files) self.assertEqual(len(result), 0) self.lint.lintargs['use_filters'] = False result = self.lint.roll(self.files) self.assertEqual(len(result), 2)
class TestLinterTypes(TestCase): def __init__(self, *args, **kwargs): TestCase.__init__(self, *args, **kwargs) self.lintdir = os.path.join(here, 'linters') self.filedir = os.path.join(here, 'files') self.files = [os.path.join(self.filedir, f) for f in os.listdir(self.filedir)] def setUp(self): TestCase.setUp(self) self.lint = LintRoller() def path(self, name): return os.path.join(self.filedir, name) def test_string_linter(self): self.lint.read(os.path.join(self.lintdir, 'string.lint')) result = self.lint.roll(self.files) self.assertIsInstance(result, dict) self.assertIn(self.path('foobar.js'), result.keys()) self.assertNotIn(self.path('no_foobar.js'), result.keys()) result = result[self.path('foobar.js')][0] self.assertIsInstance(result, ResultContainer) self.assertEqual(result.linter, 'StringLinter') def test_regex_linter(self): self.lint.read(os.path.join(self.lintdir, 'regex.lint')) result = self.lint.roll(self.files) self.assertIsInstance(result, dict) self.assertIn(self.path('foobar.js'), result.keys()) self.assertNotIn(self.path('no_foobar.js'), result.keys()) result = result[self.path('foobar.js')][0] self.assertIsInstance(result, ResultContainer) self.assertEqual(result.linter, 'RegexLinter') def test_external_linter(self): self.lint.read(os.path.join(self.lintdir, 'external.lint')) result = self.lint.roll(self.files) self.assertIsInstance(result, dict) self.assertIn(self.path('foobar.js'), result.keys()) self.assertNotIn(self.path('no_foobar.js'), result.keys()) result = result[self.path('foobar.js')][0] self.assertIsInstance(result, ResultContainer) self.assertEqual(result.linter, 'ExternalLinter') def test_no_filter(self): self.lint.read(os.path.join(self.lintdir, 'explicit_path.lint')) result = self.lint.roll(self.files) self.assertEqual(len(result), 0) self.lint.lintargs['use_filters'] = False result = self.lint.roll(self.files) self.assertEqual(len(result), 1)
def run(paths, linters, formats, outgoing, workdir, edit, setup=False, list_linters=False, **lintargs): from mozlint import LintRoller, formatters from mozlint.editor import edit_issues if list_linters: lint_paths = find_linters(linters) print("Available linters: {}".format( [os.path.splitext(os.path.basename(l))[0] for l in lint_paths])) return 0 lint = LintRoller(**lintargs) lint.read(find_linters(linters)) # Always run bootstrapping, but return early if --setup was passed in. ret = lint.setup() if setup: return ret # run all linters result = lint.roll(paths, outgoing=outgoing, workdir=workdir) if edit and result.issues: edit_issues(result) result = lint.roll(result.issues.keys()) for formatter_name, path in formats: formatter = formatters.get(formatter_name) out = formatter(result) if sys.version_info[0] == 2: # Encode output with 'replace' to avoid UnicodeEncodeErrors on # environments that aren't using utf-8. out = formatter(result).encode(sys.stdout.encoding or 'ascii', 'replace') if out: output_file = open(path, 'w') if path else sys.stdout print(out, file=output_file) return result.returncode
def run(paths, linters, fmt, rev, workdir, **lintargs): from mozlint import LintRoller, formatters lint = LintRoller(**lintargs) lint.read(find_linters(linters)) # run all linters results = lint.roll(paths, rev=rev, workdir=workdir) formatter = formatters.get(fmt) # Explicitly utf-8 encode the output as some of the formatters make # use of unicode characters. This will prevent a UnicodeEncodeError # on environments where utf-8 isn't the default print(formatter(results).encode('utf-8', 'replace')) return lint.return_code
def run(paths, linters, fmt, rev, workdir, **lintargs): from mozlint import LintRoller, formatters lint = LintRoller(**lintargs) lint.read(find_linters(linters)) # run all linters results = lint.roll(paths, rev=rev, workdir=workdir) formatter = formatters.get(fmt) # Encode output with 'replace' to avoid UnicodeEncodeErrors on # environments that aren't using utf-8. print(formatter(results, failed=lint.failed).encode( sys.stdout.encoding or 'ascii', 'replace')) return 1 if results or lint.failed else 0
def lint(self, paths, linters=None, fmt="stylish", **lintargs): """Run linters.""" from mozlint import LintRoller, formatters paths = paths or ["."] lint_files = self.find_linters(linters) lintargs["exclude"] = ["obj*"] lint = LintRoller(**lintargs) lint.read(lint_files) # run all linters results = lint.roll(paths) formatter = formatters.get(fmt) print(formatter(results))
def lint(self, paths, linters=None, fmt='stylish', **lintargs): """Run linters.""" from mozlint import LintRoller, formatters paths = paths or ['.'] lint_files = self.find_linters(linters) lintargs['exclude'] = ['obj*'] lint = LintRoller(**lintargs) lint.read(lint_files) # run all linters results = lint.roll(paths) formatter = formatters.get(fmt) print(formatter(results))
def run(paths, linters, fmt, outgoing, workdir, edit, list_linters=None, **lintargs): from mozlint import LintRoller, formatters if list_linters: lint_paths = find_linters(linters) print("Available linters: {}".format( [os.path.splitext(os.path.basename(l))[0] for l in lint_paths])) return 0 lint = LintRoller(**lintargs) lint.read(find_linters(linters)) # Check if the path that is entered is a valid one. invalid_paths = [path for path in paths if not os.path.exists(path)] if invalid_paths: print("Error: The following paths do not exist:\n{}".format( "\n".join(invalid_paths))) return 1 # run all linters results = lint.roll(paths, outgoing=outgoing, workdir=workdir) if edit: editor = os.environ['EDITOR'] for path in results: subprocess.call([editor, path]) return 1 if lint.failed else 0 formatter = formatters.get(fmt) # Encode output with 'replace' to avoid UnicodeEncodeErrors on # environments that aren't using utf-8. out = formatter(results, failed=lint.failed).encode(sys.stdout.encoding or 'ascii', 'replace') if out: print(out) return 1 if results or lint.failed else 0
def run(paths, linters, fmt, rev, workdir, **lintargs): from mozlint import LintRoller, formatters # Calculate files from VCS vcfiles = VCFiles() if rev: paths.extend(vcfiles.by_rev(rev)) if workdir: paths.extend(vcfiles.by_workdir()) paths = paths or ['.'] lint = LintRoller(**lintargs) lint.read(find_linters(linters)) # run all linters results = lint.roll(paths) formatter = formatters.get(fmt) print(formatter(results)) return 1 if results else 0
def lint(self, paths, linters=None, fmt='stylish', **lintargs): """Run linters.""" from mozlint import LintRoller, formatters paths = paths or ['.'] lint_files = self.find_linters(linters) lintargs['exclude'] = ['obj*'] lint = LintRoller(**lintargs) lint.read(lint_files) # run all linters results = lint.roll(paths) status = 0 if results: status = 1 formatter = formatters.get(fmt) print(formatter(results)) return status
class TestLintRoller(TestCase): def __init__(self, *args, **kwargs): TestCase.__init__(self, *args, **kwargs) filedir = os.path.join(here, "files") self.files = [os.path.join(filedir, f) for f in os.listdir(filedir)] self.lintdir = os.path.join(here, "linters") names = ("string.lint", "regex.lint", "external.lint") self.linters = [os.path.join(self.lintdir, n) for n in names] def setUp(self): TestCase.setUp(self) self.lint = LintRoller() def test_roll_no_linters_configured(self): with self.assertRaises(LintersNotConfigured): self.lint.roll(self.files) def test_roll_successful(self): self.lint.read(self.linters) result = self.lint.roll(self.files) self.assertEqual(len(result), 1) path = result.keys()[0] self.assertEqual(os.path.basename(path), "foobar.js") errors = result[path] self.assertIsInstance(errors, list) self.assertEqual(len(errors), 6) container = errors[0] self.assertIsInstance(container, ResultContainer) self.assertEqual(container.rule, "no-foobar") def test_roll_catch_exception(self): self.lint.read(os.path.join(self.lintdir, "raises.lint")) # suppress printed traceback from test output old_stderr = sys.stderr sys.stderr = open(os.devnull, "w") with self.assertRaises(LintException): self.lint.roll(self.files) sys.stderr = old_stderr def test_roll_with_excluded_path(self): self.lint.lintargs = {"exclude": ["**/foobar.js"]} self.lint.read(self.linters) result = self.lint.roll(self.files) self.assertEqual(len(result), 0)
def run( paths, linters, formats, outgoing, workdir, rev, edit, check_exclude_list, setup=False, list_linters=False, num_procs=None, virtualenv_manager=None, **lintargs ): from mozlint import LintRoller, formatters from mozlint.editor import edit_issues lintargs["config_paths"] = [ os.path.join(lintargs["root"], p) for p in lintargs["config_paths"] ] # Always perform exhaustive linting for exclude list paths lintargs["use_filters"] = lintargs["use_filters"] and not check_exclude_list if list_linters: lint_paths = find_linters(lintargs["config_paths"], linters) linters = [ os.path.splitext(os.path.basename(l))[0] for l in lint_paths["lint_paths"] ] print("\n".join(sorted(linters))) return 0 lint = LintRoller(**lintargs) linters_info = find_linters(lintargs["config_paths"], linters) result = None try: lint.read(linters_info["lint_paths"]) if check_exclude_list: if len(lint.linters) > 1: print("error: specify a single linter to check with `-l/--linter`") return 1 paths = lint.linters[0]["local_exclude"] if ( not linters and not paths and os.getcwd() == lint.root and not (outgoing or workdir) ): print( "warning: linting the entire repo takes a long time, using --outgoing and " "--workdir instead. If you want to lint the entire repo, run `./mach lint .`" ) # Setting the default values outgoing = "default" workdir = "all" # Always run bootstrapping, but return early if --setup was passed in. ret = lint.setup(virtualenv_manager=virtualenv_manager) if setup: return ret if linters_info["linters_not_found"] != []: raise NoValidLinter # run all linters result = lint.roll( paths, outgoing=outgoing, workdir=workdir, rev=rev, num_procs=num_procs ) except NoValidLinter as e: result = lint.result print(str(e)) if edit and result.issues: edit_issues(result) result = lint.roll(result.issues.keys(), num_procs=num_procs) for every in linters_info["linters_not_found"]: result.failed_setup.add(every) if check_exclude_list: # Get and display all those paths in the exclude list which are # now green and can be safely removed from the list out = get_exclude_list_output(result, paths) print(out, file=sys.stdout) return result.returncode for formatter_name, path in formats: formatter = formatters.get(formatter_name) out = formatter(result) if out: fh = open(path, "w") if path else sys.stdout if not path and fh.encoding == "ascii": # If sys.stdout.encoding is ascii, printing output will fail # due to the stylish formatter's use of unicode characters. # Ideally the user should fix their environment by setting # `LC_ALL=C.UTF-8` or similar. But this is a common enough # problem that we help them out a little here by manually # encoding and writing to the stdout buffer directly. out += "\n" fh.buffer.write(out.encode("utf-8", errors="replace")) fh.buffer.flush() else: print(out, file=fh) return result.returncode
def run( paths, linters, formats, outgoing, workdir, rev, edit, setup=False, list_linters=False, num_procs=None, virtualenv_manager=None, **lintargs ): from mozlint import LintRoller, formatters from mozlint.editor import edit_issues lintargs["config_paths"] = [ os.path.join(lintargs["root"], p) for p in lintargs["config_paths"] ] if list_linters: lint_paths = find_linters(lintargs["config_paths"], linters) linters = [ os.path.splitext(os.path.basename(l))[0] for l in lint_paths["lint_paths"] ] print("\n".join(sorted(linters))) return 0 lint = LintRoller(**lintargs) linters_info = find_linters(lintargs["config_paths"], linters) result = None try: lint.read(linters_info["lint_paths"]) # Always run bootstrapping, but return early if --setup was passed in. ret = lint.setup(virtualenv_manager=virtualenv_manager) if setup: return ret if linters_info["linters_not_found"] != []: raise NoValidLinter # run all linters result = lint.roll( paths, outgoing=outgoing, workdir=workdir, rev=rev, num_procs=num_procs ) except NoValidLinter as e: result = lint.result print(str(e)) if edit and result.issues: edit_issues(result) result = lint.roll(result.issues.keys(), num_procs=num_procs) for every in linters_info["linters_not_found"]: result.failed_setup.add(every) for formatter_name, path in formats: formatter = formatters.get(formatter_name) out = formatter(result) if out: fh = open(path, "w") if path else sys.stdout if not path and fh.encoding == "ascii": # If sys.stdout.encoding is ascii, printing output will fail # due to the stylish formatter's use of unicode characters. # Ideally the user should fix their environment by setting # `LC_ALL=C.UTF-8` or similar. But this is a common enough # problem that we help them out a little here by manually # encoding and writing to the stdout buffer directly. out += "\n" fh.buffer.write(out.encode("utf-8", errors="replace")) fh.buffer.flush() else: print(out, file=fh) return result.returncode