Handle missing / deleted / renamed files correctly. 33/40233/1
authorlhinds <lhinds@redhat.com>
Fri, 25 Aug 2017 10:05:00 +0000 (11:05 +0100)
committerlhinds <lhinds@redhat.com>
Fri, 25 Aug 2017 10:05:00 +0000 (11:05 +0100)
Previously git rm or mv'ed files would be listed in the patchset.

Anteater would then attempt to open the files and fail (as they don't
exist).

This patch resolves the issue by first not sys.exit'ing on a file not
existing, and also not logging with ERROR level (which made the
security audit job incorrectly fail).

JIRA: RELENG-302

Change-Id: I6a0c56f691f4d80aca1b3509472c1d6e56d492e1
Signed-off-by: lhinds <lhinds@redhat.com>
anteater/src/patch_scan.py

index cd98523..ba0acdf 100644 (file)
@@ -118,32 +118,33 @@ def scan_patch(project, patch_file, binary_list, file_audit_list,
         try:
             fo = open(patch_file, 'r')
             lines = fo.readlines()
+            file_exists = True
         except IOError:
-            logger.error('%s does not exist', patch_file)
-            sys.exit(1)
-
-        for line in lines:
-            for key, value in master_list.iteritems():
-                regex = value['regex']
-                desc = value['desc']
-                if re.search(regex, line) and not re.search(project_list_re, line):
-                    logger.error('File contains violation: %s', patch_file)
-                    logger.error('Flagged Content: %s', line.rstrip())
-                    logger.error('Matched Regular Exp: %s', regex)
-                    logger.error('Rationale: %s', desc.rstrip())
-                    failure = True
-                    with open(reports_dir + "contents_" + project + ".log",
-                              "a") as gate_report:
-                        gate_report.write('File contains violation: {0}\n'.
-                                          format(patch_file))
-                        gate_report.write('Flagged Content: {0}'.
-                                          format(line))
-                        gate_report.write('Matched Regular Exp: {0}'.
-                                          format(regex))
-                        gate_report.write('Rationale: {0}'.
-                                          format(desc.rstrip()))
-        # Run license check
-        licence_check(project, licence_ext, licence_ignore, patch_file)
+            file_exists = False
+
+        if file_exists:
+            for line in lines:
+                for key, value in master_list.iteritems():
+                    regex = value['regex']
+                    desc = value['desc']
+                    if re.search(regex, line) and not re.search(project_list_re, line):
+                        logger.error('File contains violation: %s', patch_file)
+                        logger.error('Flagged Content: %s', line.rstrip())
+                        logger.error('Matched Regular Exp: %s', regex)
+                        logger.error('Rationale: %s', desc.rstrip())
+                        failure = True
+                        with open(reports_dir + "contents_" + project + ".log",
+                                  "a") as gate_report:
+                            gate_report.write('File contains violation: {0}\n'.
+                                              format(patch_file))
+                            gate_report.write('Flagged Content: {0}'.
+                                              format(line))
+                            gate_report.write('Matched Regular Exp: {0}'.
+                                              format(regex))
+                            gate_report.write('Rationale: {0}'.
+                                              format(desc.rstrip()))
+            # Run license check
+            licence_check(project, licence_ext, licence_ignore, patch_file)
 
 
 def licence_check(project, licence_ext,