Mercurial > cgi-bin > hgweb.cgi > tincan
comparison 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 |
comparison
equal
deleted
inserted
replaced
48:f89b560b7278 | 49:15f665a620a2 |
---|---|
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 | |
11 | |
12 # V a r i a b l e s | |
13 | |
14 NOT_STATIC = set([".pspx", ".pt", ".py", ".pyc"]) | |
15 MYNAME = os.path.basename(sys.argv[0]) | |
16 WINF = "WEB-INF" | |
17 copyfile = shutil.copy2 | |
18 | |
19 # F u n c t i o n s | |
20 | |
21 def dir_must_exist(d): | |
22 if not os.path.isdir(d): | |
23 sys.stderr.write( | |
24 "{0}: {1!r} - no such directory\n".format(MYNAME, d)) | |
25 sys.exit(2) | |
26 | |
27 def copydir(source, target, exclude=True): | |
28 global args | |
29 for entry in os.listdir(source): | |
30 if exclude and entry == WINF: | |
31 continue | |
32 if entry.startswith(".") or os.path.splitext(entry)[1] in NOT_STATIC: | |
33 continue | |
34 spath = os.path.join(source, entry) | |
35 tpath = os.path.join(target, entry) | |
36 stype = os.stat(spath).st_mode | |
37 if S_ISREG(stype): | |
38 if args.move: | |
39 print("mv", repr(spath), repr(tpath)) | |
40 shutil.move(spath, tpath) | |
41 else: | |
42 print("cp", repr(spath), repr(tpath)) | |
43 copyfile(spath, tpath) | |
44 elif S_ISDIR(stype): | |
45 if not os.path.isdir(tpath): | |
46 print("mkdir", repr(tpath)) | |
47 os.mkdir(tpath) | |
48 copydir(spath, tpath, False) | |
49 else: | |
50 sys.stderr.write( | |
51 "{0}: warning - {1!r} not a file or directory\n".format(spath)) | |
52 | |
53 # M a i n P r o g r a m | |
54 | |
55 parser = ArgumentParser(prog=sys.argv[0], usage="%(prog)s [options] source target") | |
56 group = parser.add_mutually_exclusive_group() | |
57 group.add_argument("-m", "--move", | |
58 action="store_true", help="move files instead of copying them") | |
59 group.add_argument("-n", "--no-preserve", | |
60 action="store_true", help="DO NOT preserve modes, times, etc.") | |
61 parser.add_argument("source", nargs=1) | |
62 parser.add_argument("target", nargs=1) | |
63 args = parser.parse_args(sys.argv[1:]) | |
64 source = args.source[0] | |
65 target = args.target[0] | |
66 | |
67 if args.no_preserve: | |
68 copyfile = shutil.copyfile | |
69 | |
70 dir_must_exist(source) | |
71 dir_must_exist(os.path.join(source, WINF)) | |
72 | |
73 try: | |
74 if not os.path.isdir(target): | |
75 print("mkdir", repr(target)) | |
76 os.mkdir(target) | |
77 copydir(source, target) | |
78 except (OSError, shutil.Error) as e: | |
79 sys.stderr.write("{0}: {1!s}\n".format(MYNAME, e)) | |
80 sys.exit(1) | |
81 | |
82 sys.exit(0) |