Skip to content
This repository was archived by the owner on Apr 30, 2020. It is now read-only.

Commit c5fa7d0

Browse files
committed
Decode early and only deal with bytes when reading arbitrary files
1 parent ea226b3 commit c5fa7d0

File tree

6 files changed

+48
-60
lines changed

6 files changed

+48
-60
lines changed

taskotron_python_versions/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ def nvr(self):
108108

109109
@property
110110
def require_names(self):
111-
return self.hdr[rpm.RPMTAG_REQUIRENAME]
111+
return [r.decode() for r in self.hdr[rpm.RPMTAG_REQUIRENAME]]
112112

113113
@property
114114
def require_nevrs(self):
115-
return self.hdr[rpm.RPMTAG_REQUIRENEVRS]
115+
return [r.decode() for r in self.hdr[rpm.RPMTAG_REQUIRENEVRS]]
116116

117117
@property
118118
def files(self):

taskotron_python_versions/python_usage.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ def task_python_usage(packages, koji_build, artifact):
3434
log.debug('Checking {}'.format(package.filename))
3535

3636
for name in package.require_names:
37-
name = name.decode()
38-
3937
if name in PYTHON_COMMAND:
4038
log.error(
4139
'{} requires {}'.format(package.filename, name))

taskotron_python_versions/requires.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ def check_requires_naming_scheme(package, repoquery):
107107
misnamed_requires = set()
108108

109109
for name in package.require_names:
110-
name = name.decode()
111-
112110
if is_unversioned(name):
113111
versioned = get_versioned_name(name, repoquery)
114112

taskotron_python_versions/two_three.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,52 @@
22

33

44
NEVRS_STARTS = {
5-
2: (b'python(abi) = 2.',),
6-
3: (b'python(abi) = 3.',)
5+
2: ('python(abi) = 2.',),
6+
3: ('python(abi) = 3.',)
77
}
88

99
NAME_STARTS = {
1010
2: (
11-
b'python-',
12-
b'python2',
13-
b'/usr/bin/python2',
14-
b'libpython2',
15-
b'pygtk2',
16-
b'pygobject2',
17-
b'pycairo',
18-
b'py-',
11+
'python-',
12+
'python2',
13+
'/usr/bin/python2',
14+
'libpython2',
15+
'pygtk2',
16+
'pygobject2',
17+
'pycairo',
18+
'py-',
1919
),
2020
3: (
21-
b'python3',
22-
b'/usr/bin/python3',
23-
b'libpython3',
24-
b'system-python'
21+
'python3',
22+
'/usr/bin/python3',
23+
'libpython3',
24+
'system-python'
2525
)
2626
}
2727

2828
NAME_EXACTS = {
2929
2: (
30-
b'/usr/bin/python',
31-
b'python',
30+
'/usr/bin/python',
31+
'python',
3232
)
3333
}
3434

3535
NAME_NOTS = (
36-
b'python-rpm-macros',
37-
b'python-srpm-macros',
38-
b'python-sphinx-locale',
39-
b'python-multilib-conf',
40-
b'python-ldb-devel-common',
41-
b'python-qt5-rpm-macros',
42-
b'python-btchip-common',
43-
b'python-matplotlib-data',
44-
b'python-matplotlib-data-fonts',
45-
b'python-cartopy-common',
46-
b'python-django-bash-completion',
47-
b'python-jupyter-filesystem',
48-
b'python-pip-wheel',
49-
b'python-setuptools-wheel',
50-
b'python-wheel-wheel',
36+
'python-rpm-macros',
37+
'python-srpm-macros',
38+
'python-sphinx-locale',
39+
'python-multilib-conf',
40+
'python-ldb-devel-common',
41+
'python-qt5-rpm-macros',
42+
'python-btchip-common',
43+
'python-matplotlib-data',
44+
'python-matplotlib-data-fonts',
45+
'python-cartopy-common',
46+
'python-django-bash-completion',
47+
'python-jupyter-filesystem',
48+
'python-pip-wheel',
49+
'python-setuptools-wheel',
50+
'python-wheel-wheel',
5151
)
5252

5353

