Merge ~jslarraz/review-tools:rework-get-files-list into review-tools:master

Proposed by Jorge Sancho Larraz
Status: Merged
Merged at revision: 72448de02f57f31d74d1a80e61da03837cfb0ae5
Proposed branch: ~jslarraz/review-tools:rework-get-files-list
Merge into: review-tools:master
Diff against target: 203 lines (+117/-22)
4 files modified
reviewtools/common.py (+0/-6)
reviewtools/containers/base_container.py (+37/-13)
reviewtools/sr_common.py (+2/-2)
reviewtools/tests/containers/test_base_container.py (+78/-1)
Reviewer Review Type Date Requested Status
Alex Murray Approve
Review via email: mp+466450@code.launchpad.net

Commit message

many: rework get_files_list and get_compiled_binaries_list

To post a comment you must log in.
Revision history for this message
Alex Murray (alexmurray) wrote :

LGTM!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/reviewtools/common.py b/reviewtools/common.py
2index 4b50dc5..e2e80ff 100644
3--- a/reviewtools/common.py
4+++ b/reviewtools/common.py
5@@ -86,12 +86,6 @@ MAX_COMPRESSED_SIZE = 5
6 # 90% of disk but not larger than this
7 MAX_UNCOMPRESSED_SIZE = 25
8
9-# cache the expensive magic calls
10-PKG_BIN_FILES = None
11-
12-# cache gathering all the files
13-PKG_FILES = None
14-
15 # os release map
16 OS_RELEASE_MAP = {
17 "ubuntu": {
18diff --git a/reviewtools/containers/base_container.py b/reviewtools/containers/base_container.py
19index 3cd069f..9e6f104 100644
20--- a/reviewtools/containers/base_container.py
21+++ b/reviewtools/containers/base_container.py
22@@ -125,32 +125,56 @@ class BaseContainer:
23 with open(os.path.join(self.unpack_dir, fn), "rb") as fd:
24 return fd.read()
25
26- def get_files_list(self):
27- """List all files included in this package."""
28+ def get_files_list(self, abs: bool = False) -> list:
29+ """
30+ Returns a list of files present in the container.
31+
32+ Args:
33+ abs (bool): if ``True``, absolute paths are used, otherwise paths are
34+ relative to unpack_dir directory. Defaults to ``False``
35+
36+ Returns:
37+ list(str): list of files present in the container
38+ """
39 files = []
40 for root, dirnames, filenames in os.walk(self.unpack_dir):
41 for f in filenames:
42- files.append(os.path.join(root, f))
43+ file = os.path.join(root, f)
44+ if not abs:
45+ file = os.path.relpath(file, self.unpack_dir)
46+ files.append(file)
47 return files
48
49- def get_compiled_binaries_list(self):
50- """List all compiled binaries in this package."""
51- self.mime = magic.open(magic.MAGIC_MIME)
52- self.mime.load()
53+ def get_compiled_binaries_list(self, abs: bool = False) -> list:
54+ """
55+ Returns a list of compiled binaries present in the container.
56+
57+ Args:
58+ abs (bool): if ``True``, absolute paths are used, otherwise paths are
59+ relative to unpack_dir directory. Defaults to ``False``
60+
61+ Returns:
62+ list(str): list of files present in the container
63+ """
64+ mime = magic.open(magic.MAGIC_MIME)
65+ mime.load()
66 bin_files = []
67- for i in self.files:
68+ for file in self.get_files_list(abs=True):
69 try:
70- res = self.mime.file(i)
71+ res = mime.file(file)
72 except Exception: # pragma: nocover
73 # workaround for zesty python3-magic
74- debug("could not detemine mime type of '%s'" % i)
75+ debug("could not detemine mime type of '%s'" % file)
76 continue
77
78+ if not abs:
79+ file = os.path.relpath(file, self.unpack_dir)
80+
81 if (
82 res in self.magic_binary_file_descriptions
83- and not i.endswith(".mo") # check is not a message catalog
84- and i not in bin_files
85+ and not file.endswith(".mo") # check is not a message catalog
86+ and file not in bin_files
87 ):
88- bin_files.append(i)
89+ bin_files.append(file)
90
91 return bin_files
92diff --git a/reviewtools/sr_common.py b/reviewtools/sr_common.py
93index 5ded6d8..e9f18d4 100644
94--- a/reviewtools/sr_common.py
95+++ b/reviewtools/sr_common.py
96@@ -413,8 +413,8 @@ class SnapReview(ReviewBase):
97 # TODO: update code to directly use self.snap.xxx instead of assigning here
98 self.pkg_filename = self.snap.filename
99 self.unpack_dir = self.snap.unpack_dir
100- self.pkg_files = self.snap.files
101- self.pkg_bin_files = self.snap.bin_files
102+ self.pkg_files = self.snap.get_files_list(abs=True)
103+ self.pkg_bin_files = self.snap.get_compiled_binaries_list(abs=True)
104 self.tmp_dir = self.snap.tmp_dir
105
106 snap_yaml = self._extract_snap_yaml()
107diff --git a/reviewtools/tests/containers/test_base_container.py b/reviewtools/tests/containers/test_base_container.py
108index 976c74a..740e942 100644
109--- a/reviewtools/tests/containers/test_base_container.py
110+++ b/reviewtools/tests/containers/test_base_container.py
111@@ -9,10 +9,17 @@ from reviewtools.tests.utils import make_snap2
112 class TestBaseContainer(unittest.TestCase):
113 """Tests for reviewtools.containers.base_container.BaseContainer"""
114
115+ expected_files = ["meta/snap.yaml", "meta/icon.png", "nonexecstack.bin"]
116+ expected_binaries = ["nonexecstack.bin"]
117+
118 @classmethod
119 def setUpClass(cls):
120 cls.tmp_dir = mkdtemp()
121- cls.fn = make_snap2(output_dir=cls.tmp_dir, yaml="Test")
122+ cls.fn = make_snap2(
123+ output_dir=cls.tmp_dir,
124+ yaml="Test",
125+ extra_files=["/bin/ls:nonexecstack.bin"],
126+ )
127
128 @classmethod
129 def tearDownClass(cls):
130@@ -50,3 +57,73 @@ class TestBaseContainer(unittest.TestCase):
131 container = BaseContainer(self.fn)
132 container.get_file("non/existent/file")
133 self.assertTrue("Could not find" in context.exception.value)
134+
135+ # Test get files list
136+ def test_get_files_list__happy(self):
137+ try:
138+ container = BaseContainer(self.fn)
139+ self.assertCountEqual(
140+ container.get_files_list(),
141+ self.expected_files,
142+ )
143+ except ContainerException:
144+ self.fail("An unexpected error occurred while getting files list")
145+
146+ def test_get_files_list_abs__happy(self):
147+ try:
148+ container = BaseContainer(self.fn)
149+ self.assertCountEqual(
150+ container.get_files_list(abs=True),
151+ [
152+ os.path.join(container.unpack_dir, file)
153+ for file in self.expected_files
154+ ],
155+ )
156+ except ContainerException:
157+ self.fail("An unexpected error occurred while getting files list")
158+
159+ def test_files__happy(self):
160+ try:
161+ container = BaseContainer(self.fn)
162+ self.assertCountEqual(
163+ container.files,
164+ self.expected_files,
165+ )
166+ except ContainerException:
167+ self.fail("An unexpected error occurred while getting files list")
168+
169+ # Test get binaries list
170+ def test_get_compiled_binaries_list__happy(self):
171+ try:
172+ container = BaseContainer(self.fn)
173+ self.assertCountEqual(
174+ container.get_compiled_binaries_list(), self.expected_binaries
175+ )
176+ except ContainerException:
177+ self.fail(
178+ "An unexpected error occurred while getting compiled binaries list"
179+ )
180+
181+ def test_get_compiled_binaries_list_abs__happy(self):
182+ try:
183+ container = BaseContainer(self.fn)
184+ self.assertCountEqual(
185+ container.get_compiled_binaries_list(abs=True),
186+ [
187+ os.path.join(container.unpack_dir, file)
188+ for file in self.expected_binaries
189+ ],
190+ )
191+ except ContainerException:
192+ self.fail(
193+ "An unexpected error occurred while getting compiled binaries list"
194+ )
195+
196+ def test_bin_files__happy(self):
197+ try:
198+ container = BaseContainer(self.fn)
199+ self.assertCountEqual(container.bin_files, self.expected_binaries)
200+ except ContainerException:
201+ self.fail(
202+ "An unexpected error occurred while getting compiled binaries list"
203+ )

Subscribers

People subscribed via source and target branches