Browse Source

convert upgrade code to logger as well

Antoine Beaupré 9 years ago
parent
commit
e414203ce2
2 changed files with 19 additions and 14 deletions
  1. 4 2
      borg/testsuite/upgrader.py
  2. 15 12
      borg/upgrader.py

+ 4 - 2
borg/testsuite/upgrader.py

@@ -1,3 +1,5 @@
+import logging
+logger = logging.getLogger(__name__)
 import os
 import shutil
 import tempfile
@@ -77,7 +79,7 @@ def test_convert_segments(tmpdir, attic_repo):
     """
     # check should fail because of magic number
     assert not repo_valid(tmpdir)
-    print("opening attic repository with borg and converting")
+    logger.info("opening attic repository with borg and converting")
     repo = AtticRepositoryUpgrader(str(tmpdir), create=False)
     segments = [filename for i, filename in repo.io.segment_iterator()]
     repo.close()
@@ -156,7 +158,7 @@ def test_convert_all(tmpdir, attic_repo, attic_key_file):
     """
     # check should fail because of magic number
     assert not repo_valid(tmpdir)
-    print("opening attic repository with borg and converting")
+    logger.info("opening attic repository with borg and converting")
     repo = AtticRepositoryUpgrader(str(tmpdir), create=False)
     repo.upgrade(dryrun=False)
     assert key_valid(attic_key_file.path)

+ 15 - 12
borg/upgrader.py

@@ -1,4 +1,6 @@
 from binascii import hexlify
+import logging
+logger = logging.getLogger(__name__)
 import os
 import shutil
 import time
@@ -23,14 +25,14 @@ class AtticRepositoryUpgrader(Repository):
         we nevertheless do the order in reverse, as we prefer to do
         the fast stuff first, to improve interactivity.
         """
-        print("reading segments from attic repository using borg")
+        logger.info("reading segments from attic repository using borg")
         # we need to open it to load the configuration and other fields
         self.open(self.path, exclusive=False)
         segments = [filename for i, filename in self.io.segment_iterator()]
         try:
             keyfile = self.find_attic_keyfile()
         except KeyfileNotFoundError:
-            print("no key file found for repository")
+            logger.warning("no key file found for repository")
         else:
             self.convert_keyfiles(keyfile, dryrun)
         self.close()
@@ -53,17 +55,18 @@ class AtticRepositoryUpgrader(Repository):
 
         luckily the magic string length didn't change so we can just
         replace the 8 first bytes of all regular files in there."""
-        print("converting %d segments..." % len(segments))
+        logger.info("converting %d segments..." % len(segments))
         i = 0
         for filename in segments:
             i += 1
             print("\rconverting segment %d/%d in place, %.2f%% done (%s)"
-                  % (i, len(segments), 100*float(i)/len(segments), filename), end='')
+                  % (i, len(segments), 100*float(i)/len(segments), filename),
+                  end='', file=sys.stderr)
             if dryrun:
                 time.sleep(0.001)
             else:
                 AtticRepositoryUpgrader.header_replace(filename, ATTIC_MAGIC, MAGIC)
-        print()
+        print(file=sys.stderr)
 
     @staticmethod
     def header_replace(filename, old_magic, new_magic):
@@ -107,12 +110,12 @@ class AtticRepositoryUpgrader(Repository):
         key file because magic string length changed, but that's not a
         problem because the keyfiles are small (compared to, say,
         all the segments)."""
-        print("converting keyfile %s" % keyfile)
+        logger.info("converting keyfile %s" % keyfile)
         with open(keyfile, 'r') as f:
             data = f.read()
         data = data.replace(AtticKeyfileKey.FILE_ID, KeyfileKey.FILE_ID, 1)
         keyfile = os.path.join(get_keys_dir(), os.path.basename(keyfile))
-        print("writing borg keyfile to %s" % keyfile)
+        logger.info("writing borg keyfile to %s" % keyfile)
         if not dryrun:
             with open(keyfile, 'w') as f:
                 f.write(data)
@@ -138,7 +141,7 @@ class AtticRepositoryUpgrader(Repository):
         caches = []
         transaction_id = self.get_index_transaction_id()
         if transaction_id is None:
-            print('no index file found for repository %s' % self.path)
+            logger.warning('no index file found for repository %s' % self.path)
         else:
             caches += [os.path.join(self.path, 'index.%d' % transaction_id).encode('utf-8')]
 
@@ -168,14 +171,14 @@ class AtticRepositoryUpgrader(Repository):
             if os.path.exists(attic_file):
                 borg_file = os.path.join(borg_cache_dir, path)
                 if os.path.exists(borg_file):
-                    print("borg cache file already exists in %s, skipping conversion of %s" % (borg_file, attic_file))
+                    logger.warning("borg cache file already exists in %s, skipping conversion of %s" % (borg_file, attic_file))
                 else:
-                    print("copying attic cache file from %s to %s" % (attic_file, borg_file))
+                    logger.info("copying attic cache file from %s to %s" % (attic_file, borg_file))
                     if not dryrun:
                         shutil.copyfile(attic_file, borg_file)
                     return borg_file
             else:
-                print("no %s cache file found in %s" % (path, attic_file))
+                logger.warning("no %s cache file found in %s" % (path, attic_file))
             return None
 
         # XXX: untested, because generating cache files is a PITA, see
@@ -192,7 +195,7 @@ class AtticRepositoryUpgrader(Repository):
             for cache in ['chunks']:
                 copied = copy_cache_file(cache)
                 if copied:
-                    print("converting cache %s" % cache)
+                    logger.info("converting cache %s" % cache)
                     if not dryrun:
                         AtticRepositoryUpgrader.header_replace(cache, b'ATTICIDX', b'BORG_IDX')