]> git.decadent.org.uk Git - dak.git/blob - dak/process_policy.py
variable name fixes
[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         print comm
56         lines = open("%s/%s" % (dir, comm)).readlines()
57         if len(lines) == 0 or lines[0] != line + "\n": continue
58         changes_files = [ x for x in os.listdir(".") if x.startswith(comm[7:]+"_")
59                                 and x.endswith(".changes") ]
60         print changes_files
61         changes_files = sort_changes(changes_files, session)
62         for f in changes_files:
63             print " Changes file: %s" % f
64             f = utils.validate_changes_file_arg(f, 0)
65             if not f:
66                 print "Couldn't validate changes file %s" % f
67                 continue
68             print "\n" + f
69             fn(f, srcqueue, "".join(lines[1:]), session)
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     print "*** Accept for %s (%s)" % (changes_file, comments)
79
80     u = Upload()
81     u.pkg.changes_file = changes_file
82     u.load_changes(changes_file)
83     u.update_subst()
84
85     if not Options["No-Action"]:
86         destqueue = get_policy_queue('newstage', session)
87         changes_to_queue(u, srcqueue, destqueue, session)
88
89         Logger.log(["Policy Queue Accept: %s:  %s" % (srcqueue.queue_name, u.pkg.changes_file)])
90
91 ################################################################################
92
93 def comment_reject(changes_file, srcqueue, comments, session):
94     print "Reject for %s (%s)" % (changes_file, comments)
95
96     u = Upload()
97     u.pkg.changes_file = changes_file
98     u.load_changes(changes_file)
99     u.update_subst()
100
101     u.rejects.append(comments)
102
103     print "REJECT\n" + '\n'.join(u.rejects)
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
108         Logger.log(["Policy Queue Reject: %s:  %s" % (srcqueue.queue_name, u.pkg.changes_file)])
109
110
111 ################################################################################
112
113 def main():
114     global Options, Logger
115
116     cnf = Config()
117     session = DBConn().session()
118
119     Arguments = [('h',"help","Process-Policy::Options::Help"),
120                  ('n',"no-action","Process-Policy::Options::No-Action")]
121
122     for i in ["help", "no-action"]:
123         if not cnf.has_key("Process-Policy::Options::%s" % (i)):
124             cnf["Process-Policy::Options::%s" % (i)] = ""
125
126     queue_name = apt_pkg.ParseCommandLine(cnf.Cnf,Arguments,sys.argv)
127
128     if len(queue_name) != 1:
129         print "E: Specify exactly one policy queue"
130         sys.exit(1)
131
132     queue_name = queue_name[0]
133
134     Options = cnf.SubTree("Process-Policy::Options")
135
136     if Options["Help"]:
137         usage()
138
139     if not Options["No-Action"]:
140         try:
141             Logger = daklog.Logger(cnf, "process-new")
142         except CantOpenError, e:
143             Logger = None
144
145     # Find policy queue
146     session.query(PolicyQueue)
147
148     try:
149         pq = session.query(PolicyQueue).filter_by(queue_name=queue_name).one()
150         commentsdir = os.path.join(pq.path, 'COMMENTS')
151         # The comments stuff relies on being in the right directory
152         os.chdir(pq.path)
153         do_comments(commentsdir, pq, "ACCEPT.", "ACCEPTED.", "OK", comment_accept, session)
154         do_comments(commentsdir, pq, "REJECT.", "REJECTED.", "NOTOK", comment_reject, session)
155     except NoResultFound:
156         print "E: Cannot find policy queue %s" % queue_name
157         sys.exit(1)
158
159 ################################################################################
160
161 if __name__ == '__main__':
162     main()