37 import sys,logging,getopt
44 """!Prints a usage message to stderr
46 print>>sys.stderr,
'''Format: %s [options] [--] from to
48 This is the test program for produtil.fileop.deliver_file. It will
49 copy or move file "from" to file or directory "to". This is similar
50 to the cp and commands, but the "to" file will never be seen in an
51 incomplete state. This is accomplished either via a "mv" or by
52 copying to a temporary file and "mv"ing that file to the destination.
53 All "mv" operations are within one filesystem, to ensure the
54 operations are unit operations. Be sure to use the -m option if you
55 do not need the original "from" file any more to allow a simple "mv".
58 -v verbose (multiple -v for higher verbosity)
59 -m use "mv" to deliver if "from" and "to" are in the same filesystem
60 Note: if that is done, it will cause "from" to cease to exist.
61 -c verify: check to see that the temporary file has the same contents as
62 "from" before moving it to the destination
63 -b bytes copy "bytes" bytes at a time (must be >= %d)
64 -t template prefix to prepend to temporary filenames
65 Can only contain: a-z A-Z 0-9 _ . -
66 -k Keep the temporary file if the delivery fails.
67 -- Terminate option processing. Use this if you need to deliver a
68 file whose name begins with a dash (-)''' % (sys.argv[0],minblock)
71 'SCRIPT IS EXITING DUE TO INCORRECT ARGUMENTS: %s'%(str(s),minblock)
74 """!Main program. Parses arguments, delivers file."""
80 (optarg,rest) = getopt.getopt(sys.argv[1:],
'kmcrvb:t:')
81 except getopt.GetoptError
as e:
85 usage(
'You must provide exactly two non-option arguments: the source and destination (you gave %d args).'%(len(rest),))
88 for opt,arg
in optarg:
96 if not (bs>=minblock):
97 usage(
'Minimum allowed block size is %d'%(minblock,))
98 kwargs[
'blocksize']=bs
99 except ValueError
as e:
104 if(re.search(
'[^a-zA-Z0-9_-.]',arg)):
105 usage(
'The temp prefix must only contain alphanumerics, underscore (_), dash (-) and period (.).')
106 kwargs[
'tempprefix']=arg
108 kwargs[
'removefailed']=
False
110 usage(
'Unknown option %s'%(opt,))
112 level=logging.WARNING
117 logging.basicConfig(level=level)
120 deliver_file(infile,outfile,logger=logging.getLogger(),**kwargs)
122 except DeliveryFailed
as d:
123 logging.critical(str(d))
125 except (IOError,OSError)
as e:
126 logging.critical(
'%s delivery: %s' % (infile,str(e)))
128 except (KeyboardInterrupt,CaughtSignal)
as e:
129 logging.critical(
'%s delivery: abort due to signal: %s'%(infile,str(e)))
132 if __name__==
'__main__': main()
This module provides a set of utility functions to do filesystem operations.
Sets up signal handlers to ensure a clean exit.