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
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
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",
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
)
60 options
.m
= int(options
.m
, 8)
62 sys
.stderr
.write('nsinstall: {0} is not a valid mode\n'
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
)
79 os
.makedirs(dir, mode
)
82 except Exception as e
:
83 # We might have hit EEXIST due to a race condition (see bug 463411) -- try again once
85 return maybe_create_dir(dir, mode
, False)
87 "nsinstall: failed to create directory {0}: {1}".format(dir, e
))
93 options
.X
= [os
.path
.abspath(path
) for path
in options
.X
]
96 return maybe_create_dir(args
[0], options
.m
, True)
98 # nsinstall arg1 [...] directory
100 p
.error('not enough arguments')
102 def copy_all_entries(entries
, target
):
104 e
= os
.path
.abspath(e
)
105 if options
.X
and e
in options
.X
:
108 dest
= os
.path
.join(target
, os
.path
.basename(e
))
109 dest
= os
.path
.abspath(dest
)
110 handleTarget(e
, dest
)
112 os
.chmod(dest
, options
.m
)
116 # we're supposed to create directories
117 def handleTarget(srcpath
, targetpath
):
118 # target directory was already created, just use mkdir
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
):
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
131 os
.chmod(targetpath
, options
.m
)
133 if os
.path
.exists(targetpath
):
134 if sys
.platform
== "win32":
135 mozfile
.remove(targetpath
)
137 os
.remove(targetpath
)
139 shutil
.copy2(srcpath
, targetpath
)
141 shutil
.copy(srcpath
, targetpath
)
143 # the last argument is the target directory
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)
151 copy_all_entries(args
, target
)
154 # nsinstall as a native command is always UTF-8
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:]))