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 datetime 

21import os 

22import sys 

23import time 

24 

25from daklib.config import Config 

26from daklib.command import CommandFile, CommandError 

27from daklib.daklog import Logger 

28from daklib.fstransactions import FilesystemTransaction 

29from daklib.utils import find_next_free 

30 

31 

32def usage(): 

33 print("""Usage: dak process-commands [-d <directory>] [<command-file>...] 

34 

35process command files 

36""") 

37 

38 

39def main(argv=None): 

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

41 argv = sys.argv 

42 

43 arguments = [('h', 'help', 'Process-Commands::Options::Help'), 

44 ('d', 'directory', 'Process-Commands::Options::Directory', 'HasArg')] 

45 

46 cnf = Config() 

47 cnf['Process-Commands::Options::Dummy'] = '' 

48 filenames = apt_pkg.parse_commandline(cnf.Cnf, arguments, argv) 

49 options = cnf.subtree('Process-Commands::Options') 

50 

51 if 'Help' in options or (len(filenames) == 0 and 'Directory' not in options): 

52 usage() 

53 sys.exit(0) 

54 

55 log = Logger('command') 

56 

57 now = datetime.datetime.now() 

58 donedir = os.path.join(cnf['Dir::Done'], now.strftime('%Y/%m/%d')) 

59 rejectdir = cnf['Dir::Reject'] 

60 

61 if len(filenames) == 0: 61 ↛ 65line 61 didn't jump to line 65, because the condition on line 61 was never false

62 cdir = options['Directory'] 

63 filenames = [os.path.join(cdir, fn) for fn in os.listdir(cdir) if fn.endswith('.dak-commands')] 

64 

65 for fn in filenames: 

66 basename = os.path.basename(fn) 

67 if not fn.endswith('.dak-commands'): 67 ↛ 68line 67 didn't jump to line 68, because the condition on line 67 was never true

68 log.log(['unexpected filename', basename]) 

69 continue 

70 

71 with open(fn, 'rb') as fh: 

72 data = fh.read() 

73 

74 try: 

75 command = CommandFile(basename, data, log) 

76 command.evaluate() 

77 except CommandError as e: 

78 created = os.stat(fn).st_mtime 

79 now = time.time() 

80 too_new = (now - created < int(cnf.get('Dinstall::SkipTime', '60'))) 

81 if too_new: 

82 log.log(['skipped (too new)']) 

83 continue 

84 log.log(['reject', basename, e]) 

85 except Exception as e: 

86 log.log_traceback('Exception while processing %s:' % (basename), e) 

87 dst = find_next_free(os.path.join(rejectdir, basename)) 

88 else: 

89 log.log(['done', basename]) 

90 dst = find_next_free(os.path.join(donedir, basename)) 

91 

92 with FilesystemTransaction() as fs: 

93 fs.unlink(fn) 

94 fs.create(dst, mode=0o644, text=False).write(data) 

95 fs.commit() 

96 

97 log.close() 

98 

99 

100if __name__ == '__main__': 

101 main()