]> git.decadent.org.uk Git - dak.git/blob - dak/import.py
Add new import subcommand.
[dak.git] / dak / import.py
1 #! /usr/bin/env python
2 #
3 # Copyright (C) 2012, Ansgar Burchardt <ansgar@debian.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 import apt_pkg
20 import os
21 import sys
22
23 from daklib.dbconn import *
24 import daklib.archive
25 import daklib.config
26 import daklib.upload
27 import daklib.regexes
28
29 def usage():
30     print """Usage:
31
32 dak import <suite> <component> <files...>
33
34 Options:
35   -h, --help:             show this help message
36   -a, --add-overrides:    add missing overrides automatically
37   -c, --changed-by:       Changed-By for imported source packages
38                           (default: maintainer)
39   -s, --ignore-signature: ignore signature for imported source packages
40
41 WARNING: This command does no sanity checks. Only use it on trusted packages.
42 """
43
44 def import_source(transaction, suite, component, directory, filename,
45                   changed_by=None, keyrings=None, require_signature=True,
46                   add_overrides=False):
47     if keyrings is None:
48         keyrings = []
49     session = transaction.session
50
51     source = daklib.upload.Source.from_file(directory, filename, keyrings, require_signature)
52     fingerprint = None
53     if source.valid_signature:
54         fingerprint = session.query(Fingerprint).filter_by(fingerprint=source.primary_fingerprint).one()
55     if changed_by is None:
56         changed_by = source.dsc['Maintainer']
57     db_changed_by = get_or_set_maintainer(changed_by, session)
58
59     transaction.install_source(directory, source, suite, component, db_changed_by, fingerprint=fingerprint)
60
61     if add_overrides and not session.query(Override).filter_by(suite=suite.get_overridesuite(), component=component, package=source.dsc['Source']).join(OverrideType).filter(OverrideType.overridetype == 'dsc').first():
62         overridetype = session.query(OverrideType).filter_by(overridetype='dsc').one()
63         overridesuite = suite.get_overridesuite()
64         section_name = 'misc'
65         if component.component_name != 'main':
66             section_name = "{0}/{1}".format(component.component_name, section_name)
67         section = session.query(Section).filter_by(section=section).one()
68         priority = session.query(Priority).filter_by(priority='extra').one()
69
70         override = Override(package=control.dsc['Source'], suite=overridesuite, component=component,
71                             section=section, priority=priority, overridetype=overridetype)
72         session.add(override)
73
74 def import_binary(transaction, suite, component, directory, filename, add_overrides=False):
75     session = transaction.session
76
77     binary = daklib.upload.Binary.from_file(directory, filename)
78     transaction.install_binary(directory, binary, suite, component)
79
80     if add_overrides and not session.query(Override).filter_by(suite=suite.get_overridesuite(), component=component, package=binary.control['Package']).join(OverrideType).filter(OverrideType.overridetype == binary.type).first():
81         overridetype = session.query(OverrideType).filter_by(overridetype=binary.type).one()
82         overridesuite = suite.get_overridesuite()
83         section = session.query(Section).filter_by(section=binary.control['Section']).one()
84         priority = session.query(Priority).filter_by(priority=binary.control['Priority']).one()
85
86         override = Override(package=binary.control['Package'], suite=overridesuite, component=component,
87                             section=section, priority=priority, overridetype=overridetype)
88         session.add(override)
89
90 def import_file(transaction, suite, component, directory, filename,
91                 changed_by=None, keyrings=None, require_signature=False,
92                 add_overrides = False):
93     if keyrings is None:
94         keyrings = []
95
96     if daklib.regexes.re_file_binary.match(filename):
97         import_binary(transaction, suite, component, directory, filename, add_overrides)
98     elif daklib.regexes.re_file_dsc.match(filename):
99         import_source(transaction, suite, component, directory, filename,
100                       changed_by=changed_by, keyrings=keyrings,
101                       require_signature=require_signature, add_overrides=add_overrides)
102     else:
103         raise Exception('File is neither source nor binary package: {0}'.format(filename))
104
105 def main(argv=None):
106     if argv is None:
107         argv = sys.argv
108
109     arguments = [
110         ('h', 'help', 'Import::Options::Help'),
111         ('a', 'add-overrides', 'Import::Options::AddOverrides'),
112         ('c', 'changed-by', 'Import::Options::ChangedBy', 'HasArg'),
113         ('s', 'ignore-signature', 'Import::Options::IgnoreSignature'),
114         ]
115
116     cnf = daklib.config.Config()
117     cnf['Import::Options::Dummy'] = ''
118     argv = apt_pkg.parse_commandline(cnf.Cnf, arguments, argv)
119     options = cnf.subtree('Import::Options')
120
121     if 'Help' in options or len(argv) < 3:
122         usage()
123         sys.exit(0)
124
125     suite_name = argv[0]
126     component_name = argv[1]
127     files = argv[2:]
128
129     add_overrides = options.find_b('AddOverrides')
130     require_signature = not options.find_b('IgnoreSignature')
131     changed_by = options.find('ChangedBy') or None
132
133     with daklib.archive.ArchiveTransaction() as transaction:
134         session = transaction.session
135         suite = session.query(Suite).filter_by(suite_name=suite_name).one()
136         component = session.query(Component).filter_by(component_name=component_name).one()
137         keyrings = session.query(Keyring).filter_by(active=True).order_by(Keyring.priority)
138         keyring_files = [ k.keyring_name for k in keyrings ]
139
140         for f in files:
141             directory, filename = os.path.split(os.path.abspath(f))
142             import_file(transaction, suite, component, directory, filename,
143                         changed_by=changed_by,
144                         keyrings=keyring_files, require_signature=require_signature,
145                         add_overrides=add_overrides)
146
147         transaction.commit()
148
149 if __name__ == '__main__':
150     main()