]> git.decadent.org.uk Git - dak.git/blob - dak/process_policy.py
First implementation of make-changelog command
[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 len(changes_files) and not Options["No-Action"]:
69             store_changelog(changes_files[0], srcqueue)
70
71         if opref != npref and not Options["No-Action"]:
72             newcomm = npref + comm[len(opref):]
73             os.rename("%s/%s" % (dir, comm), "%s/%s" % (dir, newcomm))
74
75 ################################################################################
76
77 def comment_accept(changes_file, srcqueue, comments, session):
78     u = Upload()
79     u.pkg.changes_file = changes_file
80     u.load_changes(changes_file)
81     u.update_subst()
82
83     if not Options["No-Action"]:
84         destqueue = get_policy_queue('newstage', session)
85         if changes_to_queue(u, srcqueue, destqueue, session):
86             print "  ACCEPT"
87             Logger.log(["Policy Queue ACCEPT: %s:  %s" % (srcqueue.queue_name, u.pkg.changes_file)])
88         else:
89             print "E: Failed to migrate %s" % u.pkg.changes_file
90
91 ################################################################################
92
93 def comment_reject(changes_file, srcqueue, comments, session):
94     u = Upload()
95     u.pkg.changes_file = changes_file
96     u.load_changes(changes_file)
97     u.update_subst()
98
99     u.rejects.append(comments)
100
101     cnf = Config()
102     bcc = "X-DAK: dak process-policy"
103     if cnf.has_key("Dinstall::Bcc"):
104         u.Subst["__BCC__"] = bcc + "\nBcc: %s" % (cnf["Dinstall::Bcc"])
105     else:
106         u.Subst["__BCC__"] = bcc
107
108     if not Options["No-Action"]:
109         u.do_reject(manual=0, reject_message='\n'.join(u.rejects))
110         u.pkg.remove_known_changes(session=session)
111         session.commit()
112
113         print "  REJECT"
114         Logger.log(["Policy Queue REJECT: %s:  %s" % (srcqueue.queue_name, u.pkg.changes_file)])
115
116
117 ################################################################################
118
119 def store_changelog(changes_file, srcqueue):
120     Cnf = Config()
121     u = Upload()
122     u.pkg.changes_file = os.path.join(Cnf['Dir::Queue::Newstage'], changes_file)
123     u.load_changes(u.pkg.changes_file)
124     u.update_subst()
125     query = """INSERT INTO changelogs (source, version, suite, changelog)
126                VALUES (:source, :version, :suite, :changelog)"""
127     session = DBConn().session()
128     session.execute(query, {'source': u.pkg.changes['source'], 'version': u.pkg.changes['version'], \
129                     'suite': srcqueue.queue_name, 'changelog': u.pkg.changes['changes']})
130     session.commit()
131
132 ################################################################################
133
134 def main():
135     global Options, Logger
136
137     cnf = Config()
138     session = DBConn().session()
139
140     Arguments = [('h',"help","Process-Policy::Options::Help"),
141                  ('n',"no-action","Process-Policy::Options::No-Action")]
142
143     for i in ["help", "no-action"]:
144         if not cnf.has_key("Process-Policy::Options::%s" % (i)):
145             cnf["Process-Policy::Options::%s" % (i)] = ""
146
147     queue_name = apt_pkg.ParseCommandLine(cnf.Cnf,Arguments,sys.argv)
148
149     if len(queue_name) != 1:
150         print "E: Specify exactly one policy queue"
151         sys.exit(1)
152
153     queue_name = queue_name[0]
154
155     Options = cnf.SubTree("Process-Policy::Options")
156
157     if Options["Help"]:
158         usage()
159
160     if not Options["No-Action"]:
161         try:
162             Logger = daklog.Logger(cnf, "process-policy")
163         except CantOpenError, e:
164             Logger = None
165
166     # Find policy queue
167     session.query(PolicyQueue)
168
169     try:
170         pq = session.query(PolicyQueue).filter_by(queue_name=queue_name).one()
171     except NoResultFound:
172         print "E: Cannot find policy queue %s" % queue_name
173         sys.exit(1)
174
175     commentsdir = os.path.join(pq.path, 'COMMENTS')
176     # The comments stuff relies on being in the right directory
177     os.chdir(pq.path)
178     do_comments(commentsdir, pq, "ACCEPT.", "ACCEPTED.", "OK", comment_accept, session)
179     do_comments(commentsdir, pq, "REJECT.", "REJECTED.", "NOTOK", comment_reject, session)
180
181
182 ################################################################################
183
184 if __name__ == '__main__':
185     main()