1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import datetime
20 import os
21 import sys
22 import time
23
24 import apt_pkg
25
26 from daklib.command import CommandError, CommandFile
27 from daklib.config import Config
28 from daklib.daklog import Logger
29 from daklib.fstransactions import FilesystemTransaction
30 from daklib.utils import find_next_free
31
32
34 print(
35 """Usage: dak process-commands [-d <directory>] [<command-file>...]
36
37 process command files
38 """
39 )
40
41
43 if argv is None:
44 argv = sys.argv
45
46 arguments = [
47 ("h", "help", "Process-Commands::Options::Help"),
48 ("d", "directory", "Process-Commands::Options::Directory", "HasArg"),
49 ]
50
51 cnf = Config()
52 cnf["Process-Commands::Options::Dummy"] = ""
53 filenames = apt_pkg.parse_commandline(cnf.Cnf, arguments, argv)
54 options = cnf.subtree("Process-Commands::Options")
55
56 if "Help" in options or (len(filenames) == 0 and "Directory" not in options):
57 usage()
58 sys.exit(0)
59
60 log = Logger("command")
61
62 now = datetime.datetime.now()
63 donedir = os.path.join(cnf["Dir::Done"], now.strftime("%Y/%m/%d"))
64 rejectdir = cnf["Dir::Reject"]
65
66 if len(filenames) == 0:
67 cdir = options["Directory"]
68 filenames = [
69 os.path.join(cdir, fn)
70 for fn in os.listdir(cdir)
71 if fn.endswith(".dak-commands")
72 ]
73
74 for fn in filenames:
75 basename = os.path.basename(fn)
76 if not fn.endswith(".dak-commands"):
77 log.log(["unexpected filename", basename])
78 continue
79
80 with open(fn, "rb") as fh:
81 data = fh.read()
82
83 try:
84 command = CommandFile(basename, data, log)
85 command.evaluate()
86 except CommandError as e:
87 created = os.stat(fn).st_mtime
88 now = time.time()
89 too_new = now - created < int(cnf.get("Dinstall::SkipTime", "60"))
90 if too_new:
91 log.log(["skipped (too new)"])
92 continue
93 log.log(["reject", basename, e])
94 except Exception as e:
95 log.log_traceback("Exception while processing %s:" % (basename), e)
96 dst = find_next_free(os.path.join(rejectdir, basename))
97 else:
98 log.log(["done", basename])
99 dst = find_next_free(os.path.join(donedir, basename))
100
101 with FilesystemTransaction() as fs:
102 fs.unlink(fn)
103 fs.create(dst, mode=0o644, text=False).write(data)
104 fs.commit()
105
106 log.close()
107
108
109 if __name__ == "__main__":
110 main()
111