| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Copyright (c) 2012 Samuel Bronson |
| 4 |
# Copyright (c) 2012, 2014, 2015, 2017 GUO Yixuan |
| 5 |
# |
| 6 |
# Made available under the "expat" license, which follows: |
| 7 |
# |
| 8 |
# Permission is hereby granted, free of charge, to any person obtaining |
| 9 |
# a copy of this software and associated documentation files (the |
| 10 |
# "Software"), to deal in the Software without restriction, including |
| 11 |
# without limitation the rights to use, copy, modify, merge, publish, |
| 12 |
# distribute, sublicense, and/or sell copies of the Software, and to |
| 13 |
# permit persons to whom the Software is furnished to do so, subject to |
| 14 |
# the following conditions: |
| 15 |
# |
| 16 |
# The above copyright notice and this permission notice shall be included |
| 17 |
# in all copies or substantial portions of the Software. |
| 18 |
# |
| 19 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 22 |
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 23 |
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 24 |
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 25 |
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 |
|
| 27 |
import sys |
| 28 |
import tarfile |
| 29 |
import re |
| 30 |
import posixpath |
| 31 |
from pprint import pprint |
| 32 |
|
| 33 |
|
| 34 |
try: |
| 35 |
infn, pkg, ver = sys.argv[1:] |
| 36 |
except ValueError: |
| 37 |
print("usage: %s gcc-X.Y.Z.tar.(gz|bz2) gcc-X.Y-doc X.Y.Z[.mumble]" % sys.argv[0]) |
| 38 |
exit(1) |
| 39 |
|
| 40 |
# Output type, "xz" or "gz" |
| 41 |
outtype = "xz" |
| 42 |
|
| 43 |
# output filename |
| 44 |
#outfn = '%s_%s.orig.tar.gz' % (pkg, ver) |
| 45 |
outfn = ( '%s_%s.orig.tar.' + outtype ) % (pkg, ver) |
| 46 |
|
| 47 |
# directory that the output tarball would extract to |
| 48 |
# (the prefix in the tarball) |
| 49 |
outdir = '%s-%s.orig' % (pkg, ver) |
| 50 |
|
| 51 |
intar = tarfile.open(infn, 'r|*', format=tarfile.PAX_FORMAT) |
| 52 |
|
| 53 |
# Now tarfile supports compression on write |
| 54 |
outtar = tarfile.open(name=outfn, mode='w:' + outtype) |
| 55 |
|
| 56 |
pat_s = r""" |
| 57 |
^.*\.(7|texi|rst|texinfo)$ |
| 58 |
| ^gcc/ada/(xgnatugn.adb|ug_words)$ |
| 59 |
| ^gcc/ada/doc/ |
| 60 |
| ^contrib/texi2pod\.pl$ |
| 61 |
""" |
| 62 |
pat = re.compile(pat_s, re.VERBOSE) |
| 63 |
|
| 64 |
for member in intar: |
| 65 |
if not member.isreg(): |
| 66 |
continue |
| 67 |
|
| 68 |
_, path = member.name.split('/', 1) |
| 69 |
|
| 70 |
mo = pat.search(path) |
| 71 |
if not mo: |
| 72 |
continue |
| 73 |
|
| 74 |
outpath = posixpath.join(outdir, path) |
| 75 |
print(outpath) |
| 76 |
|
| 77 |
mf = intar.extractfile(member) |
| 78 |
member.name = outpath |
| 79 |
outtar.addfile(member, mf) |
| 80 |
|
| 81 |
intar.close() |
| 82 |
outtar.close() |