Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions cachier/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import re
import subprocess
import sys
from contextlib import suppress
from typing import Callable, Dict


Expand Down Expand Up @@ -126,9 +127,10 @@ def git_get_keywords(versionfile_abs):
# so we do it with a regexp instead. This function is not used from
# _version.py.
keywords = {}
try:
f = open(versionfile_abs, "r")
for line in f.readlines():
with suppress(EnvironmentError):
with open(versionfile_abs, "r") as f:
lines = f.readlines()
for line in lines:
if line.strip().startswith("git_refnames ="):
mo = re.search(r'=\s*"(.*)"', line)
if mo:
Expand All @@ -138,8 +140,6 @@ def git_get_keywords(versionfile_abs):
if mo:
keywords["full"] = mo.group(1)
f.close()
except EnvironmentError:
pass
return keywords


Expand Down Expand Up @@ -242,8 +242,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe)
if not mo:
# unparsable. Maybe git-describe is misbehaving?
pieces["error"] = ("unable to parse git-describe output: '%s'"
% describe_out)
pieces["error"] = ("unable to parse git-describe output: '%s'" % describe_out)
return pieces

# tag
Expand Down Expand Up @@ -450,11 +449,9 @@ def get_versions():
cfg = get_config()
verbose = cfg.verbose

try:
return git_versions_from_keywords(get_keywords(), cfg.tag_prefix,
verbose)
except NotThisMethod:
pass
with suppress(NotThisMethod):
return git_versions_from_keywords(
get_keywords(), cfg.tag_prefix, verbose)

try:
root = os.path.realpath(__file__)
Expand All @@ -468,17 +465,13 @@ def get_versions():
"dirty": None,
"error": "unable to find root of source tree"}

try:
with suppress(NotThisMethod):
pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose)
return render(pieces, cfg.style)
except NotThisMethod:
pass

try:
with suppress(NotThisMethod):
if cfg.parentdir_prefix:
return versions_from_parentdir(cfg.parentdir_prefix, root, verbose)
except NotThisMethod:
pass

return {"version": "0+unknown", "full-revisionid": None,
"dirty": None,
Expand Down
9 changes: 3 additions & 6 deletions cachier/mongo_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@

import sys # to make sure that pymongo was imported
import pickle # for serialization of python objects
from contextlib import suppress
from datetime import datetime
import time # to sleep when waiting on Mongo cache\
import warnings # to warn if pymongo is missing

try:
with suppress(ImportError):
from pymongo import (IndexModel, ASCENDING)
from pymongo.errors import OperationFailure
from bson.binary import Binary # to save binary data to mongodb
except ImportError: # pragma: no cover
pass

from .base_core import _BaseCore, RecalculationNeeded

Expand Down Expand Up @@ -107,7 +106,7 @@ def mark_entry_being_calculated(self, key):
)

def mark_entry_not_calculated(self, key):
try:
with suppress(OperationFailure): # don't care in this case
self.mongo_collection.update_one(
filter={
'func': _MongoCore._get_func_str(self.func),
Expand All @@ -118,8 +117,6 @@ def mark_entry_not_calculated(self, key):
},
upsert=False # should not insert in this case
)
except OperationFailure:
pass # don't care in this case

def wait_on_entry_calc(self, key):
time_spent = 0
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ select = [
# "D", # see: https://pypi.org/project/pydocstyle
# "N", # see: https://pypi.org/project/pep8-naming
# "S", # see: https://pypi.org/project/flake8-bandit
"SIM",
]
ignore = [
"E203",
Expand Down
5 changes: 2 additions & 3 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import queue
import subprocess # nosec: B404
import threading
from contextlib import suppress
from random import random
from time import sleep, time

Expand All @@ -33,10 +34,8 @@ def test_information():

def test_max_workers():
"""Just call this function for coverage."""
try:
with suppress(KeyError):
del os.environ[MAX_WORKERS_ENVAR_NAME]
except KeyError:
pass
assert _max_workers() == DEFAULT_MAX_WORKERS


Expand Down
4 changes: 2 additions & 2 deletions tests/test_pickle_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def _helper_bad_cache_file(sleeptime, separate_files):
thread2.start()
thread1.join(timeout=2)
thread2.join(timeout=2)
if not res_queue.qsize() == 2:
if res_queue.qsize() != 2:
return False
res1 = res_queue.get()
if not isinstance(res1, float):
Expand Down Expand Up @@ -459,7 +459,7 @@ def _helper_delete_cache_file(sleeptime, separate_files):
thread2.start()
thread1.join(timeout=2)
thread2.join(timeout=2)
if not res_queue.qsize() == 2:
if res_queue.qsize() != 2:
return False
res1 = res_queue.get()
# print(res1)
Expand Down
43 changes: 14 additions & 29 deletions versioneer.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@

from __future__ import print_function

from contextlib import suppress

try:
import configparser
except ImportError:
Expand Down Expand Up @@ -387,7 +389,7 @@ def get_root():
"or in a way that lets it use sys.argv[0] to find the root "
"(like 'python path/to/setup.py COMMAND').")
raise VersioneerBadRootError(err)
try:
with suppress(NameError):
# Certain runtime workflows (setup.py install/develop in a setuptools
# tree) execute all dependencies in a single python process, so
# "versioneer" may be imported multiple times, and python's shared
Expand All @@ -398,8 +400,6 @@ def get_root():
if os.path.splitext(me)[0] != os.path.splitext(versioneer_py)[0]:
print("Warning: build in %s is using versioneer.py from %s"
% (os.path.dirname(me), versioneer_py))
except NameError:
pass
return root


