Skip to content

Commit

Permalink
Add new --extract-rename option
Browse files Browse the repository at this point in the history
Allows extracting a file and renaming it. Useful for Debian builds where
the recipe file is in git, but the naming doesn't match OBS expectations:
usually git stores debian/control but OBS wants debian.control.
Also useful for multibuild, eg, to extract foo.spec to foo_TargetRepository.spec.
  • Loading branch information
bluca committed Jan 18, 2025
1 parent e5e3488 commit 3d23aac
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
18 changes: 18 additions & 0 deletions TarSCM/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ def extract_from_archive(self, repodir, files, outdir):

shutil.copy2(src, outdir)

def extract_rename_from_archive(self, repodir, tuples, outdir):
"""Extract and rename all files directly outside of the archive.
"""
if tuples is None:
return

for tuple in tuples:
path = os.path.join(repodir, tuple.split(':')[0])

if not os.path.exists(path):
sys.exit("%s: No such file or directory" % path)

r_src = os.path.realpath(path)
if not r_src.startswith(repodir):
sys.exit("%s: tries to escape the repository" % path)

shutil.copy2(path, os.path.join(outdir, tuple.split(':')[1]))


class ObsCpio(BaseArchive):
def create_archive(self, scm_object, **kwargs):
Expand Down
37 changes: 37 additions & 0 deletions TarSCM/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,34 @@ def contains_dotdot(files):
return 0


def validate_extract_rename(extract_rename):
if not extract_rename:
return True

if contains_dotdot(extract_rename):
print('--extract-rename is not allowed to contain ".."')
return False

for tuple in extract_rename:
if '*' in tuple:
print('--extract-rename is not allowed to contain wildcards')
return False

if tuple.count(':') != 1:
print('--extract-rename must contain a single ":". Format: <source>:<dest>')
return False

if len(tuple.split(':')[0]) == 0:
print('--extract-rename source file must not be empty')
return False

if len(tuple.split(':')[1]) == 0:
print('--extract-rename destination file must not be empty')
return False

return True


def check_locale(loc):
try:
aloc_tmp = subprocess.check_output(['locale', '-a'])
Expand Down Expand Up @@ -107,6 +135,12 @@ def parse_args(self, options):
parser.add_argument('--extract', action='append',
help='Extract a file directly. Useful for build'
'descriptions')
parser.add_argument('--extract-rename', action='append',
help='Extract a file directly and rename it. Useful'
'for build descriptions with multibuild.'
'Format: <source>:<dest>'
'Does not support wildcards, must not contain'
'colons in the filenames.')
parser.add_argument('--filename',
help='Name of package - used together with version'
' to determine tarball name')
Expand Down Expand Up @@ -221,6 +255,9 @@ def verify_args(self, args):
if contains_dotdot(args.extract):
sys.exit('--extract is not allowed to contain ".."')

if not validate_extract_rename(args.extract_rename):
sys.exit('--extract-rename is not valid. Format: <source>:<dest>')

if args.filename and "/" in args.filename:
sys.exit('--filename must not specify a path')

Expand Down
3 changes: 3 additions & 0 deletions TarSCM/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ def process_single_task(self, args):
arch.extract_from_archive(extract_src, args.extract,
args.outdir)

arch.extract_rename_from_archive(extract_src, args.extract_rename,
args.outdir)

arch.create_archive(
scm_object,
basename = basename,
Expand Down
5 changes: 5 additions & 0 deletions tar_scm.service.in
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@
<parameter name="extract">
<description>Specify a file/glob to be exported directly. Useful for build descriptions like spec files
which get maintained in the SCM. Can be used multiple times.</description>
</parameter>
<parameter name="extract-rename">
<description>Specify a file to be renamed and exported directly. Useful for build descriptions like spec files
which get maintained in the SCM and are used with multibuild. Does not support wildcards and must contain only one colon.
Can be used multiple times. Format: source:dest</description>
</parameter>
<parameter name="package-meta">
<description>Package the metadata of SCM to allow the user or OBS to update after un-tar.</description>
Expand Down

0 comments on commit 3d23aac

Please sign in to comment.