1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
#!/usr/bin/env python
# Copyright 2004-2005 Canonical Ltd. All rights reserved.
__metaclass__ = type
"""
Gina launcher script. Handles commandline options and makes the proper
calls to the other classes and instances.
The callstack is essentially:
main -> run_gina
-> import_sourcepackages -> do_one_sourcepackage
-> import_binarypackages -> do_one_binarypackage
"""
# Set to non-zero if you'd like to be warned every so often
COUNTDOWN = 0
import _pythonpath
import os
import sys
import time
import psycopg
from optparse import OptionParser
from datetime import timedelta
from canonical.lp import initZopeless, dbschema
from canonical.config import config
from canonical.launchpad.scripts import logger_options, log
from canonical.launchpad.scripts.lockfile import LockFile
from canonical.launchpad.scripts.gina.katie import Katie
from canonical.launchpad.scripts.gina.archive import (ArchiveComponentItems,
PackagesMap, MangledArchiveError)
from canonical.launchpad.scripts.gina.handlers import (ImporterHandler,
MultiplePackageReleaseError, NoSourcePackageError, DataSetupError)
from canonical.launchpad.scripts.gina.packages import (SourcePackageData,
BinaryPackageData, MissingRequiredArguments, DisplayNameDecodingError,
PoolFileNotFound, InvalidVersionError)
def _get_keyring(keyrings_root):
# XXX: untested
keyrings = ""
for keyring in os.listdir(keyrings_root):
path = os.path.join(keyrings_root, keyring)
keyrings += " --keyring=%s" % path
if not keyrings:
raise AttributeError("Keyrings not found in ./keyrings/")
return keyrings
def main():
parser = OptionParser("Usage: %prog [OPTIONS] [target ...]")
logger_options(parser)
parser.add_option("-n", "--dry-run", action="store_true",
help="Don't commit changes to the database",
dest="dry_run", default=False)
parser.add_option("-a", "--all", action="store_true",
help="Run all sections defined in launchpad.conf (in order)",
dest="all", default=False)
parser.add_option( "-l", "--lockfile",
default="/var/lock/launchpad-gina.lock",
help="Ensure only one process is running that locks LOCKFILE",
metavar="LOCKFILE"
)
(options, targets) = parser.parse_args()
possible_targets = [target.getSectionName() for target
in config.gina.target]
if options.all:
targets = possible_targets[:]
else:
if not targets:
parser.error("Must specify at least one target to run, or --all")
for target in targets:
if target not in possible_targets:
parser.error("No Gina target %s in config file" % target)
lockfile = LockFile(options.lockfile, timeout=timedelta(days=1),
logger=log)
try:
lockfile.acquire()
except OSError:
log.info('Lockfile %s already locked. Exiting.', options.lockfile)
sys.exit(1)
ztm = initZopeless(dbuser=config.gina.dbuser)
try:
for target in targets:
target_sections = [section for section in config.gina.target
if section.getSectionName() == target]
# XXX: should be a proper exception -- kiko, 2005-10-18
assert len(target_sections) == 1
run_gina(options, ztm, target_sections[0])
finally:
lockfile.release()
def run_gina(options, ztm, target_section):
package_root = target_section.root
keyrings_root = target_section.keyrings
distro = target_section.distro
# XXX I honestly think having a separate distrorelease section is a
# bit silly. Can't we construct this based on `distrorelease-pocket`?
pocket_distrorelease = target_section.pocketrelease
distrorelease = target_section.distrorelease
components = [c.strip() for c in target_section.components.split(",")]
archs = [a.strip() for a in target_section.architectures.split(",")]
pocket = target_section.pocket
source_only = target_section.source_only
spnames_only = target_section.sourcepackagenames_only
dry_run = options.dry_run
LPDB = config.dbname
LPDB_HOST = config.dbhost
LPDB_USER = config.gina.dbuser
KTDB = target_section.katie_dbname
LIBRHOST = config.librarian.upload_host
LIBRPORT = config.librarian.upload_port
log.info("")
log.info("=== Processing %s/%s/%s ===" % (distro, distrorelease, pocket))
log.debug("Packages read from: %s" % package_root)
log.debug("Keyrings read from: %s" % keyrings_root)
log.info("Components to import: %s" % ", ".join(components))
log.info("Architectures to import: %s" % ", ".join(archs))
log.debug("Launchpad database: %s" % LPDB)
log.debug("Launchpad database host: %s" % LPDB_HOST)
log.debug("Launchpad database user: %s" % LPDB_USER)
log.info("Katie database: %s" % KTDB)
log.info("SourcePackage Only: %s" % source_only)
log.info("SourcePackageName Only: %s" % spnames_only)
log.debug("Librarian: %s:%s" % (LIBRHOST, LIBRPORT))
log.info("Dry run: %s" % (dry_run))
log.info("")
if hasattr(dbschema.PackagePublishingPocket, pocket.upper()):
pocket = getattr(dbschema.PackagePublishingPocket, pocket.upper())
# XXX: should we ensure that the pocket release contains the
# pocket name?
else:
log.error("Could not find a pocket schema for %s" % pocket)
sys.exit(1)
kdb = None
keyrings = None
if KTDB:
kdb = Katie(KTDB, distrorelease, dry_run)
keyrings = _get_keyring(keyrings_root)
try:
arch_component_items = ArchiveComponentItems(package_root,
pocket_distrorelease,
components, archs)
except MangledArchiveError:
log.exception("Failed to analyze archive for %s" % pocket_distrorelease)
sys.exit(1)
packages_map = PackagesMap(arch_component_items)
importer_handler = ImporterHandler(ztm, distro, distrorelease,
dry_run, kdb, package_root, keyrings,
pocket)
for archtag in archs:
try:
importer_handler.ensure_archinfo(archtag)
except DataSetupError:
log.exception("Database setup required for run on %s" % archtag)
sys.exit(1)
if spnames_only:
log.info('Running in SourcePackageName-only mode...')
for source in packages_map.src_map.itervalues():
log.info('Ensuring %s name' % source['Package'])
importer_handler.ensure_sourcepackagename(source['Package'])
log.info('done')
sys.exit(0)
import_sourcepackages(packages_map, kdb, package_root, keyrings,
importer_handler)
importer_handler.commit()
if source_only:
log.info('Source only mode... done')
sys.exit(0)
import_binarypackages(packages_map, kdb, package_root, keyrings,
importer_handler)
importer_handler.commit()
def import_sourcepackages(packages_map, kdb, package_root,
keyrings, importer_handler):
# Goes over src_map importing the sourcepackages packages.
count = 0
npacks = len(packages_map.src_map)
log.info('%i Source Packages to be imported' % npacks)
for source in sorted(packages_map.src_map.values(),
key=lambda x: x.get("Package")):
count += 1
package_name = source.get("Package", "unknown")
try:
try:
do_one_sourcepackage(source, kdb, package_root, keyrings,
importer_handler)
except psycopg.Error:
log.exception("Database error: unable to create "
"Source Package for %s. Retrying once.."
% package_name)
importer_handler.abort()
time.sleep(15)
do_one_sourcepackage(source, kdb, package_root, keyrings,
importer_handler)
except (InvalidVersionError, MissingRequiredArguments,
DisplayNameDecodingError):
log.exception("Unable to create SourcePackageData for %s" %
package_name)
continue
except PoolFileNotFound:
# Problems with katie db stuff of opening files
log.exception("Error processing package files for %s" %
package_name)
continue
except psycopg.Error:
log.exception("Database errors made me give up: unable to create "
"Source Package for %s" % package_name)
importer_handler.abort()
continue
except MultiplePackageReleaseError:
log.exception("Database duplication processing %s" %
package_name)
continue
if COUNTDOWN and count % COUNTDOWN == 0:
log.warn('%i/%i sourcepackages processed' % (count, npacks))
def do_one_sourcepackage(source, kdb, package_root, keyrings,
importer_handler):
source_data = SourcePackageData(**source)
if importer_handler.preimport_sourcecheck(source_data):
# Don't bother reading package information if the source package
# already exists in the database
log.info('%s already exists in the archive' % source_data.package)
return
source_data.process_package(kdb, package_root, keyrings)
source_data.ensure_complete(kdb)
importer_handler.import_sourcepackage(source_data)
importer_handler.commit()
def import_binarypackages(packages_map, kdb, package_root, keyrings,
importer_handler):
nosource = []
# Run over all the architectures we have
for archtag in packages_map.bin_map.keys():
count = 0
npacks = len(packages_map.bin_map[archtag])
log.info('%i Binary Packages to be imported for %s' %
(npacks, archtag))
# Go over binarypackages importing them for this architecture
for binary in sorted(packages_map.bin_map[archtag].values(),
key=lambda x: x.get("Package")):
count += 1
package_name = binary.get("Package", "unknown")
try:
try:
do_one_binarypackage(binary, archtag, kdb, package_root,
keyrings, importer_handler)
except psycopg.Error:
log.exception("Database errors when importing a"
"Binary Package for %s. Retrying once.."
% package_name)
importer_handler.abort()
time.sleep(15)
do_one_binarypackage(binary, archtag, kdb, package_root,
keyrings, importer_handler)
except (InvalidVersionError, MissingRequiredArguments):
log.exception("Unable to create BinaryPackageData for %s" %
package_name)
continue
except PoolFileNotFound:
# Problems with katie db stuff of opening files
log.exception("Error processing package files for %s" %
package_name)
continue
except MultiplePackageReleaseError:
log.exception("Database duplication processing %s" %
package_name)
continue
except psycopg.Error:
log.exception("Database errors made me give up: unable to "
"create Binary Package for %s" % package_name)
importer_handler.abort()
continue
except NoSourcePackageError:
log.exception("Failed to create Binary Package for %s" %
package_name)
nosource.append(binary)
continue
if COUNTDOWN and count % COUNTDOWN == 0:
# XXX: untested
log.warn('%i/%i binary packages processed' % (count, npacks))
if nosource:
# XXX: untested
log.warn('%i source packages not found' % len(nosource))
for pkg in nosource:
log.warn(pkg)
def do_one_binarypackage(binary, archtag, kdb, package_root, keyrings,
importer_handler):
binary_data = BinaryPackageData(**binary)
if importer_handler.preimport_binarycheck(archtag, binary_data):
log.info('%s already exists in the archive' % binary_data.package)
return
binary_data.process_package(kdb, package_root, keyrings)
importer_handler.import_binarypackage(archtag, binary_data)
importer_handler.commit()
if __name__ == "__main__":
main()
|