annotate bin/install-static @ 66:f33cb3e93473 draft

Fix bad log invocations.
author David Barts <n5jrn@me.com>
date Fri, 12 Jul 2019 20:27:52 -0700
parents d59811b95a62
children
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
59
60907204a265 Support case-insensitive filesystems properly.
David Barts <n5jrn@me.com>
parents: 52
diff changeset
11 from tincan import _casef
49
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
12
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
13 # 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
14
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
15 NOT_STATIC = set([".pspx", ".pt", ".py", ".pyc"])
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
16 MYNAME = os.path.basename(sys.argv[0])
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
17 WINF = "WEB-INF"
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
18 copyfile = shutil.copy2
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
19
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
20 # 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
21
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 def dir_must_exist(d):
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
23 if not os.path.isdir(d):
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 sys.stderr.write(
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 "{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
26 sys.exit(2)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
27
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
28 def copydir(source, target, exclude=True):
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
29 for entry in os.listdir(source):
59
60907204a265 Support case-insensitive filesystems properly.
David Barts <n5jrn@me.com>
parents: 52
diff changeset
30 if exclude and _casef(entry, "upper") == WINF:
49
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
31 continue
59
60907204a265 Support case-insensitive filesystems properly.
David Barts <n5jrn@me.com>
parents: 52
diff changeset
32 if entry.startswith(".") or _casef(os.path.splitext(entry)[1]) in NOT_STATIC:
49
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
33 continue
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
34 spath = os.path.join(source, entry)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
35 tpath = os.path.join(target, entry)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
36 stype = os.stat(spath).st_mode
50
ccd52fb45cff Fix some bugs.
David Barts <n5jrn@me.com>
parents: 49
diff changeset
37 try:
ccd52fb45cff Fix some bugs.
David Barts <n5jrn@me.com>
parents: 49
diff changeset
38 ttype = os.stat(tpath).st_mode
ccd52fb45cff Fix some bugs.
David Barts <n5jrn@me.com>
parents: 49
diff changeset
39 except FileNotFoundError:
ccd52fb45cff Fix some bugs.
David Barts <n5jrn@me.com>
parents: 49
diff changeset
40 ttype = None
49
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
41 if S_ISREG(stype):
50
ccd52fb45cff Fix some bugs.
David Barts <n5jrn@me.com>
parents: 49
diff changeset
42 if ttype is not None and not S_ISREG(ttype):
ccd52fb45cff Fix some bugs.
David Barts <n5jrn@me.com>
parents: 49
diff changeset
43 sys.stderr.write(
ccd52fb45cff Fix some bugs.
David Barts <n5jrn@me.com>
parents: 49
diff changeset
44 "{0}: {1!r} not a file\n".format(MYNAME, tpath))
ccd52fb45cff Fix some bugs.
David Barts <n5jrn@me.com>
parents: 49
diff changeset
45 sys.exit(1)
49
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
46 if args.move:
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 print("mv", repr(spath), repr(tpath))
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
48 shutil.move(spath, tpath)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 else:
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
50 print("cp", repr(spath), repr(tpath))
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
51 copyfile(spath, tpath)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
52 elif S_ISDIR(stype):
52
243603aaab7e Clean up the code a bit.
David Barts <n5jrn@me.com>
parents: 51
diff changeset
53 if ttype is None:
49
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
54 print("mkdir", repr(tpath))
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
55 os.mkdir(tpath)
52
243603aaab7e Clean up the code a bit.
David Barts <n5jrn@me.com>
parents: 51
diff changeset
56 elif not S_ISDIR(ttype):
243603aaab7e Clean up the code a bit.
David Barts <n5jrn@me.com>
parents: 51
diff changeset
57 sys.stderr.write(
243603aaab7e Clean up the code a bit.
David Barts <n5jrn@me.com>
parents: 51
diff changeset
58 "{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
59 sys.exit(1)
49
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
60 copydir(spath, tpath, False)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 else:
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
62 sys.stderr.write(
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 "{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
64
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 # 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
66
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
67 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
68 group = parser.add_mutually_exclusive_group()
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
69 group.add_argument("-m", "--move",
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
70 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
71 group.add_argument("-n", "--no-preserve",
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 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
73 parser.add_argument("source", nargs=1)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
74 parser.add_argument("target", nargs=1)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
75 args = parser.parse_args(sys.argv[1:])
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
76 source = args.source[0]
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
77 target = args.target[0]
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
78
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 if args.no_preserve:
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
80 copyfile = shutil.copyfile
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
81
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
82 dir_must_exist(source)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
83 dir_must_exist(os.path.join(source, WINF))
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
84
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
85 try:
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
86 if not os.path.isdir(target):
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
87 print("mkdir", repr(target))
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
88 os.mkdir(target)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
89 copydir(source, target)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
90 except (OSError, shutil.Error) as e:
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
91 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
92 sys.exit(1)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
93
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
94 sys.exit(0)