]> git.decadent.org.uk Git - dak.git/blob - dak/process_policy.py
fill in headers at reject time
[dak.git] / dak / process_policy.py
1 #!/usr/bin/env python
2 # vim:set et ts=4 sw=4:
3
4 """ Handles packages from policy queues
5
6 @contact: Debian FTP Master <ftpmaster@debian.org>
7 @copyright: 2001, 2002, 2003, 2004, 2005, 2006  James Troup <james@nocrew.org>
8 @copyright: 2009 Joerg Jaspert <joerg@debian.org>
9 @copyright: 2009 Frank Lichtenheld <djpig@debian.org>
10 @copyright: 2009 Mark Hymers <mhy@debian.org>
11 @license: GNU General Public License version 2 or later
12 """
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
27 ################################################################################
28
29 # <mhy> So how do we handle that at the moment?
30 # <stew> Probably incorrectly.
31
32 ################################################################################
33
34 import os
35 import copy
36 import sys
37 import apt_pkg
38
39 from daklib.dbconn import *
40 from daklib.queue import *
41 from daklib import daklog
42 from daklib import utils
43 from daklib.dak_exceptions import CantOpenError, AlreadyLockedError, CantGetLockError
44 from daklib.config import Config
45 from daklib.changesutils import *
46
47 # Globals
48 Options = None
49 Logger = None
50
51 ################################################################################
52
53 def do_comments(dir, srcqueue, opref, npref, line, fn, session):
54     for comm in [ x for x in os.listdir(dir) if x.startswith(opref) ]:
55         lines = open("%s/%s" % (dir, comm)).readlines()
56         if len(lines) == 0 or lines[0] != line + "\n": continue
57         changes_files = [ x for x in os.listdir(".") if x.startswith(comm[7:]+"_")
58                                 and x.endswith(".changes") ]
59         changes_files = sort_changes(changes_files, session)
60         for f in changes_files:
61             print "Processing changes file: %s" % f
62             f = utils.validate_changes_file_arg(f, 0)
63             if not f:
64                 print "Couldn't validate changes file %s" % f
65                 continue
66             fn(f, srcqueue, "".join(lines[1:]), session)
67
68         if opref != npref and not Options["No-Action"]:
69             newcomm = npref + comm[len(opref):]
70             os.rename("%s/%s" % (dir, comm), "%s/%s" % (dir, newcomm))
71
72 ################################################################################
73
74 def comment_accept(changes_file, srcqueue, comments, session):
75     u = Upload()
76     u.pkg.changes_file = changes_file
77     u.load_changes(changes_file)
78     u.update_subst()
79
80     if not Options["No-Action"]:
81         destqueue = get_policy_queue('newstage', session)
82         if changes_to_queue(u, srcqueue, destqueue, session):
83             Logger.log(["Policy Queue ACCEPT: %s:  %s" % (srcqueue.queue_name, u.pkg.changes_file)])
84         else:
85             print "E: Failed to migrate %s" % u.pkg.changes_file
86
87 ################################################################################
88
89 def comment_reject(changes_file, srcqueue, comments, session):
90     u = Upload()
91     u.pkg.changes_file = changes_file
92     u.load_changes(changes_file)
93     u.update_subst()
94
95     u.rejects.append(comments)
96
97     cnf = Config()
98     bcc = "X-DAK: dak process-policy"
99     if cnf.has_key("Dinstall::Bcc"):
100         u.Subst["__BCC__"] = bcc + "\nBcc: %s" % (cnf["Dinstall::Bcc"])
101     else:
102         u.Subst["__BCC__"] = bcc
103
104     if not Options["No-Action"]:
105         u.do_reject(manual=0, reject_message='\n'.join(u.rejects))
106         u.pkg.remove_known_changes(session=session)
107         session.commit()
108
109         Logger.log(["Policy Queue REJECT: %s:  %s" % (srcqueue.queue_name, u.pkg.changes_file)])
110
111
112 ################################################################################
113
114 def main():
115     global Options, Logger
116
117     cnf = Config()
118     session = DBConn().session()
119
120     Arguments = [('h',"help","Process-Policy::Options::Help"),
121                  ('n',"no-action","Process-Policy::Options::No-Action")]
122
123     for i in ["help", "no-action"]:
124         if not cnf.has_key("Process-Policy::Options::%s" % (i)):
125             cnf["Process-Policy::Options::%s" % (i)] = ""
126
127     queue_name = apt_pkg.ParseCommandLine(cnf.Cnf,Arguments,sys.argv)
128
129     if len(queue_name) != 1:
130         print "E: Specify exactly one policy queue"
131         sys.exit(1)
132
133     queue_name = queue_name[0]
134
135     Options = cnf.SubTree("Process-Policy::Options")
136
137     if Options["Help"]:
138         usage()
139
140     if not Options["No-Action"]:
141         try:
142             Logger = daklog.Logger(cnf, "process-policy")
143         except CantOpenError, e:
144             Logger = None
145
146     # Find policy queue
147     session.query(PolicyQueue)
148
149     try:
150         pq = session.query(PolicyQueue).filter_by(queue_name=queue_name).one()
151     except NoResultFound:
152         print "E: Cannot find policy queue %s" % queue_name
153         sys.exit(1)
154
155     commentsdir = os.path.join(pq.path, 'COMMENTS')
156     # The comments stuff relies on being in the right directory
157     os.chdir(pq.path)
158     do_comments(commentsdir, pq, "ACCEPT.", "ACCEPTED.", "OK", comment_accept, session)
159     do_comments(commentsdir, pq, "REJECT.", "REJECTED.", "NOTOK", comment_reject, session)
160
161
162 ################################################################################
163
164 if __name__ == '__main__':
165     main()