]> git.decadent.org.uk Git - dak.git/blob - dak/process_commands.py
dak/process_commands.py: typo
[dak.git] / dak / process_commands.py
1 #! /usr/bin/env python
2 #
3 # Copyright (C) 2012, Ansgar Burchardt <ansgar@debian.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 import apt_pkg
20 import datetime
21 import os
22 import sys
23
24 from daklib.config import Config
25 from daklib.command import CommandError, CommandFile
26 from daklib.daklog import Logger
27 from daklib.fstransactions import FilesystemTransaction
28 from daklib.utils import find_next_free
29
30 def usage():
31     print """Usage: dak process-commands [-d <directory>] [<command-file>...]
32
33 process command files
34 """
35
36 def main(argv=None):
37     if argv is None:
38         argv = sys.argv
39
40     arguments = [('h', 'help', 'Process-Commands::Options::Help'),
41                  ('d', 'directory', 'Process-Commands::Options::Directory', 'HasArg')]
42
43     cnf = Config()
44     cnf['Process-Commands::Options::Dummy'] = ''
45     filenames = apt_pkg.parse_commandline(cnf.Cnf, arguments, argv)
46     options = cnf.subtree('Process-Commands::Options')
47
48     if 'Help' in options or (len(filenames) == 0 and 'Directory' not in options):
49         usage()
50         sys.exit(0)
51
52     log = Logger('command')
53
54     now = datetime.datetime.now()
55     donedir = os.path.join(cnf['Dir::Done'], now.strftime('%Y/%m/%d'))
56     rejectdir = cnf['Dir::Reject']
57
58     if len(filenames) == 0:
59         filenames = [ fn for fn in os.listdir(options['Directory']) if fn.endswith('.dak-commands') ]
60
61     for fn in filenames:
62         basename = os.path.basename(fn)
63         if not fn.endswith('.dak-commands'):
64             log.log(['unexpected filename', basename])
65             continue
66
67         command = CommandFile(fn, log)
68         if command.evaluate():
69             log.log(['moving to done', basename])
70             dst = find_next_free(os.path.join(donedir, basename))
71         else:
72             log.log(['moving to reject', basename])
73             dst = find_next_free(os.path.join(rejectdir, basename))
74
75         with FilesystemTransaction() as fs:
76             fs.move(fn, dst, mode=0o644)
77             fs.commit()
78
79     log.close()
80
81 if __name__ == '__main__':
82     main()