Back to index

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