@@ -80,22 +80,22 @@ def check_two_three(package):
8080
for nevr in package.require_nevrs:
8181
for py_version, starts in NEVRS_STARTS.items():
8282
if nevr.startswith(starts):
83-
log.debug('Found dependency {}'.format(nevr.decode()))
83+
log.debug('Found dependency {}'.format(nevr))
8484
log.debug('Requires Python {}'.format(py_version))
8585
py_versions[py_version] = nevr
8686

8787
for name in package.require_names:
8888
for py_version, starts in NAME_STARTS.items():
8989
if py_version not in py_versions:
9090
if name.startswith(starts) and name not in NAME_NOTS:
91-
log.debug('Found dependency {}'.format(name.decode()))
91+
log.debug('Found dependency {}'.format(name))
9292
log.debug('Requires Python {}'.format(py_version))
9393
py_versions[py_version] = name
9494

9595
for py_version, exacts in NAME_EXACTS.items():
9696
if py_version not in py_versions:
9797
if name in exacts:
98-
log.debug('Found dependency {}'.format(name.decode()))
98+
log.debug('Found dependency {}'.format(name))
9999
log.debug('Requires Python {}'.format(py_version))
100100
py_versions[py_version] = name
101101

taskotron_python_versions/unversioned_shebangs.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def get_problematic_files(archive, query):
4242
"""Search for the files inside archive with the first line
4343
matching given query. Some of the files can contain data, which
4444
are not in the plain text format. Bytes are read from the file and
45-
the shebang query has to be of the same type.
45+
the shebang query is encoded as well. We only test for ASCII shebangs.
4646
"""
4747
problematic = set()
4848
with libarchive.file_reader(archive) as a:
@@ -51,21 +51,15 @@ def get_problematic_files(archive, query):
5151
first_line = next(entry.get_blocks(), '').splitlines()[0]
5252
except IndexError:
5353
continue # file is empty
54-
if matches(first_line, query.encode()):
54+
if matches(first_line, query.encode('ascii')):
5555
problematic.add(entry.pathname.lstrip('.'))
5656

5757
return problematic
5858

5959

60-
def shebang_to_require(shebang, use_bytes=True):
61-
"""Convert shebang to the format of requirement.
62-
If the use_bytes argument is set to False, executable path
63-
is returned as a string instead of the default bytes type."""
64-
executable_path = shebang.split()[0][2:]
65-
if use_bytes:
66-
return executable_path.encode()
67-
else:
68-
return executable_path
60+
def shebang_to_require(shebang):
61+
"""Convert shebang to the format of requirement."""
62+
return shebang.split()[0][2:]
6963

7064

7165
def get_scripts_summary(package):
@@ -79,7 +73,7 @@ def get_scripts_summary(package):
7973
if shebang_to_require(shebang) in package.require_names:
8074
log.debug('Package {} requires {}'.format(
8175
package.filename, shebang_to_require(
82-
shebang, use_bytes=False)))
76+
shebang)))
8377
problematic = get_problematic_files(package.path, shebang)
8478
if problematic:
8579
log.debug('{} shebang was found in scripts: {}'.format(

test/functional/test_unversioned_shebangs.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,13 @@ def test_get_problematic_files(archive, query, expected):
4040
assert get_problematic_files(gpkg_path(archive), query) == expected
4141

4242

43-
@pytest.mark.parametrize(('shebang', 'use_bytes', 'expected'), (
44-
("#!/foo", True, b"/foo"),
45-
("#!/usr/bin/python", True, b"/usr/bin/python"),
46-
("#!/usr/bin/env python", True, b"/usr/bin/env"),
47-
("#!/usr/bin/python", False, "/usr/bin/python"),
48-
("#!/usr/bin/env python", False, "/usr/bin/env"),
43+
@pytest.mark.parametrize(('shebang', 'expected'), (
44+
("#!/foo", "/foo"),
45+
("#!/usr/bin/python", "/usr/bin/python"),
46+
("#!/usr/bin/env python", "/usr/bin/env"),
4947
))
50-
def test_shebang_to_require(shebang, use_bytes, expected):
51-
assert shebang_to_require(shebang, use_bytes) == expected
48+
def test_shebang_to_require(shebang, expected):
49+
assert shebang_to_require(shebang) == expected
5250

5351

5452
@pytest.mark.parametrize(('glob', 'expected'), (

0 commit comments

Comments
 (0)