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 global args
|
|
29 for entry in os.listdir(source):
|
|
30 if exclude and entry == WINF:
|
|
31 continue
|
|
32 if entry.startswith(".") or os.path.splitext(entry)[1] in NOT_STATIC:
|
|
33 continue
|
|
34 spath = os.path.join(source, entry)
|
|
35 tpath = os.path.join(target, entry)
|
|
36 stype = os.stat(spath).st_mode
|
50
|
37 try:
|
|
38 ttype = os.stat(tpath).st_mode
|
|
39 except FileNotFoundError:
|
|
40 ttype = None
|
49
|
41 if S_ISREG(stype):
|
50
|
42 if ttype is not None and not S_ISREG(ttype):
|
|
43 sys.stderr.write(
|
|
44 "{0}: {1!r} not a file\n".format(MYNAME, tpath))
|
|
45 sys.exit(1)
|
49
|
46 if args.move:
|
|
47 print("mv", repr(spath), repr(tpath))
|
|
48 shutil.move(spath, tpath)
|
|
49 else:
|
|
50 print("cp", repr(spath), repr(tpath))
|
|
51 copyfile(spath, tpath)
|
|
52 elif S_ISDIR(stype):
|
50
|
53 if ttype is not None and not S_ISDIR(ttype):
|
|
54 sys.stderr.write(
|
|
55 "{0}: {1!r} not a directory\n".format(MYNAME, tpath))
|
|
56 sys.exit(1)
|
|
57 if not S_ISDIR(ttype):
|
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)
|