Skip to content

Commit 1725290

Browse files
authored
Merge pull request #514 from M0ses/refactor_archive
Refactor archive
2 parents 82674ef + cfb2e44 commit 1725290

File tree

6 files changed

+73
-74
lines changed

6 files changed

+73
-74
lines changed

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,4 +432,4 @@ exclude-protected=_asdict,_fields,_replace,_source,_make
432432

433433
# Exceptions that will emit a warning when being caught. Defaults to
434434
# "Exception"
435-
overgeneral-exceptions=Exception
435+
overgeneral-exceptions=builtins.Exception

.pylinttestsrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,4 @@ exclude-protected=_asdict,_fields,_replace,_source,_make
430430

431431
# Exceptions that will emit a warning when being caught. Defaults to
432432
# "Exception"
433-
overgeneral-exceptions=Exception
433+
overgeneral-exceptions=builtins.Exception

GNUmakefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ flake8:
113113
echo "Skipping flake8 - python2 in CI" \
114114
;else \
115115
if [ "x$(FLAKE83)" != "x" ]; then \
116-
echo "Running flake83";\
117-
$(FLAKE83);\
118-
echo "Finished flake83";\
116+
echo "Running flake83"\
117+
$(FLAKE83)\
118+
echo "Finished flake83"\
119119
else \
120120
echo "flake8 for python3 not found or python major version == 2 ($(python_version_major))";\
121121
fi \

TarSCM/archive.py

Lines changed: 54 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -46,42 +46,12 @@ def extract_from_archive(self, repodir, files, outdir):
4646

4747
shutil.copy2(src, outdir)
4848

