1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
#!/usr/bin/env python3
#
# Copyright (c) 2017 Jon Turney
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import argparse
import hashlib
import re
import os
import sys
import tarfile
import xtarfile
from . import common_constants
#
# look for archives which are duplicated between x86 and x86_64
# (these should probably be moved to noarch or src)
#
#
# helper function to compute sha512 for a particular file
# (block_size should be some multiple of sha512 block size which can be
# efficiently read)
#
def sha512_file(f, block_size=256 * 128):
sha512 = hashlib.sha512()
for chunk in iter(lambda: f.read(block_size), b''):
sha512.update(chunk)
return sha512.hexdigest()
#
#
#
class TarMemberInfo:
def __init__(self, info, sha512):
self.info = info
self.sha512 = sha512
def read_tar(f):
result = {}
try:
with xtarfile.open(f, mode='r') as t:
for m in t:
if m.isfile():
f = t.extractfile(m)
sha512 = sha512_file(f)
else:
sha512 = None
result[m.name] = TarMemberInfo(m, sha512)
except tarfile.ReadError:
# if we can't read the tar archive, we should never consider it to have
# the same contents as another tar archive...
result[f] = None
return result
#
#
#
def compare_archives(f1, f2):
# for speed, first check that archives are of the same size
if os.path.getsize(f1) != os.path.getsize(f2):
return 'different archive size'
# if they are both compressed empty files (rather than compressed empty tar
# archives), they are the same
if os.path.getsize(f1) <= 32:
return None
t1 = read_tar(f1)
t2 = read_tar(f2)
if t1.keys() != t2.keys():
return 'different member lists'
for m in t1:
# compare size of member
if t1[m].info.size != t2[m].info.size:
return 'different size for member %s' % m
# compare type of member
if t1[m].info.type != t2[m].info.type:
return 'different type for member %s' % m
# for files, compare hash of file content
if t1[m].info.isfile():
if t1[m].sha512 != t2[m].sha512:
return 'different hash for member %s' % m
# for links, compare target
elif t1[m].info.islnk() or t1[m].info.issym():
if t1[m].info.linkname != t2[m].info.linkname:
return 'different linkname for member %s' % m
# permitted differences: mtime, mode, owner uid/gid
return None
#
#
#
def find_duplicates(args):
basedir = os.path.join(args.rel_area, common_constants.ARCHES[0], 'release')
for (dirpath, _subdirs, files) in os.walk(basedir):
relpath = os.path.relpath(dirpath, basedir)
otherdir = os.path.join(args.rel_area, common_constants.ARCHES[1], 'release', relpath)
for f in files:
# not an archive
if not re.match(r'^.*\.tar' + common_constants.PACKAGE_COMPRESSIONS_RE + r'$', f):
continue
f1 = os.path.join(dirpath, f)
f2 = os.path.join(otherdir, f)
if os.path.exists(f2):
difference = compare_archives(f1, f2)
if difference is None:
print(os.path.join('release', relpath, f))
elif args.verbose:
print('%s: %s' % (os.path.join('release', relpath, f), difference))
#
#
#
def main():
relarea_default = common_constants.FTP
parser = argparse.ArgumentParser(description='Source package deduplicator')
parser.add_argument('--releasearea', action='store', metavar='DIR', help="release directory (default: " + relarea_default + ")", default=relarea_default, dest='rel_area')
parser.add_argument('-v', '--verbose', action='count', dest='verbose', help='verbose output')
(args) = parser.parse_args()
return find_duplicates(args)
#
#
#
if __name__ == "__main__":
sys.exit(main())
|