]> git.decadent.org.uk Git - dak.git/blob - dak/export_suite.py
Merge remote-tracking branch 'nthykier/auto-decruft'
[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  -r --relative     use symlinks relative to target directory
36  -s <suite>        suite to grab uploads from
37 """
38
39 def main(argv=None):
40     if argv is None:
41         argv = sys.argv
42
43     arguments = [('h', 'help', 'Export::Options::Help'),
44                  ('c', 'copy', 'Export::Options::Copy'),
45                  ('d', 'directory', 'Export::Options::Directory', 'HasArg'),
46                  ('r', 'relative', 'Export::Options::Relative'),
47                  ('s', 'suite', 'Export::Options::Suite', 'HasArg')]
48
49     cnf = Config()
50     apt_pkg.parse_commandline(cnf.Cnf, arguments, argv)
51     options = cnf.subtree('Export::Options')
52
53     if 'Help' in options or 'Suite' not in options:
54         usage()
55         sys.exit(0)
56
57     session = DBConn().session()
58
59     suite = session.query(Suite).filter_by(suite_name=options['Suite']).first()
60     if suite is None:
61         print "Unknown suite '{0}'".format(options['Suite'])
62         sys.exit(1)
63
64     directory = options.get('Directory')
65     if not directory:
66         print "No target directory."
67         sys.exit(1)
68
69     symlink = 'Copy' not in options
70     relative = 'Relative' in options
71
72     if relative and not symlink:
73         print "E: --relative and --copy cannot be used together."
74         sys.exit(1)
75
76     binaries = suite.binaries
77     sources = suite.sources
78
79     files = []
80     files.extend([ b.poolfile for b in binaries ])
81     for s in sources:
82         files.extend([ ds.poolfile for ds in s.srcfiles ])
83
84     with FilesystemTransaction() as fs:
85         for f in files:
86             af = session.query(ArchiveFile) \
87                         .join(ArchiveFile.component).join(ArchiveFile.file) \
88                         .filter(ArchiveFile.archive == suite.archive) \
89                         .filter(ArchiveFile.file == f).first()
90             src = af.path
91             if relative:
92                 src = os.path.relpath(src, directory)
93             dst = os.path.join(directory, f.basename)
94             if not os.path.exists(dst):
95                 fs.copy(src, dst, symlink=symlink)
96         fs.commit()
97
98 if __name__ == '__main__':
99     main()