Skip to content

Commit 8f7dfa5

Browse files
committed
Use bagit logger not root logger
1 parent ac70f0f commit 8f7dfa5

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

bagit.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ def make_bag(bag_dir, bag_info=None, processes=1, checksum=None):
8181
the bag_info dictionary.
8282
"""
8383
bag_dir = os.path.abspath(bag_dir)
84-
logging.info("creating bag for directory %s" % bag_dir)
84+
logger.info("creating bag for directory %s" % bag_dir)
8585
# assume md5 checksum if not specified
8686
if not checksum:
8787
checksum = ['md5']
8888

8989
if not os.path.isdir(bag_dir):
90-
logging.error("no such bag directory %s" % bag_dir)
90+
logger.error("no such bag directory %s" % bag_dir)
9191
raise RuntimeError("no such bag directory %s" % bag_dir)
9292

9393
old_dir = os.path.abspath(os.path.curdir)
@@ -96,17 +96,17 @@ def make_bag(bag_dir, bag_info=None, processes=1, checksum=None):
9696
try:
9797
unbaggable = _can_bag(os.curdir)
9898
if unbaggable:
99-
logging.error("no write permissions for the following directories and files: \n%s", unbaggable)
99+
logger.error("no write permissions for the following directories and files: \n%s", unbaggable)
100100
sys.exit("\nNot all files/folders can be moved.")
101101
unreadable_dirs, unreadable_files = _can_read(os.curdir)
102102
if unreadable_dirs or unreadable_files:
103103
if unreadable_dirs:
104-
logging.error("The following directories do not have read permissions: \n%s", unreadable_dirs)
104+
logger.error("The following directories do not have read permissions: \n%s", unreadable_dirs)
105105
if unreadable_files:
106-
logging.error("The following files do not have read permissions: \n%s", unreadable_files)
106+
logger.error("The following files do not have read permissions: \n%s", unreadable_files)
107107
sys.exit("\nRead permissions are required to calculate file fixities.")
108108
else:
109-
logging.info("creating data dir")
109+
logger.info("creating data dir")
110110

111111
cwd = os.getcwd()
112112
temp_data = tempfile.mkdtemp(dir=cwd)
@@ -115,26 +115,26 @@ def make_bag(bag_dir, bag_info=None, processes=1, checksum=None):
115115
if os.path.abspath(f) == temp_data:
116116
continue
117117
new_f = os.path.join(temp_data, f)
118-
logging.info("moving %s to %s" % (f, new_f))
118+
logger.info("moving %s to %s" % (f, new_f))
119119
os.rename(f, new_f)
120120

121-
logging.info("moving %s to %s" % (temp_data, 'data'))
121+
logger.info("moving %s to %s" % (temp_data, 'data'))
122122
os.rename(temp_data, 'data')
123123

124124
# permissions for the payload directory should match those of the
125125
# original directory
126126
os.chmod('data', os.stat(cwd).st_mode)
127127

128128
for c in checksum:
129-
logging.info("writing manifest-%s.txt" % c)
129+
logger.info("writing manifest-%s.txt" % c)
130130
Oxum = _make_manifest('manifest-%s.txt' % c, 'data', processes, c)
131131

132-
logging.info("writing bagit.txt")
132+
logger.info("writing bagit.txt")
133133
txt = """BagIt-Version: 0.97\nTag-File-Character-Encoding: UTF-8\n"""
134134
with open("bagit.txt", "w") as bagit_file:
135135
bagit_file.write(txt)
136136

137-
logging.info("writing bag-info.txt")
137+
logger.info("writing bag-info.txt")
138138
if bag_info is None:
139139
bag_info = {}
140140

@@ -152,7 +152,7 @@ def make_bag(bag_dir, bag_info=None, processes=1, checksum=None):
152152

153153
except Exception as e:
154154
os.chdir(old_dir)
155-
logging.exception(e)
155+
logger.exception(e)
156156
raise e
157157

158158
os.chdir(old_dir)
@@ -289,24 +289,24 @@ def save(self, processes=1, manifests=False):
289289
if manifests:
290290
unbaggable = _can_bag(self.path)
291291
if unbaggable:
292-
logging.error("no write permissions for the following directories and files: \n%s", unbaggable)
292+
logger.error("no write permissions for the following directories and files: \n%s", unbaggable)
293293
raise BagError("Not all files/folders can be moved.")
294294
unreadable_dirs, unreadable_files = _can_read(self.path)
295295
if unreadable_dirs or unreadable_files:
296296
if unreadable_dirs:
297-
logging.error("The following directories do not have read permissions: \n%s", unreadable_dirs)
297+
logger.error("The following directories do not have read permissions: \n%s", unreadable_dirs)
298298
if unreadable_files:
299-
logging.error("The following files do not have read permissions: \n%s", unreadable_files)
299+
logger.error("The following files do not have read permissions: \n%s", unreadable_files)
300300
raise BagError("Read permissions are required to calculate file fixities.")
301301

302302
oxum = None
303303
self.algs = list(set(self.algs)) # Dedupe
304304
for alg in self.algs:
305-
logging.info('updating manifest-%s.txt', alg)
305+
logger.info('updating manifest-%s.txt', alg)
306306
oxum = _make_manifest('manifest-%s.txt' % alg, 'data', processes, alg)
307307

308308
# Update Payload-Oxum
309-
logging.info('updating %s', self.tag_file_name)
309+
logger.info('updating %s', self.tag_file_name)
310310
if oxum:
311311
self.info['Payload-Oxum'] = oxum
312312

@@ -409,7 +409,7 @@ def _load_manifests(self):
409409

410410
# Format is FILENAME *CHECKSUM
411411
if len(entry) != 2:
412-
logging.error("%s: Invalid %s manifest entry: %s", self, alg, line)
412+
logger.error("%s: Invalid %s manifest entry: %s", self, alg, line)
413413
continue
414414

415415
entry_hash = entry[0]
@@ -489,11 +489,11 @@ def _validate_entries(self, processes):
489489
only_in_manifests, only_on_fs = self.compare_manifests_with_fs()
490490
for path in only_in_manifests:
491491
e = FileMissing(path)
492-
logging.warning(str(e))
492+
logger.warning(str(e))
493493
errors.append(e)
494494
for path in only_on_fs:
495495
e = UnexpectedFile(path)
496-
logging.warning(str(e))
496+
logger.warning(str(e))
497497
errors.append(e)
498498

499499
# To avoid the overhead of reading the file more than once or loading
@@ -507,7 +507,7 @@ def _validate_entries(self, processes):
507507
hashlib.new(alg)
508508
available_hashers.add(alg)
509509
except ValueError:
510-
logging.warning("Unable to validate file contents using unknown %s hash algorithm", alg)
510+
logger.warning("Unable to validate file contents using unknown %s hash algorithm", alg)
511511

512512
if not available_hashers:
513513
raise RuntimeError("%s: Unable to validate bag contents: none of the hash algorithms in %s are supported!" % (self, self.algs))
@@ -532,15 +532,15 @@ def _init_worker():
532532
pass
533533
# Any unhandled exceptions are probably fatal
534534
except:
535-
logging.exception("unable to calculate file hashes for %s", self)
535+
logger.exception("unable to calculate file hashes for %s", self)
536536
raise
537537

538538
for rel_path, f_hashes, hashes in hash_results:
539539
for alg, computed_hash in list(f_hashes.items()):
540540
stored_hash = hashes[alg]
541541
if stored_hash.lower() != computed_hash:
542542
e = ChecksumMismatch(rel_path, alg, stored_hash.lower(), computed_hash)
543-
logging.warning(str(e))
543+
logger.warning(str(e))
544544
errors.append(e)
545545

546546
if errors:
@@ -725,7 +725,7 @@ def _make_tag_file(bag_info_path, bag_info):
725725

726726

727727
def _make_manifest(manifest_file, data_dir, processes, algorithm='md5'):
728-
logging.info('writing manifest with %s processes' % processes)
728+
logger.info('writing manifest with %s processes' % processes)
729729

730730
if algorithm == 'md5':
731731
manifest_line = _manifest_line_md5
@@ -764,7 +764,7 @@ def _make_manifest(manifest_file, data_dir, processes, algorithm='md5'):
764764

765765
def _make_tagmanifest_file(alg, bag_dir):
766766
tagmanifest_file = join(bag_dir, "tagmanifest-%s.txt" % alg)
767-
logging.info("writing %s", tagmanifest_file)
767+
logger.info("writing %s", tagmanifest_file)
768768
files = [f for f in listdir(bag_dir) if isfile(join(bag_dir, f))]
769769
checksums = []
770770
for f in files:
@@ -928,7 +928,6 @@ def _configure_logging(opts):
928928
opt_parser.error("number of processes needs to be 0 or more")
929929

930930
_configure_logging(opts)
931-
log = logging.getLogger()
932931

933932
rc = 0
934933
for bag_dir in args:
@@ -940,11 +939,11 @@ def _configure_logging(opts):
940939
# validate throws a BagError or BagValidationError
941940
valid = bag.validate(processes=opts.processes, fast=opts.fast)
942941
if opts.fast:
943-
log.info("%s valid according to Payload-Oxum", bag_dir)
942+
logger.info("%s valid according to Payload-Oxum", bag_dir)
944943
else:
945-
log.info("%s is valid", bag_dir)
944+
logger.info("%s is valid", bag_dir)
946945
except BagError as e:
947-
log.info("%s is invalid: %s", bag_dir, e)
946+
logger.info("%s is invalid: %s", bag_dir, e)
948947
rc = 1
949948

950949
# make the bag

0 commit comments

Comments
 (0)