31
31
% bagit.py --help
32
32
"""
33
33
34
+ import argparse
34
35
import codecs
35
36
import hashlib
36
37
import logging
37
38
import multiprocessing
38
- import optparse
39
39
import os
40
40
import re
41
41
import signal
@@ -247,7 +247,7 @@ def compare_fetch_with_fs(self):
247
247
def payload_files (self ):
248
248
payload_dir = os .path .join (self .path , "data" )
249
249
250
- for dirpath , dirnames , filenames in os .walk (payload_dir ):
250
+ for dirpath , _ , filenames in os .walk (payload_dir ):
251
251
for f in filenames :
252
252
# Jump through some hoops here to make the payload files come out
253
253
# looking like data/dir/file, rather than having the entire path.
@@ -344,7 +344,7 @@ def fetch_entries(self):
344
344
yield (parts [0 ], parts [1 ], parts [2 ])
345
345
346
346
def files_to_be_fetched (self ):
347
- for f , size , path in self .fetch_entries ():
347
+ for f , _ , _ in self .fetch_entries ():
348
348
yield f
349
349
350
350
def has_oxum (self ):
@@ -521,7 +521,7 @@ def _init_worker():
521
521
finally :
522
522
try :
523
523
pool .terminate ()
524
- except :
524
+ except Exception :
525
525
# we really don't care about any exception in terminate()
526
526
pass
527
527
# Any unhandled exceptions are probably fatal
@@ -877,42 +877,45 @@ def _decode_filename(s):
877
877
878
878
# following code is used for command line program
879
879
880
- class BagOptionParser ( optparse . OptionParser ):
881
- def __init__ (self , * args , ** opts ):
880
+ class BagArgumentParser ( argparse . ArgumentParser ):
881
+ def __init__ (self , * args , ** kwargs ):
882
882
self .bag_info = {}
883
- optparse . OptionParser .__init__ (self , * args , ** opts )
883
+ argparse . ArgumentParser .__init__ (self , * args , ** kwargs )
884
884
885
885
886
- def _bag_info_store (option , opt , value , parser ):
887
- opt = opt .lstrip ('--' )
888
- opt_caps = '-' .join ([o .capitalize () for o in opt .split ('-' )])
889
- parser .bag_info [opt_caps ] = value
886
+ class BagHeaderAction (argparse .Action ):
887
+ def __call__ (self , parser , _ , values , option_string = None ):
888
+ opt = option_string .lstrip ('--' )
889
+ opt_caps = '-' .join ([o .capitalize () for o in opt .split ('-' )])
890
+ parser .bag_info [opt_caps ] = values
890
891
891
892
892
- def _make_opt_parser ():
893
- parser = BagOptionParser (usage = 'usage: %prog [options] dir1 dir2 ...' )
894
- parser .add_option ('--processes' , action = 'store' , type = "int" ,
895
- dest = 'processes' , default = 1 ,
893
+ def _make_parser ():
894
+ parser = BagArgumentParser ()
895
+ parser .add_argument ('--processes' , type = int , dest = 'processes' , default = 1 ,
896
896
help = 'parallelize checksums generation and verification' )
897
- parser .add_option ('--log' , action = 'store' , dest = ' log' , help = 'The name of the log file' )
898
- parser .add_option ('--quiet' , action = 'store_true' , dest = 'quiet ' )
899
- parser .add_option ('--validate' , action = 'store_true' , dest = 'validate ' )
900
- parser .add_option ('--fast' , action = 'store_true' , dest = 'fast ' )
897
+ parser .add_argument ('--log' , help = 'The name of the log file' )
898
+ parser .add_argument ('--quiet' , action = 'store_true' )
899
+ parser .add_argument ('--validate' , action = 'store_true' )
900
+ parser .add_argument ('--fast' , action = 'store_true' )
901
901
902
902
# optionally specify which checksum algorithm(s) to use when creating a bag
903
903
# NOTE: could generate from checksum_algos ?
904
- parser .add_option ('--md5' , action = 'append_const' , dest = 'checksum' , const = 'md5' ,
904
+ parser .add_argument ('--md5' , action = 'append_const' , dest = 'checksum' , const = 'md5' ,
905
905
help = 'Generate MD5 manifest when creating a bag (default)' )
906
- parser .add_option ('--sha1' , action = 'append_const' , dest = 'checksum' , const = 'sha1' ,
906
+ parser .add_argument ('--sha1' , action = 'append_const' , dest = 'checksum' , const = 'sha1' ,
907
907
help = 'Generate SHA1 manifest when creating a bag' )
908
- parser .add_option ('--sha256' , action = 'append_const' , dest = 'checksum' , const = 'sha256' ,
908
+ parser .add_argument ('--sha256' , action = 'append_const' , dest = 'checksum' , const = 'sha256' ,
909
909
help = 'Generate SHA-256 manifest when creating a bag' )
910
- parser .add_option ('--sha512' , action = 'append_const' , dest = 'checksum' , const = 'sha512' ,
910
+ parser .add_argument ('--sha512' , action = 'append_const' , dest = 'checksum' , const = 'sha512' ,
911
911
help = 'Generate SHA-512 manifest when creating a bag' )
912
912
913
913
for header in STANDARD_BAG_INFO_HEADERS :
914
- parser .add_option ('--%s' % header .lower (), type = "string" ,
915
- action = 'callback' , callback = _bag_info_store )
914
+ parser .add_argument ('--%s' % header .lower (), type = str ,
915
+ action = BagHeaderAction )
916
+
917
+ parser .add_argument ('directory' , nargs = '+' , help = 'directory to make a bag from' )
918
+
916
919
return parser
917
920
918
921
@@ -929,24 +932,24 @@ def _configure_logging(opts):
929
932
930
933
931
934
def main ():
932
- opt_parser = _make_opt_parser ()
933
- opts , args = opt_parser .parse_args ()
935
+ parser = _make_parser ()
936
+ args = parser .parse_args ()
934
937
935
- if opts .processes < 0 :
936
- opt_parser .error ("number of processes needs to be 0 or more" )
938
+ if args .processes < 0 :
939
+ parser .error ("number of processes needs to be 0 or more" )
937
940
938
- _configure_logging (opts )
941
+ _configure_logging (args )
939
942
940
943
rc = 0
941
- for bag_dir in args :
944
+ for bag_dir in args . directory :
942
945
943
946
# validate the bag
944
- if opts .validate :
947
+ if args .validate :
945
948
try :
946
949
bag = Bag (bag_dir )
947
950
# validate throws a BagError or BagValidationError
948
- bag .validate (processes = opts .processes , fast = opts .fast )
949
- if opts .fast :
951
+ bag .validate (processes = args .processes , fast = args .fast )
952
+ if args .fast :
950
953
LOGGER .info ("%s valid according to Payload-Oxum" , bag_dir )
951
954
else :
952
955
LOGGER .info ("%s is valid" , bag_dir )
@@ -957,9 +960,9 @@ def main():
957
960
# make the bag
958
961
else :
959
962
try :
960
- make_bag (bag_dir , bag_info = opt_parser .bag_info ,
961
- processes = opts .processes ,
962
- checksum = opts .checksum )
963
+ make_bag (bag_dir , bag_info = parser .bag_info ,
964
+ processes = args .processes ,
965
+ checksum = args .checksum )
963
966
except Exception as exc :
964
967
LOGGER .error ("Failed to create bag in %s: %s" , bag_dir , exc , exc_info = True )
965
968
rc = 1
0 commit comments