main.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Use of this source code is governed by a BSD-style
  2. # license that can be found in the LICENSE file.
  3. # Copyright 2019 The OSArchiver Authors. All rights reserved.
  4. """
  5. main file providing osarchiver program
  6. """
  7. import sys
  8. import os
  9. import logging
  10. import argparse
  11. import traceback
  12. from osarchiver.config import Config
  13. def parse_args():
  14. """
  15. function to parse CLI arguments
  16. return parse_args() of ArgumentParser
  17. """
  18. parser = argparse.ArgumentParser()
  19. def file_exists(one_file):
  20. if not os.path.exists(one_file):
  21. raise argparse.ArgumentTypeError(
  22. '{f} no such file'.format(f=one_file))
  23. return one_file
  24. parser.add_argument('--config',
  25. help='Configuration file to read',
  26. default=None,
  27. required=True,
  28. type=file_exists)
  29. parser.add_argument('--log-file',
  30. help='Append log to the specified file',
  31. default=None)
  32. parser.add_argument('--log-level',
  33. help='Set log level',
  34. choices=['info', 'warn', 'error', 'debug'],
  35. default='info')
  36. parser.add_argument('--debug',
  37. help='Enable debug mode',
  38. default=False,
  39. action='store_true')
  40. parser.add_argument('--dry-run',
  41. help='Display what would be done without'
  42. ' really deleting or writing data',
  43. default=False,
  44. action='store_true')
  45. args = parser.parse_args()
  46. if args.debug:
  47. args.log_level = 'debug'
  48. return args
  49. def configure_logger(level='info', log_file=None):
  50. """
  51. function that configure logging module
  52. """
  53. logger = logging.getLogger()
  54. logger.setLevel(getattr(logging, level.upper()))
  55. formatter = logging.Formatter(fmt='%(asctime)s %(levelname)s: %(message)s')
  56. stdout_handler = logging.StreamHandler(stream=sys.stdout)
  57. stdout_handler.setFormatter(formatter)
  58. logger.addHandler(stdout_handler)
  59. if log_file is not None:
  60. file_handler = logging.FileHandler(filename=log_file, encoding='utf-8')
  61. file_handler.setFormatter(formatter)
  62. logger.addHandler(file_handler)
  63. def run():
  64. """
  65. main function that is called when running osarchiver script
  66. It parses arguments, configure logging, load the configuration file and for
  67. each archiver call the run() method
  68. """
  69. try:
  70. args = parse_args()
  71. config = Config(file_path=args.config, dry_run=args.dry_run)
  72. configure_logger(level=args.log_level, log_file=args.log_file)
  73. for archiver in config.archivers:
  74. logging.info("Running archiver %s", archiver.name)
  75. archiver.run()
  76. except KeyboardInterrupt:
  77. logging.info("Keyboard interrupt detected")
  78. for archiver in config.archivers:
  79. archiver.clean_exit()
  80. return 1
  81. except Exception as my_exception:
  82. logging.error(my_exception)
  83. logging.error("Full traceback is: %s", traceback.format_exc())
  84. for archiver in config.archivers:
  85. archiver.clean_exit()
  86. return 1
  87. return 0