49
|
1 #!/usr/bin/env python3
|
|
2 # Install the static files from the source webapp tree into the target
|
|
3 # directory.
|
|
4
|
|
5 # I m p o r t s
|
|
6
|
|
7 import os, sys
|
|
8 from argparse import ArgumentParser
|
|
9 import shutil
|
|
10 from stat import S_ISDIR, S_ISREG
|
|
11
|
|
12 # V a r i a b l e s
|
|
13
|
|
14 NOT_STATIC = set([".pspx", ".pt", ".py", ".pyc"])
|
|
15 MYNAME = os.path.basename(sys.argv[0])
|
|
16 WINF = "WEB-INF"
|
|
17 copyfile = shutil.copy2
|
|
18
|
|
19 # F u n c t i o n s
|
|
20
|
|
21 def dir_must_exist(d):
|
|
22 if not os.path.isdir(d):
|
|
23 sys.stderr.write(
|
|
24 "{0}: {1!r} - no such directory\n".format(MYNAME, d))
|
|
25 sys.exit(2)
|
|
26
|
|
27 def copydir(source, target, exclude=True):
|
|
28 for entry in os.listdir(source):
|
|
29 if exclude and entry == WINF:
|
|
30 continue
|
|
31 if entry.startswith(".") or os.path.splitext(entry)[1] in NOT_STATIC:
|
|
32 continue
|
|
33 spath = os.path.join(source, entry)
|
|
34 tpath = os.path.join(target, entry)
|
|
35 stype = os.stat(spath).st_mode
|
50
|
36 try:
|
|
37 ttype = os.stat(tpath).st_mode
|
|
38 except FileNotFoundError:
|
|
39 ttype = None
|
49
|
40 if S_ISREG(stype):
|
50
|
41 if ttype is not None and not S_ISREG(ttype):
|
|
42 sys.stderr.write(
|
|
43 "{0}: {1!r} not a file\n".format(MYNAME, tpath))
|
|
44 sys.exit(1)
|
49
|
45 if args.move:
|
|
46 print("mv", repr(spath), repr(tpath))
|
|
47 shutil.move(spath, tpath)
|
|
48 else:
|
|
49 print("cp", repr(spath), repr(tpath))
|
|
50 copyfile(spath, tpath)
|
|
51 elif S_ISDIR(stype):
|
51
|
52 if ttype is not None:
|
|
53 if not S_ISDIR(ttype):
|
|
54 sys.stderr.write(
|
|
55 "{0}: {1!r} not a directory\n".format(MYNAME, tpath))
|
|
56 sys.exit(1)
|
|
57 else:
|
49
|
58 print("mkdir", repr(tpath))
|
|
59 os.mkdir(tpath)
|
|
60 copydir(spath, tpath, False)
|
|
61 else:
|
|
62 sys.stderr.write(
|
|
63 "{0}: warning - {1!r} not a file or directory\n".format(spath))
|
|
64
|
|
65 # M a i n P r o g r a m
|
|
66
|
|
67 parser = ArgumentParser(prog=sys.argv[0], usage="%(prog)s [options] source target")
|
|
68 group = parser.add_mutually_exclusive_group()
|
|
69 group.add_argument("-m", "--move",
|
|
70 action="store_true", help="move files instead of copying them")
|
|
71 group.add_argument("-n", "--no-preserve",
|
|
72 action="store_true", help="DO NOT preserve modes, times, etc.")
|
|
73 parser.add_argument("source", nargs=1)
|
|
74 parser.add_argument("target", nargs=1)
|
|
75 args = parser.parse_args(sys.argv[1:])
|
|
76 source = args.source[0]
|
|
77 target = args.target[0]
|
|
78
|
|
79 if args.no_preserve:
|
|
80 copyfile = shutil.copyfile
|
|
81
|
|
82 dir_must_exist(source)
|
|
83 dir_must_exist(os.path.join(source, WINF))
|
|
84
|
|
85 try:
|
|
86 if not os.path.isdir(target):
|
|
87 print("mkdir", repr(target))
|
|
88 os.mkdir(target)
|
|
89 copydir(source, target)
|
|
90 except (OSError, shutil.Error) as e:
|
|
91 sys.stderr.write("{0}: {1!s}\n".format(MYNAME, e))
|
|
92 sys.exit(1)
|
|
93
|
|
94 sys.exit(0)
|