[scm-wrapper] Reduce code duplication in RPM wrapper

The logic for expanding the list of patterns into a list of actual RPM
paths was duplicated for both exporting a directory and file.

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2016-03-11 13:44:03 +01:00
parent 770ca9c202
commit 6daf43f72e

View File

@ -154,58 +154,39 @@ class GitWrapper(ScmBase):
class RpmScmWrapper(ScmBase): class RpmScmWrapper(ScmBase):
def _list_rpms(self, pats):
for pat in force_list(pats):
for rpm in glob.glob(pat):
yield rpm
def export_dir(self, scm_root, scm_dir, target_dir, scm_branch=None, tmp_dir=None, log_file=None): def export_dir(self, scm_root, scm_dir, target_dir, scm_branch=None, tmp_dir=None, log_file=None):
# if scm_root is a list, recursively process all RPMs for rpm in self._list_rpms(scm_root):
if isinstance(scm_root, list): scm_dir = scm_dir.lstrip("/")
for i in scm_root: tmp_dir = self._create_temp_dir(tmp_dir=tmp_dir)
self.export_dir(i, scm_dir, target_dir, scm_branch, tmp_dir, log_file) self.log_debug("Extracting directory %s from RPM package %s..." % (scm_dir, rpm))
return explode_rpm_package(rpm, tmp_dir)
# if scm_root is a glob, recursively process all RPMs makedirs(target_dir)
rpms = glob.glob(scm_root) # "dir" includes the whole directory while "dir/" includes it's content
if len(rpms) > 1 or (rpms and rpms[0] != scm_root): if scm_dir.endswith("/"):
for i in rpms: _copy_all(os.path.join(tmp_dir, scm_dir), target_dir)
self.export_dir(i, scm_dir, target_dir, scm_branch, tmp_dir, log_file) else:
return run("cp -a %s %s/" % (pipes.quote(os.path.join(tmp_dir, scm_dir)), pipes.quote(target_dir)))
self._delete_temp_dir(tmp_dir)
scm_dir = scm_dir.lstrip("/")
tmp_dir = self._create_temp_dir(tmp_dir=tmp_dir)
self.log_debug("Extracting directory %s from RPM package %s..." % (scm_dir, scm_root))
explode_rpm_package(scm_root, tmp_dir)
makedirs(target_dir)
# "dir" includes the whole directory while "dir/" includes it's content
if scm_dir.endswith("/"):
_copy_all(os.path.join(tmp_dir, scm_dir), target_dir)
else:
run("cp -a %s %s/" % (pipes.quote(os.path.join(tmp_dir, scm_dir)), pipes.quote(target_dir)))
self._delete_temp_dir(tmp_dir)
def export_file(self, scm_root, scm_file, target_dir, scm_branch=None, tmp_dir=None, log_file=None): def export_file(self, scm_root, scm_file, target_dir, scm_branch=None, tmp_dir=None, log_file=None):
# if scm_root is a list, recursively process all RPMs for rpm in self._list_rpms(scm_root):
if isinstance(scm_root, list): scm_file = scm_file.lstrip("/")
for i in scm_root: tmp_dir = self._create_temp_dir(tmp_dir=tmp_dir)
self.export_file(i, scm_file, target_dir, scm_branch, tmp_dir, log_file)
return
# if scm_root is a glob, recursively process all RPMs self.log_debug("Exporting file %s from RPM file %s..." % (scm_file, rpm))
rpms = glob.glob(scm_root) explode_rpm_package(rpm, tmp_dir)
if len(rpms) > 1 or (rpms and rpms[0] != scm_root):
for i in rpms:
self.export_file(i, scm_file, target_dir, scm_branch, tmp_dir, log_file)
return
scm_file = scm_file.lstrip("/") makedirs(target_dir)
tmp_dir = self._create_temp_dir(tmp_dir=tmp_dir) for src in glob.glob(os.path.join(tmp_dir, scm_file)):
dst = os.path.join(target_dir, os.path.basename(src))
self.log_debug("Exporting file %s from RPM file %s..." % (scm_file, scm_root)) shutil.copy2(src, dst)
explode_rpm_package(scm_root, tmp_dir) self._delete_temp_dir(tmp_dir)
makedirs(target_dir)
for src in glob.glob(os.path.join(tmp_dir, scm_file)):
dst = os.path.join(target_dir, os.path.basename(src))
shutil.copy2(src, dst)
self._delete_temp_dir(tmp_dir)
def _get_wrapper(scm_type, *args, **kwargs): def _get_wrapper(scm_type, *args, **kwargs):