14
14
import subprocess , shutil
15
15
16
16
def writeRandomFile (fn , size , start = b'' , add = b'a' ):
17
+ "Create a pseudo-random reproducible file from seeds `start` amd `add`"
17
18
with open (fn , 'wb' ) as f :
18
19
m = md5 ()
19
20
m .update (start )
@@ -43,7 +44,12 @@ def setUp(self):
43
44
writeRandomFile ('archive/data' , 10491 )
44
45
with open ('archive/data' , 'rb' ) as fd :
45
46
self .md5data = md5 (fd .read ()).hexdigest ()
46
- check_call ("find archive | cpio -o -H newc > archive.cpio" )
47
+ # fixed timestamps for cpio reproducibility
48
+ os .utime ('archive/data' , (0 , 0 ))
49
+ os .utime ('archive' , (0 , 0 ))
50
+
51
+ check_call (
52
+ "find archive | cpio --reproducible -o -H newc > archive.cpio" )
47
53
check_call ("gzip -c < archive.cpio > archive.cpio.gz" )
48
54
check_call ("bzip2 -c < archive.cpio > archive.cpio.bz2" )
49
55
try :
@@ -55,7 +61,7 @@ def setUp(self):
55
61
self .doXZ = False
56
62
57
63
def tearDown (self ):
58
- check_call ("rm -rf archive archive.cpio*" )
64
+ check_call ("rm -rf archive archive.cpio* archive2.cpio* " )
59
65
60
66
# TODO check with file (like 'r:*')
61
67
# TODO use cat to check properly for pipes
@@ -71,7 +77,8 @@ def archiveExtract(self, fn, fmt='r|*'):
71
77
self .assertTrue (found )
72
78
73
79
def archiveCreate (self , fn , fmt = 'w' ):
74
- os .unlink (fn )
80
+ if os .path .exists (fn ):
81
+ os .unlink (fn )
75
82
arc = CpioFile .open (fn , fmt )
76
83
f = arc .getcpioinfo ('archive/data' )
77
84
arc .addfile (f , open ('archive/data' , 'rb' ))
@@ -80,15 +87,23 @@ def archiveCreate(self, fn, fmt='w'):
80
87
# special case for XZ, test check type (crc32)
81
88
if fmt .endswith ('xz' ):
82
89
with open (fn , 'rb' ) as f :
83
- f .seek (6 )
84
- self .assertEqual (f .read (2 ), b'\x00 \x01 ' )
90
+ # check xz magic
91
+ self .assertEqual (f .read (6 ), b"\xfd 7zXZ\0 " )
92
+ # check stream flags
93
+ if sys .version_info < (3 , 0 ):
94
+ expected_flags = b'\x00 \x01 ' # pylzma defaults to CRC32
95
+ else :
96
+ expected_flags = b'\x00 \x04 ' # python3 defaults to CRC64
97
+ self .assertEqual (f .read (2 ), expected_flags )
85
98
self .archiveExtract (fn )
86
99
87
100
def doArchive (self , fn , fmt = None ):
88
101
self .archiveExtract (fn )
89
- self .archiveCreate (fn , fmt is None and 'w' or 'w|%s' % fmt )
90
- if not fmt is None :
91
- self .archiveExtract (fn , 'r|%s' % fmt )
102
+ fn2 = "archive2" + fn [len ("archive" ):]
103
+ print ("creating %s" % fn2 )
104
+ self .archiveCreate (fn2 , fmt is None and 'w' or 'w|%s' % fmt )
105
+ if fmt is not None :
106
+ self .archiveExtract (fn2 , 'r|%s' % fmt )
92
107
93
108
def test_plain (self ):
94
109
self .doArchive ('archive.cpio' )
@@ -101,7 +116,7 @@ def test_bz2(self):
101
116
102
117
def test_xz (self ):
103
118
if not self .doXZ :
104
- return
119
+ raise unittest . SkipTest ( "lzma package or xz tool not available" )
105
120
print ('Running test for XZ' )
106
121
self .doArchive ('archive.cpio.xz' , 'xz' )
107
122
0 commit comments