Bug 1593728 Part 2: [devtools] Add a simple log message param and function to a conso...
[gecko.git] / config / nsinstall.py
blob1d5ead6ff867bdd9481d25f97fa0c1c43f9ec2af
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 six
19 import sys
20 import shutil
23 def _nsinstall_internal(argv):
24 usage = "usage: %prog [options] arg1 [arg2 ...] target-directory"
25 p = OptionParser(usage=usage)
27 p.add_option('-D', action="store_true",
28 help="Create a single directory only")
29 p.add_option('-t', action="store_true",
30 help="Preserve time stamp")
31 p.add_option('-m', action="store",
32 help="Set mode", metavar="mode")
33 p.add_option('-d', action="store_true",
34 help="Create directories in target")
35 p.add_option('-R', action="store_true",
36 help="Use relative symbolic links (ignored)")
37 p.add_option('-L', action="store", metavar="linkprefix",
38 help="Link prefix (ignored)")
39 p.add_option('-X', action="append", metavar="file",
40 help="Ignore a file when installing a directory recursively.")
42 # The remaining arguments are not used in our tree, thus they're not
43 # implented.
44 def BadArg(option, opt, value, parser):
45 parser.error('option not supported: {0}'.format(opt))
47 p.add_option('-C', action="callback", metavar="CWD",
48 callback=BadArg,
49 help="NOT SUPPORTED")
50 p.add_option('-o', action="callback", callback=BadArg,
51 help="Set owner (NOT SUPPORTED)", metavar="owner")
52 p.add_option('-g', action="callback", callback=BadArg,
53 help="Set group (NOT SUPPORTED)", metavar="group")
55 (options, args) = p.parse_args(argv)
57 if options.m:
58 # mode is specified
59 try:
60 options.m = int(options.m, 8)
61 except Exception:
62 sys.stderr.write('nsinstall: {0} is not a valid mode\n'
63 .format(options.m))
64 return 1
66 # just create one directory?
67 def maybe_create_dir(dir, mode, try_again):
68 dir = os.path.abspath(dir)
69 if os.path.exists(dir):
70 if not os.path.isdir(dir):
71 print('nsinstall: {0} is not a directory'.format(dir), file=sys.stderr)
72 return 1
73 if mode:
74 os.chmod(dir, mode)
75 return 0
77 try:
78 if mode:
79 os.makedirs(dir, mode)
80 else:
81 os.makedirs(dir)
82 except Exception as e:
83 # We might have hit EEXIST due to a race condition (see bug 463411) -- try again once
84 if try_again:
85 return maybe_create_dir(dir, mode, False)
86 print(
87 "nsinstall: failed to create directory {0}: {1}".format(dir, e))
88 return 1
89 else:
90 return 0
92 if options.X:
93 options.X = [os.path.abspath(path) for path in options.X]
95 if options.D:
96 return maybe_create_dir(args[0], options.m, True)
98 # nsinstall arg1 [...] directory
99 if len(args) < 2:
100 p.error('not enough arguments')
102 def copy_all_entries(entries, target):
103 for e in entries:
104 e = os.path.abspath(e)
105 if options.X and e in options.X:
106 continue
108 dest = os.path.join(target, os.path.basename(e))
109 dest = os.path.abspath(dest)
110 handleTarget(e, dest)
111 if options.m:
112 os.chmod(dest, options.m)
114 # set up handler
115 if options.d:
116 # we're supposed to create directories
117 def handleTarget(srcpath, targetpath):
118 # target directory was already created, just use mkdir
119 os.mkdir(targetpath)
120 else:
121 # we're supposed to copy files
122 def handleTarget(srcpath, targetpath):
123 if os.path.isdir(srcpath):
124 if not os.path.exists(targetpath):
125 os.mkdir(targetpath)
126 entries = [os.path.join(srcpath, e)
127 for e in os.listdir(srcpath)]
128 copy_all_entries(entries, targetpath)
129 # options.t is not relevant for directories
130 if options.m:
131 os.chmod(targetpath, options.m)
132 else:
133 if os.path.exists(targetpath):
134 if sys.platform == "win32":
135 mozfile.remove(targetpath)
136 else:
137 os.remove(targetpath)
138 if options.t:
139 shutil.copy2(srcpath, targetpath)
140 else:
141 shutil.copy(srcpath, targetpath)
143 # the last argument is the target directory
144 target = args.pop()
145 # ensure target directory (importantly, we do not apply a mode to the directory
146 # because we want to copy files into it and the mode might be read-only)
147 rv = maybe_create_dir(target, None, True)
148 if rv != 0:
149 return rv
151 copy_all_entries(args, target)
152 return 0
154 # nsinstall as a native command is always UTF-8
157 def nsinstall(argv):
158 return _nsinstall_internal([six.ensure_text(arg, "utf-8") for arg in argv])
161 if __name__ == '__main__':
162 sys.exit(_nsinstall_internal(sys.argv[1:]))