Merge lp:~james-w/linaro-image-tools/set-content-mtime into lp:linaro-image-tools/11.11

Proposed by James Westby
Status: Merged
Merged at revision: 82
Proposed branch: lp:~james-w/linaro-image-tools/set-content-mtime
Merge into: lp:linaro-image-tools/11.11
Prerequisite: lp:~james-w/linaro-image-tools/fix-package-regex
Diff against target: 111 lines (+27/-5)
4 files modified
hwpack/hardwarepack.py (+3/-0)
hwpack/tarfile_matchers.py (+12/-4)
hwpack/testing.py (+5/-1)
hwpack/tests/test_tarfile_matchers.py (+7/-0)
To merge this branch: bzr merge lp:~james-w/linaro-image-tools/set-content-mtime
Reviewer Review Type Date Requested Status
Zygmunt Krynicki (community) Approve
Review via email: mp+35329@code.launchpad.net

Description of the change

Hi,

Here's another small branch to set an mtime for all the files that we
are putting in the hardware pack.

I opted to just allow a skew from the current time in the matchers, rather
than trying to construct things such that I could do an exact match.
While it's not ideal, I don't think it will actually cause us any issues.

Thanks,

James

To post a comment you must log in.
Revision history for this message
Zygmunt Krynicki (zyga) wrote :

Looks good but I'd simplify one piece:

65 + if self.mtime is not None:
66 + if self.mtime_skew is None:
67 + if self.mtime != info.mtime:
68 + return TarfileWrongValueMismatch(
69 + "mtime", tarball, self.path, self.mtime, info.mtime)
70 + else:
71 + if ((self.mtime > info.mtime
72 + and self.mtime - self.mtime_skew > info.mtime)
73 + or (self.mtime <= info.mtime
74 + and self.mtime + self.mtime_skew < info.mtime)):
75 + return TarfileWrongValueMismatch(
76 + "mtime", tarball, self.path, self.mtime,
77 + info.mtime)

Instead:

mtime_skew = self.mtime_skew or 0
...
if abs(self.mtime - info.mtime) > mtime_skew:
   return TarfileWrongValueMismatch(...)

This is much easier than your code while doing roughly the same thing (less conditions, less comparisons to track)

review: Approve
Revision history for this message
James Westby (james-w) wrote :

Fixed, thanks.

James

144. By James Westby

Merged fix-package-regex into set-content-mtime.

145. By James Westby

Simplify mtime_skew handling. Thanks Zygmunt.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hwpack/hardwarepack.py'
2--- hwpack/hardwarepack.py 2010-09-13 21:22:45 +0000
3+++ hwpack/hardwarepack.py 2010-09-13 21:22:45 +0000
4@@ -1,3 +1,5 @@
5+import time
6+
7 from hwpack.better_tarfile import writeable_tarfile
8 from hwpack.packages import get_packages_file
9
10@@ -166,6 +168,7 @@
11 kwargs["default_gid"] = 1000
12 kwargs["default_uname"] = "user"
13 kwargs["default_gname"] = "group"
14+ kwargs["default_mtime"] = time.time()
15 with writeable_tarfile(fileobj, mode="w:gz", **kwargs) as tf:
16 tf.create_file_from_string(
17 self.FORMAT_FILENAME, self.FORMAT + "\n")
18
19=== modified file 'hwpack/tarfile_matchers.py'
20--- hwpack/tarfile_matchers.py 2010-08-31 16:33:16 +0000
21+++ hwpack/tarfile_matchers.py 2010-09-13 21:22:45 +0000
22@@ -65,9 +65,9 @@
23 class TarfileHasFile(Matcher):
24 """Check that a tarfile has an entry with certain values."""
25
26- def __init__(self, path, type=None, size=None, mtime=None, mode=None,
27- linkname=None, uid=None, gid=None, uname=None, gname=None,
28- content=None):
29+ def __init__(self, path, type=None, size=None, mtime=None,
30+ mtime_skew=None, mode=None, linkname=None, uid=None,
31+ gid=None, uname=None, gname=None, content=None):
32 """Create a TarfileHasFile Matcher.
33
34 :param path: the path that must be present.
35@@ -78,6 +78,8 @@
36 to not check.
37 :param mtime: the mtime that the entry at `path` must have, or None
38 to not check.
39+ :param mtime_skew: the number of seconds that the file mtime can
40+ be different to the required.
41 :param mode: the mode that the entry at `path` must have, or None
42 to not check.
43 :param linkname: the linkname that the entry at `path` must have,
44@@ -97,6 +99,7 @@
45 self.type = type
46 self.size = size
47 self.mtime = mtime
48+ self.mtime_skew = mtime_skew
49 self.mode = mode
50 self.linkname = linkname
51 self.uid = uid
52@@ -111,7 +114,7 @@
53 return TarfileMissingPathMismatch(tarball, self.path)
54 info = tarball.getmember(self.path)
55 for attr in (
56- "type", "size", "mtime", "mode", "linkname", "uid", "gid",
57+ "type", "size", "mode", "linkname", "uid", "gid",
58 "uname", "gname"):
59 expected = getattr(self, attr, None)
60 if expected is not None:
61@@ -119,6 +122,11 @@
62 if expected != actual:
63 return TarfileWrongValueMismatch(
64 attr, tarball, self.path, expected, actual)
65+ if self.mtime is not None:
66+ mtime_skew = self.mtime_skew or 0
67+ if abs(self.mtime - info.mtime) > self.mtime_skew:
68+ return TarfileWrongValueMismatch(
69+ "mtime", tarball, self.path, self.mtime, info.mtime)
70 if self.content is not None:
71 actual = tarball.extractfile(self.path).read()
72 if actual != self.content:
73
74=== modified file 'hwpack/testing.py'
75--- hwpack/testing.py 2010-09-13 21:22:45 +0000
76+++ hwpack/testing.py 2010-09-13 21:22:45 +0000
77@@ -5,6 +5,7 @@
78 import tempfile
79 from StringIO import StringIO
80 import tarfile
81+import time
82
83 from testtools import TestCase
84 from testtools.matchers import Matcher, Mismatch
85@@ -255,7 +256,10 @@
86 kwargs.setdefault("gid", 1000)
87 kwargs.setdefault("uname", "user")
88 kwargs.setdefault("gname", "group")
89- # TODO: mtime checking
90+ kwargs.setdefault("mtime", time.time())
91+ # Enough that it won't fail if the test is slow to execute, but
92+ # not enough that we can have a wildly wrong timestamp.
93+ kwargs.setdefault("mtime_skew", 100)
94 super(HardwarePackHasFile, self).__init__(path, **kwargs)
95
96
97
98=== modified file 'hwpack/tests/test_tarfile_matchers.py'
99--- hwpack/tests/test_tarfile_matchers.py 2010-08-31 16:35:41 +0000
100+++ hwpack/tests/test_tarfile_matchers.py 2010-09-13 21:22:45 +0000
101@@ -226,3 +226,10 @@
102 mismatch = matcher.match(tf)
103 self.assertValueMismatch(
104 mismatch, tf, "foo", "content", "othercontent", "somecontent")
105+
106+ def test_matches_mtime_with_skew(self):
107+ backing_file = StringIO()
108+ with test_tarfile(contents=[("foo", "")], default_mtime=12345) as tf:
109+ matcher = TarfileHasFile("foo", mtime=12346, mtime_skew=1)
110+ mismatch = matcher.match(tf)
111+ self.assertIs(None, mismatch)

Subscribers

People subscribed via source and target branches