]> git.decadent.org.uk Git - dak.git/blob - dak/process_policy.py
Make policy queues work and remove print debugging
[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     if not Options["No-Action"]:
98         u.do_reject(manual=0, reject_message='\n'.join(u.rejects))
99         u.pkg.remove_known_changes(session=session)
100
101         Logger.log(["Policy Queue REJECT: %s:  %s" % (srcqueue.queue_name, u.pkg.changes_file)])
102
103
104 ################################################################################
105
106 def main():
107     global Options, Logger
108
109     cnf = Config()
110     session = DBConn().session()
111
112     Arguments = [('h',"help","Process-Policy::Options::Help"),
113                  ('n',"no-action","Process-Policy::Options::No-Action")]
114
115     for i in ["help", "no-action"]:
116         if not cnf.has_key("Process-Policy::Options::%s" % (i)):
117             cnf["Process-Policy::Options::%s" % (i)] = ""
118
119     queue_name = apt_pkg.ParseCommandLine(cnf.Cnf,Arguments,sys.argv)
120
121     if len(queue_name) != 1:
122         print "E: Specify exactly one policy queue"
123         sys.exit(1)
124
125     queue_name = queue_name[0]
126
127     Options = cnf.SubTree("Process-Policy::Options")
128
129     if Options["Help"]:
130         usage()
131
132     if not Options["No-Action"]:
133         try:
134             Logger = daklog.Logger(cnf, "process-policy")
135         except CantOpenError, e:
136             Logger = None
137
138     # Find policy queue
139     session.query(PolicyQueue)
140
141     try:
142         pq = session.query(PolicyQueue).filter_by(queue_name=queue_name).one()
143     except NoResultFound:
144         print "E: Cannot find policy queue %s" % queue_name
145         sys.exit(1)
146
147     commentsdir = os.path.join(pq.path, 'COMMENTS')
148     # The comments stuff relies on being in the right directory
149     os.chdir(pq.path)
150     do_comments(commentsdir, pq, "ACCEPT.", "ACCEPTED.", "OK", comment_accept, session)
151     do_comments(commentsdir, pq, "REJECT.", "REJECTED.", "NOTOK", comment_reject, session)
152
153
154 ################################################################################
155
156 if __name__ == '__main__':
157     main()