]> git.decadent.org.uk Git - dak.git/blob - dak/import_new_files.py
import_new_files is close to working, right now it misses on the non-null constraints...
[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
38 from daklib.config import Config
39 from daklib.queue import Upload
40
41 # where in dak.conf all of our configuration will be stowed
42 options_prefix = "NewFiles"
43 options_prefix = "%s::Options" % options_prefix
44
45 log = logging.getLogger()
46
47 ################################################################################
48
49
50 def usage (exit_code=0):
51     print """Usage: dak import-new-files [options]
52
53 OPTIONS
54      -v, --verbose
55         show verbose information messages
56
57      -q, --quiet
58         supress all output but errors
59
60 """
61     sys.exit(exit_code)
62
63 class ImportNewFiles(object):
64     @session_wrapper
65     def __init__(self, session=None):
66         try:
67             newq = get_policy_queue('new', session)
68             for changes_fn in glob.glob(newq.path + "/*.changes"):
69                 changes_bn = os.path.basename(changes_fn)
70                 chg = get_dbchange(changes_bn, session)
71
72                 u = Upload()
73                 success = u.load_changes(changes_fn)
74                 u.pkg.changes_file = changes_bn
75
76                 if not chg:
77                     chg = u.pkg.add_known_changes(newq.path, newq.policy_queue_id, session)
78                     session.add(chg)
79
80                 if not success:
81                     log.critical("failed to load %s" % changes_fn)
82                     sys.exit(1)
83                 else:
84                     log.critical("ACCLAIM: %s" % changes_fn)
85
86                 files=[]
87                 for chg_fn in u.pkg.files.keys():
88                     cpf = ChangePendingFile()
89                     cpf.filename = chg_fn
90                     cpf.size = u.pkg.files[chg_fn]['size']
91                     cpf.md5sum = u.pkg.files[chg_fn]['md5sum']
92
93                     session.add(cpf)
94                     files.append(cpf)
95
96                 chg.files = files
97
98
99             session.commit()
100             
101         except KeyboardInterrupt:
102             print("Caught C-c; terminating.")
103             utils.warn("Caught C-c; terminating.")
104             self.plsDie()
105
106
107 def main():
108     cnf = Config()
109
110     arguments = [('h',"help", "%s::%s" % (options_prefix,"Help")),
111                  ('q',"quiet", "%s::%s" % (options_prefix,"Quiet")),
112                  ('v',"verbose", "%s::%s" % (options_prefix,"Verbose")),
113                 ]
114
115     args = apt_pkg.ParseCommandLine(cnf.Cnf, arguments,sys.argv)
116
117     num_threads = 1
118
119     if len(args) > 0:
120         usage(1)
121
122     if cnf.has_key("%s::%s" % (options_prefix,"Help")):
123         usage(0)
124
125     level=logging.INFO
126     if cnf.has_key("%s::%s" % (options_prefix,"Quiet")):
127         level=logging.ERROR
128
129     elif cnf.has_key("%s::%s" % (options_prefix,"Verbose")):
130         level=logging.DEBUG
131
132
133     logging.basicConfig( level=level,
134                          format='%(asctime)s %(levelname)s %(message)s',
135                          stream = sys.stderr )
136
137     ImportNewFiles()
138
139
140 if __name__ == '__main__':
141     main()