Mercurial > cgi-bin > hgweb.cgi > tincan
annotate bin/install-static @ 65:d59811b95a62 draft
Package-ize it.
author | David Barts <n5jrn@me.com> |
---|---|
date | Thu, 04 Jul 2019 08:52:15 -0700 |
parents | install-static@60907204a265 |
children |
rev | line source |
---|---|
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 | |
59
60907204a265
Support case-insensitive filesystems properly.
David Barts <n5jrn@me.com>
parents:
52
diff
changeset
|
11 from tincan import _casef |
49 | 12 |
13 # V a r i a b l e s | |
14 | |
15 NOT_STATIC = set([".pspx", ".pt", ".py", ".pyc"]) | |
16 MYNAME = os.path.basename(sys.argv[0]) | |
17 WINF = "WEB-INF" | |
18 copyfile = shutil.copy2 | |
19 | |
20 # F u n c t i o n s | |
21 | |
22 def dir_must_exist(d): | |
23 if not os.path.isdir(d): | |
24 sys.stderr.write( | |
25 "{0}: {1!r} - no such directory\n".format(MYNAME, d)) | |
26 sys.exit(2) | |
27 | |
28 def copydir(source, target, exclude=True): | |
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 | 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 | 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): | |
52 | 53 if ttype is None: |
49 | 54 print("mkdir", repr(tpath)) |
55 os.mkdir(tpath) | |
52 | 56 elif not S_ISDIR(ttype): |
57 sys.stderr.write( | |
58 "{0}: {1!r} not a directory\n".format(MYNAME, tpath)) | |
59 sys.exit(1) | |
49 | 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) |