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