annotate install-static @ 51:caed7c5e8318 draft

Another bug fix.
author David Barts <n5jrn@me.com>
date Thu, 30 May 2019 22:02:02 -0700
parents ccd52fb45cff
children 243603aaab7e
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):
51
caed7c5e8318 Another bug fix.
David Barts <n5jrn@me.com>
parents: 50
diff changeset
52 if ttype is not None:
caed7c5e8318 Another bug fix.
David Barts <n5jrn@me.com>
parents: 50
diff changeset
53 if not S_ISDIR(ttype):
caed7c5e8318 Another bug fix.
David Barts <n5jrn@me.com>
parents: 50
diff changeset
54 sys.stderr.write(
caed7c5e8318 Another bug fix.
David Barts <n5jrn@me.com>
parents: 50
diff changeset
55 "{0}: {1!r} not a directory\n".format(MYNAME, tpath))
caed7c5e8318 Another bug fix.
David Barts <n5jrn@me.com>
parents: 50
diff changeset
56 sys.exit(1)
caed7c5e8318 Another bug fix.
David Barts <n5jrn@me.com>
parents: 50
diff changeset
57 else:
49
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 print("mkdir", repr(tpath))
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
59 os.mkdir(tpath)
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)