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 datetime
20import os
21import sys
22import time
24import apt_pkg
26from daklib.command import CommandError, CommandFile
27from daklib.config import Config
28from daklib.daklog import Logger
29from daklib.fstransactions import FilesystemTransaction
30from daklib.utils import find_next_free
33def usage():
34 print(
35 """Usage: dak process-commands [-d <directory>] [<command-file>...]
37process command files
38"""
39 )
42def main(argv=None):
43 if argv is None: 43 ↛ 46line 43 didn't jump to line 46
44 argv = sys.argv
46 arguments = [
47 ("h", "help", "Process-Commands::Options::Help"),
48 ("d", "directory", "Process-Commands::Options::Directory", "HasArg"),
49 ]
51 cnf = Config()
52 cnf["Process-Commands::Options::Dummy"] = ""
53 filenames = apt_pkg.parse_commandline(cnf.Cnf, arguments, argv)
54 options = cnf.subtree("Process-Commands::Options")
56 if "Help" in options or (len(filenames) == 0 and "Directory" not in options):
57 usage()
58 sys.exit(0)
60 log = Logger("command")
62 now = datetime.datetime.now()
63 donedir = os.path.join(cnf["Dir::Done"], now.strftime("%Y/%m/%d"))
64 rejectdir = cnf["Dir::Reject"]
66 if len(filenames) == 0: 66 ↛ 74line 66 didn't jump to line 74, because the condition on line 66 was never false
67 cdir = options["Directory"]
68 filenames = [
69 os.path.join(cdir, fn)
70 for fn in os.listdir(cdir)
71 if fn.endswith(".dak-commands")
72 ]
74 for fn in filenames:
75 basename = os.path.basename(fn)
76 if not fn.endswith(".dak-commands"): 76 ↛ 77line 76 didn't jump to line 77, because the condition on line 76 was never true
77 log.log(["unexpected filename", basename])
78 continue
80 with open(fn, "rb") as fh:
81 data = fh.read()
83 try:
84 command = CommandFile(basename, data, log)
85 command.evaluate()
86 except CommandError as e:
87 created = os.stat(fn).st_mtime
88 now = time.time()
89 too_new = now - created < int(cnf.get("Dinstall::SkipTime", "60"))
90 if too_new:
91 log.log(["skipped (too new)"])
92 continue
93 log.log(["reject", basename, e])
94 except Exception as e:
95 log.log_traceback("Exception while processing %s:" % (basename), e)
96 dst = find_next_free(os.path.join(rejectdir, basename))
97 else:
98 log.log(["done", basename])
99 dst = find_next_free(os.path.join(donedir, basename))
101 with FilesystemTransaction() as fs:
102 fs.unlink(fn)
103 fs.create(dst, mode=0o644, text=False).write(data)
104 fs.commit()
106 log.close()
109if __name__ == "__main__":
110 main()