Created
December 21, 2014 21:10
-
-
Save dirkpetersen/e86306067718f66e1af7 to your computer and use it in GitHub Desktop.
allow passing of alternative units such as GB, MB to --segment-size in python-swiftclient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
st_upload_help = ''' | |
Uploads specified files and directories to the given container | |
Positional arguments: | |
<container> Name of container to upload to | |
<file_or_directory> Name of file or directory to upload. Specify multiple | |
times for multiple uploads | |
Optional arguments: | |
--changed Only upload files that have changed since the last | |
upload | |
--segment-size <size> Upload files in segments no larger than <size> and | |
then create a "manifest" file that will download all | |
the segments as if it were the original file. Valid | |
args are N, NG, Ng, NM, Nm, NK, Nk, where N is some | |
integer, and G or g represents GiB, M or m represents | |
MiB, and K or k represents KiB. N without a modifier | |
represents a number of bytes. Can also be set via | |
the environment variable ST_SEGMENT_SIZE | |
def st_upload(parser, args, thread_manager): | |
#. | |
#. | |
#. | |
#. | |
parser.add_option( | |
'', '--use-slo', action='store_true', default=False, | |
help='When used in conjunction with --segment-size will ' | |
'create a Static Large Object instead of the default ' | |
'Dynamic Large Object.') | |
(options, args) = parse_args(parser, args) | |
############# BEGIN MOD ############################################ | |
# allowed size modifiers : multiplier | |
# none : 1 | |
# G or g: 1073741824 (GiB) | |
# M or m : 1048576 (MiB) | |
# K or k : 1024 (KiB) | |
def _parse_segment_size( size ): | |
l2m = { 'g' : 1073741824, 'm' : 1048576, 'k' : 1024 } | |
# if size is a number, return integer representation | |
try: | |
int( size ) | |
return size | |
except: | |
pass | |
# if size is not a number, see if it matches the form | |
# 5G, 5g, 5 G, 5 g, 5M, 5m, 5 M, 5 m, 5k, 5K, 5 k, 5K | |
# 0.5G, etc, as above | |
# if not throw an error | |
mo = re.match( r'^(\d+\.?\d*)(?:\s+)?([gmk])$', size.lower() ) | |
if ( mo ): | |
digits = mo.group(1) | |
multiplier = mo.group(2) | |
if ( not digits and multiplier ): | |
raise ClientException( "bad segment_size: '%s'" % ( size, ) ) | |
try: | |
size = float( digits ) * l2m[ multiplier ] | |
except ValueError: | |
raise ClientException( "bad segment_size: '%s'" % ( size, ) ) | |
return int( size ) | |
else: | |
raise ClientException( "bad segment_size: '%s'" % ( size, ) ) | |
# need to process segment_size | |
# order of precedence: commandline setting, envvar setting, default | |
if ( options.segment_size ): | |
options.segment_size = _parse_segment_size( options.segment_size ) | |
elif ( environ.get( 'ST_SEGMENT_SIZE' ) ): | |
options.segment_size = _parse_segment_size( environ[ 'ST_SEGMENT_SIZE' ] ) | |
else: | |
options.segment_size = _parse_segment_size( '5G' ) | |
#### END MOD ###################################################### | |
args = args[1:] | |
if len(args) < 2: | |
thread_manager.error( | |
'Usage: %s upload %s\n%s', basename(argv[0]), st_upload_options, | |
st_upload_help) | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment