49 |
import sys |
import sys |
50 |
import shutil |
import shutil |
51 |
|
|
52 |
usage = "usage: %prog [options] arg1 [arg2 ...] target-directory" |
def nsinstall(argv): |
53 |
p = OptionParser(usage=usage) |
usage = "usage: %prog [options] arg1 [arg2 ...] target-directory" |
54 |
|
p = OptionParser(usage=usage) |
55 |
|
|
56 |
|
p.add_option('-D', action="store_true", |
57 |
|
help="Create a single directory only") |
58 |
|
p.add_option('-t', action="store_true", |
59 |
|
help="Preserve time stamp") |
60 |
|
p.add_option('-m', action="store", |
61 |
|
help="Set mode", metavar="mode") |
62 |
|
p.add_option('-d', action="store_true", |
63 |
|
help="Create directories in target") |
64 |
|
p.add_option('-R', action="store_true", |
65 |
|
help="Use relative symbolic links (ignored)") |
66 |
|
p.add_option('-l', action="store_true", |
67 |
|
help="Create link (ignored)") |
68 |
|
p.add_option('-L', action="store", metavar="linkprefix", |
69 |
|
help="Link prefix (ignored)") |
70 |
|
|
71 |
|
# The remaining arguments are not used in our tree, thus they're not |
72 |
|
# implented. |
73 |
|
def BadArg(option, opt, value, parser): |
74 |
|
parser.error('option not supported: %s' % opt) |
75 |
|
|
76 |
|
p.add_option('-C', action="callback", metavar="CWD", |
77 |
|
callback=BadArg, |
78 |
|
help="NOT SUPPORTED") |
79 |
|
p.add_option('-o', action="callback", callback=BadArg, |
80 |
|
help="Set owner (NOT SUPPORTED)", metavar="owner") |
81 |
|
p.add_option('-g', action="callback", callback=BadArg, |
82 |
|
help="Set group (NOT SUPPORTED)", metavar="group") |
83 |
|
|
84 |
|
(options, args) = p.parse_args(argv) |
85 |
|
|
|
p.add_option('-D', action="store_true", |
|
|
help="Create a single directory only") |
|
|
p.add_option('-t', action="store_true", |
|
|
help="Preserve time stamp") |
|
|
p.add_option('-m', action="store", |
|
|
help="Set mode", metavar="mode") |
|
|
p.add_option('-d', action="store_true", |
|
|
help="Create directories in target") |
|
|
p.add_option('-R', action="store_true", |
|
|
help="Use relative symbolic links (ignored)") |
|
|
p.add_option('-l', action="store_true", |
|
|
help="Create link (ignored)") |
|
|
p.add_option('-L', action="store", metavar="linkprefix", |
|
|
help="Link prefix (ignored)") |
|
|
|
|
|
# The remaining arguments are not used in our tree, thus they're not |
|
|
# implented. |
|
|
def BadArg(option, opt, value, parser): |
|
|
parser.error('option not supported: %s' % opt) |
|
|
|
|
|
p.add_option('-C', action="callback", metavar="CWD", |
|
|
callback=BadArg, |
|
|
help="NOT SUPPORTED") |
|
|
p.add_option('-o', action="callback", callback=BadArg, |
|
|
help="Set owner (NOT SUPPORTED)", metavar="owner") |
|
|
p.add_option('-g', action="callback", callback=BadArg, |
|
|
help="Set group (NOT SUPPORTED)", metavar="group") |
|
|
|
|
|
(options, args) = p.parse_args() |
|
|
|
|
|
if options.m: |
|
|
# mode is specified |
|
|
try: |
|
|
options.m = int(options.m, 8) |
|
|
except: |
|
|
sys.stderr.write('nsinstall: ' + options.m + ' is not a valid mode\n') |
|
|
sys.exit(1) |
|
|
|
|
|
# just create one directory? |
|
|
if options.D: |
|
|
if len(args) != 1: |
|
|
sys.exit(1) |
|
|
if os.path.exists(args[0]): |
|
|
if not os.path.isdir(args[0]): |
|
|
sys.stderr.write('nsinstall: ' + args[0] + ' is not a directory\n') |
|
|
sys.exit(1) |
|
|
if options.m: |
|
|
os.chmod(args[0], options.m) |
|
|
sys.exit() |
|
86 |
if options.m: |
if options.m: |
87 |
os.makedirs(args[0], options.m) |
# mode is specified |
88 |
|
try: |
89 |
|
options.m = int(options.m, 8) |
90 |
|
except: |
91 |
|
sys.stderr.write('nsinstall: ' + options.m + ' is not a valid mode\n') |
92 |
|
return 1 |
93 |
|
|
94 |
|
# just create one directory? |
95 |
|
if options.D: |
96 |
|
if len(args) != 1: |
97 |
|
return 1 |
98 |
|
if os.path.exists(args[0]): |
99 |
|
if not os.path.isdir(args[0]): |
100 |
|
sys.stderr.write('nsinstall: ' + args[0] + ' is not a directory\n') |
101 |
|
sys.exit(1) |
102 |
|
if options.m: |
103 |
|
os.chmod(args[0], options.m) |
104 |
|
sys.exit() |
105 |
|
if options.m: |
106 |
|
os.makedirs(args[0], options.m) |
107 |
|
else: |
108 |
|
os.makedirs(args[0]) |
109 |
|
return 0 |
110 |
|
|
111 |
|
# nsinstall arg1 [...] directory |
112 |
|
if len(args) < 2: |
113 |
|
p.error('not enough arguments') |
114 |
|
|
115 |
|
def copy_all_entries(entries, target): |
116 |
|
for e in entries: |
117 |
|
dest = os.path.join(target, |
118 |
|
os.path.basename(os.path.normpath(e))) |
119 |
|
handleTarget(e, dest) |
120 |
|
if options.m: |
121 |
|
os.chmod(dest, options.m) |
122 |
|
|
123 |
|
# set up handler |
124 |
|
if options.d: |
125 |
|
# we're supposed to create directories |
126 |
|
def handleTarget(srcpath, targetpath): |
127 |
|
# target directory was already created, just use mkdir |
128 |
|
os.mkdir(targetpath) |
129 |
else: |
else: |
130 |
os.makedirs(args[0]) |
# we're supposed to copy files |
131 |
sys.exit() |
def handleTarget(srcpath, targetpath): |
132 |
|
if os.path.isdir(srcpath): |
133 |
|
if not os.path.exists(targetpath): |
134 |
|
os.mkdir(targetpath) |
135 |
|
entries = [os.path.join(srcpath, e) for e in os.listdir(srcpath)] |
136 |
|
copy_all_entries(entries, targetpath) |
137 |
|
# options.t is not relevant for directories |
138 |
|
if options.m: |
139 |
|
os.chmod(targetpath, options.m) |
140 |
|
elif options.t: |
141 |
|
shutil.copy2(srcpath, targetpath) |
142 |
|
else: |
143 |
|
shutil.copy(srcpath, targetpath) |
144 |
|
|
145 |
|
# the last argument is the target directory |
146 |
|
target = args.pop() |
147 |
|
# ensure target directory |
148 |
|
if not os.path.isdir(target): |
149 |
|
os.makedirs(target) |
150 |
|
|
151 |
# nsinstall arg1 [...] directory |
copy_all_entries(args, target) |
152 |
if len(args) < 2: |
return 0 |
|
p.error('not enough arguments') |
|
|
|
|
|
# set up handler |
|
|
if options.d: |
|
|
# we're supposed to create directories |
|
|
def handleTarget(srcpath, targetpath): |
|
|
# target directory was already created, just use mkdir |
|
|
os.mkdir(dest) |
|
|
else: |
|
|
# we're supposed to copy files |
|
|
def handleTarget(srcpath, targetpath): |
|
|
if options.t: |
|
|
shutil.copy2(srcpath, targetpath) |
|
|
else: |
|
|
shutil.copy(srcpath, targetpath) |
|
153 |
|
|
154 |
# the last argument is the target directory |
if __name__ == '__main__': |
155 |
target = args.pop() |
sys.exit(nsinstall(sys.argv[1:])) |
|
# ensure target directory |
|
|
if not os.path.isdir(target): |
|
|
os.makedirs(target) |
|
|
|
|
|
for f in args: |
|
|
dest = os.path.join(target, |
|
|
os.path.basename(os.path.normpath(f))) |
|
|
handleTarget(f, dest) |
|
|
if options.m: |
|
|
os.chmod(dest, options.m) |
|