4 Create all the Release files
6 @contact: Debian FTPMaster <ftpmaster@debian.org>
7 @copyright: 2011 Joerg Jaspert <joerg@debian.org>
8 @copyright: 2011 Mark Hymers <mhy@debian.org>
9 @license: GNU General Public License version 2 or later
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.
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.
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
27 ################################################################################
29 # <mhy> I wish they wouldnt leave biscuits out, thats just tempting. Damnit.
31 ################################################################################
43 from tempfile import mkstemp, mkdtemp
45 from sqlalchemy.orm import object_session
47 from daklib import utils, daklog
48 from daklib.regexes import re_gensubrelease, re_includeinrelease
49 from daklib.dak_exceptions import *
50 from daklib.dbconn import *
51 from daklib.config import Config
52 from daklib.dakmultiprocessing import DakProcessPool, PROC_STATUS_SUCCESS
53 import daklib.daksubprocess
55 ################################################################################
56 Logger = None #: Our logging object
58 ################################################################################
60 def usage (exit_code=0):
61 """ Usage information"""
63 print """Usage: dak generate-releases [OPTIONS]
64 Generate the Release files
66 -a, --archive=ARCHIVE process suites in ARCHIVE
67 -s, --suite=SUITE(s) process this suite
68 Default: All suites not marked 'untouchable'
69 -f, --force Allow processing of untouchable suites
70 CAREFUL: Only to be used at (point) release time!
71 -h, --help show this help and exit
72 -q, --quiet Don't output progress
74 SUITE can be a space separated list, e.g.
75 --suite=unstable testing
79 ########################################################################
81 def sign_release_dir(suite, dirname):
84 if cnf.has_key("Dinstall::SigningKeyring"):
85 keyring = "--secret-keyring \"%s\"" % cnf["Dinstall::SigningKeyring"]
86 if cnf.has_key("Dinstall::SigningPubKeyring"):
87 keyring += " --keyring \"%s\"" % cnf["Dinstall::SigningPubKeyring"]
89 arguments = "--no-options --batch --no-tty --armour --personal-digest-preferences=SHA256"
91 relname = os.path.join(dirname, 'Release')
93 dest = os.path.join(dirname, 'Release.gpg')
94 if os.path.exists(dest):
97 inlinedest = os.path.join(dirname, 'InRelease')
98 if os.path.exists(inlinedest):
102 for keyid in suite.signingkeys or []:
103 defkeyid += "--local-user %s " % keyid
105 os.system("gpg %s %s %s --detach-sign <%s >>%s" %
106 (keyring, defkeyid, arguments, relname, dest))
107 os.system("gpg %s %s %s --clearsign <%s >>%s" %
108 (keyring, defkeyid, arguments, relname, inlinedest))
110 class XzFile(object):
111 def __init__(self, filename, mode='r'):
112 self.filename = filename
115 with open(self.filename, 'r') as stdin:
116 process = daklib.daksubprocess.Popen(cmd, stdin=stdin, stdout=subprocess.PIPE)
117 (stdout, stderr) = process.communicate()
121 class HashFunc(object):
122 def __init__(self, release_field, func, db_name):
123 self.release_field = release_field
125 self.db_name = db_name
128 HashFunc('MD5Sum', apt_pkg.md5sum, 'md5sum'),
129 HashFunc('SHA1', apt_pkg.sha1sum, 'sha1'),
130 HashFunc('SHA256', apt_pkg.sha256sum, 'sha256'),
134 class ReleaseWriter(object):
135 def __init__(self, suite):
138 def suite_path(self):
140 Absolute path to the suite-specific files.
143 suite_suffix = cnf.find("Dinstall::SuiteSuffix", "")
145 return os.path.join(self.suite.archive.path, 'dists',
146 self.suite.suite_name, suite_suffix)
148 def suite_release_path(self):
150 Absolute path where Release files are physically stored.
151 This should be a path that sorts after the dists/ directory.
153 # TODO: Eventually always create Release in `zzz-dists` to avoid
154 # special cases. However we don't want to move existing Release files
155 # for released suites.
156 # See `create_release_symlinks` below.
157 if not self.suite.byhash:
158 return self.suite_path()
161 suite_suffix = cnf.find("Dinstall::SuiteSuffix", "")
163 return os.path.join(self.suite.archive.path, 'zzz-dists',
164 self.suite.suite_name, suite_suffix)
166 def create_release_symlinks(self):
168 Create symlinks for Release files.
169 This creates the symlinks for Release files in the `suite_path`
170 to the actual files in `suite_release_path`.
172 # TODO: Eventually always create the links.
173 # See `suite_release_path` above.
174 if not self.suite.byhash:
177 relpath = os.path.relpath(self.suite_release_path(), self.suite_path())
178 for f in ("Release", "Release.gpg", "InRelease"):
179 source = os.path.join(relpath, f)
180 dest = os.path.join(self.suite_path(), f)
181 if not os.path.islink(dest):
183 elif os.readlink(dest) == source:
187 os.symlink(source, dest)
189 def create_output_directories(self):
190 for path in (self.suite_path(), self.suite_release_path()):
194 if e.errno != errno.EEXIST:
197 def _update_hashfile_table(self, session, fileinfo, hashes):
198 # Mark all by-hash files as obsolete. We will undo that for the ones
199 # we still reference later.
201 UPDATE hashfile SET unreferenced = CURRENT_TIMESTAMP
202 WHERE suite_id = :id AND unreferenced IS NULL"""
203 session.execute(query, {'id': self.suite.suite_id})
205 if self.suite.byhash:
206 query = "SELECT path FROM hashfile WHERE suite_id = :id"
207 q = session.execute(query, {'id': self.suite.suite_id})
208 known_hashfiles = set(row[0] for row in q)
212 # Update the hashfile table with new or updated files
213 for filename in fileinfo:
214 if not os.path.exists(filename):
215 # probably an uncompressed index we didn't generate
217 byhashdir = os.path.join(os.path.dirname(filename), 'by-hash')
219 field = h.release_field
220 hashfile = os.path.join(byhashdir, field, fileinfo[filename][field])
221 if hashfile in known_hashfiles:
222 updated.add(hashfile)
228 UPDATE hashfile SET unreferenced = NULL
229 WHERE path = ANY(:p) AND suite_id = :id""",
230 {'p': list(updated), 'id': self.suite.suite_id})
233 INSERT INTO hashfile (path, suite_id)
235 [{'p': hashfile, 'id': self.suite.suite_id} for hashfile in new])
239 def _make_byhash_links(self, fileinfo, hashes):
240 # Create hardlinks in by-hash directories
241 for filename in fileinfo:
242 if not os.path.exists(filename):
243 # probably an uncompressed index we didn't generate
247 field = h.release_field
248 hashfile = os.path.join(os.path.dirname(filename), 'by-hash', field, fileinfo[filename][field])
250 os.makedirs(os.path.dirname(hashfile))
251 except OSError as exc:
252 if exc.errno != errno.EEXIST:
255 os.link(filename, hashfile)
256 except OSError as exc:
257 if exc.errno != errno.EEXIST:
260 def generate_release_files(self):
262 Generate Release files for the given suite
265 @param suite: Suite name
269 session = object_session(suite)
271 architectures = get_suite_architectures(suite.suite_name, skipall=True, skipsrc=True, session=session)
273 # Attribs contains a tuple of field names and the database names to use to
275 attribs = ( ('Origin', 'origin'),
277 ('Suite', 'release_suite_output'),
278 ('Version', 'version'),
279 ('Codename', 'codename'),
280 ('Changelogs', 'changelog_url'),
283 # A "Sub" Release file has slightly different fields
284 subattribs = ( ('Archive', 'suite_name'),
285 ('Origin', 'origin'),
287 ('Version', 'version') )
289 # Boolean stuff. If we find it true in database, write out "yes" into the release file
290 boolattrs = ( ('NotAutomatic', 'notautomatic'),
291 ('ButAutomaticUpgrades', 'butautomaticupgrades'),
292 ('Acquire-By-Hash', 'byhash'),
297 suite_suffix = cnf.find("Dinstall::SuiteSuffix", "")
299 self.create_output_directories()
300 self.create_release_symlinks()
302 outfile = os.path.join(self.suite_release_path(), "Release")
303 out = open(outfile + ".new", "w")
305 for key, dbfield in attribs:
306 # Hack to skip NULL Version fields as we used to do this
307 # We should probably just always ignore anything which is None
308 if key in ("Version", "Changelogs") and getattr(suite, dbfield) is None:
311 out.write("%s: %s\n" % (key, getattr(suite, dbfield)))
313 out.write("Date: %s\n" % (time.strftime("%a, %d %b %Y %H:%M:%S UTC", time.gmtime(time.time()))))
316 validtime=float(suite.validtime)
317 out.write("Valid-Until: %s\n" % (time.strftime("%a, %d %b %Y %H:%M:%S UTC", time.gmtime(time.time()+validtime))))
319 for key, dbfield in boolattrs:
320 if getattr(suite, dbfield, False):
321 out.write("%s: yes\n" % (key))
323 out.write("Architectures: %s\n" % (" ".join([a.arch_string for a in architectures])))
325 components = [ c.component_name for c in suite.components ]
327 out.write("Components: %s\n" % (" ".join(components)))
329 # For exact compatibility with old g-r, write out Description here instead
330 # of with the rest of the DB fields above
331 if getattr(suite, 'description') is not None:
332 out.write("Description: %s\n" % suite.description)
334 for comp in components:
335 for dirpath, dirnames, filenames in os.walk(os.path.join(self.suite_path(), comp), topdown=True):
336 if not re_gensubrelease.match(dirpath):
339 subfile = os.path.join(dirpath, "Release")
340 subrel = open(subfile + '.new', "w")
342 for key, dbfield in subattribs:
343 if getattr(suite, dbfield) is not None:
344 subrel.write("%s: %s\n" % (key, getattr(suite, dbfield)))
346 for key, dbfield in boolattrs:
347 if getattr(suite, dbfield, False):
348 subrel.write("%s: yes\n" % (key))
350 subrel.write("Component: %s%s\n" % (suite_suffix, comp))
352 # Urgh, but until we have all the suite/component/arch stuff in the DB,
354 arch = os.path.split(dirpath)[-1]
355 if arch.startswith('binary-'):
358 subrel.write("Architecture: %s\n" % (arch))
361 os.rename(subfile + '.new', subfile)
363 # Now that we have done the groundwork, we want to get off and add the files with
364 # their checksums to the main Release file
367 os.chdir(self.suite_path())
369 hashes = [x for x in RELEASE_HASHES if x.db_name in suite.checksums]
375 for dirpath, dirnames, filenames in os.walk(".", followlinks=True, topdown=True):
376 for entry in filenames:
377 # Skip things we don't want to include
378 if not re_includeinrelease.match(entry):
381 if dirpath == '.' and entry in ["Release", "Release.gpg", "InRelease"]:
384 filename = os.path.join(dirpath.lstrip('./'), entry)
385 fileinfo[filename] = {}
386 contents = open(filename, 'r').read()
388 # If we find a file for which we have a compressed version and
389 # haven't yet seen the uncompressed one, store the possibility
391 if entry.endswith(".gz") and filename[:-3] not in uncompnotseen:
392 uncompnotseen[filename[:-3]] = (gzip.GzipFile, filename)
393 elif entry.endswith(".bz2") and filename[:-4] not in uncompnotseen:
394 uncompnotseen[filename[:-4]] = (bz2.BZ2File, filename)
395 elif entry.endswith(".xz") and filename[:-3] not in uncompnotseen:
396 uncompnotseen[filename[:-3]] = (XzFile, filename)
398 fileinfo[filename]['len'] = len(contents)
401 fileinfo[filename][hf.release_field] = hf.func(contents)
403 for filename, comp in uncompnotseen.items():
404 # If we've already seen the uncompressed file, we don't
405 # need to do anything again
406 if filename in fileinfo:
409 fileinfo[filename] = {}
411 # File handler is comp[0], filename of compressed file is comp[1]
412 contents = comp[0](comp[1], 'r').read()
414 fileinfo[filename]['len'] = len(contents)
417 fileinfo[filename][hf.release_field] = hf.func(contents)
420 for field in sorted(h.release_field for h in hashes):
421 out.write('%s:\n' % field)
422 for filename in sorted(fileinfo.keys()):
423 out.write(" %s %8d %s\n" % (fileinfo[filename][field], fileinfo[filename]['len'], filename))
426 os.rename(outfile + '.new', outfile)
428 self._update_hashfile_table(session, fileinfo, hashes)
430 self._make_byhash_links(fileinfo, hashes)
432 sign_release_dir(suite, os.path.dirname(outfile))
444 for i in ["Help", "Suite", "Force", "Quiet"]:
445 if not cnf.has_key("Generate-Releases::Options::%s" % (i)):
446 cnf["Generate-Releases::Options::%s" % (i)] = ""
448 Arguments = [('h',"help","Generate-Releases::Options::Help"),
449 ('a','archive','Generate-Releases::Options::Archive','HasArg'),
450 ('s',"suite","Generate-Releases::Options::Suite"),
451 ('f',"force","Generate-Releases::Options::Force"),
452 ('q',"quiet","Generate-Releases::Options::Quiet"),
453 ('o','option','','ArbItem')]
455 suite_names = apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv)
456 Options = cnf.subtree("Generate-Releases::Options")
461 Logger = daklog.Logger('generate-releases')
462 pool = DakProcessPool()
464 session = DBConn().session()
468 for s in suite_names:
469 suite = get_suite(s.lower(), session)
473 print "cannot find suite %s" % s
474 Logger.log(['cannot find suite %s' % s])
476 query = session.query(Suite).filter(Suite.untouchable == False)
477 if 'Archive' in Options:
478 query = query.join(Suite.archive).filter(Archive.archive_name==Options['Archive'])
484 # Setup a multiprocessing Pool. As many workers as we have CPU cores.
485 if s.untouchable and not Options["Force"]:
486 print "Skipping %s (untouchable)" % s.suite_name
489 if not Options["Quiet"]:
490 print "Processing %s" % s.suite_name
491 Logger.log(['Processing release file for Suite: %s' % (s.suite_name)])
492 pool.apply_async(generate_helper, (s.suite_id, ))
494 # No more work will be added to our pool, close it and then wait for all to finish
498 retcode = pool.overall_status()
501 # TODO: CENTRAL FUNCTION FOR THIS / IMPROVE LOGGING
502 Logger.log(['Release file generation broken: %s' % (','.join([str(x[1]) for x in pool.results]))])
508 def generate_helper(suite_id):
510 This function is called in a new subprocess.
512 session = DBConn().session()
513 suite = Suite.get(suite_id, session)
515 # We allow the process handler to catch and deal with any exceptions
516 rw = ReleaseWriter(suite)
517 rw.generate_release_files()
519 return (PROC_STATUS_SUCCESS, 'Release file written for %s' % suite.suite_name)
521 #######################################################################################
523 if __name__ == '__main__':