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
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
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",
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
)
58 options
.m
= int(options
.m
, 8)
60 sys
.stderr
.write('nsinstall: {0} is not a valid mode\n'
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
)
77 os
.makedirs(dir, mode
)
80 except Exception as e
:
81 # We might have hit EEXIST due to a race condition (see bug 463411) -- try again once
83 return maybe_create_dir(dir, mode
, False)
85 "nsinstall: failed to create directory {0}: {1}".format(dir, e
))
91 options
.X
= [os
.path
.abspath(path
) for path
in options
.X
]
94 return maybe_create_dir(args
[0], options
.m
, True)
96 # nsinstall arg1 [...] directory
98 p
.error('not enough arguments')
100 def copy_all_entries(entries
, target
):
102 e
= os
.path
.abspath(e
)
103 if options
.X
and e
in options
.X
:
106 dest
= os
.path
.join(target
, os
.path
.basename(e
))
107 dest
= os
.path
.abspath(dest
)
108 handleTarget(e
, dest
)
110 os
.chmod(dest
, options
.m
)
114 # we're supposed to create directories
115 def handleTarget(srcpath
, targetpath
):
116 # target directory was already created, just use mkdir
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
):
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
129 os
.chmod(targetpath
, options
.m
)
131 if os
.path
.exists(targetpath
):
132 if sys
.platform
== "win32":
133 mozfile
.remove(targetpath
)
135 os
.remove(targetpath
)
137 shutil
.copy2(srcpath
, targetpath
)
139 shutil
.copy(srcpath
, targetpath
)
141 # the last argument is the target directory
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)
149 copy_all_entries(args
, target
)
152 # nsinstall as a native command is always UTF-8
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":
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
]
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
]
186 argv
= [unicode(arg
) for arg
in sys
.argv
]
188 sys
.exit(_nsinstall_internal(argv
[1:]))