Bug 1564761 [wpt PR 17620] - Document::CheckComplete should be nop when called from...
[gecko.git] / config / nsinstall.py
blobadf03164ccd480616a58e147f9cb6396e94ac52b
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.
12 from __future__ import absolute_import
13 from __future__ import print_function
14 from optparse import OptionParser
15 import mozfile
16 import os
17 import os.path
18 import sys
19 import shutil
22 def _nsinstall_internal(argv):
23 usage = "usage: %prog [options] arg1 [arg2 ...] target-directory"
24 p = OptionParser(usage=usage)
26 p.add_option('-D', action="store_true",
27 help="Create a single directory only")
28 p.add_option('-t', action="store_true",
29 help="Preserve time stamp")
30 p.add_option('-m', action="store",
31 help="Set mode", metavar="mode")
32 p.add_option('-d', action="store_true",
33 help="Create directories in target")
34 p.add_option('-R', action="store_true",
35 help="Use relative symbolic links (ignored)")
36 p.add_option('-L', action="store", metavar="linkprefix",
37 help="Link prefix (ignored)")
38 p.add_option('-X', action="append", metavar="file",
39 help="Ignore a file when installing a directory recursively.")
41 # The remaining arguments are not used in our tree, thus they're not
42 # implented.
43 def BadArg(option, opt, value, parser):
44 parser.error('option not supported: {0}'.format(opt))
46 p.add_option('-C', action="callback", metavar="CWD",
47 callback=BadArg,
48 help="NOT SUPPORTED")
49 p.add_option('-o', action="callback", callback=BadArg,
50 help="Set owner (NOT SUPPORTED)", metavar="owner")
51 p.add_option('-g', action="callback", callback=BadArg,
52 help="Set group (NOT SUPPORTED)", metavar="group")
54 (options, args) = p.parse_args(argv)
56 if options.m:
57 # mode is specified
58 try:
59 options.m = int(options.m, 8)
60 except Exception:
61 sys.stderr.write('nsinstall: {0} is not a valid mode\n'
62 .format(options.m))
63 return 1
65 # just create one directory?
66 def maybe_create_dir(dir, mode, try_again):
67 dir = os.path.abspath(dir)
68 if os.path.exists(dir):
69 if not os.path.isdir(dir):
70 print('nsinstall: {0} is not a directory'.format(dir), file=sys.stderr)
71 return 1
72 if mode:
73 os.chmod(dir, mode)
74 return 0
76 try:
77 if mode:
78 os.makedirs(dir, mode)
79 else:
80 os.makedirs(dir)
81 except Exception as e:
82 # We might have hit EEXIST due to a race condition (see bug 463411) -- try again once
83 if try_again:
84 return maybe_create_dir(dir, mode, False)
85 print(
86 "nsinstall: failed to create directory {0}: {1}".format(dir, e))
87 return 1
88 else:
89 return 0
91 if options.X:
92 options.X = [os.path.abspath(path) for path in options.X]
94 if options.D:
95 return maybe_create_dir(args[0], options.m, True)
97 # nsinstall arg1 [...] directory
98 if len(args) < 2:
99 p.error('not enough arguments')
101 def copy_all_entries(entries, target):
102 for e in entries:
103 e = os.path.abspath(e)
104 if options.X and e in options.X:
105 continue
107 dest = os.path.join(target, os.path.basename(e))
108 dest = os.path.abspath(dest)
109 handleTarget(e, dest)
110 if options.m:
111 os.chmod(dest, options.m)
113 # set up handler
114 if options.d:
115 # we're supposed to create directories
116 def handleTarget(srcpath, targetpath):
117 # target directory was already created, just use mkdir
118 os.mkdir(targetpath)
119 else:
120 # we're supposed to copy files
121 def handleTarget(srcpath, targetpath):
122 if os.path.isdir(srcpath):
123 if not os.path.exists(targetpath):
124 os.mkdir(targetpath)
125 entries = [os.path.join(srcpath, e)
126 for e in os.listdir(srcpath)]
127 copy_all_entries(entries, targetpath)
128 # options.t is not relevant for directories
129 if options.m:
130 os.chmod(targetpath, options.m)
131 else:
132 if os.path.exists(targetpath):
133 if sys.platform == "win32":
134 mozfile.remove(targetpath)
135 else:
136 os.remove(targetpath)
137 if options.t:
138 shutil.copy2(srcpath, targetpath)
139 else:
140 shutil.copy(srcpath, targetpath)
142 # the last argument is the target directory
143 target = args.pop()
144 # ensure target directory (importantly, we do not apply a mode to the directory
145 # because we want to copy files into it and the mode might be read-only)
146 rv = maybe_create_dir(target, None, True)
147 if rv != 0:
148 return rv
150 copy_all_entries(args, target)
151 return 0
153 # nsinstall as a native command is always UTF-8
156 def nsinstall(argv):
157 return _nsinstall_internal([unicode(arg, "utf-8") for arg in argv])
160 if __name__ == '__main__':
161 # sys.argv corrupts characters outside the system code page on Windows
162 # <http://bugs.python.org/issue2128>. Use ctypes instead. This is also
163 # useful because switching to Unicode strings makes python use the wide
164 # Windows APIs, which is what we want here since the wide APIs normally do a
165 # better job at handling long paths and such.
166 if sys.platform == "win32":
167 import ctypes
168 from ctypes import wintypes
169 GetCommandLine = ctypes.windll.kernel32.GetCommandLineW
170 GetCommandLine.argtypes = []
171 GetCommandLine.restype = wintypes.LPWSTR
173 CommandLineToArgv = ctypes.windll.shell32.CommandLineToArgvW
174 CommandLineToArgv.argtypes = [
175 wintypes.LPWSTR, ctypes.POINTER(ctypes.c_int)]
176 CommandLineToArgv.restype = ctypes.POINTER(wintypes.LPWSTR)
178 argc = ctypes.c_int(0)
179 argv_arr = CommandLineToArgv(GetCommandLine(), ctypes.byref(argc))
180 # The first argv will be "python", the second will be the .py file
181 argv = argv_arr[1:argc.value]
182 else:
183 # For consistency, do it on Unix as well
184 if sys.stdin.encoding is not None:
185 argv = [unicode(arg, sys.stdin.encoding) for arg in sys.argv]
186 else:
187 argv = [unicode(arg) for arg in sys.argv]
189 sys.exit(_nsinstall_internal(argv[1:]))