Merge ~jslarraz/review-tools:rework-extract-file into review-tools:master

Proposed by Jorge Sancho Larraz
Status: Merged
Merged at revision: 50e283a3278d17ba6d0be701a209ac8df0ef2de9
Proposed branch: ~jslarraz/review-tools:rework-extract-file
Merge into: review-tools:master
Diff against target: 125 lines (+40/-17)
3 files modified
reviewtools/containers/base_container.py (+20/-9)
reviewtools/sr_lint.py (+3/-4)
reviewtools/tests/containers/test_base_container.py (+17/-4)
Reviewer Review Type Date Requested Status
Alex Murray Approve
Review via email: mp+466381@code.launchpad.net

Commit message

rework BaseContainer:_extract_file and add relevant tests

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

LGTM - nice use of type hints too!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/reviewtools/containers/base_container.py b/reviewtools/containers/base_container.py
index 173036a..3cd069f 100644
--- a/reviewtools/containers/base_container.py
+++ b/reviewtools/containers/base_container.py
@@ -1,6 +1,7 @@
1import os1import os
2import shutil2import shutil
3import magic3import magic
4from typing import Union
4from tempfile import mkdtemp5from tempfile import mkdtemp
56
6from reviewtools.common import (7from reviewtools.common import (
@@ -9,7 +10,6 @@ from reviewtools.common import (
9 debug,10 debug,
10 unpack_pkg,11 unpack_pkg,
11 recursive_rm,12 recursive_rm,
12 open_file_read,
13)13)
1414
1515
@@ -104,15 +104,26 @@ class BaseContainer:
104 if os.path.exists(self.tmp_dir):104 if os.path.exists(self.tmp_dir):
105 recursive_rm(self.tmp_dir)105 recursive_rm(self.tmp_dir)
106106
107 def _extract_file(self, fn):107 def get_file(self, fn: str) -> Union[str, None]:
108 """Extract file"""108 """
109 if not fn.startswith("/"):109 Reads and return the raw content of the specified file.
110 raise ContainerException("_extract_file() expects absolute path")110
111 rel = os.path.relpath(fn, self.unpack_dir)111 Args:
112 fn (str): file path relative to the container root (i.e. unpack_dir)
113
114 Returns:
115 str: raw content of the specified file. The output will need to be
116 decoded() to use it as a string
117
118 Raises:
119 ContainerException: Calling this method will raise an exception if
120 the requested file is not found.
121 """
122 if not os.path.exists(os.path.join(self.unpack_dir, fn)):
123 raise ContainerException("Could not find '%s'" % fn)
112124
113 if not os.path.isfile(fn):125 with open(os.path.join(self.unpack_dir, fn), "rb") as fd:
114 raise ContainerException("Could not find '%s'" % rel)126 return fd.read()
115 return open_file_read(fn)
116127
117 def get_files_list(self):128 def get_files_list(self):
118 """List all files included in this package."""129 """List all files included in this package."""
diff --git a/reviewtools/sr_lint.py b/reviewtools/sr_lint.py
index 518e9a2..aeea216 100644
--- a/reviewtools/sr_lint.py
+++ b/reviewtools/sr_lint.py
@@ -1982,11 +1982,10 @@ class SnapReviewLint(SnapReview):
1982 appnames.append("%s.%s" % (self.snap_yaml["name"], app))1982 appnames.append("%s.%s" % (self.snap_yaml["name"], app))
19831983
1984 try:1984 try:
1985 fh = self.snap._extract_file(fn)1985 file = self.snap.get_file(fn)
1986 except ContainerException as e:1986 except ContainerException as e:
1987 error(str(e))1987 error(str(e))
1988 lines = fh.readlines()1988 lines = file.decode().split("\n")
1989 fh.close()
19901989
1991 # For now, just check Exec=, Icon= and bools since:1990 # For now, just check Exec=, Icon= and bools since:
1992 # - snapd strips out anything it doesn't understand1991 # - snapd strips out anything it doesn't understand
@@ -2130,7 +2129,7 @@ class SnapReviewLint(SnapReview):
2130 for f in self.pkg_files:2129 for f in self.pkg_files:
2131 fn = os.path.relpath(f, self._get_unpack_dir())2130 fn = os.path.relpath(f, self._get_unpack_dir())
2132 if fn.startswith("meta/gui/") and fn.endswith(".desktop"):2131 if fn.startswith("meta/gui/") and fn.endswith(".desktop"):
2133 self._verify_desktop_file(f)2132 self._verify_desktop_file(fn)
2134 has_desktop_files = True2133 has_desktop_files = True
2135 break2134 break
21362135
diff --git a/reviewtools/tests/containers/test_base_container.py b/reviewtools/tests/containers/test_base_container.py
index 4d0563f..976c74a 100644
--- a/reviewtools/tests/containers/test_base_container.py
+++ b/reviewtools/tests/containers/test_base_container.py
@@ -12,9 +12,7 @@ class TestBaseContainer(unittest.TestCase):
12 @classmethod12 @classmethod
13 def setUpClass(cls):13 def setUpClass(cls):
14 cls.tmp_dir = mkdtemp()14 cls.tmp_dir = mkdtemp()
15 cls.fn = make_snap2(15 cls.fn = make_snap2(output_dir=cls.tmp_dir, yaml="Test")
16 output_dir=cls.tmp_dir,
17 )
1816
19 @classmethod17 @classmethod
20 def tearDownClass(cls):18 def tearDownClass(cls):
@@ -25,7 +23,7 @@ class TestBaseContainer(unittest.TestCase):
25 def test_initialization_container__happy(self):23 def test_initialization_container__happy(self):
26 try:24 try:
27 BaseContainer(self.fn)25 BaseContainer(self.fn)
28 except Exception:26 except ContainerException:
29 self.fail(27 self.fail(
30 "An unexpected error occurred during BaseContainer initialization with container file"28 "An unexpected error occurred during BaseContainer initialization with container file"
31 )29 )
@@ -37,3 +35,18 @@ class TestBaseContainer(unittest.TestCase):
37 "does not exists or has incorrect permissions"35 "does not exists or has incorrect permissions"
38 in context.exception.value36 in context.exception.value
39 )37 )
38
39 # Test get file
40 def test_get_file__happy(self):
41 try:
42 container = BaseContainer(self.fn)
43 snap_yaml = container.get_file("meta/snap.yaml")
44 self.assertEqual(snap_yaml.decode(), "Test")
45 except ContainerException:
46 self.fail("An unexpected error occurred while getting meta/snap.yaml")
47
48 def test_get_file__missing_file(self):
49 with self.assertRaises(ContainerException) as context:
50 container = BaseContainer(self.fn)
51 container.get_file("non/existent/file")
52 self.assertTrue("Could not find" in context.exception.value)

Subscribers

People subscribed via source and target branches