Back to index

enigmail  1.4.3
nsinstall.py
Go to the documentation of this file.
00001 # ***** BEGIN LICENSE BLOCK *****
00002 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003 #
00004 # The contents of this file are subject to the Mozilla Public License Version
00005 # 1.1 (the "License"); you may not use this file except in compliance with
00006 # the License. You may obtain a copy of the License at
00007 # http://www.mozilla.org/MPL/
00008 #
00009 # Software distributed under the License is distributed on an "AS IS" basis,
00010 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011 # for the specific language governing rights and limitations under the
00012 # License.
00013 #
00014 # The Original Code is Mozilla.
00015 #
00016 # The Initial Developer of the Original Code is
00017 # the Mozilla Foundation.
00018 # Portions created by the Initial Developer are Copyright (C) 2007
00019 # the Initial Developer. All Rights Reserved.
00020 #
00021 # Contributor(s):
00022 #   Axel Hecht <axel@pike.org>
00023 #
00024 # Alternatively, the contents of this file may be used under the terms of
00025 # either the GNU General Public License Version 2 or later (the "GPL"), or
00026 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027 # in which case the provisions of the GPL or the LGPL are applicable instead
00028 # of those above. If you wish to allow use of your version of this file only
00029 # under the terms of either the GPL or the LGPL, and not to allow others to
00030 # use your version of this file under the terms of the MPL, indicate your
00031 # decision by deleting the provisions above and replace them with the notice
00032 # and other provisions required by the GPL or the LGPL. If you do not delete
00033 # the provisions above, a recipient may use your version of this file under
00034 # the terms of any one of the MPL, the GPL or the LGPL.
00035 #
00036 # ***** END LICENSE BLOCK *****
00037 
00038 # This is a partial python port of nsinstall.
00039 # It's intended to be used when there's no natively compile nsinstall
00040 # available, and doesn't intend to be fully equivalent.
00041 # Its major use is for l10n repackaging on systems that don't have
00042 # a full build environment set up.
00043 # The basic limitation is, it doesn't even try to link and ignores
00044 # all related options.
00045 
00046 from optparse import OptionParser
00047 import os
00048 import os.path
00049 import sys
00050 import shutil
00051 
00052 def nsinstall(argv):
00053   usage = "usage: %prog [options] arg1 [arg2 ...] target-directory"
00054   p = OptionParser(usage=usage)
00055 
00056   p.add_option('-D', action="store_true",
00057                help="Create a single directory only")
00058   p.add_option('-t', action="store_true",
00059                help="Preserve time stamp")
00060   p.add_option('-m', action="store",
00061                help="Set mode", metavar="mode")
00062   p.add_option('-d', action="store_true",
00063                help="Create directories in target")
00064   p.add_option('-R', action="store_true",
00065                help="Use relative symbolic links (ignored)")
00066   p.add_option('-l', action="store_true",
00067                help="Create link (ignored)")
00068   p.add_option('-L', action="store", metavar="linkprefix",
00069                help="Link prefix (ignored)")
00070 
00071   # The remaining arguments are not used in our tree, thus they're not
00072   # implented.
00073   def BadArg(option, opt, value, parser):
00074     parser.error('option not supported: %s' % opt)
00075     
00076   p.add_option('-C', action="callback", metavar="CWD",
00077                callback=BadArg,
00078                help="NOT SUPPORTED")
00079   p.add_option('-o', action="callback", callback=BadArg,
00080                help="Set owner (NOT SUPPORTED)", metavar="owner")
00081   p.add_option('-g', action="callback", callback=BadArg,
00082                help="Set group (NOT SUPPORTED)", metavar="group")
00083 
00084   (options, args) = p.parse_args(argv)
00085 
00086   if options.m:
00087     # mode is specified
00088     try:
00089       options.m = int(options.m, 8)
00090     except:
00091       sys.stderr.write('nsinstall: ' + options.m + ' is not a valid mode\n')
00092       return 1
00093 
00094   # just create one directory?
00095   if options.D:
00096     if len(args) != 1:
00097       return 1
00098     if os.path.exists(args[0]):
00099       if not os.path.isdir(args[0]):
00100         sys.stderr.write('nsinstall: ' + args[0] + ' is not a directory\n')
00101         sys.exit(1)
00102       if options.m:
00103         os.chmod(args[0], options.m)
00104       sys.exit()
00105     if options.m:
00106       os.makedirs(args[0], options.m)
00107     else:
00108       os.makedirs(args[0])
00109     return 0
00110 
00111   # nsinstall arg1 [...] directory
00112   if len(args) < 2:
00113     p.error('not enough arguments')
00114 
00115   def copy_all_entries(entries, target):
00116     for e in entries:
00117       dest = os.path.join(target,
00118                           os.path.basename(os.path.normpath(e)))
00119       handleTarget(e, dest)
00120       if options.m:
00121         os.chmod(dest, options.m)
00122 
00123   # set up handler
00124   if options.d:
00125     # we're supposed to create directories
00126     def handleTarget(srcpath, targetpath):
00127       # target directory was already created, just use mkdir
00128       os.mkdir(targetpath)
00129   else:
00130     # we're supposed to copy files
00131     def handleTarget(srcpath, targetpath):
00132       if os.path.isdir(srcpath):
00133         if not os.path.exists(targetpath):
00134           os.mkdir(targetpath)
00135         entries = [os.path.join(srcpath, e) for e in os.listdir(srcpath)]
00136         copy_all_entries(entries, targetpath)
00137         # options.t is not relevant for directories
00138         if options.m:
00139           os.chmod(targetpath, options.m)
00140       elif options.t:
00141         shutil.copy2(srcpath, targetpath)
00142       else:
00143         shutil.copy(srcpath, targetpath)
00144 
00145   # the last argument is the target directory
00146   target = args.pop()
00147   # ensure target directory
00148   if not os.path.isdir(target):
00149     os.makedirs(target)
00150 
00151   copy_all_entries(args, target)
00152   return 0
00153 
00154 if __name__ == '__main__':
00155   sys.exit(nsinstall(sys.argv[1:]))