process_commands.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. from __future__ import print_function
  19. import apt_pkg
  20. import datetime
  21. import os
  22. import sys
  23. import time
  24. from daklib.config import Config
  25. from daklib.command import CommandError, CommandFile
  26. from daklib.daklog import Logger
  27. from daklib.fstransactions import FilesystemTransaction
  28. from daklib.gpg import GpgException
  29. from daklib.utils import find_next_free
  30. def usage():
  31. print("""Usage: dak process-commands [-d <directory>] [<command-file>...]
  32. process command files
  33. """)
  34. def main(argv=None):
  35. if argv is None:
  36. argv = sys.argv
  37. arguments = [('h', 'help', 'Process-Commands::Options::Help'),
  38. ('d', 'directory', 'Process-Commands::Options::Directory', 'HasArg')]
  39. cnf = Config()
  40. cnf['Process-Commands::Options::Dummy'] = ''
  41. filenames = apt_pkg.parse_commandline(cnf.Cnf, arguments, argv)
  42. options = cnf.subtree('Process-Commands::Options')
  43. if 'Help' in options or (len(filenames) == 0 and 'Directory' not in options):
  44. usage()
  45. sys.exit(0)
  46. log = Logger('command')
  47. now = datetime.datetime.now()
  48. donedir = os.path.join(cnf['Dir::Done'], now.strftime('%Y/%m/%d'))
  49. rejectdir = cnf['Dir::Reject']
  50. if len(filenames) == 0:
  51. filenames = [fn for fn in os.listdir(options['Directory']) if fn.endswith('.dak-commands')]
  52. for fn in filenames:
  53. basename = os.path.basename(fn)
  54. if not fn.endswith('.dak-commands'):
  55. log.log(['unexpected filename', basename])
  56. continue
  57. with open(fn, 'r') as fh:
  58. data = fh.read()
  59. try:
  60. command = CommandFile(basename, data, log)
  61. command.evaluate()
  62. except:
  63. created = os.stat(fn).st_mtime
  64. now = time.time()
  65. too_new = (now - created < int(cnf.get('Dinstall::SkipTime', '60')))
  66. if too_new:
  67. log.log(['skipped (too new)'])
  68. continue
  69. log.log(['reject', basename])
  70. dst = find_next_free(os.path.join(rejectdir, basename))
  71. else:
  72. log.log(['done', basename])
  73. dst = find_next_free(os.path.join(donedir, basename))
  74. with FilesystemTransaction() as fs:
  75. fs.unlink(fn)
  76. fs.create(dst, mode=0o644).write(data)
  77. fs.commit()
  78. log.close()
  79. if __name__ == '__main__':
  80. main()