Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery refactored main branch #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Apr 6, 2022

Branch main refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Review changes via command line

To manually merge these changes, make sure you're on the main branch, then run:

git fetch origin sourcery/main
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from admariner April 6, 2022 22:44
Copy link
Author

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to GitHub API limits, only the first 60 comments can be shown.

Comment on lines -331 to +333
"Warning: build in %s is using versioneer.py from %s"
% (os.path.dirname(me), versioneer_py)
f"Warning: build in {os.path.dirname(me)} is using versioneer.py from {versioneer_py}"
)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_root refactored with the following changes:

Comment on lines -412 to +425
print("unable to run %s" % dispcmd)
print(f"unable to run {dispcmd}")
print(e)
return None, None
else:
if verbose:
print("unable to find command, tried %s" % (commands,))
print(f"unable to find command, tried {commands}")
return None, None
stdout = p.communicate()[0].strip()
if sys.version_info[0] >= 3:
stdout = stdout.decode()
if p.returncode != 0:
if verbose:
print("unable to run %s (error)" % dispcmd)
print("stdout was %s" % stdout)
print(f"unable to run {dispcmd} (error)")
print(f"stdout was {stdout}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function run_command refactored with the following changes:

Comment on lines -964 to +974
f = open(versionfile_abs, "r")
for line in f.readlines():
if line.strip().startswith("git_refnames ="):
mo = re.search(r'=\s*"(.*)"', line)
if mo:
keywords["refnames"] = mo.group(1)
if line.strip().startswith("git_full ="):
mo = re.search(r'=\s*"(.*)"', line)
if mo:
keywords["full"] = mo.group(1)
if line.strip().startswith("git_date ="):
mo = re.search(r'=\s*"(.*)"', line)
if mo:
keywords["date"] = mo.group(1)
f.close()
with open(versionfile_abs, "r") as f:
for line in f.readlines():
if line.strip().startswith("git_refnames ="):
if mo := re.search(r'=\s*"(.*)"', line):
keywords["refnames"] = mo[1]
if line.strip().startswith("git_full ="):
if mo := re.search(r'=\s*"(.*)"', line):
keywords["full"] = mo[1]
if line.strip().startswith("git_date ="):
if mo := re.search(r'=\s*"(.*)"', line):
keywords["date"] = mo[1]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function git_get_keywords refactored with the following changes:

refs = set([r.strip() for r in refnames.strip("()").split(",")])
refs = {r.strip() for r in refnames.strip("()").split(",")}
# starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of
# just "foo-1.0". If we see a "tag: " prefix, prefer those.
TAG = "tag: "
tags = set([r[len(TAG) :] for r in refs if r.startswith(TAG)])
tags = {r[len(TAG) :] for r in refs if r.startswith(TAG)}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function git_versions_from_keywords refactored with the following changes:

GITS = ["git"]
if sys.platform == "win32":
GITS = ["git.cmd", "git.exe"]

GITS = ["git.cmd", "git.exe"] if sys.platform == "win32" else ["git"]
out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, hide_stderr=True)
if rc != 0:
if verbose:
print("Directory %s not under git control" % root)
print(f"Directory {root} not under git control")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function git_pieces_from_vcs refactored with the following changes:

This removes the following comments ( why? ):

# maybe improved later

