Merge ~jslarraz/review-tools:strip-pkg-functionality into review-tools:master

Proposed by Jorge Sancho Larraz
Status: Merged
Merged at revision: 80a9f97548e7365a60879e2a46940a6f85542941
Proposed branch: ~jslarraz/review-tools:strip-pkg-functionality
Merge into: review-tools:master
Diff against target: 220 lines (+46/-48)
4 files modified
reviewtools/common.py (+24/-35)
reviewtools/sr_common.py (+12/-3)
reviewtools/sr_lint.py (+1/-1)
reviewtools/sr_tests.py (+9/-9)
Reviewer Review Type Date Requested Status
Alex Murray Approve
Review via email: mp+466128@code.launchpad.net

Commit message

Strip snap related operations (unpack, extract file, list files, list binaries) from review class

Description of the change

Currently unpacking the snap, listing files and binaries and other operations are performed in the __init__function of the Review class. That means that those operations are repeated every time that the Review class or any of its subclasses (i.e. SnapReviewDeclaration, SnapReviewFunctional, SnapReviewLint, SnapReviewSecurity and potential future review classes) is instantiated.

This MR is intended to strip this functionality from the review class and define a self contained `container` class that handle this functionality. It will enable a followup MR where the `container` object is passed to review classes (instead of file name), thus removing the overhead caused by those duplicated operations.

Additionally pkg_filename, pkg_files and pkg_bin_files have been renamed to filename, files and bin_files as the pkg_prefix looks now redundant in the context of the `container`

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

LGTM - just a couple minor things to update.

review: Needs Fixing
Revision history for this message
Jorge Sancho Larraz (jslarraz) wrote :
Revision history for this message
Alex Murray (alexmurray) wrote :

LGTM - thanks @jslarraz!

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 3e3ebdb..a61314a 100644
3--- a/reviewtools/common.py
4+++ b/reviewtools/common.py
5@@ -296,8 +296,8 @@ class ReviewBase(object):
6 self.review_type = name
7
8
9-class Review(ReviewBase):
10- """Common review class"""
11+class BaseContainer:
12+ """Base container class"""
13
14 magic_binary_file_descriptions = [
15 "application/x-executable; charset=binary",
16@@ -310,37 +310,32 @@ class Review(ReviewBase):
17 "application/x-pie-executable",
18 ]
19
20- pkg_files = None
21- pkg_bin_files = None
22-
23- def __init__(self, fn, review_type, overrides=None):
24- ReviewBase.__init__(self, review_type, overrides)
25+ files = None
26+ bin_files = None
27
28+ def __init__(self, fn):
29 self.tmp_dir = mkdtemp(prefix=MKDTEMP_PREFIX, dir=MKDTEMP_DIR)
30 if os.path.isfile(fn):
31 if os.path.basename(fn).count(".") == 0:
32 error("container filename must include an extension")
33- self.pkg_filename = os.path.join(
34+ self.filename = os.path.join(
35 self.tmp_dir,
36 os.path.basename(fn),
37 )
38- shutil.copyfile(fn, self.pkg_filename)
39+ shutil.copyfile(fn, self.filename)
40 else:
41 error("Could not find '%s'" % fn)
42
43- self.unpack_dir = ".".join(self.pkg_filename.split(".")[:-1])
44+ self.unpack_dir = ".".join(self.filename.split(".")[:-1])
45 unpack_pkg(fn, dest=self.unpack_dir)
46
47- # unpack_pkg() now only supports snap v2, so just hardcode these
48- self.pkgfmt = {"type": "snap", "version": "16.04"}
49-
50 # Get a list of all unpacked files
51- if self.pkg_files is None:
52- self.pkg_files = self._list_all_files()
53+ if self.files is None:
54+ self.files = self.get_files_list()
55
56 # Setup what is needed to get a list of all unpacked compiled binaries
57- if self.pkg_bin_files is None:
58- self.pkg_bin_files = self._list_all_compiled_binaries()
59+ if self.bin_files is None:
60+ self.bin_files = self.get_compiled_binaries_list()
61
62 def __del__(self):
63 """
64@@ -360,26 +355,20 @@ class Review(ReviewBase):
65 error("Could not find '%s'" % rel)
66 return open_file_read(fn)
67
68- def _list_all_files(self):
69+ def get_files_list(self):
70 """List all files included in this package."""
71- pkg_files = []
72+ files = []
73 for root, dirnames, filenames in os.walk(self.unpack_dir):
74 for f in filenames:
75- pkg_files.append(os.path.join(root, f))
76- return pkg_files
77-
78- def _check_if_message_catalog(self, fn):
79- """Check if file is a message catalog (.mo file)."""
80- if fn.endswith(".mo"):
81- return True
82- return False
83+ files.append(os.path.join(root, f))
84+ return files
85
86- def _list_all_compiled_binaries(self):
87+ def get_compiled_binaries_list(self):
88 """List all compiled binaries in this package."""
89 self.mime = magic.open(magic.MAGIC_MIME)
90 self.mime.load()
91- pkg_bin_files = []
92- for i in self.pkg_files:
93+ bin_files = []
94+ for i in self.files:
95 try:
96 res = self.mime.file(i)
97 except Exception: # pragma: nocover
98@@ -389,12 +378,12 @@ class Review(ReviewBase):
99
100 if (
101 res in self.magic_binary_file_descriptions
102- and not self._check_if_message_catalog(i)
103- and i not in pkg_bin_files
104+ and not i.endswith(".mo") # check is not a message catalog
105+ and i not in bin_files
106 ):
107- pkg_bin_files.append(i)
108+ bin_files.append(i)
109
110- return pkg_bin_files
111+ return bin_files
112
113
114 #
115@@ -408,7 +397,7 @@ def error(out, exit_code=1, do_exit=True, output_type=None):
116 global RESULT_TYPES
117
118 if output_type is not None:
119- Review.set_report_type(None, output_type)
120+ ReviewBase.set_report_type(None, output_type)
121
122 try:
123 if REPORT_OUTPUT == "json":
124diff --git a/reviewtools/sr_common.py b/reviewtools/sr_common.py
125index fbd0472..adb0a86 100644
126--- a/reviewtools/sr_common.py
127+++ b/reviewtools/sr_common.py
128@@ -21,8 +21,9 @@ import yaml
129
130
131 from reviewtools.common import (
132- Review,
133+ ReviewBase,
134 ReviewException,
135+ BaseContainer,
136 error,
137 open_file_read,
138 read_snapd_base_declaration,
139@@ -40,7 +41,7 @@ class SnapReviewException(ReviewException):
140 """This class represents SnapReview exceptions"""
141
142
143-class SnapReview(Review):
144+class SnapReview(ReviewBase):
145 """This class represents snap reviews"""
146
147 snappy_required = ["name", "version"]
148@@ -414,7 +415,15 @@ class SnapReview(Review):
149 def __init__(self, fn, review_type, overrides=None):
150 if review_type is None: # for using utility functions
151 return
152- Review.__init__(self, fn, review_type, overrides=overrides)
153+ ReviewBase.__init__(self, review_type, overrides=overrides)
154+
155+ self.snap = BaseContainer(fn)
156+ # TODO: update code to directly use self.snap.xxx instead of assigning here
157+ self.pkg_filename = self.snap.filename
158+ self.unpack_dir = self.snap.unpack_dir
159+ self.pkg_files = self.snap.files
160+ self.pkg_bin_files = self.snap.bin_files
161+ self.tmp_dir = self.snap.tmp_dir
162
163 snap_yaml = self._extract_snap_yaml()
164 try:
165diff --git a/reviewtools/sr_lint.py b/reviewtools/sr_lint.py
166index 3ddaf3f..095acf7 100644
167--- a/reviewtools/sr_lint.py
168+++ b/reviewtools/sr_lint.py
169@@ -2008,7 +2008,7 @@ class SnapReviewLint(SnapReview):
170 else:
171 appnames.append("%s.%s" % (self.snap_yaml["name"], app))
172
173- fh = self._extract_file(fn)
174+ fh = self.snap._extract_file(fn)
175 lines = fh.readlines()
176 fh.close()
177
178diff --git a/reviewtools/sr_tests.py b/reviewtools/sr_tests.py
179index 27b80ed..54c2a6f 100644
180--- a/reviewtools/sr_tests.py
181+++ b/reviewtools/sr_tests.py
182@@ -22,7 +22,6 @@ import yaml
183 from unittest.mock import patch
184 from unittest import TestCase
185 from reviewtools.common import check_results as common_check_results
186-from reviewtools.common import ReviewBase
187
188 # These should be set in the test cases
189 TEST_SNAP_YAML = ""
190@@ -43,13 +42,12 @@ def _mock_func(self):
191 return
192
193
194-class _Review:
195- def __init__(self, fn, review_type, overrides):
196- ReviewBase.__init__(self, review_type, overrides)
197- self.pkg_filename = fn
198- self.unpack_dir = ".".join(self.pkg_filename.split(".")[:-1])
199- self.pkg_files = []
200- self.pkg_bin_files = []
201+class _BaseContainer:
202+ def __init__(self, fn):
203+ self.filename = fn
204+ self.unpack_dir = ".".join(self.filename.split(".")[:-1])
205+ self.files = []
206+ self.bin_files = []
207 self.tmp_dir = "/faketmpdir"
208
209
210@@ -88,7 +86,9 @@ def create_patches():
211 # Mock patching. Don't use decorators but instead patch in setUp() of the
212 # child.
213 patches = []
214- patches.append(patch("reviewtools.sr_common.Review.__init__", _Review.__init__))
215+ patches.append(
216+ patch("reviewtools.sr_common.BaseContainer.__init__", _BaseContainer.__init__)
217+ )
218 patches.append(
219 patch("reviewtools.sr_common.SnapReview._extract_snap_yaml", _extract_snap_yaml)
220 )

Subscribers

People subscribed via source and target branches