Skip to content

Commit

Permalink
Automatically update Python sources to use python-3.7 syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
bbannier committed Jan 10, 2023
1 parent 6fc3fd3 commit 3bb0df7
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 171 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ repos:
rev: 23.1a1
hooks:
- id: black
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
args: ["--py37-plus"]
3 changes: 1 addition & 2 deletions doc/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Zeek Package Manager documentation build configuration file, created by
# sphinx-quickstart on Fri Jul 15 13:46:04 2016.
Expand Down Expand Up @@ -59,7 +58,7 @@
# built documents.
#
# The short X.Y version.
with open("../VERSION", "r") as f:
with open("../VERSION") as f:
version = f.readline().strip()

# The full version, including alpha/beta/rc tags.
Expand Down
2 changes: 1 addition & 1 deletion doc/ext/sphinxarg/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def env_get_outdated_hook(app, env, added, changed, removed):
for docname in rval:
from sphinx.util.console import blue

msg = blue("found outdated argparse doc: {0}".format(docname))
msg = blue(f"found outdated argparse doc: {docname}")
logger.info(msg)

return list(rval)
Expand Down
6 changes: 3 additions & 3 deletions doc/ext/sphinxarg/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def parser_navigate(parser_result, path, current_path=None):
if isinstance(path, str):
if path == "":
return parser_result
path = re.split("\s+", path)
path = re.split(r"\s+", path)
current_path = current_path or []
if len(path) == 0:
return parser_result
Expand Down Expand Up @@ -87,11 +87,11 @@ def parse_parser(parser, data=None, **kwargs):
if name in subsection_alias_names:
continue
subalias = subsection_alias[subaction]
subaction.prog = "%s %s" % (parser.prog, name)
subaction.prog = f"{parser.prog} {name}"
subdata = {
"name": name
if not subalias
else "%s (%s)" % (name, ", ".join(subalias)),
else "{} ({})".format(name, ", ".join(subalias)),
"help": helps.get(name, ""),
"usage": subaction.format_usage().strip(),
"bare_usage": _format_usage_without_prefix(subaction),
Expand Down
2 changes: 1 addition & 1 deletion zeekpkg/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def git_remote_urls(repo):
cases. We use the config subsystem to query the URLs directly -- one of the
fallback mechanisms in GitPython's Remote.urls() implementation.
"""
remote_details = repo.git.config("--get-regexp", "remote\..+\.url")
remote_details = repo.git.config("--get-regexp", r"remote\..+\.url")
remotes = {}

for line in remote_details.split("\n"):
Expand Down
58 changes: 29 additions & 29 deletions zeekpkg/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
)


class Stage(object):
class Stage:
def __init__(self, manager, state_dir=None):
self.manager = manager

Expand Down Expand Up @@ -158,7 +158,7 @@ def get_subprocess_env(self):
return env, ""


class Manager(object):
class Manager:
"""A package manager object performs various operations on packages.
It uses a state directory and a manifest file within it to keep
Expand Down Expand Up @@ -388,7 +388,7 @@ def _write_autoloader(self):

for ipkg in self.loaded_packages():
if self.has_scripts(ipkg):
content += "@load ./{}\n".format(ipkg.package.name)
content += f"@load ./{ipkg.package.name}\n"

f.write(content)

Expand Down Expand Up @@ -443,7 +443,7 @@ def _read_manifest(self):
Raises:
IOError: when the manifest file can't be read
"""
with open(self.manifest, "r") as f:
with open(self.manifest) as f:
data = json.load(f)
version = data["manifest_version"]
pkg_list = data["installed_packages"]
Expand Down Expand Up @@ -631,7 +631,7 @@ def package_build_log(self, pkg_path):
to the package: "foo", "alice/foo", or "zeek/alice/foo".
"""
name = name_from_path(pkg_path)
return os.path.join(self.log_dir, "{}-build.log".format(name))
return os.path.join(self.log_dir, f"{name}-build.log")

def match_source_packages(self, pkg_path):
"""Return a list of :class:`.package.Package` that match a given path.
Expand Down Expand Up @@ -725,7 +725,7 @@ def save_temporary_config_files(self, installed_pkg):
import re

metadata = installed_pkg.package.metadata
config_files = re.split(",\s*", metadata.get("config_files", ""))
config_files = re.split(r",\s*", metadata.get("config_files", ""))

if not config_files:
return []
Expand Down Expand Up @@ -769,7 +769,7 @@ def modified_config_files(self, installed_pkg):
import re

metadata = installed_pkg.package.metadata
config_files = re.split(",\s*", metadata.get("config_files", ""))
config_files = re.split(r",\s*", metadata.get("config_files", ""))

if not config_files:
return []
Expand Down Expand Up @@ -867,7 +867,7 @@ def backup_modified_files(self, backup_subdir, modified_files):

return rval

class SourceAggregationResults(object):
class SourceAggregationResults:
"""The return value of a call to :meth:`.Manager.aggregate_source()`.
Attributes:
Expand Down Expand Up @@ -972,7 +972,7 @@ def _refresh_source(self, name, aggregate=False, push=False):
except git.exc.GitCommandError as error:
LOG.error("failed to pull source %s: %s", name, error)
return self.SourceAggregationResults(
"failed to pull from remote source: {}".format(error)
f"failed to pull from remote source: {error}"
)

if os.path.isfile(agg_file_ours):
Expand Down Expand Up @@ -1495,7 +1495,7 @@ def list_depender_pkgs(self, pkg_path):
item = queue.popleft()

for _pkg_name in pkg_dependencies:
pkg_dependees = set([_pkg for _pkg in pkg_dependencies.get(_pkg_name)])
pkg_dependees = {_pkg for _pkg in pkg_dependencies.get(_pkg_name)}

if item in pkg_dependees:
# check if there is a cyclic dependency
Expand Down Expand Up @@ -1586,7 +1586,7 @@ def _has_all_dependers_unloaded(item, dependers):
dep_listing = ""

for _name in dep_packages:
dep_listing += '"{}", '.format(_name)
dep_listing += f'"{_name}", '

errors.append(
(
Expand Down Expand Up @@ -1716,7 +1716,7 @@ def info(self, pkg_path, version="", prefer_installed=True):
name = name_from_path(pkg_path)

if not is_valid_package_name(name):
reason = "Package name {!r} is not valid.".format(name)
reason = f"Package name {name!r} is not valid."
return PackageInfo(Package(git_url=pkg_path), invalid_reason=reason)

LOG.debug('getting info on "%s"', pkg_path)
Expand Down Expand Up @@ -1795,7 +1795,7 @@ def _info(self, package, status, version):
try:
git_checkout(clone, version)
except git.exc.GitCommandError:
reason = 'no such commit, branch, or version tag: "{}"'.format(version)
reason = f'no such commit, branch, or version tag: "{version}"'
return PackageInfo(package=package, status=status, invalid_reason=reason)

LOG.debug('checked out "%s", branch/version "%s"', package, version)
Expand Down Expand Up @@ -1864,7 +1864,7 @@ def validate_dependencies(
prior to the depender packages.
"""

class Node(object):
class Node:
def __init__(self, name):
self.name = name
self.info = None
Expand Down Expand Up @@ -1895,7 +1895,7 @@ def __str__(self):

if info.invalid_reason:
return (
'invalid package "{}": {}'.format(name, info.invalid_reason),
f'invalid package "{name}": {info.invalid_reason}',
[],
)

Expand Down Expand Up @@ -2435,7 +2435,7 @@ def match_package_url_and_version(git_url, version):
try:
git_clone(git_url, clonepath, shallow=(not is_sha1(version)))
except git.exc.GitCommandError as error:
return "failed to clone {}: {}".format(git_url, error)
return f"failed to clone {git_url}: {error}"

with open(manifest_file, "w") as f:
config.write(f)
Expand Down Expand Up @@ -2577,7 +2577,7 @@ def test(self, pkg_path, version="", test_dependencies=False):
except git.exc.GitCommandError as error:
LOG.warning("failed to clone git repo: %s", error)
return (
"failed to clone {}".format(info.package.git_url),
f"failed to clone {info.package.git_url}",
False,
stage.state_dir,
)
Expand Down Expand Up @@ -2651,7 +2651,7 @@ def test(self, pkg_path, version="", test_dependencies=False):

if rc != 0:
return (
"test_command failed with exit code {}".format(rc),
f"test_command failed with exit code {rc}",
False,
stage.state_dir,
)
Expand Down Expand Up @@ -2749,7 +2749,7 @@ def _stage(self, package, version, clone, stage, env=None):
else:
break

except EnvironmentError as error:
except OSError as error:
LOG.warning(
'installing "%s": failed to write build log %s %s: %s',
package,
Expand All @@ -2761,7 +2761,7 @@ def _stage(self, package, version, clone, stage, env=None):
returncode = build.wait()

if returncode != 0:
return "package build_command failed, see log in {}".format(buildlog)
return f"package build_command failed, see log in {buildlog}"

pkg_script_dir = metadata.get("script_dir", "")
script_dir_src = os.path.join(clone.working_dir, pkg_script_dir)
Expand Down Expand Up @@ -2789,8 +2789,8 @@ def _stage(self, package, version, clone, stage, env=None):
make_symlink(os.path.join("packages", package.name), symlink_path)

except OSError as exception:
error = "could not create symlink at {}".format(symlink_path)
error += ": {}: {}".format(type(exception).__name__, exception)
error = f"could not create symlink at {symlink_path}"
error += f": {type(exception).__name__}: {exception}"
return error

error = _copy_package_dir(
Expand Down Expand Up @@ -2930,7 +2930,7 @@ def install(self, pkg_path, version=""):
return self._install(matches[0], version)
except git.exc.GitCommandError as error:
LOG.warning('installing "%s": source package git repo is invalid', pkg_path)
return 'failed to clone package "{}": {}'.format(pkg_path, error)
return f'failed to clone package "{pkg_path}": {error}'

def _install(self, package, version, use_existing_clone=False):
"""Install a :class:`.package.Package`.
Expand Down Expand Up @@ -2971,7 +2971,7 @@ def _install(self, package, version, use_existing_clone=False):
LOG.info(
'branch "%s" not in available branches: %s', version, branches
)
return 'no such branch or version tag: "{}"'.format(version)
return f'no such branch or version tag: "{version}"'

else:
if len(version_tags):
Expand Down Expand Up @@ -3152,12 +3152,12 @@ def _copy_package_dir(package, dirname, src, dst, scratch_dir):
ld = os.listdir(tmp_dir)

if len(ld) != 1:
return "failed to copy package {}: invalid tarfile".format(dirname)
return f"failed to copy package {dirname}: invalid tarfile"

src = os.path.join(tmp_dir, ld[0])

if not os.path.isdir(src):
return "failed to copy package {}: not a dir or tarfile".format(dirname)
return f"failed to copy package {dirname}: not a dir or tarfile"

def ignore(_, files):
rval = []
Expand All @@ -3176,11 +3176,11 @@ def ignore(_, files):

for err in errors:
src, dst, msg = err
reason = "failed to copy {}: {} -> {}: {}".format(dirname, src, dst, msg)
reason = f"failed to copy {dirname}: {src} -> {dst}: {msg}"
reasons += "\n" + reason
LOG.warning('installing "%s": %s', package, reason)

return "failed to copy package {}: {}".format(dirname, reasons)
return f"failed to copy package {dirname}: {reasons}"

return ""

Expand Down Expand Up @@ -3232,7 +3232,7 @@ def _parse_package_metadata(parser, metadata_file):

if not parser.has_section("package"):
LOG.warning("%s: metadata missing [package]", metadata_file)
return "{} is missing [package] section".format(os.path.basename(metadata_file))
return f"{os.path.basename(metadata_file)} is missing [package] section"

return ""

Expand Down
16 changes: 8 additions & 8 deletions zeekpkg/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ def aliases(metadata_dict):
if "aliases" not in metadata_dict:
return []

return re.split(",\s*|\s+", metadata_dict["aliases"])
return re.split(r",\s*|\s+", metadata_dict["aliases"])


def tags(metadata_dict):
"""Return a list of tag strings found in the metadata's 'tags' field."""
if "tags" not in metadata_dict:
return []

return re.split(",\s*", metadata_dict["tags"])
return re.split(r",\s*", metadata_dict["tags"])


def short_description(metadata_dict):
Expand Down Expand Up @@ -138,7 +138,7 @@ def dependencies(metadata_dict, field="depends"):


@total_ordering
class InstalledPackage(object):
class InstalledPackage:
"""An installed package and its current status.
Attributes:
Expand All @@ -161,7 +161,7 @@ def __lt__(self, other):
return str(self.package) < str(other.package)


class PackageStatus(object):
class PackageStatus:
"""The status of an installed package.
This class contains properties of a package related to how the package
Expand Down Expand Up @@ -203,7 +203,7 @@ def __init__(
self.current_hash = current_hash


class PackageInfo(object):
class PackageInfo:
"""Contains information on an arbitrary package.
If the package is installed, then its status is also available.
Expand Down Expand Up @@ -314,7 +314,7 @@ def best_version(self):


@total_ordering
class Package(object):
class Package:
"""A Zeek package.
This class contains properties of a package that are defined by the package
Expand Down Expand Up @@ -442,7 +442,7 @@ def name_with_source_directory(self):
just the package name is returned.
"""
if self.directory:
return "{}/{}".format(self.directory, self.name)
return f"{self.directory}/{self.name}"

return self.name

Expand All @@ -454,7 +454,7 @@ def qualified_name(self):
git URL is returned.
"""
if self.source:
return "{}/{}".format(self.source, self.name_with_source_directory())
return f"{self.source}/{self.name_with_source_directory()}"

return self.git_url

Expand Down
2 changes: 1 addition & 1 deletion zeekpkg/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
AGGREGATE_DATA_FILE = "aggregate.meta"


class Source(object):
class Source:
"""A Zeek package source.
This class contains properties of a package source like its name, remote git
Expand Down
Loading

0 comments on commit 3bb0df7

Please sign in to comment.