From c5d8cf8eb6a6e84347b9379945f308f29a2fe5cb Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 30 Nov 2018 03:27:41 -0500 Subject: [PATCH] Use pylint. --- build_extra/rust/get_rustc_info.py | 2 +- tools/benchmark.py | 5 +++-- tools/deno_dir_test.py | 5 ++++- tools/gcloud_upload.py | 2 +- tools/http_server.py | 8 ++++++-- tools/lint.py | 6 +++++- tools/permission_prompt_test.py | 6 +++++- tools/repl_test.py | 8 ++++++-- tools/setup.py | 6 +++--- tools/setup_test.py | 10 +++++----- tools/sha256sum.py | 8 ++++---- tools/sync_third_party.py | 3 ++- tools/test_format.py | 4 ++-- tools/third_party.py | 26 ++++++++++++++----------- tools/throughput_benchmark.py | 9 +++++++-- tools/unit_tests.py | 4 +++- tools/util.py | 31 ++++++++++++++++++------------ tools/util_test.py | 3 ++- 18 files changed, 93 insertions(+), 53 deletions(-) diff --git a/build_extra/rust/get_rustc_info.py b/build_extra/rust/get_rustc_info.py index 1b61bdbc04..66751c20b3 100755 --- a/build_extra/rust/get_rustc_info.py +++ b/build_extra/rust/get_rustc_info.py @@ -48,7 +48,7 @@ def get_ldflags(rustc_args): # On Posix systems, this file is directly executable thanks to it's shebang. # On Windows, we use a .cmd wrapper file. if os.name == "nt": - rustc_linker_base, rustc_linker_ext = path.splitext(__file__) + rustc_linker_base, _rustc_linker_ext = path.splitext(__file__) rustc_linker = rustc_linker_base + ".cmd" else: rustc_linker = __file__ diff --git a/tools/benchmark.py b/tools/benchmark.py index 760bad1009..fe71cc88e6 100755 --- a/tools/benchmark.py +++ b/tools/benchmark.py @@ -1,7 +1,8 @@ #!/usr/bin/env python # Copyright 2018 the Deno authors. All rights reserved. MIT license. # Performs benchmark and append data to //website/data.json. -# If //website/data.json doesn't exist, this script tries to import it from gh-pages branch. +# If //website/data.json doesn't exist, this script tries to import it from +# gh-pages branch. # To view the results locally run ./tools/http_server.py and visit # http://localhost:4545/website @@ -49,7 +50,7 @@ def import_data_from_gh_pages(): "https://github.com/denoland/deno.git", "gh-pages" ]) shutil.copy(gh_pages_data_file, all_data_file) - except: + except ValueError: write_json(all_data_file, []) # writes empty json data diff --git a/tools/deno_dir_test.py b/tools/deno_dir_test.py index bb07b75445..9f53cf3b9d 100755 --- a/tools/deno_dir_test.py +++ b/tools/deno_dir_test.py @@ -40,9 +40,12 @@ def run_deno(deno_exe, deno_dir=None): run(cmd, quiet=True, env=deno_dir_env) +USAGE = "./tools/deno_dir_test.py target/debug/deno target/debug/.deno_dir" + + def main(argv): if len(sys.argv) != 3: - print "Usage ./tools/deno_dir_test.py target/debug/deno target/debug/.deno_dir" + print "Usage: " + USAGE sys.exit(1) deno_dir_test(argv[1], argv[2]) diff --git a/tools/gcloud_upload.py b/tools/gcloud_upload.py index ed9c48ec86..b919a1ed98 100755 --- a/tools/gcloud_upload.py +++ b/tools/gcloud_upload.py @@ -14,7 +14,7 @@ from third_party import tp def print_usage(): - print "Usage: ./tools/gcloud_upload.py target/release/obj/third_party/v8/libv8.a" + print "Usage: ./tools/gcloud_upload.py target/release/libv8.a" sys.exit(1) diff --git a/tools/http_server.py b/tools/http_server.py index 065635c6f5..f9820ff195 100755 --- a/tools/http_server.py +++ b/tools/http_server.py @@ -62,7 +62,7 @@ def redirect_server(): Handler = RedirectHandler SocketServer.TCPServer.allow_reuse_address = True s = SocketServer.TCPServer(("", REDIRECT_PORT), Handler) - print "Deno redirect server http://localhost:%d/ -> http://localhost:%d/" % ( + print "redirect server http://localhost:%d/ -> http://localhost:%d/" % ( REDIRECT_PORT, PORT) return s @@ -82,7 +82,7 @@ def spawn(): return thread -if __name__ == '__main__': +def main(): try: thread = spawn() while thread.is_alive(): @@ -90,3 +90,7 @@ if __name__ == '__main__': except KeyboardInterrupt: pass sys.exit(1) + + +if __name__ == '__main__': + main() diff --git a/tools/lint.py b/tools/lint.py index f163a3264e..e297c04784 100755 --- a/tools/lint.py +++ b/tools/lint.py @@ -3,7 +3,8 @@ # Does google-lint on c++ files and ts-lint on typescript files import os -from util import enable_ansi_colors, run +import sys +from util import enable_ansi_colors, run, find_exts enable_ansi_colors() @@ -24,3 +25,6 @@ run([ "node", tslint, "./js/**/*_test.ts", "./tests/**/*.ts", "--exclude", "**/gen/**/*.ts", "--project", "tsconfig.json" ]) + +run([sys.executable, "third_party/depot_tools/pylint.py"] + + find_exts(["tools", "build_extra"], [".py"], skip=["tools/clang"])) diff --git a/tools/permission_prompt_test.py b/tools/permission_prompt_test.py index 2bc24d12ca..7f0ae1c972 100755 --- a/tools/permission_prompt_test.py +++ b/tools/permission_prompt_test.py @@ -138,6 +138,10 @@ def permission_prompt_test(deno_exe): p.test_net_no() -if __name__ == "__main__": +def main(): deno_exe = os.path.join(build_path(), "deno" + executable_suffix) permission_prompt_test(deno_exe) + + +if __name__ == "__main__": + main() diff --git a/tools/repl_test.py b/tools/repl_test.py index ce3db89534..afce192f52 100644 --- a/tools/repl_test.py +++ b/tools/repl_test.py @@ -35,7 +35,7 @@ class Repl(object): except CalledProcessError as e: p.kill() p.wait() - raise + raise e retcode = p.poll() # Ignore Windows CRLF (\r\n). return out.replace('\r\n', '\n'), err.replace('\r\n', '\n'), retcode @@ -125,6 +125,10 @@ def repl_tests(deno_exe): Repl(deno_exe).run() -if __name__ == "__main__": +def main(): deno_exe = os.path.join(build_path(), "deno" + executable_suffix) repl_tests(deno_exe) + + +if __name__ == "__main__": + main() diff --git a/tools/setup.py b/tools/setup.py index 44c1835539..8660ee6d7b 100755 --- a/tools/setup.py +++ b/tools/setup.py @@ -94,9 +94,9 @@ def write_gn_args(args_filename, args): assert gn_args_are_generated(lines) # With header -> generated. # Ensure the directory where args.gn goes exists. - dir = os.path.dirname(args_filename) - if not os.path.isdir(dir): - os.makedirs(dir) + d = os.path.dirname(args_filename) + if not os.path.isdir(d): + os.makedirs(d) with open(args_filename, "w") as f: f.write("\n".join(lines) + "\n") diff --git a/tools/setup_test.py b/tools/setup_test.py index 15dff00fae..bc0a9ecbac 100644 --- a/tools/setup_test.py +++ b/tools/setup_test.py @@ -40,22 +40,22 @@ def read_gn_args_test(): def write_gn_args_test(): # Build a nonexistent path; write_gn_args() should call mkdir as needed. - dir = mktemp() - filename = os.path.join(dir, "args.gn") - assert not os.path.exists(dir) + d = mktemp() + filename = os.path.join(d, "args.gn") + assert not os.path.exists(d) assert not os.path.exists(filename) # Write some args. args = ['lalala=42', 'foo_bar_baz="lorem ipsum dolor#amet"'] write_gn_args(filename, args) # Directory and args file should now be created. - assert os.path.isdir(dir) + assert os.path.isdir(d) assert os.path.isfile(filename) # Validate that the right contents were written. (check_args, hand_edited) = read_gn_args(filename) assert check_args == args assert hand_edited == False # Clean up. - rmtree(dir) + rmtree(d) def setup_test(): diff --git a/tools/sha256sum.py b/tools/sha256sum.py index d3273d5ba9..139368e893 100644 --- a/tools/sha256sum.py +++ b/tools/sha256sum.py @@ -55,15 +55,15 @@ def main(): hasher = sha256() for data in args.input: hasher.update(data) - hash = hasher.hexdigest() + h = hasher.hexdigest() # Format and write to specified out file (or the default, stdout). - args.outfile.write(args.format % hash) + args.outfile.write(args.format % h) def read_file(filename): - with open(filename, "rb") as file: - return file.read() + with open(filename, "rb") as f: + return f.read() if __name__ == '__main__': diff --git a/tools/sync_third_party.py b/tools/sync_third_party.py index 5fe78c407e..4205c48834 100755 --- a/tools/sync_third_party.py +++ b/tools/sync_third_party.py @@ -3,7 +3,8 @@ # Run this script if you are changing Deno's dependencies. # To update the deno_third_party git repo after running this, try the following: # cd third_party -# find . -type f | grep -v "\.git" | xargs -I% git add -f --no-warn-embedded-repo "%" +# find . -type f | grep -v "\.git" | \ +# xargs -I% git add -f --no-warn-embedded-repo "%" import third_party import util diff --git a/tools/test_format.py b/tools/test_format.py index 5e93286b60..a0d5ba08ec 100755 --- a/tools/test_format.py +++ b/tools/test_format.py @@ -7,7 +7,7 @@ import sys import subprocess -def main(argv): +def main(): util.run([sys.executable, "tools/format.py"]) output = util.run_output( ["git", "status", "-uno", "--porcelain", "--ignore-submodules"]) @@ -18,4 +18,4 @@ def main(argv): if __name__ == '__main__': - main(sys.argv) + main() diff --git a/tools/third_party.py b/tools/third_party.py index 46a9cf82e0..d7b5878457 100644 --- a/tools/third_party.py +++ b/tools/third_party.py @@ -34,7 +34,9 @@ python_site_env = None # Creates/modifies an environment so python can find packages that are bundled # in the 'third_party' directory. -def python_env(env=None, merge_env={}, depot_tools_path=depot_tools_path): +def python_env(env=None, merge_env=None): + if merge_env is None: + merge_env = {} global python_site_env # Use site.addsitedir() to determine which search paths would be considered @@ -59,12 +61,14 @@ def python_env(env=None, merge_env={}, depot_tools_path=depot_tools_path): # This function creates or modifies an environment so that it matches the # expectations of various google tools (gn, gclient, etc). -def google_env(env=None, merge_env={}, depot_tools_path=depot_tools_path): +def google_env(env=None, merge_env=None, depot_tools_path_=depot_tools_path): + if merge_env is None: + merge_env = {} # Google tools need the python env too. env = python_env(env=env, merge_env=merge_env) # Depot_tools to be in the PATH, before Python. - add_env_path(depot_tools_path, env=env, prepend=True) + add_env_path(depot_tools_path_, env=env, prepend=True) if os.name == "nt": # Windows-only enviroment tweaks. # We're not using Google's internal infrastructure. @@ -89,7 +93,7 @@ def fix_symlinks(): # Ensure the third_party directory exists. try: os.makedirs(third_party_path) - except: + except OSError: pass # Make symlinks to Yarn metadata living in the root repo. @@ -102,8 +106,8 @@ def fix_symlinks(): remove_and_symlink("v8/third_party/markupsafe", tp("markupsafe"), True) # On Windows, git doesn't create the right type of symlink if the symlink - # and it's target are in different repos. Here we fix the symlinks that exist - # in the root repo while their target is in the third_party repo. + # and it's target are in different repos. Here we fix the symlinks that + # exist in the root repo while their target is in the third_party repo. remove_and_symlink("third_party/node_modules", root("node_modules"), True) remove_and_symlink("third_party/v8/buildtools", root("buildtools"), True) remove_and_symlink("third_party/v8/build_overrides", @@ -120,9 +124,9 @@ def run_yarn(): # Run Cargo to install Rust dependencies. def run_cargo(): - # Deletes the cargo index lockfile; it appears that cargo itself doesn't do it. - # If the lockfile ends up in the git repo, it'll make cargo hang for everyone - # else who tries to run sync_third_party. + # Deletes the cargo index lockfile; it appears that cargo itself doesn't do + # it. If the lockfile ends up in the git repo, it'll make cargo hang for + # everyone else who tries to run sync_third_party. def delete_lockfile(): lockfiles = find_exts([path.join(rust_crates_path, "registry/index")], ['.cargo-index-lock']) @@ -186,7 +190,7 @@ def run_gclient_sync(): # Rename depot_tools to depot_tools_temp. try: os.rename(depot_tools_path, depot_tools_temp_path) - except: + except OSError: # If renaming failed, and the depot_tools_temp directory already exists, # assume that it's still there because a prior run_gclient_sync() call # failed half-way, before we got the chance to remove the temp dir. @@ -204,7 +208,7 @@ def run_gclient_sync(): 'DEPOT_TOOLS_UPDATE': "0", 'GCLIENT_FILE': root("gclient_config.py") } - env = google_env(depot_tools_path=depot_tools_temp_path, merge_env=envs) + env = google_env(depot_tools_path_=depot_tools_temp_path, merge_env=envs) run(args, cwd=third_party_path, env=env) # Delete the depot_tools_temp directory, but not before verifying that diff --git a/tools/throughput_benchmark.py b/tools/throughput_benchmark.py index 457f50bae2..7afd3cb81e 100755 --- a/tools/throughput_benchmark.py +++ b/tools/throughput_benchmark.py @@ -1,7 +1,8 @@ #!/usr/bin/env python # Copyright 2018 the Deno authors. All rights reserved. MIT license. # Performs benchmark and append data to //website/data.json. -# If //website/data.json doesn't exist, this script tries to import it from gh-pages branch. +# If //website/data.json doesn't exist, this script tries to import it from +# gh-pages branch. # To view the results locally run ./tools/http_server.py and visit # http://localhost:4545/website @@ -44,7 +45,7 @@ def tcp(deno_exe, megs): echo_server.kill() -if __name__ == '__main__': +def main(): deno_exe = sys.argv[1] megs = int(sys.argv[2]) if not deno_exe or not megs: @@ -52,3 +53,7 @@ if __name__ == '__main__': sys.exit(1) secs = tcp(sys.argv[1], megs) print secs, "seconds" + + +if __name__ == '__main__': + main() diff --git a/tools/unit_tests.py b/tools/unit_tests.py index 47dfcf8860..8b36fec44f 100755 --- a/tools/unit_tests.py +++ b/tools/unit_tests.py @@ -29,7 +29,9 @@ def run_unit_test2(cmd): sys.exit(errcode) -def run_unit_test(deno_exe, permStr, flags=[]): +def run_unit_test(deno_exe, permStr, flags=None): + if flags is None: + flags = [] cmd = [deno_exe, "--reload", "js/unit_tests.ts", permStr] + flags run_unit_test2(cmd) diff --git a/tools/util.py b/tools/util.py index 8c126bd83c..b77f17282c 100644 --- a/tools/util.py +++ b/tools/util.py @@ -14,10 +14,12 @@ executable_suffix = ".exe" if os.name == "nt" else "" root_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -def make_env(merge_env={}, env=None): +def make_env(merge_env=None, env=None): if env is None: env = os.environ env = env.copy() + if merge_env is None: + merge_env = {} for key in merge_env.keys(): env[key] = merge_env[key] return env @@ -25,19 +27,21 @@ def make_env(merge_env={}, env=None): def add_env_path(add, env, key="PATH", prepend=False): dirs_left = env[key].split(os.pathsep) if key in env else [] - dirs_right = add.split(os.pathsep) if type(add) is str else add + dirs_right = add.split(os.pathsep) if isinstance(add, str) else add if prepend: dirs_left, dirs_right = dirs_right, dirs_left - for dir in dirs_right: - if not dir in dirs_left: - dirs_left += [dir] + for d in dirs_right: + if not d in dirs_left: + dirs_left += [d] env[key] = os.pathsep.join(dirs_left) -def run(args, quiet=False, cwd=None, env=None, merge_env={}): +def run(args, quiet=False, cwd=None, env=None, merge_env=None): + if merge_env is None: + merge_env = {} args[0] = os.path.normpath(args[0]) if not quiet: print " ".join(args) @@ -48,7 +52,9 @@ def run(args, quiet=False, cwd=None, env=None, merge_env={}): sys.exit(rc) -def run_output(args, quiet=False, cwd=None, env=None, merge_env={}): +def run_output(args, quiet=False, cwd=None, env=None, merge_env=None): + if merge_env is None: + merge_env = {} args[0] = os.path.normpath(args[0]) if not quiet: print " ".join(args) @@ -92,7 +98,7 @@ def remove_and_symlink(target, name, target_is_dir=False): os.rmdir(name) else: os.unlink(name) - except: + except OSError: pass symlink(target, name, target_is_dir) @@ -146,7 +152,9 @@ def touch(fname): # * Recursive glob doesn't exist in python 2.7. # * On windows, `os.walk()` unconditionally follows symlinks. # The `skip` parameter should be used to avoid recursing through those. -def find_exts(directories, extensions, skip=[]): +def find_exts(directories, extensions, skip=None): + if skip is None: + skip = [] assert isinstance(directories, list) assert isinstance(extensions, list) skip = [os.path.normpath(i) for i in skip] @@ -247,7 +255,7 @@ def enable_ansi_colors_win10(): # Function factory for errcheck callbacks that raise WinError on failure. def raise_if(error_result): - def check(result, func, args): + def check(result, _func, args): if result == error_result: raise ctypes.WinError(ctypes.get_last_error()) return args @@ -321,7 +329,7 @@ def enable_ansi_colors_win10(): # Try to set the flag that controls ANSI escape code support. try: SetConsoleMode(conout, mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING) - except WindowsError as e: + except WindowsError as e: # pylint:disable=undefined-variable if e.winerror == ERROR_INVALID_PARAMETER: return False # Not supported, likely an older version of Windows. raise @@ -332,7 +340,6 @@ def enable_ansi_colors_win10(): def parse_unit_test_output(output, print_to_stdout): - first = True expected = None actual = None result = None diff --git a/tools/util_test.py b/tools/util_test.py index 24fd2eba1c..bf2b92b5a3 100644 --- a/tools/util_test.py +++ b/tools/util_test.py @@ -21,7 +21,8 @@ def pattern_match_test(): # Iterate through the fixture lists, testing each one for (pattern, string, expected) in fixtures: actual = pattern_match(pattern, string) - assert expected == actual, "expected %s for\nExpected:\n%s\nTo equal actual:\n%s" % ( + assert expected == actual, \ + "expected %s for\nExpected:\n%s\nTo equal actual:\n%s" % ( expected, pattern, string) assert pattern_match("foo[BAR]baz", "foobarbaz",