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
diff --git a/reviewtools/common.py b/reviewtools/common.py
index 4b50dc5..e2e80ff 100644
--- a/reviewtools/common.py
+++ b/reviewtools/common.py
@@ -86,12 +86,6 @@ MAX_COMPRESSED_SIZE = 5
86# 90% of disk but not larger than this86# 90% of disk but not larger than this
87MAX_UNCOMPRESSED_SIZE = 2587MAX_UNCOMPRESSED_SIZE = 25
8888
89# cache the expensive magic calls
90PKG_BIN_FILES = None
91
92# cache gathering all the files
93PKG_FILES = None
94
95# os release map89# os release map
96OS_RELEASE_MAP = {90OS_RELEASE_MAP = {
97 "ubuntu": {91 "ubuntu": {
diff --git a/reviewtools/containers/base_container.py b/reviewtools/containers/base_container.py
index 3cd069f..9e6f104 100644
--- a/reviewtools/containers/base_container.py
+++ b/reviewtools/containers/base_container.py
@@ -125,32 +125,56 @@ class BaseContainer:
125 with open(os.path.join(self.unpack_dir, fn), "rb") as fd:125 with open(os.path.join(self.unpack_dir, fn), "rb") as fd:
126 return fd.read()126 return fd.read()
127127
128 def get_files_list(self):128 def get_files_list(self, abs: bool = False) -> list:
129 """List all files included in this package."""129 """
130 Returns a list of files present in the container.
131
132 Args:
133 abs (bool): if ``True``, absolute paths are used, otherwise paths are
134 relative to unpack_dir directory. Defaults to ``False``
135
136 Returns:
137 list(str): list of files present in the container
138 """
130 files = []139 files = []
131 for root, dirnames, filenames in os.walk(self.unpack_dir):140 for root, dirnames, filenames in os.walk(self.unpack_dir):
132 for f in filenames:141 for f in filenames:
133 files.append(os.path.join(root, f))142 file = os.path.join(root, f)
143 if not abs:
144 file = os.path.relpath(file, self.unpack_dir)
145 files.append(file)
134 return files146 return files
135147
136 def get_compiled_binaries_list(self):148 def get_compiled_binaries_list(self, abs: bool = False) -> list:
137 """List all compiled binaries in this package."""149 """
138 self.mime = magic.open(magic.MAGIC_MIME)150 Returns a list of compiled binaries present in the container.
139 self.mime.load()151
152 Args:
153 abs (bool): if ``True``, absolute paths are used, otherwise paths are
154 relative to unpack_dir directory. Defaults to ``False``
155
156 Returns:
157 list(str): list of files present in the container
158 """
159 mime = magic.open(magic.MAGIC_MIME)
160 mime.load()
140 bin_files = []161 bin_files = []
141 for i in self.files:162 for file in self.get_files_list(abs=True):
142 try:163 try:
143 res = self.mime.file(i)164 res = mime.file(file)
144 except Exception: # pragma: nocover165 except Exception: # pragma: nocover
145 # workaround for zesty python3-magic166 # workaround for zesty python3-magic
146 debug("could not detemine mime type of '%s'" % i)167 debug("could not detemine mime type of '%s'" % file)
147 continue168 continue
148169
170 if not abs:
171 file = os.path.relpath(file, self.unpack_dir)
172
149 if (173 if (
150 res in self.magic_binary_file_descriptions174 res in self.magic_binary_file_descriptions
151 and not i.endswith(".mo") # check is not a message catalog175 and not file.endswith(".mo") # check is not a message catalog
152 and i not in bin_files176 and file not in bin_files
153 ):177 ):
154 bin_files.append(i)178 bin_files.append(file)
155179
156 return bin_files180 return bin_files
diff --git a/reviewtools/sr_common.py b/reviewtools/sr_common.py
index 5ded6d8..e9f18d4 100644
--- a/reviewtools/sr_common.py
+++ b/reviewtools/sr_common.py
@@ -413,8 +413,8 @@ class SnapReview(ReviewBase):
413 # TODO: update code to directly use self.snap.xxx instead of assigning here413 # TODO: update code to directly use self.snap.xxx instead of assigning here
414 self.pkg_filename = self.snap.filename414 self.pkg_filename = self.snap.filename
415 self.unpack_dir = self.snap.unpack_dir415 self.unpack_dir = self.snap.unpack_dir
416 self.pkg_files = self.snap.files416 self.pkg_files = self.snap.get_files_list(abs=True)
417 self.pkg_bin_files = self.snap.bin_files417 self.pkg_bin_files = self.snap.get_compiled_binaries_list(abs=True)
418 self.tmp_dir = self.snap.tmp_dir418 self.tmp_dir = self.snap.tmp_dir
419419
420 snap_yaml = self._extract_snap_yaml()420 snap_yaml = self._extract_snap_yaml()
diff --git a/reviewtools/tests/containers/test_base_container.py b/reviewtools/tests/containers/test_base_container.py
index 976c74a..740e942 100644
--- a/reviewtools/tests/containers/test_base_container.py
+++ b/reviewtools/tests/containers/test_base_container.py
@@ -9,10 +9,17 @@ from reviewtools.tests.utils import make_snap2
9class TestBaseContainer(unittest.TestCase):9class TestBaseContainer(unittest.TestCase):
10 """Tests for reviewtools.containers.base_container.BaseContainer"""10 """Tests for reviewtools.containers.base_container.BaseContainer"""
1111
12 expected_files = ["meta/snap.yaml", "meta/icon.png", "nonexecstack.bin"]
13 expected_binaries = ["nonexecstack.bin"]
14
12 @classmethod15 @classmethod
13 def setUpClass(cls):16 def setUpClass(cls):
14 cls.tmp_dir = mkdtemp()17 cls.tmp_dir = mkdtemp()
15 cls.fn = make_snap2(output_dir=cls.tmp_dir, yaml="Test")18 cls.fn = make_snap2(
19 output_dir=cls.tmp_dir,
20 yaml="Test",
21 extra_files=["/bin/ls:nonexecstack.bin"],
22 )
1623
17 @classmethod24 @classmethod
18 def tearDownClass(cls):25 def tearDownClass(cls):
@@ -50,3 +57,73 @@ class TestBaseContainer(unittest.TestCase):
50 container = BaseContainer(self.fn)57 container = BaseContainer(self.fn)
51 container.get_file("non/existent/file")58 container.get_file("non/existent/file")
52 self.assertTrue("Could not find" in context.exception.value)59 self.assertTrue("Could not find" in context.exception.value)
60
61 # Test get files list
62 def test_get_files_list__happy(self):
63 try:
64 container = BaseContainer(self.fn)
65 self.assertCountEqual(
66 container.get_files_list(),
67 self.expected_files,
68 )
69 except ContainerException:
70 self.fail("An unexpected error occurred while getting files list")
71
72 def test_get_files_list_abs__happy(self):
73 try:
74 container = BaseContainer(self.fn)
75 self.assertCountEqual(
76 container.get_files_list(abs=True),
77 [
78 os.path.join(container.unpack_dir, file)
79 for file in self.expected_files
80 ],
81 )
82 except ContainerException:
83 self.fail("An unexpected error occurred while getting files list")
84
85 def test_files__happy(self):
86 try:
87 container = BaseContainer(self.fn)
88 self.assertCountEqual(
89 container.files,
90 self.expected_files,
91 )
92 except ContainerException:
93 self.fail("An unexpected error occurred while getting files list")
94
95 # Test get binaries list
96 def test_get_compiled_binaries_list__happy(self):
97 try:
98 container = BaseContainer(self.fn)
99 self.assertCountEqual(
100 container.get_compiled_binaries_list(), self.expected_binaries
101 )
102 except ContainerException:
103 self.fail(
104 "An unexpected error occurred while getting compiled binaries list"
105 )
106
107 def test_get_compiled_binaries_list_abs__happy(self):
108 try:
109 container = BaseContainer(self.fn)
110 self.assertCountEqual(
111 container.get_compiled_binaries_list(abs=True),
112 [
113 os.path.join(container.unpack_dir, file)
114 for file in self.expected_binaries
115 ],
116 )
117 except ContainerException:
118 self.fail(
119 "An unexpected error occurred while getting compiled binaries list"
120 )
121
122 def test_bin_files__happy(self):
123 try:
124 container = BaseContainer(self.fn)
125 self.assertCountEqual(container.bin_files, self.expected_binaries)
126 except ContainerException:
127 self.fail(
128 "An unexpected error occurred while getting compiled binaries list"
129 )

Subscribers

People subscribed via source and target branches