annotate install-static @ 58:e08e24707da1 draft

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