Implements full path for hash checks of binaries
[releng-anteater.git] / anteater / src / get_lists.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 """
13     Gathers various values from the gate check yaml file and return them to the
14     calling instance
15 """
16 from __future__ import absolute_import
17
18 import logging
19 import six.moves.configparser
20 import copy
21 import os
22 import yaml
23 import re
24
25
26 config = six.moves.configparser.RawConfigParser()
27 config.read('anteater.conf')
28 logger = logging.getLogger(__name__)
29 master_list = config.get('config', 'master_list')
30 ignore_list = config.get('config', 'ignore_list')
31
32 with open(master_list, 'r') as f:
33     ml = yaml.safe_load(f)
34
35 with open(ignore_list, 'r') as f:
36     il = yaml.safe_load(f)
37
38
39 def _remove_nullvalue(contents):
40     if contents and len(contents) > 2 and 'nullvalue' in contents:
41         contents.remove('nullvalue')
42
43
44 def _merge(org, ded):
45     ret = copy.deepcopy(org)
46     for key in list(set([k for k in org] + [k for k in ded])):
47         if key in org and key in ded:
48             ret[key] = list(set(ret[key] + ded[key]))
49             _remove_nullvalue(ret[key])
50         elif key in ded:
51             ret[key] = ded[key]
52     return ret
53
54
55 class GetLists(object):
56     def __init__(self, *args):
57         # Placeholder for future args if more filters are needed
58         self.args = args
59         self.loaded = False
60
61     def load_project_exception_file(self, project_exceptions, project):
62         if self.loaded:
63             return
64         exception_file = None
65         for item in project_exceptions:
66             if project in item:
67                 exception_file = item.get(project)
68         if exception_file is not None:
69             with open(exception_file, 'r') as f:
70                 ex = yaml.safe_load(f)
71             for key in ex:
72                 if key in ml:
73                     ml[key][project] = _merge(ml[key][project], ex.get(key, None)) \
74                             if project in ml[key] else ex.get(key, None)
75             self.loaded = True
76
77     def binary_list(self, project):
78         try:
79             default_list = (ml['binaries']['binary_ignore'])
80         except KeyError:
81             logger.error('Key Error processing binary list values')
82
83         binary_re = re.compile("|".join(default_list),
84                                flags=re.IGNORECASE)
85         return binary_re
86
87     def binary_hash(self, project, patch_file):
88         self.load_project_exception_file(ml.get('project_exceptions'), project)
89         try:
90             binary_hash = (ml['binaries'][project][patch_file])
91             return binary_hash
92         except KeyError:
93             binary_hash = 'null'
94             return binary_hash
95
96
97     def file_audit_list(self, project):
98         project_list = False
99         self.load_project_exception_file(ml.get('project_exceptions'), project)
100         try:
101             default_list = set((ml['file_audits']['file_names']))
102         except KeyError:
103             logger.error('Key Error processing file_names list values')
104         try:
105             project_list = set((ml['file_audits'][project]['file_names']))
106             logger.info('file_names waivers found for %s', project)
107         except KeyError:
108             logger.info('No file_names waivers found for %s', project)
109
110         file_names_re = re.compile("|".join(default_list),
111                                    flags=re.IGNORECASE)
112
113         if project_list:
114             file_names_proj_re = re.compile("|".join(project_list),
115                                             flags=re.IGNORECASE)
116             return file_names_re, file_names_proj_re
117         else:
118             file_names_proj_re = re.compile("")
119             return file_names_re, file_names_proj_re
120
121     def file_content_list(self,  project):
122         project_list = False
123         self.load_project_exception_file(ml.get('project_exceptions'), project)
124         try:
125             master_list = (ml['file_audits']['file_contents'])
126
127         except KeyError:
128             logger.error('Key Error processing file_contents list values')
129
130         try:
131             ignore_list = il['file_audits']['file_contents']
132
133         except KeyError:
134             logger.error('Key Error processing file_contents list values')
135
136         try:
137             project_list = ml['file_audits'][project]['file_contents']
138
139         except KeyError:
140             logger.info('No file_contents waivers found  for %s', project)
141
142         ignore_list_merge = project_list + ignore_list
143
144         ignore_list_re = re.compile("|".join(ignore_list_merge), flags=re.IGNORECASE)
145
146         return master_list, ignore_list_re
147
148     def file_ignore(self):
149         try:
150             file_ignore = (ml['file_ignore'])
151         except KeyError:
152             logger.error('Key Error processing file_ignore list values')
153         return file_ignore
154
155     def licence_extensions(self):
156         try:
157             licence_extensions = (ml['licence']['licence_ext'])
158         except KeyError:
159             logger.error('Key Error processing licence_extensions list values')
160         return licence_extensions
161
162     def licence_ignore(self):
163         try:
164             licence_ignore = (ml['licence']['licence_ignore'])
165         except KeyError:
166             logger.error('Key Error processing licence_ignore list values')
167         return licence_ignore