Browse code

Simplify the code to use _build_attr_header in remote_copy

Florent Viard authored on 2020/04/19 10:55:20
Showing 2 changed files
... ...
@@ -542,7 +542,7 @@ def compare_filelists(src_list, dst_list, src_remote, dst_remote):
542 542
             try:
543 543
                 src_md5 = src_list.get_md5(file)
544 544
                 dst_md5 = dst_list.get_md5(file)
545
-            except (IOError,OSError):
545
+            except (IOError, OSError):
546 546
                 # md5 sum verification failed - ignore that file altogether
547 547
                 debug(u"IGNR: %s (disappeared)" % (file))
548 548
                 warning(u"%s: file disappeared, ignoring." % (file))
... ...
@@ -604,7 +604,7 @@ def compare_filelists(src_list, dst_list, src_remote, dst_remote):
604 604
                     # Found one, we want to copy
605 605
                     dst1 = dst_list.find_md5_one(md5)
606 606
                     debug(u"DST COPY src: %s -> %s" % (dst1, relative_file))
607
-                    copy_pairs.append((src_list[relative_file], dst1, relative_file))
607
+                    copy_pairs.append((src_list[relative_file], dst1, relative_file, md5))
608 608
                     del(src_list[relative_file])
609 609
                     del(dst_list[relative_file])
610 610
                 else:
... ...
@@ -626,7 +626,8 @@ def compare_filelists(src_list, dst_list, src_remote, dst_remote):
626 626
             if dst1 is not None:
627 627
                 # Found one, we want to copy
628 628
                 debug(u"DST COPY dst: %s -> %s" % (dst1, relative_file))
629
-                copy_pairs.append((src_list[relative_file], dst1, relative_file))
629
+                copy_pairs.append((src_list[relative_file], dst1,
630
+                                   relative_file, md5))
630 631
                 del(src_list[relative_file])
631 632
             else:
632 633
                 # we don't have this file, and we don't have a copy of this file elsewhere.  Get it.
... ...
@@ -417,11 +417,14 @@ def cmd_object_put(args):
417 417
 
418 418
     seq = 0
419 419
     ret = EX_OK
420
-    local_list_get_md5_func = local_list.get_md5
421 420
     for key in local_list:
422 421
         seq += 1
423 422
 
424 423
         uri_final = S3Uri(local_list[key]['remote_uri'])
424
+        try:
425
+            src_md5 = local_list.get_md5(key)
426
+        except IOError:
427
+            src_md5 = None
425 428
 
426 429
         extra_headers = copy(cfg.extra_headers)
427 430
         full_name_orig = local_list[key]['full_name']
... ...
@@ -429,9 +432,7 @@ def cmd_object_put(args):
429 429
         seq_label = "[%d of %d]" % (seq, local_count)
430 430
         if Config().encrypt:
431 431
             gpg_exitcode, full_name, extra_headers["x-amz-meta-s3tools-gpgenc"] = gpg_encrypt(full_name_orig)
432
-        attr_header = _build_attr_header(local_list[key],
433
-                                         key,
434
-                                         local_list_get_md5_func)
432
+        attr_header = _build_attr_header(local_list[key], key, src_md5)
435 433
         debug(u"attr_header: %s" % attr_header)
436 434
         extra_headers.update(attr_header)
437 435
         try:
... ...
@@ -1162,9 +1163,8 @@ def cmd_sync_remote2remote(args):
1162 1162
     total_files_copied += nb_files
1163 1163
     total_size_copied += size
1164 1164
 
1165
-    src_list_get_md5_func = src_list.get_md5
1166 1165
     n_copied, bytes_saved, failed_copy_files = remote_copy(
1167
-        s3, copy_pairs, destination_base, None, src_list_get_md5_func)
1166
+        s3, copy_pairs, destination_base, None, False)
1168 1167
     total_files_copied += n_copied
1169 1168
     total_size_copied += bytes_saved
1170 1169
 
... ...
@@ -1556,7 +1556,7 @@ def local_copy(copy_pairs, destination_base):
1556 1556
     # For instance all empty files would become hardlinked together!
1557 1557
     saved_bytes = 0
1558 1558
     failed_copy_list = FileDict()
1559
-    for (src_obj, dst1, relative_file) in copy_pairs:
1559
+    for (src_obj, dst1, relative_file, md5) in copy_pairs:
1560 1560
         src_file = os.path.join(destination_base, dst1)
1561 1561
         dst_file = os.path.join(destination_base, relative_file)
1562 1562
         dst_dir = os.path.dirname(deunicodise(dst_file))
... ...
@@ -1573,13 +1573,13 @@ def local_copy(copy_pairs, destination_base):
1573 1573
     return len(copy_pairs), saved_bytes, failed_copy_list
1574 1574
 
1575 1575
 def remote_copy(s3, copy_pairs, destination_base, uploaded_objects_list=None,
1576
-                metadata_update=False, local_list_get_md5_func=None):
1576
+                metadata_update=False):
1577 1577
     cfg = Config()
1578 1578
     saved_bytes = 0
1579 1579
     failed_copy_list = FileDict()
1580 1580
     seq = 0
1581 1581
     src_count = len(copy_pairs)
1582
-    for (src_obj, dst1, dst2) in copy_pairs:
1582
+    for (src_obj, dst1, dst2, src_md5) in copy_pairs:
1583 1583
         seq += 1
