Skip to content

Commit 6d38fa2

Browse files
committed
Do not use sys.exit in library
Raise an exception in case of error instead. sys.exit will exit Python, which is not desireable when running as a library. Return RC 1 in these cases when running as a script. Move the chdir into a finally block, to ensure it is always run. Reraise any exceptions caught to preserve traceback.
1 parent 8f7dfa5 commit 6d38fa2

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

bagit.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ def make_bag(bag_dir, bag_info=None, processes=1, checksum=None):
9797
unbaggable = _can_bag(os.curdir)
9898
if unbaggable:
9999
logger.error("no write permissions for the following directories and files: \n%s", unbaggable)
100-
sys.exit("\nNot all files/folders can be moved.")
100+
raise BagError("Not 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:
104104
logger.error("The following directories do not have read permissions: \n%s", unreadable_dirs)
105105
if unreadable_files:
106106
logger.error("The following files do not have read permissions: \n%s", unreadable_files)
107-
sys.exit("\nRead permissions are required to calculate file fixities.")
107+
raise BagError("Read permissions are required to calculate file fixities.")
108108
else:
109109
logger.info("creating data dir")
110110

@@ -148,14 +148,12 @@ def make_bag(bag_dir, bag_info=None, processes=1, checksum=None):
148148

149149
for c in checksum:
150150
_make_tagmanifest_file(c, bag_dir)
151-
152-
153-
except Exception as e:
151+
except Exception:
152+
logger.exception("An error occurred creating the bag")
153+
raise
154+
finally:
154155
os.chdir(old_dir)
155-
logger.exception(e)
156-
raise e
157156

158-
os.chdir(old_dir)
159157
return Bag(bag_dir)
160158

161159

@@ -347,9 +345,9 @@ def fetch_entries(self):
347345
for line in fetch_file:
348346
parts = line.strip().split(None, 2)
349347
yield (parts[0], parts[1], parts[2])
350-
except Exception as e:
348+
except Exception:
351349
fetch_file.close()
352-
raise e
350+
raise
353351

354352
fetch_file.close()
355353

@@ -573,7 +571,7 @@ def __str__(self):
573571
return "%s: %s" % (self.message, details)
574572
return self.message
575573

576-
class ManifestErrorDetail():
574+
class ManifestErrorDetail(BagError):
577575
def __init__(self, path):
578576
self.path = path
579577

@@ -948,8 +946,12 @@ def _configure_logging(opts):
948946

949947
# make the bag
950948
else:
951-
make_bag(bag_dir, bag_info=opt_parser.bag_info,
952-
processes=opts.processes,
953-
checksum=opts.checksum)
949+
try:
950+
make_bag(bag_dir, bag_info=opt_parser.bag_info,
951+
processes=opts.processes,
952+
checksum=opts.checksum)
953+
except Exception:
954+
logger.info("%s failed to create: %s", bag_dir, e)
955+
rc = 1
954956

955957
sys.exit(rc)

0 commit comments

Comments
 (0)