Update with curl statements
[releng-anteater.git] / anteater / main.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 ##############################################################################
4 # Copyright (c) 2017 Luke Hinds <lhinds@redhat.com>, Red Hat
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 # from __future__ import division, print_function, absolute_import
13
14 """Anteater - CI Gate Checks.
15
16 Usage:
17   anteater (-p |--project) <project> [(-ps |--patchset) <patchset>]
18   anteater (-p |--project) <project> [--path <project_path>]
19   anteater (-h | --help)
20   anteater --version
21
22 Options:
23   -h --help     Show this screen.
24   --version     Show version.
25 """
26 from __future__ import absolute_import
27
28 import errno
29 import logging
30
31 import os
32 import six.moves.configparser
33 from docopt import docopt
34
35 from anteater import LOG
36 from anteater.src.patch_scan import prepare_patchset
37 from anteater.src.project_scan import prepare_project
38
39 config = six.moves.configparser.RawConfigParser()
40 config.read('anteater.conf')
41 reports_dir = config.get('config', 'reports_dir')
42 __version__ = "0.1"
43 logger = logging.getLogger(__name__)
44
45
46 def _init_logging(anteater_log):
47     """ Setup root logger for package """
48
49     LOG.setLevel(logging.DEBUG)
50     ch = logging.StreamHandler()
51     formatter = logging.Formatter('%(asctime)s - %(name)s - '
52                                   '%(levelname)s - %(message)s')
53     ch.setFormatter(formatter)
54     ch.setLevel(logging.DEBUG)
55
56     # create the directory if it does not exist
57     path = os.path.dirname(anteater_log)
58     try:
59         os.makedirs(path)
60     except OSError as e:
61         if e.errno != errno.EEXIST:
62             raise
63
64     handler = logging.FileHandler(anteater_log)
65     handler.setFormatter(formatter)
66     handler.setLevel(logging.DEBUG)
67     del logging.root.handlers[:]
68     logging.root.addHandler(ch)
69     logging.root.addHandler(handler)
70
71
72 def check_dir():
73     """ Creates a directory for scan reports """
74     try:
75         os.makedirs(reports_dir)
76         logger.info('Creating reports directory: %s', reports_dir)
77     except OSError as e:
78         if e.errno != errno.EEXIST:
79             raise
80
81
82 def main():
83     """ Main function, mostly for passing arguments """
84     _init_logging(config.get('config', 'anteater_log'))
85     check_dir()
86     arguments = docopt(__doc__, version=__version__)
87
88     if arguments['<patchset>']:
89         prepare_patchset(arguments['<project>'], arguments['<patchset>'])
90     elif arguments['<project_path>']:
91         prepare_project(arguments['<project>'], arguments['<project_path>'])
92
93
94 if __name__ == "__main__":
95     main()