From 1ded51aa9d2cf28a937c32a73d3f9501044ee6f2 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Tue, 1 Sep 2020 21:59:07 -0700 Subject: [PATCH] python: Replace list literal concatenation with * unpacking. Signed-off-by: Anders Kaseorg --- analytics/tests/test_views.py | 2 +- scripts/lib/create-production-venv | 4 +- scripts/lib/create-thumbor-venv | 4 +- scripts/lib/node_cache.py | 2 +- scripts/lib/setup_venv.py | 6 +- scripts/lib/upgrade-zulip | 4 +- scripts/lib/upgrade-zulip-from-git | 2 +- scripts/lib/upgrade-zulip-stage-2 | 2 +- scripts/lib/zulip_tools.py | 2 +- scripts/purge-old-deployments | 2 +- scripts/restart-server | 4 +- scripts/setup/restore-backup | 2 +- scripts/zulip-puppet-apply | 4 +- tools/lib/html_branches.py | 9 +- tools/lib/provision.py | 89 ++++++++++--------- tools/lint | 2 +- tools/linter_lib/custom_check.py | 19 ++-- tools/run-dev.py | 10 +-- tools/run-mypy | 4 +- tools/setup/emoji/import_emoji_names_from_csv | 2 +- tools/test-help-documentation | 12 +-- tools/test-js-with-puppeteer | 2 +- tools/test-locked-requirements | 2 +- zerver/lib/event_schema.py | 2 +- zerver/lib/export.py | 2 +- .../markdown/api_arguments_table_generator.py | 2 +- .../api_return_values_table_generator.py | 2 +- zerver/lib/markdown/help_relative_links.py | 2 +- zerver/lib/markdown/help_settings_links.py | 2 +- zerver/lib/markdown/tabbed_sections.py | 2 +- zerver/lib/test_fixtures.py | 8 +- zerver/lib/test_helpers.py | 5 +- zerver/lib/topic_mutes.py | 2 +- zerver/lib/users.py | 2 +- zerver/management/commands/backup.py | 8 +- zerver/openapi/markdown_extension.py | 8 +- zerver/templatetags/app_filters.py | 4 +- zerver/tests/test_auth_backends.py | 6 +- zerver/tests/test_events.py | 2 +- zerver/tests/test_message_fetch.py | 25 +++--- zerver/tests/test_retention.py | 2 +- zerver/tests/test_soft_deactivation.py | 4 +- zerver/tests/test_subs.py | 4 +- zproject/backends.py | 2 +- zproject/computed_settings.py | 10 +-- 45 files changed, 156 insertions(+), 141 deletions(-) diff --git a/analytics/tests/test_views.py b/analytics/tests/test_views.py index 6376e5d46b..f1067d64e0 100644 --- a/analytics/tests/test_views.py +++ b/analytics/tests/test_views.py @@ -302,7 +302,7 @@ class TestGetChartData(ZulipTestCase): data = result.json() end_times = [ceiling_to_day(self.realm.date_created) + timedelta(days=i) for i in range(-1, 4)] self.assertEqual(data['end_times'], [datetime_to_timestamp(dt) for dt in end_times]) - self.assertEqual(data['everyone'], {'_1day': [0]+self.data(100), '_15day': [0]+self.data(100), 'all_time': [0]+self.data(100)}) + self.assertEqual(data['everyone'], {'_1day': [0, *self.data(100)], '_15day': [0, *self.data(100)], 'all_time': [0, *self.data(100)]}) def test_non_existent_chart(self) -> None: result = self.client_get('/json/analytics/chart_data', diff --git a/scripts/lib/create-production-venv b/scripts/lib/create-production-venv index 16c27a4a13..969ead5f9c 100755 --- a/scripts/lib/create-production-venv +++ b/scripts/lib/create-production-venv @@ -20,9 +20,9 @@ vendor = distro_info['ID'] os_version = distro_info['VERSION_ID'] VENV_DEPENDENCIES = get_venv_dependencies(vendor, os_version) if "debian" in os_families(): - run(["apt-get", "-y", "install"] + VENV_DEPENDENCIES) + run(["apt-get", "-y", "install", *VENV_DEPENDENCIES]) elif "fedora" in os_families(): - run(["yum", "-y", "install"] + VENV_DEPENDENCIES) + run(["yum", "-y", "install", *VENV_DEPENDENCIES]) else: print("Unsupported platform: {}".format(distro_info['ID'])) sys.exit(1) diff --git a/scripts/lib/create-thumbor-venv b/scripts/lib/create-thumbor-venv index 45cf5c6e7d..d9b9ba29f4 100755 --- a/scripts/lib/create-thumbor-venv +++ b/scripts/lib/create-thumbor-venv @@ -21,9 +21,9 @@ args = parser.parse_args() # install dependencies for setting up the virtualenv distro_info = parse_os_release() if "debian" in os_families(): - run(["apt-get", "-y", "install"] + THUMBOR_VENV_DEPENDENCIES) + run(["apt-get", "-y", "install", *THUMBOR_VENV_DEPENDENCIES]) elif "fedora" in os_families(): - run(["yum", "-y", "install"] + YUM_THUMBOR_VENV_DEPENDENCIES) + run(["yum", "-y", "install", *YUM_THUMBOR_VENV_DEPENDENCIES]) else: print("Unsupported platform: {}".format(distro_info['ID'])) sys.exit(1) diff --git a/scripts/lib/node_cache.py b/scripts/lib/node_cache.py index c2d086e9bd..8a568b19ff 100644 --- a/scripts/lib/node_cache.py +++ b/scripts/lib/node_cache.py @@ -83,7 +83,7 @@ def do_yarn_install( shutil.copytree("node_modules/", cached_node_modules, symlinks=True) if os.environ.get('CUSTOM_CA_CERTIFICATES'): run([YARN_BIN, "config", "set", "cafile", os.environ['CUSTOM_CA_CERTIFICATES']]) - run([YARN_BIN, "install", "--non-interactive", "--frozen-lockfile"] + yarn_args, + run([YARN_BIN, "install", "--non-interactive", "--frozen-lockfile", *yarn_args], cwd=target_path) with open(success_stamp, 'w'): pass diff --git a/scripts/lib/setup_venv.py b/scripts/lib/setup_venv.py index 330e333905..62776f2b6a 100644 --- a/scripts/lib/setup_venv.py +++ b/scripts/lib/setup_venv.py @@ -59,12 +59,14 @@ COMMON_YUM_VENV_DEPENDENCIES = [ "jq", ] -REDHAT_VENV_DEPENDENCIES = COMMON_YUM_VENV_DEPENDENCIES + [ +REDHAT_VENV_DEPENDENCIES = [ + *COMMON_YUM_VENV_DEPENDENCIES, "python36-devel", "python-virtualenv", ] -FEDORA_VENV_DEPENDENCIES = COMMON_YUM_VENV_DEPENDENCIES + [ +FEDORA_VENV_DEPENDENCIES = [ + *COMMON_YUM_VENV_DEPENDENCIES, "python3-pip", "virtualenv", # see https://unix.stackexchange.com/questions/27877/install-virtualenv-on-fedora-16 ] diff --git a/scripts/lib/upgrade-zulip b/scripts/lib/upgrade-zulip index 5a1105ea19..1d8fc5c379 100755 --- a/scripts/lib/upgrade-zulip +++ b/scripts/lib/upgrade-zulip @@ -65,7 +65,7 @@ try: # version is much better for fixing bugs in the upgrade process). deploy_path = deploy_path.strip() os.chdir(deploy_path) - subprocess.check_call([os.path.abspath("./scripts/lib/upgrade-zulip-stage-2"), deploy_path] - + deploy_options) + subprocess.check_call([os.path.abspath("./scripts/lib/upgrade-zulip-stage-2"), deploy_path, + *deploy_options]) finally: release_deployment_lock() diff --git a/scripts/lib/upgrade-zulip-from-git b/scripts/lib/upgrade-zulip-from-git index a91ebb8db2..cfb4fe3c7c 100755 --- a/scripts/lib/upgrade-zulip-from-git +++ b/scripts/lib/upgrade-zulip-from-git @@ -78,6 +78,6 @@ try: overwrite_symlink(deploy_path, os.path.join(DEPLOYMENTS_DIR, "next")) subprocess.check_call([os.path.join(deploy_path, "scripts", "lib", "upgrade-zulip-stage-2"), - deploy_path, "--from-git"] + deploy_options) + deploy_path, "--from-git", *deploy_options]) finally: release_deployment_lock() diff --git a/scripts/lib/upgrade-zulip-stage-2 b/scripts/lib/upgrade-zulip-stage-2 index 746e2c6c98..0a6bb52630 100755 --- a/scripts/lib/upgrade-zulip-stage-2 +++ b/scripts/lib/upgrade-zulip-stage-2 @@ -104,7 +104,7 @@ def shutdown_server() -> None: core_server_services.append("zulip-thumbor") logging.info("Stopping Zulip...") - subprocess.check_call(["supervisorctl", "stop"] + core_server_services + worker_services, + subprocess.check_call(["supervisorctl", "stop", *core_server_services, *worker_services], preexec_fn=su_to_zulip) IS_SERVER_UP = False diff --git a/scripts/lib/zulip_tools.py b/scripts/lib/zulip_tools.py index 05a2dc2473..d333201731 100755 --- a/scripts/lib/zulip_tools.py +++ b/scripts/lib/zulip_tools.py @@ -460,7 +460,7 @@ def is_root() -> bool: def run_as_root(args: List[str], **kwargs: Any) -> None: sudo_args = kwargs.pop('sudo_args', []) if not is_root(): - args = ['sudo'] + sudo_args + ['--'] + args + args = ['sudo', *sudo_args, '--', *args] run(args, **kwargs) def assert_not_running_as_root() -> None: diff --git a/scripts/purge-old-deployments b/scripts/purge-old-deployments index 36ab43efc9..674aad8288 100755 --- a/scripts/purge-old-deployments +++ b/scripts/purge-old-deployments @@ -62,7 +62,7 @@ def main() -> None: print("Cleaning orphaned/unused caches...") # Call 'clean-unused-caches' script to clean any orphaned/unused caches. - subprocess.check_call([os.path.join(ZULIP_PATH, "scripts/lib/clean-unused-caches")] + sys.argv[1:]) + subprocess.check_call([os.path.join(ZULIP_PATH, "scripts/lib/clean-unused-caches"), *sys.argv[1:]]) print("Done!") if __name__ == "__main__": diff --git a/scripts/restart-server b/scripts/restart-server index 8553b4fc60..d0937e14ce 100755 --- a/scripts/restart-server +++ b/scripts/restart-server @@ -75,10 +75,10 @@ else: logging.info("Stopping workers") subprocess.check_call(["supervisorctl", "stop", "zulip-workers:*"]) logging.info("Stopping server core") -subprocess.check_call(["supervisorctl", "stop"] + core_server_services) +subprocess.check_call(["supervisorctl", "stop", *core_server_services]) logging.info("Starting server core") -subprocess.check_call(["supervisorctl", "start"] + core_server_services) +subprocess.check_call(["supervisorctl", "start", *core_server_services]) logging.info("Starting workers") subprocess.check_call(["supervisorctl", "start", "zulip-workers:*"]) diff --git a/scripts/setup/restore-backup b/scripts/setup/restore-backup index f4bc009620..e07c7488e5 100755 --- a/scripts/setup/restore-backup +++ b/scripts/setup/restore-backup @@ -75,7 +75,7 @@ def restore_backup(tarball_file: IO[bytes]) -> None: os.mkdir(os.path.join(tmp, "zulip-backup")) tarball_file.seek(0, 0) - run(["tar", "-C", tmp] + transform_args + ["-xPz"], stdin=tarball_file) + run(["tar", "-C", tmp, *transform_args, "-xPz"], stdin=tarball_file) # Now, extract the the database backup, destroy the old # database, and create a new, empty database. diff --git a/scripts/zulip-puppet-apply b/scripts/zulip-puppet-apply index 60442b6417..289feaf5ee 100755 --- a/scripts/zulip-puppet-apply +++ b/scripts/zulip-puppet-apply @@ -50,7 +50,7 @@ if (distro_info['ID'], distro_info['VERSION_ID']) in [('ubuntu', '20.04')]: puppet_env["RUBYOPT"] = "-W0" if not args.noop and not args.force: - subprocess.check_call(puppet_cmd + ['--noop', '--show_diff'], env=puppet_env) + subprocess.check_call([*puppet_cmd, '--noop', '--show_diff'], env=puppet_env) do_apply = None while do_apply != 'y': @@ -60,7 +60,7 @@ if not args.noop and not args.force: if do_apply == '' or do_apply == 'n': sys.exit(0) -ret = subprocess.call(puppet_cmd + ['--detailed-exitcodes'], env=puppet_env) +ret = subprocess.call([*puppet_cmd, '--detailed-exitcodes'], env=puppet_env) # ret = 0 => no changes, no errors # ret = 2 => changes, no errors # ret = 4 => no changes, yes errors diff --git a/tools/lib/html_branches.py b/tools/lib/html_branches.py index 3e5eb76075..226865ba4a 100644 --- a/tools/lib/html_branches.py +++ b/tools/lib/html_branches.py @@ -66,10 +66,11 @@ class TagInfo: self.classes = classes self.ids = ids self.token = token - self.words = \ - [self.tag] + \ - ['.' + s for s in classes] + \ - ['#' + s for s in ids] + self.words = [ + self.tag, + *('.' + s for s in classes), + *('#' + s for s in ids), + ] def text(self) -> str: s = self.tag diff --git a/tools/lib/provision.py b/tools/lib/provision.py index e9fec428bd..2b4d08feba 100755 --- a/tools/lib/provision.py +++ b/tools/lib/provision.py @@ -145,64 +145,68 @@ COMMON_DEPENDENCIES = [ # Puppeteer dependencies end here. ] -UBUNTU_COMMON_APT_DEPENDENCIES = COMMON_DEPENDENCIES + [ +UBUNTU_COMMON_APT_DEPENDENCIES = [ + *COMMON_DEPENDENCIES, "redis-server", "hunspell-en-us", "puppet-lint", "netcat", # Used for flushing memcached "default-jre-headless", # Required by vnu-jar -] + THUMBOR_VENV_DEPENDENCIES + *THUMBOR_VENV_DEPENDENCIES, +] -COMMON_YUM_DEPENDENCIES = COMMON_DEPENDENCIES + [ +COMMON_YUM_DEPENDENCIES = [ + *COMMON_DEPENDENCIES, "redis", "hunspell-en-US", "rubygem-puppet-lint", "nmap-ncat", -] + YUM_THUMBOR_VENV_DEPENDENCIES + *YUM_THUMBOR_VENV_DEPENDENCIES, +] BUILD_PGROONGA_FROM_SOURCE = False if vendor == 'debian' and os_version in [] or vendor == 'ubuntu' and os_version in []: # For platforms without a pgroonga release, we need to build it # from source. BUILD_PGROONGA_FROM_SOURCE = True - SYSTEM_DEPENDENCIES = UBUNTU_COMMON_APT_DEPENDENCIES + [ - pkg.format(POSTGRES_VERSION) for pkg in [ - "postgresql-{0}", - # Dependency for building pgroonga from source - "postgresql-server-dev-{0}", - "libgroonga-dev", - "libmsgpack-dev", - "clang-9", - "llvm-9-dev", - ] - ] + VENV_DEPENDENCIES + SYSTEM_DEPENDENCIES = [ + *UBUNTU_COMMON_APT_DEPENDENCIES, + "postgresql-{0}".format(POSTGRES_VERSION), + # Dependency for building pgroonga from source + "postgresql-server-dev-{0}".format(POSTGRES_VERSION), + "libgroonga-dev", + "libmsgpack-dev", + "clang-9", + "llvm-9-dev", + *VENV_DEPENDENCIES, + ] elif "debian" in os_families(): - SYSTEM_DEPENDENCIES = UBUNTU_COMMON_APT_DEPENDENCIES + [ - pkg.format(POSTGRES_VERSION) for pkg in [ - "postgresql-{0}", - "postgresql-{0}-pgroonga", - ] - ] + VENV_DEPENDENCIES + SYSTEM_DEPENDENCIES = [ + *UBUNTU_COMMON_APT_DEPENDENCIES, + "postgresql-{0}".format(POSTGRES_VERSION), + "postgresql-{0}-pgroonga".format(POSTGRES_VERSION), + *VENV_DEPENDENCIES, + ] elif "rhel" in os_families(): - SYSTEM_DEPENDENCIES = COMMON_YUM_DEPENDENCIES + [ - pkg.format(POSTGRES_VERSION) for pkg in [ - "postgresql{0}-server", - "postgresql{0}", - "postgresql{0}-devel", - "postgresql{0}-pgroonga", - ] - ] + VENV_DEPENDENCIES + SYSTEM_DEPENDENCIES = [ + *COMMON_YUM_DEPENDENCIES, + "postgresql{0}-server".format(POSTGRES_VERSION), + "postgresql{0}".format(POSTGRES_VERSION), + "postgresql{0}-devel".format(POSTGRES_VERSION), + "postgresql{0}-pgroonga".format(POSTGRES_VERSION), + *VENV_DEPENDENCIES, + ] elif "fedora" in os_families(): - SYSTEM_DEPENDENCIES = COMMON_YUM_DEPENDENCIES + [ - pkg.format(POSTGRES_VERSION) for pkg in [ - "postgresql{0}-server", - "postgresql{0}", - "postgresql{0}-devel", - # Needed to build pgroonga from source - "groonga-devel", - "msgpack-devel", - ] - ] + VENV_DEPENDENCIES + SYSTEM_DEPENDENCIES = [ + *COMMON_YUM_DEPENDENCIES, + "postgresql{0}-server".format(POSTGRES_VERSION), + "postgresql{0}".format(POSTGRES_VERSION), + "postgresql{0}-devel".format(POSTGRES_VERSION), + # Needed to build pgroonga from source + "groonga-devel", + "msgpack-devel", + *VENV_DEPENDENCIES, + ] BUILD_PGROONGA_FROM_SOURCE = True if "fedora" in os_families(): @@ -247,9 +251,8 @@ def install_apt_deps(deps_to_install: List[str]) -> None: run_as_root( [ "env", "DEBIAN_FRONTEND=noninteractive", - "apt-get", "-y", "install", "--no-install-recommends", + "apt-get", "-y", "install", "--no-install-recommends", *deps_to_install, ] - + deps_to_install, ) def install_yum_deps(deps_to_install: List[str]) -> None: @@ -274,7 +277,7 @@ def install_yum_deps(deps_to_install: List[str]) -> None: else: print("Unrecognized output. `subscription-manager` might not be available") - run_as_root(["yum", "install", "-y"] + yum_extra_flags + deps_to_install) + run_as_root(["yum", "install", "-y", *yum_extra_flags, *deps_to_install]) if "rhel" in os_families(): # This is how a pip3 is installed to /usr/bin in CentOS/RHEL # for python35 and later. @@ -363,7 +366,7 @@ def main(options: argparse.Namespace) -> "NoReturn": "https_proxy=" + os.environ.get("https_proxy", ""), "no_proxy=" + os.environ.get("no_proxy", ""), ] - run_as_root(proxy_env + ["scripts/lib/install-node"], sudo_args = ['-H']) + run_as_root([*proxy_env, "scripts/lib/install-node"], sudo_args = ['-H']) if not os.access(NODE_MODULES_CACHE_PATH, os.W_OK): run_as_root(["mkdir", "-p", NODE_MODULES_CACHE_PATH]) diff --git a/tools/lint b/tools/lint index 0f9dfcf6cc..6f2aef5a9d 100755 --- a/tools/lint +++ b/tools/lint @@ -61,7 +61,7 @@ def run() -> None: description="Runs the puppet parser validator, " "checking for syntax errors.") linter_config.external_linter('puppet-lint', - ['puppet-lint', '--fail-on-warnings'] + PUPPET_CHECK_RULES_TO_EXCLUDE, + ['puppet-lint', '--fail-on-warnings', *PUPPET_CHECK_RULES_TO_EXCLUDE], ['pp'], fix_arg='--fix', description="Standard puppet linter" diff --git a/tools/linter_lib/custom_check.py b/tools/linter_lib/custom_check.py index 4cfac2f88d..72dd11a951 100644 --- a/tools/linter_lib/custom_check.py +++ b/tools/linter_lib/custom_check.py @@ -75,7 +75,8 @@ comma_whitespace_rule: List["Rule"] = [ 'good_lines': ['foo(1, 2, 3)', 'foo = bar # some inline comment'], 'bad_lines': ['foo(1, 2, 3)', 'foo(1, 2, 3)']}, ] -markdown_whitespace_rules = list([rule for rule in whitespace_rules if rule['pattern'] != r'\s+$']) + [ +markdown_whitespace_rules: List["Rule"] = [ + *(rule for rule in whitespace_rules if rule['pattern'] != r'\s+$'), # Two spaces trailing a line with other content is okay--it's a Markdown line break. # This rule finds one space trailing a non-space, three or more trailing spaces, and # spaces on an empty line. @@ -246,7 +247,7 @@ python_rules = RuleList( 'exclude': {'scripts/lib/setup_venv.py'}, 'exclude_line': { ('scripts/lib/zulip_tools.py', 'sudo_args = kwargs.pop(\'sudo_args\', [])'), - ('scripts/lib/zulip_tools.py', 'args = [\'sudo\'] + sudo_args + [\'--\'] + args'), + ('scripts/lib/zulip_tools.py', 'args = [\'sudo\', *sudo_args, \'--\', *args]'), }, 'description': 'Most scripts are intended to run on systems without sudo.', 'good_lines': ['subprocess.check_call(["ls"])'], @@ -442,7 +443,9 @@ prose_style_rules: List["Rule"] = [ 'description': "Use Botserver instead of botserver or bot server."}, *comma_whitespace_rule, ] -html_rules: List["Rule"] = whitespace_rules + prose_style_rules + [ +html_rules: List["Rule"] = [ + *whitespace_rules, + *prose_style_rules, {'pattern': 'subject|SUBJECT', 'exclude': {'templates/zerver/email.html'}, 'exclude_pattern': 'email subject', @@ -578,7 +581,8 @@ html_rules: List["Rule"] = whitespace_rules + prose_style_rules + [ handlebars_rules = RuleList( langs=['hbs'], - rules=html_rules + [ + rules=[ + *html_rules, {'pattern': "[<]script", 'description': "Do not use inline