Comment on lines -1623 to +1603
print("UPDATING %s" % target_versionfile)
print(f"UPDATING {target_versionfile}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_cmdclass.cmd_build_exe.run refactored with the following changes:

Comment on lines -1656 to +1636
print("UPDATING %s" % target_versionfile)
print(f"UPDATING {target_versionfile}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_cmdclass.cmd_py2exe.run refactored with the following changes:

Comment on lines -1699 to +1679
print("UPDATING %s" % target_versionfile)
print(f"UPDATING {target_versionfile}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_cmdclass.cmd_sdist.make_release_tree refactored with the following changes:

Comment on lines -1770 to +1750
print(" creating %s" % cfg.versionfile_source)
print(f" creating {cfg.versionfile_source}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function do_setup refactored with the following changes:

value_to_append = "{}={}".format(values, os.environ[values])
value_to_append = f"{values}={os.environ[values]}"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function MimicDockerEnvHandling.__call__ refactored with the following changes:

Comment on lines -303 to +309
'Cannot mount "{}" in editable mode '
"as it is not a directory".format(args.repo),
f'Cannot mount "{args.repo}" in editable mode as it is not a directory',
extra=dict(phase="failed"),
)
sys.exit(1)

if args.image_name:
r2d.output_image_spec = args.image_name
else:
# we will pick a name after fetching the repository
r2d.output_image_spec = ""
sys.exit(1)

r2d.output_image_spec = args.image_name or ""
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function make_r2d refactored with the following changes:

This removes the following comments ( why? ):

# we will pick a name after fetching the repository

keywords = {"refnames": git_refnames, "full": git_full, "date": git_date}
return keywords
return {"refnames": git_refnames, "full": git_full, "date": git_date}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_keywords refactored with the following changes:

Comment on lines -92 to +104
print("unable to run %s" % dispcmd)
print(f"unable to run {dispcmd}")
print(e)
return None, None
else:
if verbose:
print("unable to find command, tried %s" % (commands,))
print(f"unable to find command, tried {commands}")
return None, None
stdout = p.communicate()[0].strip()
if sys.version_info[0] >= 3:
stdout = stdout.decode()
if p.returncode != 0:
if verbose:
print("unable to run %s (error)" % dispcmd)
print("stdout was %s" % stdout)
print(f"unable to run {dispcmd} (error)")
print(f"stdout was {stdout}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function run_command refactored with the following changes:

Comment on lines -119 to +118
for i in range(3):
for _ in range(3):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function versions_from_parentdir refactored with the following changes:

Comment on lines -150 to +158
f = open(versionfile_abs, "r")
for line in f.readlines():
if line.strip().startswith("git_refnames ="):
mo = re.search(r'=\s*"(.*)"', line)
if mo:
keywords["refnames"] = mo.group(1)
if line.strip().startswith("git_full ="):
mo = re.search(r'=\s*"(.*)"', line)
if mo:
keywords["full"] = mo.group(1)
if line.strip().startswith("git_date ="):
mo = re.search(r'=\s*"(.*)"', line)
if mo:
keywords["date"] = mo.group(1)
f.close()
with open(versionfile_abs, "r") as f:
for line in f.readlines():
if line.strip().startswith("git_refnames ="):
if mo := re.search(r'=\s*"(.*)"', line):
keywords["refnames"] = mo[1]
if line.strip().startswith("git_full ="):
if mo := re.search(r'=\s*"(.*)"', line):
keywords["full"] = mo[1]
if line.strip().startswith("git_date ="):
if mo := re.search(r'=\s*"(.*)"', line):
keywords["date"] = mo[1]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function git_get_keywords refactored with the following changes:

Comment on lines -706 to +707
if tag == self.output_image_spec + ":latest":
if tag == f'{self.output_image_spec}:latest':
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Repo2Docker.find_image refactored with the following changes:

Comment on lines +730 to +742
elif self.git_workdir is None:
checkout_path = tempfile.mkdtemp(prefix="repo2docker")
else:
if self.git_workdir is None:
checkout_path = tempfile.mkdtemp(prefix="repo2docker")
else:
checkout_path = self.git_workdir
checkout_path = self.git_workdir

try:
self.fetch(self.repo, self.ref, checkout_path)

if self.find_image():
self.log.info(
"Reusing existing image ({}), not "
"building.".format(self.output_image_spec)
f"Reusing existing image ({self.output_image_spec}), not building."
)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Repo2Docker.build refactored with the following changes:

return "Image(tags={},config={})".format(self.tags, self.config)
return f"Image(tags={self.tags},config={self.config})"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Image.__repr__ refactored with the following changes:

Comment on lines -23 to +30
for vstr in reversed(versions_list):
if matcher.match(str_to_version(vstr)):
return vstr
return None
return next(
(
vstr
for vstr in reversed(versions_list)
if matcher.match(str_to_version(vstr))
),
None,
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function find_semver_match refactored with the following changes:

  • Use the built-in function next instead of a for-loop (use-next)

Comment on lines -31 to +35
return tuple([int(n) for n in vstr.split(".")])
return tuple(int(n) for n in vstr.split("."))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function str_to_version refactored with the following changes:

Comment on lines -79 to +101
comparison_symbol = constraint_str[0 : first_digit.start()].strip()
comparison_symbol = constraint_str[:first_digit.start()].strip()

# Default to "^" search if no matching mode specified (up to next major version)
if (first_digit.start() == 0) or (comparison_symbol == "^"):
if major(constraint) == 0:
if major(constraint) != 0:
return VersionRange(constraint, (major(constraint) + 1,), True)

# Also, julia treats pre-1.0 releases specially, as if the first
# non-zero number is actually a major number:
# https://docs.julialang.org/en/latest/stdlib/Pkg/#Caret-specifiers-1
# So we need to handle it separately by bumping the first non-zero
# enumber.
for i, n in enumerate(constraint):
if (
for i, n in enumerate(constraint):
if (
n != 0 or i == len(constraint) - 1
): # (using the last existing number handles situations like "^0.0" or "^0")
upper = constraint[0:i] + (n + 1,)
break
return VersionRange(constraint, upper, True)
else:
return VersionRange(constraint, (major(constraint) + 1,), True)

upper = constraint[:i] + (n + 1,)
break
return VersionRange(constraint, upper, True)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function create_semver_matcher refactored with the following changes:

Comment on lines -123 to +128
raise ValueError(
'Port specification "{}" has ' "an invalid port.".format(mapping)
)
raise ValueError(f'Port specification "{mapping}" has an invalid port.')
if p > 65535:
raise ValueError(
'Port specification "{}" specifies '
"a port above 65535.".format(mapping)
f'Port specification "{mapping}" specifies a port above 65535.'
)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function validate_and_generate_port_mapping.check_port refactored with the following changes:

Comment on lines -138 to +136
raise ValueError(
'Port specification "{}" has '
"an invalid protocol.".format(mapping)
)
raise ValueError(f'Port specification "{mapping}" has an invalid protocol.')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function validate_and_generate_port_mapping.check_port_string refactored with the following changes:

Comment on lines -302 to +297
return int(float(num) * self.UNIT_SUFFIXES[suffix])
return int(num * self.UNIT_SUFFIXES[suffix])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ByteSpecification.validate refactored with the following changes:

Comment on lines -368 to +363
if ignore is not None:
ignored_names = ignore(src, names)
else:
ignored_names = set()

ignored_names = ignore(src, names) if ignore is not None else set()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function copytree refactored with the following changes:

Comment on lines -217 to +213
url = "https://mran.microsoft.com/snapshot/{}".format(try_date.isoformat())
url = f"https://mran.microsoft.com/snapshot/{try_date.isoformat()}"
r = requests.head(url)
if r.ok:
return url
raise ValueError(
"No snapshot found for {} or {} days prior in mran.microsoft.com".format(
snapshot_date.strftime("%Y-%m-%d"), max_days_prior
)
f'No snapshot found for {snapshot_date.strftime("%Y-%m-%d")} or {max_days_prior} days prior in mran.microsoft.com'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function RBuildPack.get_mran_snapshot_url refactored with the following changes:

Comment on lines -273 to +262
if V(self.r_version) >= V("4"):
vs = "40"
else:
vs = "35"

vs = "40" if V(self.r_version) >= V("4") else "35"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function RBuildPack.get_build_scripts refactored with the following changes:

Comment on lines -390 to +379
# Delete /tmp/downloaded_packages only if install.R fails, as the second
# invocation of install.R might be able to reuse them
"Rscript %s && touch /tmp/.preassembled || true && rm -rf /tmp/downloaded_packages"
% installR_path,
f"Rscript {installR_path} && touch /tmp/.preassembled || true && rm -rf /tmp/downloaded_packages",
)
]


Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function RBuildPack.get_preassemble_scripts refactored with the following changes:

This removes the following comments ( why? ):

# invocation of install.R might be able to reuse them
# Delete /tmp/downloaded_packages only if install.R fails, as the second

Comment on lines -408 to 394
# only run install.R if the pre-assembly failed
# Delete any downloaded packages in /tmp, as they aren't reused by R
"""if [ ! -f /tmp/.preassembled ]; then Rscript {}; rm -rf /tmp/downloaded_packages; fi""".format(
installR_path
),
f"""if [ ! -f /tmp/.preassembled ]; then Rscript {installR_path}; rm -rf /tmp/downloaded_packages; fi""",
)
]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function RBuildPack.get_assemble_scripts refactored with the following changes:

This removes the following comments ( why? ):

# only run install.R if the pre-assembly failed
# Delete any downloaded packages in /tmp, as they aren't reused by R

env = super().get_env() + [("CONDA_DEFAULT_ENV", "${KERNEL_PYTHON_PREFIX}")]
return env
return super().get_env() + [("CONDA_DEFAULT_ENV", "${KERNEL_PYTHON_PREFIX}")]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CondaBuildPack.get_env refactored with the following changes:

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Apr 6, 2022

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.40%.

Quality metrics Before After Change
Complexity 7.62 ⭐ 7.18 ⭐ -0.44 👍
Method Length 51.69 ⭐ 51.46 ⭐ -0.23 👍
Working memory 7.40 🙂 7.41 🙂 0.01 👎
Quality 68.50% 🙂 68.90% 🙂 0.40% 👍
Other metrics Before After Change
Lines 9166 9017 -149
Changed files Quality Before Quality After Quality Change
versioneer.py 57.43% 🙂 57.66% 🙂 0.23% 👍
repo2docker/main.py 45.36% 😞 43.74% 😞 -1.62% 👎
repo2docker/_version.py 61.03% 🙂 61.53% 🙂 0.50% 👍
repo2docker/app.py 50.91% 🙂 51.67% 🙂 0.76% 👍
repo2docker/engine.py 96.14% ⭐ 95.62% ⭐ -0.52% 👎
repo2docker/semver.py 72.71% 🙂 73.37% 🙂 0.66% 👍
repo2docker/utils.py 63.10% 🙂 62.82% 🙂 -0.28% 👎
repo2docker/buildpacks/base.py 72.89% 🙂 72.88% 🙂 -0.01% 👎
repo2docker/buildpacks/docker.py 76.76% ⭐ 78.57% ⭐ 1.81% 👍
repo2docker/buildpacks/r.py 80.37% ⭐ 80.30% ⭐ -0.07% 👎
repo2docker/buildpacks/conda/init.py 73.88% 🙂 73.69% 🙂 -0.19% 👎
repo2docker/buildpacks/conda/freeze.py 62.65% 🙂 62.60% 🙂 -0.05% 👎
repo2docker/buildpacks/julia/julia_require.py 80.33% ⭐ 79.67% ⭐ -0.66% 👎
repo2docker/buildpacks/legacy/init.py 71.48% 🙂 72.43% 🙂 0.95% 👍
repo2docker/buildpacks/pipfile/init.py 61.72% 🙂 61.69% 🙂 -0.03% 👎
repo2docker/buildpacks/python/init.py 76.60% ⭐ 77.59% ⭐ 0.99% 👍
repo2docker/contentproviders/base.py 94.85% ⭐ 94.17% ⭐ -0.68% 👎
repo2docker/contentproviders/dataverse.py 59.26% 🙂 60.00% 🙂 0.74% 👍
repo2docker/contentproviders/doi.py 64.84% 🙂 65.78% 🙂 0.94% 👍
repo2docker/contentproviders/figshare.py 68.68% 🙂 70.54% 🙂 1.86% 👍
repo2docker/contentproviders/git.py 46.80% 😞 52.51% 🙂 5.71% 👍
repo2docker/contentproviders/hydroshare.py 59.46% 🙂 59.35% 🙂 -0.11% 👎
repo2docker/contentproviders/mercurial.py 62.57% 🙂 66.95% 🙂 4.38% 👍
repo2docker/contentproviders/swhid.py 76.16% ⭐ 75.47% ⭐ -0.69% 👎
repo2docker/contentproviders/zenodo.py 78.97% ⭐ 80.39% ⭐ 1.42% 👍
tests/conftest.py 75.40% ⭐ 75.43% ⭐ 0.03% 👍
tests/conda/simple-py2/verify 71.24% 🙂 71.11% 🙂 -0.13% 👎
tests/memlimit/dockerfile/postBuild 83.99% ⭐ 83.83% ⭐ -0.16% 👎
tests/memlimit/non-dockerfile/postBuild 83.99% ⭐ 83.83% ⭐ -0.16% 👎
tests/pipfile/py2-with-server-and-kernel-req/verify 77.63% ⭐ 77.78% ⭐ 0.15% 👍
tests/unit/test_app.py 87.90% ⭐ 87.89% ⭐ -0.01% 👎
tests/unit/test_argumentvalidation.py 92.01% ⭐ 92.44% ⭐ 0.43% 👍
tests/unit/test_connect_url.py 58.62% 🙂 57.91% 🙂 -0.71% 👎
tests/unit/test_env.py 71.31% 🙂 71.20% 🙂 -0.11% 👎
tests/unit/test_external_scripts.py 74.14% 🙂 74.36% 🙂 0.22% 👍
tests/unit/test_memlimit.py 82.38% ⭐ 82.38% ⭐ 0.00%
tests/unit/test_r.py 85.90% ⭐ 85.88% ⭐ -0.02% 👎
tests/unit/test_subdir.py 92.62% ⭐ 92.57% ⭐ -0.05% 👎
tests/unit/test_users.py 78.37% ⭐ 80.05% ⭐ 1.68% 👍
tests/unit/test_utils.py 91.34% ⭐ 91.33% ⭐ -0.01% 👎
tests/unit/test_volumes.py 82.96% ⭐ 82.74% ⭐ -0.22% 👎
tests/unit/contentproviders/test_doi.py 93.04% ⭐ 94.44% ⭐ 1.40% 👍
tests/unit/contentproviders/test_figshare.py 80.02% ⭐ 82.17% ⭐ 2.15% 👍
tests/unit/contentproviders/test_hydroshare.py 84.24% ⭐ 85.25% ⭐ 1.01% 👍
tests/unit/contentproviders/test_swhid.py 85.05% ⭐ 83.95% ⭐ -1.10% 👎
tests/unit/contentproviders/test_zenodo.py 77.50% ⭐ 80.54% ⭐ 3.04% 👍
tests/venv/usr-bin/verify 92.38% ⭐ 89.75% ⭐ -2.63% 👎

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
repo2docker/main.py make_r2d 37 ⛔ 450 ⛔ 10 😞 23.74% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
repo2docker/app.py Repo2Docker.build 30 😞 371 ⛔ 12 😞 23.98% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
repo2docker/utils.py copytree 32 😞 202 😞 13 😞 28.25% 😞 Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
versioneer.py git_pieces_from_vcs 16 🙂 297 ⛔ 14 😞 31.82% 😞 Try splitting into smaller methods. Extract out complex expressions
repo2docker/_version.py git_pieces_from_vcs 16 🙂 297 ⛔ 14 😞 31.82% 😞 Try splitting into smaller methods. Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants