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
@@ -876,42 +876,45 @@ def _decode_filename(s):
876
876
877
877
# following code is used for command line program
878
878
879
- class BagOptionParser ( optparse . OptionParser ):
880
- def __init__ (self , * args , ** opts ):
879
+ class BagArgumentParser ( argparse . ArgumentParser ):
880
+ def __init__ (self , * args , ** kwargs ):
881
881
self .bag_info = {}
882
- optparse . OptionParser .__init__ (self , * args , ** opts )
882
+ argparse . ArgumentParser .__init__ (self , * args , ** kwargs )
883
883
884
884
885
- def _bag_info_store (_ , opt , value , parser ):
886
- opt = opt .lstrip ('--' )
887
- opt_caps = '-' .join ([o .capitalize () for o in opt .split ('-' )])
888
- parser .bag_info [opt_caps ] = value
885
+ class BagHeaderAction (argparse .Action ):
886
+ def __call__ (self , parser , _ , values , option_string = None ):
887
+ opt = option_string .lstrip ('--' )
888
+ opt_caps = '-' .join ([o .capitalize () for o in opt .split ('-' )])
889
+ parser .bag_info [opt_caps ] = values
889
890
890
891
891
- def _make_opt_parser ():
892
- parser = BagOptionParser (usage = 'usage: %prog [options] dir1 dir2 ...' )
893
- parser .add_option ('--processes' , action = 'store' , type = "int" ,
894
- dest = 'processes' , default = 1 ,
892
+ def _make_parser ():
893
+ parser = BagArgumentParser ()
894
+ parser .add_argument ('--processes' , type = int , dest = 'processes' , default = 1 ,
895
895
help = 'parallelize checksums generation and verification' )
896
- parser .add_option ('--log' , action = 'store' , dest = ' log' , help = 'The name of the log file' )
897
- parser .add_option ('--quiet' , action = 'store_true' , dest = 'quiet ' )
898
- parser .add_option ('--validate' , action = 'store_true' , dest = 'validate ' )
899
- parser .add_option ('--fast' , action = 'store_true' , dest = 'fast ' )
896
+ parser .add_argument ('--log' , help = 'The name of the log file' )
897
+ parser .add_argument ('--quiet' , action = 'store_true' )
898
+ parser .add_argument ('--validate' , action = 'store_true' )
899
+ parser .add_argument ('--fast' , action = 'store_true' )
900
900
901
901
# optionally specify which checksum algorithm(s) to use when creating a bag
902
902
# NOTE: could generate from checksum_algos ?
903
- parser .add_option ('--md5' , action = 'append_const' , dest = 'checksum' , const = 'md5' ,
903
+ parser .add_argument ('--md5' , action = 'append_const' , dest = 'checksum' , const = 'md5' ,
904
904
help = 'Generate MD5 manifest when creating a bag (default)' )
905
- parser .add_option ('--sha1' , action = 'append_const' , dest = 'checksum' , const = 'sha1' ,
905
+ parser .add_argument ('--sha1' , action = 'append_const' , dest = 'checksum' , const = 'sha1' ,
906
906
help = 'Generate SHA1 manifest when creating a bag' )
907
- parser .add_option ('--sha256' , action = 'append_const' , dest = 'checksum' , const = 'sha256' ,
907
+ parser .add_argument ('--sha256' , action = 'append_const' , dest = 'checksum' , const = 'sha256' ,
908
908
help = 'Generate SHA-256 manifest when creating a bag' )
909
- parser .add_option ('--sha512' , action = 'append_const' , dest = 'checksum' , const = 'sha512' ,
909
+ parser .add_argument ('--sha512' , action = 'append_const' , dest = 'checksum' , const = 'sha512' ,
910
910
help = 'Generate SHA-512 manifest when creating a bag' )
911
911
912
912
for header in STANDARD_BAG_INFO_HEADERS :
913
- parser .add_option ('--%s' % header .lower (), type = "string" ,
914
- action = 'callback' , callback = _bag_info_store )
913
+ parser .add_argument ('--%s' % header .lower (), type = str ,
914
+ action = BagHeaderAction )
915
+
916
+ parser .add_argument ('directory' , nargs = '+' , help = 'directory to make a bag from' )
917
+
915
918
return parser
916
919
917
920
@@ -928,24 +931,24 @@ def _configure_logging(opts):
928
931
929
932
930
933
def main ():
931
- opt_parser = _make_opt_parser ()
932
- opts , args = opt_parser .parse_args ()
934
+ parser = _make_parser ()
935
+ args = parser .parse_args ()
933
936
934
- if opts .processes < 0 :
935
- opt_parser .error ("number of processes needs to be 0 or more" )
937
+ if args .processes < 0 :
938
+ parser .error ("number of processes needs to be 0 or more" )
936
939
937
- _configure_logging (opts )
940
+ _configure_logging (args )
938
941
939
942
rc = 0
940
- for bag_dir in args :
943
+ for bag_dir in args . directory :
941
944
942
945
# validate the bag
943
- if opts .validate :
946
+ if args .validate :
944
947
try :
945
948
bag = Bag (bag_dir )
946
949
# validate throws a BagError or BagValidationError
947
- bag .validate (processes = opts .processes , fast = opts .fast )
948
- if opts .fast :
950
+ bag .validate (processes = args .processes , fast = args .fast )
951
+ if args .fast :
949
952
LOGGER .info ("%s valid according to Payload-Oxum" , bag_dir )
950
953
else :
951
954
LOGGER .info ("%s is valid" , bag_dir )
@@ -956,9 +959,9 @@ def main():
956
959
# make the bag
957
960
else :
958
961
try :
959
- make_bag (bag_dir , bag_info = opt_parser .bag_info ,
960
- processes = opts .processes ,
961
- checksum = opts .checksum )
962
+ make_bag (bag_dir , bag_info = parser .bag_info ,
963
+ processes = args .processes ,
964
+ checksum = args .checksum )
962
965
except Exception as exc :
963
966
LOGGER .error ("Failed to create bag in %s: %s" , bag_dir , exc , exc_info = True )
964
967
rc = 1
0 commit comments