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", help="Create a single directory only")
28 p
.add_option("-t", action
="store_true", help="Preserve time stamp")
29 p
.add_option("-m", action
="store", help="Set mode", metavar
="mode")
30 p
.add_option("-d", action
="store_true", help="Create directories in target")
32 "-R", action
="store_true", help="Use relative symbolic links (ignored)"
35 "-L", action
="store", metavar
="linkprefix", help="Link prefix (ignored)"
41 help="Ignore a file when installing a directory recursively.",
44 # The remaining arguments are not used in our tree, thus they're not
46 def BadArg(option
, opt
, value
, parser
):
47 parser
.error("option not supported: {0}".format(opt
))
50 "-C", action
="callback", metavar
="CWD", callback
=BadArg
, help="NOT SUPPORTED"
56 help="Set owner (NOT SUPPORTED)",
63 help="Set group (NOT SUPPORTED)",
67 (options
, args
) = p
.parse_args(argv
)
72 options
.m
= int(options
.m
, 8)
74 sys
.stderr
.write("nsinstall: {0} is not a valid mode\n".format(options
.m
))
77 # just create one directory?
78 def maybe_create_dir(dir, mode
, try_again
):
79 dir = os
.path
.abspath(dir)
80 if os
.path
.exists(dir):
81 if not os
.path
.isdir(dir):
82 print("nsinstall: {0} is not a directory".format(dir), file=sys
.stderr
)
90 os
.makedirs(dir, mode
)
93 except Exception as e
:
94 # We might have hit EEXIST due to a race condition (see bug 463411) -- try again once
96 return maybe_create_dir(dir, mode
, False)
97 print("nsinstall: failed to create directory {0}: {1}".format(dir, e
))
103 options
.X
= [os
.path
.abspath(path
) for path
in options
.X
]
106 return maybe_create_dir(args
[0], options
.m
, True)
108 # nsinstall arg1 [...] directory
110 p
.error("not enough arguments")
112 def copy_all_entries(entries
, target
):
114 e
= os
.path
.abspath(e
)
115 if options
.X
and e
in options
.X
:
118 dest
= os
.path
.join(target
, os
.path
.basename(e
))
119 dest
= os
.path
.abspath(dest
)
120 handleTarget(e
, dest
)
122 os
.chmod(dest
, options
.m
)
126 # we're supposed to create directories
127 def handleTarget(srcpath
, targetpath
):
128 # target directory was already created, just use mkdir
132 # we're supposed to copy files
133 def handleTarget(srcpath
, targetpath
):
134 if os
.path
.isdir(srcpath
):
135 if not os
.path
.exists(targetpath
):
137 entries
= [os
.path
.join(srcpath
, e
) for e
in os
.listdir(srcpath
)]
138 copy_all_entries(entries
, targetpath
)
139 # options.t is not relevant for directories
141 os
.chmod(targetpath
, options
.m
)
143 if os
.path
.exists(targetpath
):
144 if sys
.platform
== "win32":
145 mozfile
.remove(targetpath
)
147 os
.remove(targetpath
)
149 shutil
.copy2(srcpath
, targetpath
)
151 shutil
.copy(srcpath
, targetpath
)
153 # the last argument is the target directory
155 # ensure target directory (importantly, we do not apply a mode to the directory
156 # because we want to copy files into it and the mode might be read-only)
157 rv
= maybe_create_dir(target
, None, True)
161 copy_all_entries(args
, target
)
165 # nsinstall as a native command is always UTF-8
169 return _nsinstall_internal([six
.ensure_text(arg
, "utf-8") for arg
in argv
])
172 if __name__
== "__main__":
173 sys
.exit(_nsinstall_internal(sys
.argv
[1:]))