Back to index

enigmail  1.4.3
Functions
nsinstall Namespace Reference

Functions

def nsinstall

Function Documentation

def nsinstall.nsinstall (   argv)

Definition at line 52 of file nsinstall.py.

00052 
00053 def nsinstall(argv):
00054   usage = "usage: %prog [options] arg1 [arg2 ...] target-directory"
00055   p = OptionParser(usage=usage)
00056 
00057   p.add_option('-D', action="store_true",
00058                help="Create a single directory only")
00059   p.add_option('-t', action="store_true",
00060                help="Preserve time stamp")
00061   p.add_option('-m', action="store",
00062                help="Set mode", metavar="mode")
00063   p.add_option('-d', action="store_true",
00064                help="Create directories in target")
00065   p.add_option('-R', action="store_true",
00066                help="Use relative symbolic links (ignored)")
00067   p.add_option('-l', action="store_true",
00068                help="Create link (ignored)")
00069   p.add_option('-L', action="store", metavar="linkprefix",
00070                help="Link prefix (ignored)")
00071 
00072   # The remaining arguments are not used in our tree, thus they're not
00073   # implented.
00074   def BadArg(option, opt, value, parser):
00075     parser.error('option not supported: %s' % opt)
00076     
00077   p.add_option('-C', action="callback", metavar="CWD",
00078                callback=BadArg,
00079                help="NOT SUPPORTED")
00080   p.add_option('-o', action="callback", callback=BadArg,
00081                help="Set owner (NOT SUPPORTED)", metavar="owner")
00082   p.add_option('-g', action="callback", callback=BadArg,
00083                help="Set group (NOT SUPPORTED)", metavar="group")
00084 
00085   (options, args) = p.parse_args(argv)
00086 
00087   if options.m:
00088     # mode is specified
00089     try:
00090       options.m = int(options.m, 8)
00091     except:
00092       sys.stderr.write('nsinstall: ' + options.m + ' is not a valid mode\n')
00093       return 1
00094 
00095   # just create one directory?
00096   if options.D:
00097     if len(args) != 1:
00098       return 1
00099     if os.path.exists(args[0]):
00100       if not os.path.isdir(args[0]):
00101         sys.stderr.write('nsinstall: ' + args[0] + ' is not a directory\n')
00102         sys.exit(1)
00103       if options.m:
00104         os.chmod(args[0], options.m)
00105       sys.exit()
00106     if options.m:
00107       os.makedirs(args[0], options.m)
00108     else:
00109       os.makedirs(args[0])
00110     return 0
00111 
00112   # nsinstall arg1 [...] directory
00113   if len(args) < 2:
00114     p.error('not enough arguments')
00115 
00116   def copy_all_entries(entries, target):
00117     for e in entries:
00118       dest = os.path.join(target,
00119                           os.path.basename(os.path.normpath(e)))
00120       handleTarget(e, dest)
00121       if options.m:
00122         os.chmod(dest, options.m)
00123 
00124   # set up handler
00125   if options.d:
00126     # we're supposed to create directories
00127     def handleTarget(srcpath, targetpath):
00128       # target directory was already created, just use mkdir
00129       os.mkdir(targetpath)
00130   else:
00131     # we're supposed to copy files
00132     def handleTarget(srcpath, targetpath):
00133       if os.path.isdir(srcpath):
00134         if not os.path.exists(targetpath):
00135           os.mkdir(targetpath)
00136         entries = [os.path.join(srcpath, e) for e in os.listdir(srcpath)]
00137         copy_all_entries(entries, targetpath)
00138         # options.t is not relevant for directories
00139         if options.m:
00140           os.chmod(targetpath, options.m)
00141       elif options.t:
00142         shutil.copy2(srcpath, targetpath)
00143       else:
00144         shutil.copy(srcpath, targetpath)
00145 
00146   # the last argument is the target directory
00147   target = args.pop()
00148   # ensure target directory
00149   if not os.path.isdir(target):
00150     os.makedirs(target)
00151 
00152   copy_all_entries(args, target)
00153   return 0