1#! /usr/bin/env python3
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.
19import os
20import sys
22import apt_pkg
24from daklib.config import Config
25from daklib.dbconn import ArchiveFile, DBConn, Suite
26from daklib.fstransactions import FilesystemTransaction
29def usage():
30 print(
31 """Usage: dak export-suite -s <suite> [options]
33Export binaries and sources from a suite to a flat directory structure.
35 -c --copy copy files instead of symlinking them
36 -d <directory> target directory to export packages to
37 default: current directory
38 -r --relative use symlinks relative to target directory
39 -s <suite> suite to grab uploads from
40"""
41 )
44def main(argv=None):
45 if argv is None: 45 ↛ 48line 45 didn't jump to line 48
46 argv = sys.argv
48 arguments = [
49 ("h", "help", "Export::Options::Help"),
50 ("c", "copy", "Export::Options::Copy"),
51 ("d", "directory", "Export::Options::Directory", "HasArg"),
52 ("r", "relative", "Export::Options::Relative"),
53 ("s", "suite", "Export::Options::Suite", "HasArg"),
54 ]
56 cnf = Config()
57 apt_pkg.parse_commandline(cnf.Cnf, arguments, argv)
58 options = cnf.subtree("Export::Options")
60 if "Help" in options or "Suite" not in options: 60 ↛ 64line 60 didn't jump to line 64, because the condition on line 60 was never false
61 usage()
62 sys.exit(0)
64 session = DBConn().session()
66 suite = session.query(Suite).filter_by(suite_name=options["Suite"]).first()
67 if suite is None:
68 print("Unknown suite '{0}'".format(options["Suite"]))
69 sys.exit(1)
71 directory = options.get("Directory")
72 if not directory:
73 print("No target directory.")
74 sys.exit(1)
76 symlink = "Copy" not in options
77 relative = "Relative" in options
79 if relative and not symlink:
80 print("E: --relative and --copy cannot be used together.")
81 sys.exit(1)
83 binaries = suite.binaries
84 sources = suite.sources
86 files = []
87 files.extend([b.poolfile for b in binaries])
88 for s in sources:
89 files.extend([ds.poolfile for ds in s.srcfiles])
91 with FilesystemTransaction() as fs:
92 for f in files:
93 af = (
94 session.query(ArchiveFile)
95 .join(ArchiveFile.component)
96 .join(ArchiveFile.file)
97 .filter(ArchiveFile.archive == suite.archive)
98 .filter(ArchiveFile.file == f)
99 .first()
100 )
101 src = af.path
102 if relative:
103 src = os.path.relpath(src, directory)
104 dst = os.path.join(directory, f.basename)
105 if not os.path.exists(dst):
106 fs.copy(src, dst, symlink=symlink)
107 fs.commit()
110if __name__ == "__main__":
111 main()