From a45ded550833e9fb4d22511fc991143aad48b68e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 16 Sep 2018 12:24:07 -0500 Subject: [PATCH] chore: fix python lint warnings (#14638) * chore: fix lint warnings * chore: another try at python import errors Looks like the problem is that dbus_mock.py is running as a script but living in the `lib/` directory where it's part of a module. Moving it up into the `script/` directory seems to solve the issue. --- script/apply-patches | 2 +- script/bump-version.py | 2 +- script/check-relative-doc-links.py | 39 +++++++++++++++--------------- script/{lib => }/dbus_mock.py | 15 +++++++----- script/lib/config.py | 10 +++++--- script/lib/env_util.py | 22 +++++++++-------- script/lib/git.py | 12 +++++---- script/lib/gn.py | 5 ++-- script/lib/patches.py | 29 ++++++++++++++-------- script/lib/util.py | 32 ++++++++++++++++++------ script/pump.py | 4 +-- script/spec-runner.js | 4 ++- script/test.py | 4 +-- script/upload-node-checksums.py | 2 +- script/upload.py | 1 - 15 files changed, 110 insertions(+), 73 deletions(-) rename script/{lib => }/dbus_mock.py (66%) mode change 100644 => 100755 script/spec-runner.js diff --git a/script/apply-patches b/script/apply-patches index a4763770295d3..ea6a082293d99 100755 --- a/script/apply-patches +++ b/script/apply-patches @@ -5,7 +5,7 @@ import os import subprocess import sys -import lib.git as git +from lib import git from lib.patches import PatchesConfig diff --git a/script/bump-version.py b/script/bump-version.py index 18573494f6bd5..e3c37e78539f2 100755 --- a/script/bump-version.py +++ b/script/bump-version.py @@ -80,7 +80,7 @@ def main(): else: raise Exception("Invalid current version: " + curr_version) - if args.new_version == None and args.bump == None and args.stable == False: + if args.new_version is None and args.bump is None and not args.stable: parser.print_help() return 1 diff --git a/script/check-relative-doc-links.py b/script/check-relative-doc-links.py index 9df185471c37e..e122310bd431e 100755 --- a/script/check-relative-doc-links.py +++ b/script/check-relative-doc-links.py @@ -57,30 +57,29 @@ def getBrokenLinks(filepath): for link in links: sections = link.split('#') - if len(sections) > 1: - if str(link).startswith('#'): - if not checkSections(sections, lines): + if len(sections) < 2: + if not os.path.isfile(os.path.join(currentDir, link)): + brokenLinks.append(link) + elif str(link).startswith('#'): + if not checkSections(sections, lines): + brokenLinks.append(link) + else: + tempFile = os.path.join(currentDir, sections[0]) + if os.path.isfile(tempFile): + try: + newFile = open(tempFile, 'r') + newLines = newFile.readlines() + except KeyboardInterrupt: + print('Keyboard interruption whle parsing. Please try again.') + finally: + newFile.close() + + if not checkSections(sections, newLines): brokenLinks.append(link) else: - tempFile = os.path.join(currentDir, sections[0]) - if os.path.isfile(tempFile): - try: - newFile = open(tempFile, 'r') - newLines = newFile.readlines() - except KeyboardInterrupt: - print('Keyboard interruption whle parsing. Please try again.') - finally: - newFile.close() - - if not checkSections(sections, newLines): - brokenLinks.append(link) - else: - brokenLinks.append(link) - - else: - if not os.path.isfile(os.path.join(currentDir, link)): brokenLinks.append(link) + print_errors(filepath, brokenLinks) return len(brokenLinks) diff --git a/script/lib/dbus_mock.py b/script/dbus_mock.py similarity index 66% rename from script/lib/dbus_mock.py rename to script/dbus_mock.py index 9b9c8029cb75a..77540e402713f 100644 --- a/script/lib/dbus_mock.py +++ b/script/dbus_mock.py @@ -1,28 +1,31 @@ -from config import is_verbose_mode -from dbusmock import DBusTestCase +#!/usr/bin/env python import atexit import os +import subprocess import sys +from dbusmock import DBusTestCase + +from lib.config import is_verbose_mode def stop(): DBusTestCase.stop_dbus(DBusTestCase.system_bus_pid) DBusTestCase.stop_dbus(DBusTestCase.session_bus_pid) def start(): - dbusmock_log = sys.stdout if is_verbose_mode() else open(os.devnull, 'w') + log = sys.stdout if is_verbose_mode() else open(os.devnull, 'w') DBusTestCase.start_system_bus() - DBusTestCase.spawn_server_template('logind', None, dbusmock_log) + DBusTestCase.spawn_server_template('logind', None, log) DBusTestCase.start_session_bus() - DBusTestCase.spawn_server_template('notification_daemon', None, dbusmock_log) + DBusTestCase.spawn_server_template('notification_daemon', None, log) if __name__ == '__main__': - import subprocess start() try: + print(sys.argv) subprocess.check_call(sys.argv[1:]) finally: stop() diff --git a/script/lib/config.py b/script/lib/config.py index 9ad0fbc680e5e..0b758073070cd 100644 --- a/script/lib/config.py +++ b/script/lib/config.py @@ -6,10 +6,13 @@ import sys # URL to the mips64el sysroot image. -MIPS64EL_SYSROOT_URL = 'https://github.com/electron/debian-sysroot-image-creator/releases/download/v0.5.0/debian_jessie_mips64-sysroot.tar.bz2' +MIPS64EL_SYSROOT_URL = 'https://github.com/electron' \ + + '/debian-sysroot-image-creator/releases/download' \ + + '/v0.5.0/debian_jessie_mips64-sysroot.tar.bz2' # URL to the mips64el toolchain. MIPS64EL_GCC = 'gcc-4.8.3-d197-n64-loongson' -MIPS64EL_GCC_URL = 'http://ftp.loongnix.org/toolchain/gcc/release/' + MIPS64EL_GCC + '.tar.gz' +MIPS64EL_GCC_URL = 'http://ftp.loongnix.org/toolchain/gcc/release/' \ + + MIPS64EL_GCC + '.tar.gz' BASE_URL = os.getenv('LIBCHROMIUMCONTENT_MIRROR') or \ 'https://s3.amazonaws.com/github-janky-artifacts/libchromiumcontent' @@ -89,7 +92,8 @@ def get_zip_name(name, version, suffix=''): def build_env(): env = os.environ.copy() if get_target_arch() == "mips64el": - SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) + SOURCE_ROOT = os.path.abspath(os.path.dirname( + os.path.dirname(os.path.dirname(__file__)))) VENDOR_DIR = os.path.join(SOURCE_ROOT, 'vendor') gcc_dir = os.path.join(VENDOR_DIR, MIPS64EL_GCC) ldlib_dirs = [ diff --git a/script/lib/env_util.py b/script/lib/env_util.py index 0b40bbfd7c2bf..3a6e199d26df1 100644 --- a/script/lib/env_util.py +++ b/script/lib/env_util.py @@ -16,9 +16,10 @@ def validate_pair(ob): return True -def consume(iter): +def consume(iterator): try: - while True: next(iter) + while True: + next(iterator) except StopIteration: pass @@ -36,11 +37,11 @@ def get_environment_from_batch_command(env_cmd, initial=None): if not isinstance(env_cmd, (list, tuple)): env_cmd = [env_cmd] # Construct the command that will alter the environment. - env_cmd = subprocess.list2cmdline(env_cmd) + cmd = subprocess.list2cmdline(env_cmd) # Create a tag so we can tell in the output when the proc is done. tag = 'END OF BATCH COMMAND' # Construct a cmd.exe command to do accomplish this. - cmd = 'cmd.exe /s /c "{env_cmd} && echo "{tag}" && set"'.format(**vars()) + cmd = 'cmd.exe /s /c "{cmd} && echo "{tag}" && set"'.format(**locals()) # Launch the process. proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=initial) # Parse the output sent to stdout. @@ -63,10 +64,11 @@ def get_vs_location(vs_version): """ Returns the location of the VS building environment. - The vs_version can be strings like "[15.0,16.0)", meaning 2017, but not the next version. + The vs_version can be strings like "[15.0,16.0)", meaning 2017, + but not the next version. """ - # vswhere can't handle spaces, like "[15.0, 16.0)" should become "[15.0,16.0)" + # vswhere can't handle spaces. "[15.0, 16.0)" should become "[15.0,16.0)" vs_version = vs_version.replace(" ", "") program_files = os.environ.get('ProgramFiles(x86)') @@ -86,10 +88,10 @@ def get_vs_env(vs_version, arch): """ Returns the env object for VS building environment. - vs_version is the version of Visual Studio to use. See get_vs_location for - more details. - The arch has to be one of "x86", "amd64", "arm", "x86_amd64", "x86_arm", "amd64_x86", - "amd64_arm", i.e. the args passed to vcvarsall.bat. + vs_version is the version of Visual Studio to use. + See get_vs_location for more details. + The arch must be one of "x86", "amd64", "arm", "x86_amd64", "x86_arm", + "amd64_x86", "amd64_arm", i.e. the args passed to vcvarsall.bat. """ location = get_vs_location(vs_version) diff --git a/script/lib/git.py b/script/lib/git.py index 5f9bedef31c81..1bcb698ca521a 100644 --- a/script/lib/git.py +++ b/script/lib/git.py @@ -1,13 +1,15 @@ +#!/usr/bin/env python + """Git helper functions. -Everything in here should be project agnostic, shouldn't rely on project's structure, -and make any assumptions about the passed arguments or calls outcomes. +Everything here should be project agnostic: it shouldn't rely on project's +structure, or make assumptions about the passed arguments or calls' outcomes. """ import os import subprocess -from util import scoped_cwd +from lib.util import scoped_cwd def is_repo_root(path): @@ -40,7 +42,7 @@ def get_repo_root(path): return get_repo_root(parent_path) -def apply(repo, patch_path, directory=None, index=False, reverse=False): +def apply_patch(repo, patch_path, directory=None, index=False, reverse=False): args = ['git', 'apply', '--ignore-space-change', '--ignore-whitespace', @@ -64,7 +66,7 @@ def get_patch(repo, commit_hash): args = ['git', 'diff-tree', '-p', commit_hash, - '--' # Explicitly tell Git that `commit_hash` is a revision, not a path. + '--' # Explicitly tell Git `commit_hash` is a revision, not a path. ] with scoped_cwd(repo): diff --git a/script/lib/gn.py b/script/lib/gn.py index 0d192bcd909e9..78b9a58806400 100644 --- a/script/lib/gn.py +++ b/script/lib/gn.py @@ -3,7 +3,7 @@ import subprocess import sys -from util import scoped_cwd +from lib.util import scoped_cwd class GNProject: @@ -18,7 +18,8 @@ def _get_executable_name(self): def run(self, command_name, command_args): with scoped_cwd(self.out_dir): - complete_args = [self._get_executable_name(), command_name, '.'] + command_args + complete_args = [self._get_executable_name(), command_name, '.'] + \ + command_args return subprocess.check_output(complete_args) def args(self): diff --git a/script/lib/patches.py b/script/lib/patches.py index d65ba6bab5cac..274df0ac30333 100644 --- a/script/lib/patches.py +++ b/script/lib/patches.py @@ -1,17 +1,20 @@ +#!/usr/bin/env python + import os import sys -import git +from lib import git -SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) +SOURCE_ROOT = os.path.abspath(os.path.dirname( + os.path.dirname(os.path.dirname(__file__)))) VENDOR_DIR = os.path.join(SOURCE_ROOT, 'vendor') PYYAML_LIB_DIR = os.path.join(VENDOR_DIR, 'pyyaml', 'lib') sys.path.append(PYYAML_LIB_DIR) -import yaml - +import yaml #pylint: disable=wrong-import-position,wrong-import-order class Patch: - def __init__(self, file_path, repo_path, paths_prefix=None, author='Anonymous ', description=None): + def __init__(self, file_path, repo_path, paths_prefix=None, + author='Anonymous ', description=None): self.author = author self.description = description self.file_path = file_path @@ -21,14 +24,17 @@ def __init__(self, file_path, repo_path, paths_prefix=None, author='Anonymous 4: + return vs[0:4] + else: + return vs + ['0'] * (4 - len(vs)) + def clean_parse_version(v): return parse_version(v.split("-")[0]) @@ -230,7 +245,7 @@ def get_last_major(): def get_next_nightly(v): pv = clean_parse_version(v) - major = pv[0]; minor = pv[1]; patch = pv[2] + (major, minor, patch) = pv[0:3] if (is_stable(v)): patch = str(int(pv[2]) + 1) @@ -266,15 +281,16 @@ def get_next_beta(v): def get_next_stable_from_pre(v): pv = clean_parse_version(v) - major = pv[0]; minor = pv[1]; patch = pv[2] + (major, minor, patch) = pv[0:3] return make_version(major, minor, patch) def get_next_stable_from_stable(v): pv = clean_parse_version(v) - major = pv[0]; minor = pv[1]; patch = pv[2] + (major, minor, patch) = pv[0:3] return make_version(major, minor, str(int(patch) + 1)) def make_version(major, minor, patch, pre = None): if pre is None: return major + '.' + minor + '.' + patch return major + "." + minor + "." + patch + '-' + pre + diff --git a/script/pump.py b/script/pump.py index e7565b6b4f3a5..eb93517d4f5f8 100755 --- a/script/pump.py +++ b/script/pump.py @@ -62,12 +62,12 @@ EXPRESSION has Python syntax. """ -__author__ = 'wan@google.com (Zhanyong Wan)' - import os import re import sys +__author__ = 'wan@google.com (Zhanyong Wan)' + TOKEN_TABLE = [ (re.compile(r'\$var\s+'), '$var'), diff --git a/script/spec-runner.js b/script/spec-runner.js old mode 100644 new mode 100755 index c247908e76855..305e1500a8f1d --- a/script/spec-runner.js +++ b/script/spec-runner.js @@ -1,3 +1,5 @@ +#!/usr/bin/env node + const cp = require('child_process') const crypto = require('crypto') const fs = require('fs') @@ -39,7 +41,7 @@ getSpecHash().then(([currentSpecHash, currentSpecInstallHash]) => { let exe = path.resolve(BASE, utils.getElectronExec()) const args = process.argv.slice(2) if (process.platform === 'linux') { - args.unshift(path.resolve(__dirname, 'lib/dbus_mock.py'), exe) + args.unshift(path.resolve(__dirname, 'dbus_mock.py'), exe) exe = 'python' } const child = cp.spawn(exe, args, { diff --git a/script/test.py b/script/test.py index 46cada435e1bd..9f6fd872108e3 100755 --- a/script/test.py +++ b/script/test.py @@ -1,12 +1,14 @@ #!/usr/bin/env python import argparse +import atexit import os import shutil import subprocess import sys from lib.config import enable_verbose_mode +import lib.dbus_mock from lib.util import electron_gyp, execute_stdout, rm_rf @@ -17,8 +19,6 @@ # while also setting DBUS_SYSTEM_BUS_ADDRESS environment variable, which # will be picked up by electron. try: - import lib.dbus_mock - import atexit lib.dbus_mock.start() atexit.register(lib.dbus_mock.stop) except ImportError: diff --git a/script/upload-node-checksums.py b/script/upload-node-checksums.py index 5e08a7a7fadac..f5b37abf9e72e 100755 --- a/script/upload-node-checksums.py +++ b/script/upload-node-checksums.py @@ -4,6 +4,7 @@ import hashlib import os import shutil +import sys import tempfile from lib.config import s3_config @@ -95,5 +96,4 @@ def copy_files(source_files, output_dir): shutil.copy2(source_file, output_path) if __name__ == '__main__': - import sys sys.exit(main()) diff --git a/script/upload.py b/script/upload.py index 2d9eaa2012659..0bb9392e7dfbd 100755 --- a/script/upload.py +++ b/script/upload.py @@ -210,5 +210,4 @@ def get_release(version): return release if __name__ == '__main__': - import sys sys.exit(main())