Skip to content

Commit 8deed2f

Browse files
joschJPEWdev
authored andcommitted
Respect query part of the url when operating on the path
This allows one to use bmaptool, with artifacts from gitlab CI which have a ?job=... query parameter at the end of the URL: - ignore the query part of the URL when determining the compression type - ignore the query part of the URL when computing the basename and stripping off the filename extension - first strip off and then re-apply the query part of the URL when adding the .bmap extension to the path
1 parent 97cddaa commit 8deed2f

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

src/bmaptool/CLI.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import pathlib
4242
import subprocess
4343
import re
44+
import urllib.parse
4445
from typing import NamedTuple
4546
from . import BmapCreate, BmapCopy, BmapHelpers, TransRead
4647

@@ -259,7 +260,7 @@ def verify_bmap_signature_gpg(bmap_obj, detached_sig):
259260
)
260261

261262

262-
def verify_bmap_signature(args, bmap_obj, bmap_path):
263+
def verify_bmap_signature(args, bmap_obj, bmap_path, is_url):
263264
"""
264265
Verify GPG signature of the bmap file if it is present. The signature may
265266
be in a separate file (detached) or it may be inside the bmap file itself
@@ -301,11 +302,19 @@ def verify_bmap_signature(args, bmap_obj, bmap_path):
301302
error_out("cannot open bmap signature file '%s':\n%s", args.bmap_sig, err)
302303
else:
303304
# Check if there is a stand-alone signature file
305+
def _add_ext(p, ext):
306+
if not is_url:
307+
return p + ext
308+
# if the image is a url, add the extension to the 'path' part
309+
# before the query string and url fragment
310+
o = urllib.parse.urlparse(p)
311+
return o._replace(path=o.path + ext).geturl()
312+
304313
try:
305-
detached_sig = TransRead.TransRead(bmap_path + ".asc")
314+
detached_sig = TransRead.TransRead(_add_ext(bmap_path, ".asc"))
306315
except TransRead.Error:
307316
try:
308-
detached_sig = TransRead.TransRead(bmap_path + ".sig")
317+
detached_sig = TransRead.TransRead(_add_ext(bmap_path, ".sig"))
309318
except TransRead.Error:
310319
# No detached signatures found
311320
return None
@@ -378,7 +387,7 @@ def verify_bmap_signature(args, bmap_obj, bmap_path):
378387
return tmp_obj
379388

380389

381-
def find_and_open_bmap(args):
390+
def find_and_open_bmap(args, is_url):
382391
"""
383392
This is a helper function for 'open_files()' which discovers and opens the
384393
bmap file, then returns the corresponding file object and the bmap file
@@ -410,15 +419,27 @@ def find_and_open_bmap(args):
410419
# Automatically discover the bmap file
411420
image_path = args.image
412421
while True:
413-
bmap_path = image_path + ".bmap"
422+
if is_url:
423+
# if the image is a url, add the extention to the 'path' part
424+
# before the query string and url fragment
425+
o = urllib.parse.urlparse(image_path)
426+
bmap_path = o._replace(path=o.path + ".bmap").geturl()
427+
else:
428+
bmap_path = image_path + ".bmap"
414429
try:
415430
bmap_obj = TransRead.TransRead(bmap_path)
416431
log.info("discovered bmap file '%s'" % bmap_path)
417432
break
418433
except TransRead.Error:
419434
pass
420435

421-
image_path, ext = os.path.splitext(image_path)
436+
if is_url:
437+
# if the image is a url, split the extension from the 'path'
438+
o = urllib.parse.urlparse(image_path)
439+
p, ext = os.path.splitext(o.path)
440+
image_path = o._replace(path=p).geturl()
441+
else:
442+
image_path, ext = os.path.splitext(image_path)
422443
if ext == "":
423444
return (None, None)
424445

@@ -460,7 +481,7 @@ def open_files(args):
460481

461482
# Open the bmap file. Try to discover the bmap file automatically if it
462483
# was not specified.
463-
(bmap_obj, bmap_path) = find_and_open_bmap(args)
484+
(bmap_obj, bmap_path) = find_and_open_bmap(args, image_obj.is_url)
464485

465486
if bmap_path == args.image:
466487
# Most probably the user specified the bmap file instead of the image
@@ -563,7 +584,7 @@ def copy_command(args):
563584
"the bmap signature file was specified, but bmap file was " "not found"
564585
)
565586

566-
f_obj = verify_bmap_signature(args, bmap_obj, bmap_path)
587+
f_obj = verify_bmap_signature(args, bmap_obj, bmap_path, image_obj.is_url)
567588
if f_obj:
568589
bmap_obj.close()
569590
bmap_obj = f_obj
@@ -613,9 +634,17 @@ def copy_command(args):
613634
writer.mapped_percent,
614635
)
615636
)
637+
638+
def _get_basename(p):
639+
if image_obj.is_url:
640+
# if this is a url, strip off potential query string and
641+
# fragment from the end
642+
p = urllib.parse.urlparse(p).path
643+
return os.path.basename(p)
644+
616645
log.info(
617646
"copying image '%s' to %s using bmap file '%s'"
618-
% (os.path.basename(args.image), dest_str, os.path.basename(bmap_path))
647+
% (_get_basename(args.image), dest_str, _get_basename(bmap_path))
619648
)
620649

621650
if args.psplash_pipe:

src/bmaptool/TransRead.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,10 @@ def _print_warning(timeout):
583583

584584
parsed_url = urllib.parse.urlparse(url)
585585

586+
# figuring out the decompression program to use relies on the
587+
# extension, so strip off any potential query parts
588+
self.name = parsed_url.path
589+
586590
if parsed_url.scheme == "ssh":
587591
# Unfortunately, urllib2 does not handle "ssh://" URLs
588592
self._open_url_ssh(parsed_url)

0 commit comments

Comments
 (0)