1584 1584
         debug(u"Remote Copying from %s to %s" % (dst1, dst2))
1585 1585
         dst1_uri = S3Uri(destination_base + dst1)
... ...
@@ -1589,9 +1589,11 @@ def remote_copy(s3, copy_pairs, destination_base, uploaded_objects_list=None,
1589 1589
         extra_headers = copy(cfg.extra_headers)
1590 1590
         if metadata_update:
1591 1591
             # source is a real local file with its own personal metadata
1592
-            attr_header = _build_attr_header(src_obj, dst2, local_list_get_md5_func)
1592
+            attr_header = _build_attr_header(src_obj, dst2, src_md5)
1593 1593
             debug(u"attr_header: %s" % attr_header)
1594 1594
             extra_headers.update(attr_header)
1595
+            extra_headers['content-type'] = \
1596
+                s3.content_type(filename=src_obj['full_name'])
1595 1597
         try:
1596 1598
             s3.object_copy(dst1_uri, dst2_uri, extra_headers,
1597 1599
                            src_size=src_obj_size,
... ...
@@ -1605,8 +1607,7 @@ def remote_copy(s3, copy_pairs, destination_base, uploaded_objects_list=None,
1605 1605
             failed_copy_list[dst2] = src_obj
1606 1606
     return (len(copy_pairs), saved_bytes, failed_copy_list)
1607 1607
 
1608
-def _build_attr_header(src_obj, src_relative_name,
1609
-                       local_list_get_md5_func=None):
1608
+def _build_attr_header(src_obj, src_relative_name, md5=None):
1610 1609
     cfg = Config()
1611 1610
     attrs = {}
1612 1611
     if cfg.preserve_attrs:
... ...
@@ -1636,13 +1637,8 @@ def _build_attr_header(src_obj, src_relative_name,
1636 1636
             if val is not None:
1637 1637
                 attrs[attr] = val
1638 1638
 
1639
-    if 'md5' in cfg.preserve_attrs_list and local_list_get_md5_func:
1640
-        try:
1641
-            val = local_list_get_md5_func(src_relative_name)
1642
-            if val is not None:
1643
-                attrs['md5'] = val
1644
-        except IOError:
1645
-            pass
1639
+    if 'md5' in cfg.preserve_attrs_list and md5:
1640
+        attrs['md5'] = md5
1646 1641
 
1647 1642
     if attrs:
1648 1643
         attr_str_list = []
... ...
@@ -1709,19 +1705,21 @@ def cmd_sync_local2remote(args):
1709 1709
         def _upload(local_list, seq, total, total_size):
1710 1710
             file_list = local_list.keys()
1711 1711
             file_list.sort()
1712
-            local_list_get_md5_func = local_list.get_md5
1713 1712
             ret = EX_OK
1714 1713
             for file in file_list:
1715 1714
                 seq += 1
1716 1715
                 item = local_list[file]
1717 1716
                 src = item['full_name']
1717
+                try:
1718
+                    src_md5 = local_list.get_md5(file)
1719
+                except IOError:
1720
+                    src_md5 = None
1718 1721
                 uri = S3Uri(item['remote_uri'])
1719 1722
                 seq_label = "[%d of %d]" % (seq, total)
1720 1723
                 extra_headers = copy(cfg.extra_headers)
1721 1724
                 try:
1722 1725
                     attr_header = _build_attr_header(local_list[file],
1723
-                                                     file,
1724
-                                                     local_list_get_md5_func)
1726
+                                                     file, src_md5)
1725 1727
                     debug(u"attr_header: %s" % attr_header)
1726 1728
                     extra_headers.update(attr_header)
1727 1729
                     response = s3.object_put(src, uri, extra_headers, extra_label = seq_label)
... ...
@@ -1808,7 +1806,7 @@ def cmd_sync_local2remote(args):
1808 1808
                 output(u"upload: '%s' -> '%s'" % (local_list[key]['full_name'], local_list[key]['remote_uri']))
1809 1809
             for key in update_list:
1810 1810
                 output(u"upload: '%s' -> '%s'" % (update_list[key]['full_name'], update_list[key]['remote_uri']))
1811
-            for (src_obj, dst1, dst2) in copy_pairs:
1811
+            for (src_obj, dst1, dst2, md5) in copy_pairs:
1812 1812
                 output(u"remote copy: '%s' -> '%s'" % (dst1, dst2))
1813 1813
             if cfg.delete_removed:
1814 1814
                 for key in remote_list:
... ...
@@ -1839,10 +1837,8 @@ def cmd_sync_local2remote(args):
1839 1839
         # uploaded_objects_list reference is passed so it can be filled with
1840 1840
         # destination object of succcessful copies so that they can be
1841 1841
         # invalidated by cf
1842
-        local_list_get_md5_func = local_list.get_md5
1843 1842
         n_copies, saved_bytes, failed_copy_files  = remote_copy(
1844
-            s3, copy_pairs, destination_base, uploaded_objects_list,
1845
-            local_list_get_md5_func)
1843
+            s3, copy_pairs, destination_base, uploaded_objects_list, True)
1846 1844
 
1847 1845
         #upload file that could not be copied
1848 1846
         debug("Process files that were not remotely copied")