]> git.donarmstrong.com Git - dak.git/blob - dak/export_suite.py
23775583b67030b78da306e0b3e2440cc5c577e8
[dak.git] / dak / export_suite.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.config import Config
24 from daklib.dbconn import *
25 from daklib.fstransactions import FilesystemTransaction
26
27 def usage():
28     print """Usage: dak export-suite -s <suite> [options]
29
30 Export binaries and sources from a suite to a flat directory structure.
31
32  -c --copy         copy files instead of symlinking them
33  -d <directory>    target directory to export packages to
34                    default: current directory
35  -s <suite>        suite to grab uploads from
36 """
37
38 def main(argv=None):
39     if argv is None:
40         argv = sys.argv
41
42     arguments = [('h', 'help', 'Export::Options::Help'),
43                  ('c', 'copy', 'Export::Options::Copy'),
44                  ('d', 'directory', 'Export::Options::Directory', 'HasArg'),
45                  ('s', 'suite', 'Export::Options::Suite', 'HasArg')]
46
47     cnf = Config()
48     apt_pkg.parse_commandline(cnf.Cnf, arguments, argv)
49     options = cnf.subtree('Export::Options')
50
51     if 'Help' in options or 'Suite' not in options:
52         usage()
53         sys.exit(0)
54
55     session = DBConn().session()
56
57     suite = session.query(Suite).filter_by(suite_name=options['Suite']).first()
58     if suite is None:
59         print "Unknown suite '{0}'".format(options['Suite'])
60         sys.exit(1)
61
62     directory = options.get('Directory')
63     if not directory:
64         print "No target directory."
65         sys.exit(1)
66
67     symlink = 'Copy' not in options
68
69     binaries = suite.binaries
70     sources = suite.sources
71
72     files = []
73     files.extend([ b.poolfile for b in binaries ])
74     for s in sources:
75         files.extend([ ds.poolfile for ds in s.srcfiles ])
76
77     with FilesystemTransaction() as fs:
78         for f in files:
79             af = session.query(ArchiveFile) \
80                         .join(ArchiveFile.component).join(ArchiveFile.file) \
81                         .filter(ArchiveFile.archive == suite.archive) \
82                         .filter(ArchiveFile.file == f).first()
83             # XXX: Remove later. There was a bug that caused only the *.dsc to
84             # be installed in build queues and we do not want to break them.
85             # The bug was fixed in 55d2c7e6e2418518704623246021021e05b90e58
86             # on 2012-11-04
87             if af is None:
88                 af = session.query(ArchiveFile) \
89                             .join(ArchiveFile.component).join(ArchiveFile.file) \
90                             .filter(ArchiveFile.file == f).first()
91             dst = os.path.join(directory, f.basename)
92             if not os.path.exists(dst):
93                 fs.copy(af.path, dst, symlink=symlink)
94         fs.commit()
95
96 if __name__ == '__main__':
97     main()