def run_flake8(file_contents, extra_args=None): with open(str(TMP_DIR / "example.py"), 'w') as tempf: tempf.write(dedent(file_contents).strip() + '\n') orig_dir = os.getcwd() os.chdir(str(TMP_DIR)) orig_args = sys.argv try: # Can't pass args to flake8 but can set to sys.argv sys.argv = [ 'flake8', '--jobs', '1', '--exit-zero', 'example.py', ] if extra_args: sys.argv.extend(extra_args) # Run it with captured_stdout() as stdout: flake8_main() out = stdout.getvalue().strip() lines = out.split('\n') if lines[-1] == '': lines = lines[:-1] return lines finally: sys.argv = orig_args os.chdir(orig_dir)
def run_flake8(): print('Running flake8 code linting') try: original_argv = sys.argv sys.argv = ['flake8'] + CODE_PATHS did_fail = False flake8_main() except SystemExit: did_fail = True finally: sys.argv = original_argv print('flake8 failed' if did_fail else 'flake8 passed') return did_fail