2 # -*- coding: utf-8 -*-
3 ##############################################################################
4 # Copyright (c) 2017 Luke Hinds <lhinds@redhat.com>, Red Hat
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 ##############################################################################
13 Accepts the --patchset argument and iterates through each line of the
14 patchset file to perform various checks such as if the file is a binary, or
15 contains a blacklisted string. If any violations are found, the script
16 exits with code 1 and logs the violation(s) found.
19 from __future__ import division, print_function, absolute_import
20 from binaryornot.check import is_binary
23 import six.moves.configparser
27 from . import get_lists
29 logger = logging.getLogger(__name__)
30 config = six.moves.configparser.RawConfigParser()
31 config.read('anteater.conf')
32 reports_dir = config.get('config', 'reports_dir')
34 hasher = hashlib.sha256()
37 def prepare_patchset(project, patchset):
38 """ Create black/white lists and default / project waivers
39 and iterates over patchset file """
41 # Get Various Lists / Project Waivers
42 lists = get_lists.GetLists()
43 # Get binary white list
44 binary_list = lists.binary_list(project)
46 # Get file name black list and project waivers
47 file_audit_list, file_audit_project_list = lists.file_audit_list(project)
49 # Get file content black list and project waivers
51 file_content_project_list = lists.file_content_list(project)
54 licence_ext = lists.licence_extensions()
55 licence_ignore = lists.licence_ignore()
57 # Open patch set to get file list
58 fo = open(patchset, 'r')
59 lines = fo.readlines()
62 patch_file = line.strip('\n')
63 # Perform binary and file / content checks
64 scan_patch(project, patch_file, binary_list,
65 file_audit_list, file_audit_project_list,
66 file_content_list, file_content_project_list, licence_ext,
69 # Process each file in patch set using waivers generated above
70 # Process final result
74 def scan_patch(project, patch_file, binary_list, file_audit_list,
75 file_audit_project_list, file_content_list,
76 file_content_project_list, licence_ext, licence_ignore):
77 """ Scan actions for each commited file in patch set """
79 if is_binary(patch_file):
80 hashlist = get_lists.GetLists()
81 binary_hash = hashlist.binary_hash(project, patch_file)
82 if not binary_list.search(patch_file):
83 with open(patch_file, 'rb') as afile:
86 if hasher.hexdigest() in binary_hash:
87 logger.info('Found matching file hash for file: {0}'.
90 logger.error('Non Whitelisted Binary file: {0}'.
92 logger.error('Submit patch with the following hash: {0}'.
93 format(hasher.hexdigest()))
95 with open(reports_dir + "binaries-" + project + ".log", "a") \
97 gate_report.write('Non Whitelisted Binary file: {0}\n'.
100 # Check file names / extensions
101 if file_audit_list.search(patch_file) and not \
102 file_audit_project_list.search(patch_file):
103 match = file_audit_list.search(patch_file)
104 logger.error('Blacklisted file: {0}'.
106 logger.error('Matched String: {0}'.
107 format(match.group()))
109 with open(reports_dir + "file-names_" + project + ".log", "a") \
111 gate_report.write('Blacklisted file: {0}\n'.
113 gate_report.write('Matched String: {0}'.
114 format(match.group()))
116 # Open file to check for blacklisted content
117 fo = open(patch_file, 'r')
118 lines = fo.readlines()
121 if file_content_list.search(line) and not \
122 file_content_project_list.search(line):
123 match = file_content_list.search(line)
124 logger.error('File contains violation: {0}'.
126 logger.error('Flagged Content: {0}'.
127 format(line.rstrip()))
128 logger.error('Matched String: {0}'.
129 format(match.group()))
131 with open(reports_dir + "contents_" + project + ".log",
133 gate_report.write('File contains violation: {0}\n'.
135 gate_report.write('Flagged Content: {0}'.
137 gate_report.write('Matched String: {0}\n'.
138 format(match.group()))
141 licence_check(project, licence_ext, licence_ignore, patch_file)
144 def licence_check(project, licence_ext,
145 licence_ignore, patch_file):
146 """ Performs licence checks """
148 if patch_file.endswith(tuple(licence_ext)) \
149 and patch_file not in licence_ignore:
150 fo = open(patch_file, 'r')
152 # Note: Hardcoded use of 'copyright' & 'spdx' is the result
153 # of a decision made at 2017 plugfest to limit searches to
154 # just these two strings.
155 if re.search("copyright", content, re.IGNORECASE):
156 logger.info('Contains needed Licence string: {0}'.
158 elif re.search("spdx", content, re.IGNORECASE):
159 logger.info('Contains needed Licence string: {0}'.
162 logger.error('Licence header missing in file: {0}'.
165 with open(reports_dir + "licence-" + project + ".log", "a") \
167 gate_report.write('Licence header missing in file: {0}\n'.
171 def process_failure():
172 """ If any scan operations register a failure, sys.exit(1) is called
173 to allow jjb to register a failure"""
175 logger.error('Please visit: https://wiki.opnfv.org/x/5oey')