Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new --extract-rename option #509

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 38 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 ":"')
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,13 @@ 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 +256,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
Loading