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