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. 

18 

19import apt_pkg 

20import os 

21import sys 

22 

23from daklib.config import Config 

24from daklib.dbconn import * 

25from daklib.fstransactions import FilesystemTransaction 

26 

27 

28def usage(): 

29 print("""Usage: dak export-suite -s <suite> [options] 

30 

31Export binaries and sources from a suite to a flat directory structure. 

32 

33 -c --copy copy files instead of symlinking them 

34 -d <directory> target directory to export packages to 

35 default: current directory 

36 -r --relative use symlinks relative to target directory 

37 -s <suite> suite to grab uploads from 

38""") 

39 

40 

41def main(argv=None): 

42 if argv is None: 42 ↛ 45line 42 didn't jump to line 45, because the condition on line 42 was never false

43 argv = sys.argv 

44 

45 arguments = [('h', 'help', 'Export::Options::Help'), 

46 ('c', 'copy', 'Export::Options::Copy'), 

47 ('d', 'directory', 'Export::Options::Directory', 'HasArg'), 

48 ('r', 'relative', 'Export::Options::Relative'), 

49 ('s', 'suite', 'Export::Options::Suite', 'HasArg')] 

50 

51 cnf = Config() 

52 apt_pkg.parse_commandline(cnf.Cnf, arguments, argv) 

53 options = cnf.subtree('Export::Options') 

54 

55 if 'Help' in options or 'Suite' not in options: 55 ↛ 59line 55 didn't jump to line 59, because the condition on line 55 was never false

56 usage() 

57 sys.exit(0) 

58 

59 session = DBConn().session() 

60 

61 suite = session.query(Suite).filter_by(suite_name=options['Suite']).first() 

62 if suite is None: 

63 print("Unknown suite '{0}'".format(options['Suite'])) 

64 sys.exit(1) 

65 

66 directory = options.get('Directory') 

67 if not directory: 

68 print("No target directory.") 

69 sys.exit(1) 

70 

71 symlink = 'Copy' not in options 

72 relative = 'Relative' in options 

73 

74 if relative and not symlink: 

75 print("E: --relative and --copy cannot be used together.") 

76 sys.exit(1) 

77 

78 binaries = suite.binaries 

79 sources = suite.sources 

80 

81 files = [] 

82 files.extend([b.poolfile for b in binaries]) 

83 for s in sources: 

84 files.extend([ds.poolfile for ds in s.srcfiles]) 

85 

86 with FilesystemTransaction() as fs: 

87 for f in files: 

88 af = session.query(ArchiveFile) \ 

89 .join(ArchiveFile.component).join(ArchiveFile.file) \ 

90 .filter(ArchiveFile.archive == suite.archive) \ 

91 .filter(ArchiveFile.file == f).first() 

92 src = af.path 

93 if relative: 

94 src = os.path.relpath(src, directory) 

95 dst = os.path.join(directory, f.basename) 

96 if not os.path.exists(dst): 

97 fs.copy(src, dst, symlink=symlink) 

98 fs.commit() 

99 

100 

101if __name__ == '__main__': 

102 main()