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
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
41 def BadArg(option
, opt
, value
, parser
):
42 parser
.error('option not supported: %s' % opt
)
44 p
.add_option('-C', action
="callback", metavar
="CWD",
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
)
57 options
.m
= int(options
.m
, 8)
59 sys
.stderr
.write('nsinstall: ' + options
.m
+ ' is not a valid mode\n')
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)
75 os
.makedirs(dir, mode
)
79 # We might have hit EEXIST due to a race condition (see bug 463411) -- try again once
81 return maybe_create_dir(dir, mode
, False)
82 print >> sys
.stderr
, ("nsinstall: failed to create directory %s: %s" % (dir, e
))
88 options
.X
= [os
.path
.abspath(p
) for p
in options
.X
]
91 return maybe_create_dir(args
[0], options
.m
, True)
93 # nsinstall arg1 [...] directory
95 p
.error('not enough arguments')
97 def copy_all_entries(entries
, target
):
99 e
= os
.path
.abspath(e
)
100 if options
.X
and e
in options
.X
:
103 dest
= os
.path
.join(target
, os
.path
.basename(e
))
104 dest
= os
.path
.abspath(dest
)
105 handleTarget(e
, dest
)
107 os
.chmod(dest
, options
.m
)
111 # we're supposed to create directories
112 def handleTarget(srcpath
, targetpath
):
113 # target directory was already created, just use mkdir
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
):
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
125 os
.chmod(targetpath
, options
.m
)
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
)
132 shutil
.copy2(srcpath
, targetpath
)
134 shutil
.copy(srcpath
, targetpath
)
136 # the last argument is the target directory
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)
144 copy_all_entries(args
, target
)
147 # nsinstall as a native command is always UTF-8
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":
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
]
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
]
177 argv
= [unicode(arg
) for arg
in sys
.argv
]
179 sys
.exit(_nsinstall_internal(argv
[1:]))