Bug 737833 - Only do metro registration for metro builds. r=rstrong
[gecko.git] / config / nsinstall.py
blobb0a81d17d983a14542182c439c64efd9b2384668
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 # This is a partial python port of nsinstall.
6 # It's intended to be used when there's no natively compile nsinstall
7 # available, and doesn't intend to be fully equivalent.
8 # Its major use is for l10n repackaging on systems that don't have
9 # a full build environment set up.
10 # The basic limitation is, it doesn't even try to link and ignores
11 # all related options.
13 from optparse import OptionParser
14 import os
15 import os.path
16 import sys
17 import shutil
18 import stat
20 def _nsinstall_internal(argv):
21 usage = "usage: %prog [options] arg1 [arg2 ...] target-directory"
22 p = OptionParser(usage=usage)
24 p.add_option('-D', action="store_true",
25 help="Create a single directory only")
26 p.add_option('-t', action="store_true",
27 help="Preserve time stamp")
28 p.add_option('-m', action="store",
29 help="Set mode", metavar="mode")
30 p.add_option('-d', action="store_true",
31 help="Create directories in target")
32 p.add_option('-R', action="store_true",
33 help="Use relative symbolic links (ignored)")
34 p.add_option('-L', action="store", metavar="linkprefix",
35 help="Link prefix (ignored)")
36 p.add_option('-X', action="append", metavar="file",
37 help="Ignore a file when installing a directory recursively.")
39 # The remaining arguments are not used in our tree, thus they're not
40 # implented.
41 def BadArg(option, opt, value, parser):
42 parser.error('option not supported: %s' % opt)
44 p.add_option('-C', action="callback", metavar="CWD",
45 callback=BadArg,
46 help="NOT SUPPORTED")
47 p.add_option('-o', action="callback", callback=BadArg,
48 help="Set owner (NOT SUPPORTED)", metavar="owner")
49 p.add_option('-g', action="callback", callback=BadArg,
50 help="Set group (NOT SUPPORTED)", metavar="group")
52 (options, args) = p.parse_args(argv)
54 if options.m:
55 # mode is specified
56 try:
57 options.m = int(options.m, 8)
58 except:
59 sys.stderr.write('nsinstall: ' + options.m + ' is not a valid mode\n')
60 return 1
62 # just create one directory?
63 def maybe_create_dir(dir, mode, try_again):
64 dir = os.path.abspath(dir)
65 if os.path.exists(dir):
66 if not os.path.isdir(dir):
67 print >> sys.stderr, ('nsinstall: %s is not a directory' % dir)
68 return 1
69 if mode:
70 os.chmod(dir, mode)
71 return 0
73 try:
74 if mode:
75 os.makedirs(dir, mode)
76 else:
77 os.makedirs(dir)
78 except Exception, e:
79 # We might have hit EEXIST due to a race condition (see bug 463411) -- try again once
80 if try_again:
81 return maybe_create_dir(dir, mode, False)
82 print >> sys.stderr, ("nsinstall: failed to create directory %s: %s" % (dir, e))
83 return 1
84 else:
85 return 0
87 if options.X:
88 options.X = [os.path.abspath(p) for p in options.X]
90 if options.D:
91 return maybe_create_dir(args[0], options.m, True)
93 # nsinstall arg1 [...] directory
94 if len(args) < 2:
95 p.error('not enough arguments')
97 def copy_all_entries(entries, target):
98 for e in entries:
99 e = os.path.abspath(e)
100 if options.X and e in options.X:
101 continue
103 dest = os.path.join(target, os.path.basename(e))
104 dest = os.path.abspath(dest)
105 handleTarget(e, dest)
106 if options.m:
107 os.chmod(dest, options.m)
109 # set up handler
110 if options.d:
111 # we're supposed to create directories
112 def handleTarget(srcpath, targetpath):
113 # target directory was already created, just use mkdir
114 os.mkdir(targetpath)
115 else:
116 # we're supposed to copy files
117 def handleTarget(srcpath, targetpath):
118 if os.path.isdir(srcpath):
119 if not os.path.exists(targetpath):
120 os.mkdir(targetpath)
121 entries = [os.path.join(srcpath, e) for e in os.listdir(srcpath)]
122 copy_all_entries(entries, targetpath)
123 # options.t is not relevant for directories
124 if options.m:
125 os.chmod(targetpath, options.m)
126 else:
127 if os.path.exists(targetpath):
128 # On Windows, read-only files can't be deleted
129 os.chmod(targetpath, stat.S_IWUSR)
130 os.remove(targetpath)
131 if options.t:
132 shutil.copy2(srcpath, targetpath)
133 else:
134 shutil.copy(srcpath, targetpath)
136 # the last argument is the target directory
137 target = args.pop()
138 # ensure target directory (importantly, we do not apply a mode to the directory
139 # because we want to copy files into it and the mode might be read-only)
140 rv = maybe_create_dir(target, None, True)
141 if rv != 0:
142 return rv
144 copy_all_entries(args, target)
145 return 0
147 # nsinstall as a native command is always UTF-8
148 def nsinstall(argv):
149 return _nsinstall_internal([unicode(arg, "utf-8") for arg in argv])
151 if __name__ == '__main__':
152 # sys.argv corrupts characters outside the system code page on Windows
153 # <http://bugs.python.org/issue2128>. Use ctypes instead. This is also
154 # useful because switching to Unicode strings makes python use the wide
155 # Windows APIs, which is what we want here since the wide APIs normally do a
156 # better job at handling long paths and such.
157 if sys.platform == "win32":
158 import ctypes
159 from ctypes import wintypes
160 GetCommandLine = ctypes.windll.kernel32.GetCommandLineW
161 GetCommandLine.argtypes = []
162 GetCommandLine.restype = wintypes.LPWSTR
164 CommandLineToArgv = ctypes.windll.shell32.CommandLineToArgvW
165 CommandLineToArgv.argtypes = [wintypes.LPWSTR, ctypes.POINTER(ctypes.c_int)]
166 CommandLineToArgv.restype = ctypes.POINTER(wintypes.LPWSTR)
168 argc = ctypes.c_int(0)
169 argv_arr = CommandLineToArgv(GetCommandLine(), ctypes.byref(argc))
170 # The first argv will be "python", the second will be the .py file
171 argv = argv_arr[1:argc.value]
172 else:
173 # For consistency, do it on Unix as well
174 if sys.stdin.encoding is not None:
175 argv = [unicode(arg, sys.stdin.encoding) for arg in sys.argv]
176 else:
177 argv = [unicode(arg) for arg in sys.argv]
179 sys.exit(_nsinstall_internal(argv[1:]))