|
41 | 41 | import pathlib |
42 | 42 | import subprocess |
43 | 43 | import re |
| 44 | +import urllib.parse |
44 | 45 | from typing import NamedTuple |
45 | 46 | from . import BmapCreate, BmapCopy, BmapHelpers, TransRead |
46 | 47 |
|
@@ -259,7 +260,7 @@ def verify_bmap_signature_gpg(bmap_obj, detached_sig): |
259 | 260 | ) |
260 | 261 |
|
261 | 262 |
|
262 | | -def verify_bmap_signature(args, bmap_obj, bmap_path): |
| 263 | +def verify_bmap_signature(args, bmap_obj, bmap_path, is_url): |
263 | 264 | """ |
264 | 265 | Verify GPG signature of the bmap file if it is present. The signature may |
265 | 266 | 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): |
301 | 302 | error_out("cannot open bmap signature file '%s':\n%s", args.bmap_sig, err) |
302 | 303 | else: |
303 | 304 | # 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 | + |
304 | 313 | try: |
305 | | - detached_sig = TransRead.TransRead(bmap_path + ".asc") |
| 314 | + detached_sig = TransRead.TransRead(_add_ext(bmap_path, ".asc")) |
306 | 315 | except TransRead.Error: |
307 | 316 | try: |
308 | | - detached_sig = TransRead.TransRead(bmap_path + ".sig") |
| 317 | + detached_sig = TransRead.TransRead(_add_ext(bmap_path, ".sig")) |
309 | 318 | except TransRead.Error: |
310 | 319 | # No detached signatures found |
311 | 320 | return None |
@@ -378,7 +387,7 @@ def verify_bmap_signature(args, bmap_obj, bmap_path): |
378 | 387 | return tmp_obj |
379 | 388 |
|
380 | 389 |
|
381 | | -def find_and_open_bmap(args): |
| 390 | +def find_and_open_bmap(args, is_url): |
382 | 391 | """ |
383 | 392 | This is a helper function for 'open_files()' which discovers and opens the |
384 | 393 | bmap file, then returns the corresponding file object and the bmap file |
@@ -410,15 +419,27 @@ def find_and_open_bmap(args): |
410 | 419 | # Automatically discover the bmap file |
411 | 420 | image_path = args.image |
412 | 421 | 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" |
414 | 429 | try: |
415 | 430 | bmap_obj = TransRead.TransRead(bmap_path) |
416 | 431 | log.info("discovered bmap file '%s'" % bmap_path) |
417 | 432 | break |
418 | 433 | except TransRead.Error: |
419 | 434 | pass |
420 | 435 |
|
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) |
422 | 443 | if ext == "": |
423 | 444 | return (None, None) |
424 | 445 |
|
@@ -460,7 +481,7 @@ def open_files(args): |
460 | 481 |
|
461 | 482 | # Open the bmap file. Try to discover the bmap file automatically if it |
462 | 483 | # 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) |
464 | 485 |
|
465 | 486 | if bmap_path == args.image: |
466 | 487 | # Most probably the user specified the bmap file instead of the image |
@@ -563,7 +584,7 @@ def copy_command(args): |
563 | 584 | "the bmap signature file was specified, but bmap file was " "not found" |
564 | 585 | ) |
565 | 586 |
|
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) |
567 | 588 | if f_obj: |
568 | 589 | bmap_obj.close() |
569 | 590 | bmap_obj = f_obj |
@@ -613,9 +634,17 @@ def copy_command(args): |
613 | 634 | writer.mapped_percent, |
614 | 635 | ) |
615 | 636 | ) |
| 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 | + |
616 | 645 | log.info( |
617 | 646 | "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)) |
619 | 648 | ) |
620 | 649 |
|
621 | 650 | if args.psplash_pipe: |
|
0 commit comments