]> git.decadent.org.uk Git - dak.git/blob - dak/process_policy.py
Convert exception handling to Python3 syntax.
[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[len(opref):]+"_")
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             print "  ACCEPT"
84             Logger.log(["Policy Queue ACCEPT: %s:  %s" % (srcqueue.queue_name, u.pkg.changes_file)])
85         else:
86             print "E: Failed to migrate %s" % u.pkg.changes_file
87
88 ################################################################################
89
90 def comment_reject(changes_file, srcqueue, comments, session):
91     u = Upload()
92     u.pkg.changes_file = changes_file
93     u.load_changes(changes_file)
94     u.update_subst()
95
96     u.rejects.append(comments)
97
98     cnf = Config()
99     bcc = "X-DAK: dak process-policy"
100     if cnf.has_key("Dinstall::Bcc"):
101         u.Subst["__BCC__"] = bcc + "\nBcc: %s" % (cnf["Dinstall::Bcc"])
102     else:
103         u.Subst["__BCC__"] = bcc
104
105     if not Options["No-Action"]:
106         u.do_reject(manual=0, reject_message='\n'.join(u.rejects))
107         u.pkg.remove_known_changes(session=session)
108         session.commit()
109
110         print "  REJECT"
111         Logger.log(["Policy Queue REJECT: %s:  %s" % (srcqueue.queue_name, u.pkg.changes_file)])
112
113
114 ################################################################################
115
116 def main():
117     global Options, Logger
118
119     cnf = Config()
120     session = DBConn().session()
121
122     Arguments = [('h',"help","Process-Policy::Options::Help"),
123                  ('n',"no-action","Process-Policy::Options::No-Action")]
124
125     for i in ["help", "no-action"]:
126         if not cnf.has_key("Process-Policy::Options::%s" % (i)):
127             cnf["Process-Policy::Options::%s" % (i)] = ""
128
129     queue_name = apt_pkg.ParseCommandLine(cnf.Cnf,Arguments,sys.argv)
130
131     if len(queue_name) != 1:
132         print "E: Specify exactly one policy queue"
133         sys.exit(1)
134
135     queue_name = queue_name[0]
136
137     Options = cnf.SubTree("Process-Policy::Options")
138
139     if Options["Help"]:
140         usage()
141
142     if not Options["No-Action"]:
143         try:
144             Logger = daklog.Logger("process-policy")
145         except CantOpenError as e:
146             Logger = None
147
148     # Find policy queue
149     session.query(PolicyQueue)
150
151     try:
152         pq = session.query(PolicyQueue).filter_by(queue_name=queue_name).one()
153     except NoResultFound:
154         print "E: Cannot find policy queue %s" % queue_name
155         sys.exit(1)
156
157     commentsdir = os.path.join(pq.path, 'COMMENTS')
158     # The comments stuff relies on being in the right directory
159     os.chdir(pq.path)
160     do_comments(commentsdir, pq, "ACCEPT.", "ACCEPTED.", "OK", comment_accept, session)
161     do_comments(commentsdir, pq, "ACCEPTED.", "ACCEPTED.", "OK", comment_accept, session)
162     do_comments(commentsdir, pq, "REJECT.", "REJECTED.", "NOTOK", comment_reject, session)
163
164
165 ################################################################################
166
167 if __name__ == '__main__':
168     main()