Skip to content

Commit 3f2f12d

Browse files
authored
Address ResourceWarning in _built_utils/version.py (scikit-image#7904)
The opened `skimage/__init__.py` was never properly closed. In certain cases this caused pytest to fail with an `ResourceWarning` – because pytest would filter all warnings as errors.
1 parent 6020bf8 commit 3f2f12d

File tree

1 file changed

+43
-36
lines changed

1 file changed

+43
-36
lines changed
Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,56 @@
11
#!/usr/bin/env python3
2-
"""Extract version number from __init__.py"""
32

4-
import os
3+
"""Determine and print version number.
54
5+
Used in top level ``meson.build``.
6+
"""
67

7-
def git_version(version):
8-
"""Append last commit date and hash to version, if available"""
9-
import subprocess
10-
import os.path
8+
import subprocess
9+
from pathlib import Path
1110

12-
git_hash = ''
11+
12+
def version_from_init():
13+
"""Extract version string from ``skimage/__init__.py``."""
14+
skimage_init = Path(__file__).parent / '../__init__.py'
15+
assert skimage_init.is_file()
16+
17+
with skimage_init.open("r") as file:
18+
data = file.readlines()
19+
20+
version_line = next(line for line in data if line.startswith('__version__ ='))
21+
version = version_line.strip().split(' = ')[1].replace('"', '').replace("'", '')
22+
return version
23+
24+
25+
def append_git_revision_and_date(version):
26+
"""Try to append last commit date and hash to version.
27+
28+
Appends nothing if the current working directory is outside a git
29+
repository.
30+
"""
1331
try:
14-
p = subprocess.Popen(
32+
result = subprocess.run(
1533
['git', 'log', '-1', '--format="%H %aI"'],
16-
stdout=subprocess.PIPE,
17-
stderr=subprocess.PIPE,
18-
cwd=os.path.dirname(__file__),
34+
capture_output=True,
35+
text=True,
36+
check=True,
1937
)
20-
except FileNotFoundError:
38+
except subprocess.CalledProcessError:
2139
pass
2240
else:
23-
out, err = p.communicate()
24-
if p.returncode == 0:
25-
git_hash, git_date = (
26-
out.decode('utf-8')
27-
.strip()
28-
.replace('"', '')
29-
.split('T')[0]
30-
.replace('-', '')
31-
.split()
32-
)
33-
34-
version += f'+git{git_date}.{git_hash[:7]}'
35-
41+
git_hash, git_date = (
42+
result.stdout.strip()
43+
.replace('"', '')
44+
.split('T')[0]
45+
.replace('-', '')
46+
.split()
47+
)
48+
version += f'+git{git_date}.{git_hash[:7]}'
3649
return version
3750

3851

39-
ski_init = os.path.join(os.path.dirname(__file__), '../__init__.py')
40-
41-
data = open(ski_init).readlines()
42-
version_line = next(line for line in data if line.startswith('__version__ ='))
43-
44-
version = version_line.strip().split(' = ')[1].replace('"', '').replace("'", '')
45-
46-
if 'dev' in version:
47-
version = git_version(version)
48-
49-
print(version)
52+
if __name__ == "__main__":
53+
version = version_from_init()
54+
if 'dev' in version:
55+
version = append_git_revision_and_date(version)
56+
print(version)

0 commit comments

Comments
 (0)