]> git.decadent.org.uk Git - dak.git/blob - dak/process_commands.py
queued: remove buildd-alpha-keyring.gpg
[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 import time
24
25 from daklib.config import Config
26 from daklib.command import CommandError, CommandFile
27 from daklib.daklog import Logger
28 from daklib.fstransactions import FilesystemTransaction
29 from daklib.gpg import GpgException
30 from daklib.utils import find_next_free
31
32 def usage():
33     print """Usage: dak process-commands [-d <directory>] [<command-file>...]
34
35 process command files
36 """
37
38 def main(argv=None):
39     if argv is None:
40         argv = sys.argv
41
42     arguments = [('h', 'help', 'Process-Commands::Options::Help'),
43                  ('d', 'directory', 'Process-Commands::Options::Directory', 'HasArg')]
44
45     cnf = Config()
46     cnf['Process-Commands::Options::Dummy'] = ''
47     filenames = apt_pkg.parse_commandline(cnf.Cnf, arguments, argv)
48     options = cnf.subtree('Process-Commands::Options')
49
50     if 'Help' in options or (len(filenames) == 0 and 'Directory' not in options):
51         usage()
52         sys.exit(0)
53
54     log = Logger('command')
55
56     now = datetime.datetime.now()
57     donedir = os.path.join(cnf['Dir::Done'], now.strftime('%Y/%m/%d'))
58     rejectdir = cnf['Dir::Reject']
59
60     if len(filenames) == 0:
61         filenames = [ fn for fn in os.listdir(options['Directory']) if fn.endswith('.dak-commands') ]
62
63     for fn in filenames:
64         basename = os.path.basename(fn)
65         if not fn.endswith('.dak-commands'):
66             log.log(['unexpected filename', basename])
67             continue
68
69         with open(fn, 'r') as fh:
70             data = fh.read()
71
72         try:
73             command = CommandFile(basename, data, log)
74             command.evaluate()
75         except:
76             created = os.stat(fn).st_mtime
77             now = time.time()
78             too_new = (now - created < int(cnf.get('Dinstall::SkipTime', '60')))
79             if too_new:
80                 log.log(['skipped (too new)'])
81                 continue
82             log.log(['reject', basename])
83             dst = find_next_free(os.path.join(rejectdir, basename))
84         else:
85             log.log(['done', basename])
86             dst = find_next_free(os.path.join(donedir, basename))
87
88         with FilesystemTransaction() as fs:
89             fs.unlink(fn)
90             fs.create(dst, mode=0o644).write(data)
91             fs.commit()
92
93     log.close()
94
95 if __name__ == '__main__':
96     main()