49-
50-
class ObsCpio(BaseArchive):
51-
def create_archive(self, scm_object, **kwargs):
52-
"""Create an OBS cpio archive of repodir in destination directory.
49+
def filter_files(self, filelist, topdir, args):
50+
"""
51+
Filter filelist by exclude/include parameters
5352
"""
54-
basename = kwargs['basename']
55-
dstname = kwargs['dstname']
56-
version = kwargs['version']
57-
args = kwargs['cli']
58-
commit = scm_object.get_current_commit()
5953
package_metadata = args.package_meta
6054

61-
(workdir, topdir) = os.path.split(scm_object.arch_dir)
62-
extension = 'obscpio'
63-
64-
cwd = os.getcwd()
65-
os.chdir(workdir)
66-
67-
archivefilename = os.path.join(args.outdir, dstname + '.' + extension)
68-
archivefile = open(archivefilename, "w")
69-
70-
# detect reproducible support
71-
params = ['cpio', '--create', '--format=newc', '--owner', '0:0']
72-
chkcmd = "cpio --create --format=newc --reproducible "
73-
chkcmd += "</dev/null >/dev/null 2>&1"
74-
if os.system(chkcmd) == 0:
75-
params.append('--reproducible')
76-
77-
proc = subprocess.Popen(
78-
params,
79-
shell = False,
80-
stdin = subprocess.PIPE,
81-
stdout = archivefile,
82-
stderr = subprocess.STDOUT
83-
)
84-
8555
# transform glob patterns to regular expressions
8656
includes = ''
8757
excludes = r'$.'
@@ -96,7 +66,7 @@ def create_archive(self, scm_object, **kwargs):
9666

9767
# add topdir without filtering for now
9868
cpiolist = []
99-
for root, dirs, files in os.walk(topdir, topdown=False):
69+
for root, dirs, files in filelist:
10070
# excludes
10171
dirs[:] = [os.path.join(root, d) for d in dirs]
10272
dirs[:] = [d for d in dirs if not re.match(excludes, d)]
@@ -114,9 +84,44 @@ def create_archive(self, scm_object, **kwargs):
11484
for name in files:
11585
if not METADATA_PATTERN.match(name) or package_metadata:
11686
cpiolist.append(name)
87+
return sorted(cpiolist)
11788

89+
class ObsCpio(BaseArchive):
90+
def create_archive(self, scm_object, **kwargs):
91+
"""Create an OBS cpio archive of repodir in destination directory.
92+
"""
93+
basename = kwargs['basename']
94+
dstname = kwargs['dstname']
95+
version = kwargs['version']
96+
args = kwargs['cli']
97+
commit = scm_object.get_current_commit()
98+
99+
(workdir, topdir) = os.path.split(scm_object.arch_dir)
100+
extension = 'obscpio'
101+
102+
cwd = os.getcwd()
103+
os.chdir(workdir)
104+
105+
archivefilename = os.path.join(args.outdir, dstname + '.' + extension)
106+
archivefile = open(archivefilename, "w")
107+
108+
# detect reproducible support
109+
params = ['cpio', '--create', '--format=newc', '--owner', '0:0']
110+
chkcmd = "cpio --create --format=newc --reproducible "
111+
chkcmd += "</dev/null >/dev/null 2>&1"
112+
if os.system(chkcmd) == 0:
113+
params.append('--reproducible')
114+
115+
proc = subprocess.Popen(
116+
params,
117+
shell = False,
118+
stdin = subprocess.PIPE,
119+
stdout = archivefile,
120+
stderr = subprocess.STDOUT
121+
)
122+
filelist = os.walk(topdir, topdown=False)
118123
tstamp = self.helpers.get_timestamp(scm_object, args, topdir)
119-
for name in sorted(cpiolist):
124+
for name in self.filter_files(filelist, topdir, args):
120125
try:
121126
os.utime(name, (tstamp, tstamp), follow_symlinks=False)
122127
except OSError:
@@ -186,24 +191,6 @@ def create_archive(self, scm_object, **kwargs):
186191
pat = fnmatch.translate(os.path.join(topdir, exc))
187192
excl_patterns.append(re.compile(pat))
188193

189-
def tar_exclude(filename):
190-
"""
191-
Exclude (return True) or add (return False) file to tar achive.
192-
"""
193-
if not package_metadata and METADATA_PATTERN.match(filename):
194-
return True
195-
196-
if incl_patterns:
197-
for pat in incl_patterns:
198-
if pat.match(filename):
199-
return False
200-
return True
201-
202-
for pat in excl_patterns:
203-
if pat.match(filename):
204-
return True
205-
return False
206-
207194
def reset(tarinfo):
208195
"""Python 2.7 only: reset uid/gid to 0/0 (root)."""
209196
tarinfo.uid = tarinfo.gid = 0
@@ -212,31 +199,32 @@ def reset(tarinfo):
212199
tarinfo.mtime = timestamp
213200
return tarinfo
214201

215-
def tar_filter(tarinfo):
216-
if tar_exclude(tarinfo.name):
217-
return None
218-
219-
return reset(tarinfo)
220-
221202
cwd = os.getcwd()
222203
os.chdir(workdir)
223204
enc = locale.getpreferredencoding()
224205

225206
out_file = os.path.join(outdir, dstname + '.' + extension)
207+
filelist = os.walk(topdir, topdown=False)
208+
209+
files_added = dict()
226210

227211
with tarfile.open(out_file, "w", encoding=enc) as tar:
228212
try:
229213
tar.add(topdir, recursive=False, filter=reset)
230214
except TypeError:
231215
# Python 2.6 compatibility
232216
tar.add(topdir, recursive=False)
233-
for entry in map(lambda x: os.path.join(topdir, x),
234-
sorted(os.listdir(topdir))):
235-
try:
236-
tar.add(entry, filter=tar_filter)
237-
except TypeError:
238-
# Python 2.6 compatibility
239-
tar.add(entry, exclude=tar_exclude)
217+
for entry in self.filter_files(filelist, topdir, args):
218+
logging.debug("Filtered file: %s", entry)
219+
if not files_added.get(entry, False):
220+
logging.debug("Adding filtered file: %s", entry)
221+
try:
222+
tar.add(entry, recursive=False, filter=reset)
223+
except TypeError:
224+
# Python 2.6 compatibility
225+
tar.add(entry, exclude=tar_exclude)
226+
files_added[entry] = True
227+
logging.debug("Added filtered file: %s", entry)
240228

241229
self.archivefile = tar.name
242230

TarSCM/scm/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def __init__(self, args, task):
5555
self.task = task
5656
self.url = args.url
5757

58+
self.in_osc = bool(os.getenv('OSC_VERSION'))
59+
5860
# optional arguments
5961
self.revision = args.revision
6062
if args.user and args.keyring_passphrase:
@@ -270,9 +272,8 @@ def prepare_clone_dir(self):
270272
# of the git repo and fetch local changes
271273
is_snap = sys.argv[0].endswith("snapcraft")
272274
is_obs_scm = self.args.use_obs_scm
273-
in_osc = bool(os.getenv('OSC_VERSION'))
274275
in_git = os.path.isdir('.git')
275-
if is_snap or (is_obs_scm and in_osc and in_git):
276+
if is_snap or (is_obs_scm and self.in_osc and in_git):
276277
self.repodir = os.getcwd()
277278

278279
# construct repodir (the parent directory of the checkout)
@@ -299,7 +300,7 @@ def _calc_dir_to_clone_to(self, prefix):
299300
osc_version = 0
300301

301302
logging.debug(" - SUBDIR: %s", self.args.subdir)
302-
if not self.args.subdir and self.scm == 'git':
303+
if not self.args.subdir and self.scm == 'git' and not self.in_osc:
303304
self.partial_clone = True
304305
logging.debug("NO SUBDIR FOUND - USING PARTIAL CLONE")
305306
if self.repocachedir:

tests/commontests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ def test_tar_exclude(self):
6262
self.basename() + '/subdir/b']
6363
self.assertTrue(tarents == expected)
6464

65+
def test_tar_exclude_re(self):
66+
self.tar_scm_std('--exclude', '(a|c)')
67+
tar_file = os.path.join(self.outdir, self.basename()+'.tar')
68+
tar = tarfile.open(tar_file)
69+
tarents = tar.getnames()
70+
expected = [self.basename(),
71+
self.basename() + '/subdir',
72+
self.basename() + '/subdir/b']
73+
self.assertTrue(tarents == expected)
74+
6575
def test_tar_include(self):
6676
self.tar_scm_std('--include', self.fixtures.subdir)
6777
tar_file = os.path.join(self.outdir, self.basename()+'.tar')

0 commit comments

Comments
 (0)