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):
|
52
|
52 if ttype is None:
|
49
|
53 print("mkdir", repr(tpath))
|
|
54 os.mkdir(tpath)
|
52
|
55 elif not S_ISDIR(ttype):
|
|
56 sys.stderr.write(
|
|
57 "{0}: {1!r} not a directory\n".format(MYNAME, tpath))
|
|
58 sys.exit(1)
|
49
|
59 copydir(spath, tpath, False)
|
|
60 else:
|
|
61 sys.stderr.write(
|
|
62 "{0}: warning - {1!r} not a file or directory\n".format(spath))
|
|
63
|
|
64 # M a i n P r o g r a m
|
|
65
|
|
66 parser = ArgumentParser(prog=sys.argv[0], usage="%(prog)s [options] source target")
|
|
67 group = parser.add_mutually_exclusive_group()
|
|
68 group.add_argument("-m", "--move",
|
|
69 action="store_true", help="move files instead of copying them")
|
|
70 group.add_argument("-n", "--no-preserve",
|
|
71 action="store_true", help="DO NOT preserve modes, times, etc.")
|
|
72 parser.add_argument("source", nargs=1)
|
|
73 parser.add_argument("target", nargs=1)
|
|
74 args = parser.parse_args(sys.argv[1:])
|
|
75 source = args.source[0]
|
|
76 target = args.target[0]
|
|
77
|
|
78 if args.no_preserve:
|
|
79 copyfile = shutil.copyfile
|
|
80
|
|
81 dir_must_exist(source)
|
|
82 dir_must_exist(os.path.join(source, WINF))
|
|
83
|
|
84 try:
|
|
85 if not os.path.isdir(target):
|
|
86 print("mkdir", repr(target))
|
|
87 os.mkdir(target)
|
|
88 copydir(source, target)
|
|
89 except (OSError, shutil.Error) as e:
|
|
90 sys.stderr.write("{0}: {1!s}\n".format(MYNAME, e))
|
|
91 sys.exit(1)
|
|
92
|
|
93 sys.exit(0)
|