Expand Down Expand Up @@ -978,7 +978,7 @@ def git_get_keywords(versionfile_abs):
# so we do it with a regexp instead. This function is not used from
# _version.py.
keywords = {}
try:
with suppress(EnvironmentError):
f = open(versionfile_abs, "r")
for line in f.readlines():
if line.strip().startswith("git_refnames ="):
Expand All @@ -990,8 +990,6 @@ def git_get_keywords(versionfile_abs):
if mo:
keywords["full"] = mo.group(1)
f.close()
except EnvironmentError:
pass
return keywords


Expand Down Expand Up @@ -1146,15 +1144,13 @@ def do_vcs_install(manifest_in, versionfile_source, ipy):
versioneer_file = "versioneer.py"
files.append(versioneer_file)
present = False
try:
with suppress(EnvironmentError):
f = open(".gitattributes", "r")
for line in f.readlines():
if line.strip().startswith(versionfile_source):
if "export-subst" in line.strip().split()[1:]:
present = True
f.close()
except EnvironmentError:
pass
if not present:
f = open(".gitattributes", "a+")
f.write("%s export-subst\n" % versionfile_source)
Expand Down Expand Up @@ -1425,42 +1421,34 @@ def get_versions(verbose=False):
get_keywords_f = handlers.get("get_keywords")
from_keywords_f = handlers.get("keywords")
if get_keywords_f and from_keywords_f:
try:
with suppress(NotThisMethod):
keywords = get_keywords_f(versionfile_abs)
ver = from_keywords_f(keywords, cfg.tag_prefix, verbose)
if verbose:
print("got version from expanded keyword %s" % ver)
return ver
except NotThisMethod:
pass

try:
with suppress(NotThisMethod):
ver = versions_from_file(versionfile_abs)
if verbose:
print("got version from file %s %s" % (versionfile_abs, ver))
return ver
except NotThisMethod:
pass

from_vcs_f = handlers.get("pieces_from_vcs")
if from_vcs_f:
try:
with suppress(NotThisMethod):
pieces = from_vcs_f(cfg.tag_prefix, root, verbose)
ver = render(pieces, cfg.style)
if verbose:
print("got version from VCS %s" % ver)
return ver
except NotThisMethod:
pass

try:
with suppress(NotThisMethod):
if cfg.parentdir_prefix:
ver = versions_from_parentdir(cfg.parentdir_prefix, root, verbose)
if verbose:
print("got version from parentdir %s" % ver)
return ver
except NotThisMethod:
pass

if verbose:
print("unable to compute version")
Expand Down Expand Up @@ -1697,14 +1685,11 @@ def do_setup():
# install the package without this.
manifest_in = os.path.join(root, "MANIFEST.in")
simple_includes = set()
try:
with open(manifest_in, "r") as f:
for line in f:
if line.startswith("include "):
for include in line.split()[1:]:
simple_includes.add(include)
except EnvironmentError:
pass
with suppress(EnvironmentError), open(manifest_in, "r") as f:
for line in f:
if line.startswith("include "):
for include in line.split()[1:]:
simple_includes.add(include)
# That doesn't cover everything MANIFEST.in can do
# (http://docs.python.org/2/distutils/sourcedist.html#commands), so
# it might give some false negatives. Appending redundant 'include'
Expand Down