annotate install-static @ 49:15f665a620a2 draft

This might be useful, so add it.
author David Barts <n5jrn@me.com>
date Thu, 30 May 2019 21:44:26 -0700
parents
children ccd52fb45cff
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 global args
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
29 for entry in os.listdir(source):
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
30 if exclude and entry == WINF:
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
31 continue
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
32 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
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
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
37 if S_ISREG(stype):
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
38 if args.move:
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
39 print("mv", repr(spath), repr(tpath))
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
40 shutil.move(spath, tpath)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
41 else:
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 print("cp", repr(spath), repr(tpath))
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
43 copyfile(spath, tpath)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
44 elif S_ISDIR(stype):
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
45 if not os.path.isdir(tpath):
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
46 print("mkdir", repr(tpath))
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 os.mkdir(tpath)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
48 copydir(spath, tpath, False)
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 sys.stderr.write(
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
51 "{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
52
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
53 # 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
54
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
55 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
56 group = parser.add_mutually_exclusive_group()
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
57 group.add_argument("-m", "--move",
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 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
59 group.add_argument("-n", "--no-preserve",
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
60 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
61 parser.add_argument("source", nargs=1)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
62 parser.add_argument("target", nargs=1)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 args = parser.parse_args(sys.argv[1:])
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
64 source = args.source[0]
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 target = args.target[0]
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 if args.no_preserve:
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
68 copyfile = shutil.copyfile
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
69
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
70 dir_must_exist(source)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 dir_must_exist(os.path.join(source, WINF))
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
72
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
73 try:
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
74 if not os.path.isdir(target):
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
75 print("mkdir", repr(target))
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
76 os.mkdir(target)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
77 copydir(source, target)
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
78 except (OSError, shutil.Error) as e:
15f665a620a2 This might be useful, so add it.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 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
80 sys.exit(1)
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 sys.exit(0)