]> git.decadent.org.uk Git - dak.git/blob - dak/import_new_files.py
Merge remote branch 'bdefreese/barry_fix_new' into merge
[dak.git] / dak / import_new_files.py
1 #!/usr/bin/env python
2 # coding=utf8
3
4 """
5 Import known_changes files
6
7 @contact: Debian FTP Master <ftpmaster@debian.org>
8 @copyright: 2009  Mike O'Connor <stew@debian.org>
9 @license: GNU General Public License version 2 or later
10 """
11
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
26 ################################################################################
27
28
29 ################################################################################
30
31 import sys
32 import os
33 import logging
34 import threading
35 import glob
36 import apt_pkg
37 from daklib.dbconn import DBConn, get_dbchange, get_policy_queue, session_wrapper, ChangePendingFile, get_location, check_poolfile
38 from daklib.config import Config
39 from daklib.queue import Upload
40 from daklib.utils import poolify
41
42 # where in dak.conf all of our configuration will be stowed
43 options_prefix = "NewFiles"
44 options_prefix = "%s::Options" % options_prefix
45
46 log = logging.getLogger()
47
48 ################################################################################
49
50
51 def usage (exit_code=0):
52     print """Usage: dak import-new-files [options]
53
54 OPTIONS
55      -v, --verbose
56         show verbose information messages
57
58      -q, --quiet
59         supress all output but errors
60
61 """
62     sys.exit(exit_code)
63
64 class ImportNewFiles(object):
65     @session_wrapper
66     def __init__(self, session=None):
67         cnf = Config()
68         try:
69             newq = get_policy_queue('new', session)
70             for changes_fn in glob.glob(newq.path + "/*.changes"):
71                 changes_bn = os.path.basename(changes_fn)
72                 chg = get_dbchange(changes_bn, session)
73
74                 u = Upload()
75                 success = u.load_changes(changes_fn)
76                 u.pkg.changes_file = changes_bn
77                 u.check_hashes()
78
79                 if not chg:
80                     chg = u.pkg.add_known_changes(newq.path, newq.policy_queue_id, session)
81                     session.add(chg)
82
83                 if not success:
84                     log.critical("failed to load %s" % changes_fn)
85                     sys.exit(1)
86                 else:
87                     log.critical("ACCLAIM: %s" % changes_fn)
88
89                 files=[]
90                 for chg_fn in u.pkg.files.keys():
91                     try:
92                         f = open(os.path.join(newq.path, chg_fn))
93                         cpf = ChangePendingFile()
94                         cpf.filename = chg_fn
95                         cpf.size = u.pkg.files[chg_fn]['size']
96                         cpf.md5sum = u.pkg.files[chg_fn]['md5sum']
97
98                         if u.pkg.files[chg_fn].has_key('sha1sum'):
99                             cpf.sha1sum = u.pkg.files[chg_fn]['sha1sum']
100                         else:
101                             log.warning("Having to generate sha1sum for %s" % chg_fn)
102                             f.seek(0)
103                             cpf.sha1sum = apt_pkg.sha1sum(f)
104
105                         if u.pkg.files[chg_fn].has_key('sha256sum'):
106                             cpf.sha256sum = u.pkg.files[chg_fn]['sha256sum']
107                         else:
108                             log.warning("Having to generate sha256sum for %s" % chg_fn)
109                             f.seek(0)
110                             cpf.sha256sum = apt_pkg.sha256sum(f)
111
112                         session.add(cpf)
113                         files.append(cpf)
114                         f.close()
115                     except IOError:
116                         # Can't find the file, try to look it up in the pool
117                         poolname = poolify(u.pkg.changes["source"], u.pkg.files[chg_fn]["component"])
118                         l = get_location(cnf["Dir::Pool"], u.pkg.files[chg_fn]["component"], session=session)
119                         if not l:
120                             log.critical("ERROR: Can't find location for %s (component %s)" % (chg_fn, u.pkg.files[chg_fn]["component"]))
121
122                         found, poolfile = check_poolfile(os.path.join(poolname, chg_fn),
123                                                          u.pkg.files[chg_fn]['size'],
124                                                          u.pkg.files[chg_fn]["md5sum"],
125                                                          l.location_id,
126                                                          session=session)
127
128                         if found is None:
129                             log.critical("ERROR: Found multiple files for %s in pool" % chg_fn)
130                             sys.exit(1)
131                         elif found is False and poolfile is not None:
132                             log.critical("ERROR: md5sum / size mismatch for %s in pool" % chg_fn)
133                             sys.exit(1)
134                         else:
135                             if poolfile is None:
136                                 log.critical("ERROR: Could not find %s in pool" % chg_fn)
137                                 sys.exit(1)
138                             else:
139                                 chg.poolfiles.append(poolfile)
140
141
142                 chg.files = files
143
144
145             session.commit()
146             
147         except KeyboardInterrupt:
148             print("Caught C-c; terminating.")
149             utils.warn("Caught C-c; terminating.")
150             self.plsDie()
151
152
153 def main():
154     cnf = Config()
155
156     arguments = [('h',"help", "%s::%s" % (options_prefix,"Help")),
157                  ('q',"quiet", "%s::%s" % (options_prefix,"Quiet")),
158                  ('v',"verbose", "%s::%s" % (options_prefix,"Verbose")),
159                 ]
160
161     args = apt_pkg.ParseCommandLine(cnf.Cnf, arguments,sys.argv)
162
163     num_threads = 1
164
165     if len(args) > 0:
166         usage(1)
167
168     if cnf.has_key("%s::%s" % (options_prefix,"Help")):
169         usage(0)
170
171     level=logging.INFO
172     if cnf.has_key("%s::%s" % (options_prefix,"Quiet")):
173         level=logging.ERROR
174
175     elif cnf.has_key("%s::%s" % (options_prefix,"Verbose")):
176         level=logging.DEBUG
177
178
179     logging.basicConfig( level=level,
180                          format='%(asctime)s %(levelname)s %(message)s',
181                          stream = sys.stderr )
182
183     ImportNewFiles()
184
185
186 if __name__ == '__main__':
187     main()