chiark / gitweb /
PEP8 fix E225 missing whitespace around operator
[fdroidserver.git] / fdroidserver / scanner.py
index a291794aecf0bca282104661b1da611511a82180..e41e557057a8da4d9d5a71f5095504fedeed6819 100644 (file)
@@ -31,18 +31,18 @@ config = None
 options = None
 
 
-def get_gradle_compile_commands(thisbuild):
+def get_gradle_compile_commands(build):
     compileCommands = ['compile', 'releaseCompile']
-    if thisbuild['gradle'] and thisbuild['gradle'] != ['yes']:
-        compileCommands += [flavor + 'Compile' for flavor in thisbuild['gradle']]
-        compileCommands += [flavor + 'ReleaseCompile' for flavor in thisbuild['gradle']]
+    if build.gradle and build.gradle != ['yes']:
+        compileCommands += [flavor + 'Compile' for flavor in build.gradle]
+        compileCommands += [flavor + 'ReleaseCompile' for flavor in build.gradle]
 
     return [re.compile(r'\s*' + c, re.IGNORECASE) for c in compileCommands]
 
 
 # Scan the source code in the given directory (and all subdirectories)
 # and return the number of fatal problems encountered
-def scan_source(build_dir, root_dir, thisbuild):
+def scan_source(build_dir, root_dir, build):
 
     count = 0
 
@@ -78,13 +78,16 @@ def scan_source(build_dir, root_dir, thisbuild):
         'repo1.maven.org/maven2',  # mavenCentral()
         'jcenter.bintray.com',     # jcenter()
         'jitpack.io',
+        'repo.maven.apache.org/maven2',
         'oss.sonatype.org/content/repositories/snapshots',
         'oss.sonatype.org/content/repositories/releases',
+        'oss.sonatype.org/content/groups/public',
+        'clojars.org/repo',  # Clojure free software libs
         ]
     ]
 
-    scanignore = common.getpaths_map(build_dir, thisbuild['scanignore'])
-    scandelete = common.getpaths_map(build_dir, thisbuild['scandelete'])
+    scanignore = common.getpaths_map(build_dir, build.scanignore)
+    scandelete = common.getpaths_map(build_dir, build.scandelete)
 
     scanignore_worked = set()
     scandelete_worked = set()
@@ -115,6 +118,8 @@ def scan_source(build_dir, root_dir, thisbuild):
         return 0
 
     def warnproblem(what, fd):
+        if toignore(fd):
+            return
         logging.warn('Found %s at %s' % (what, fd))
 
     def handleproblem(what, fd, fp):
@@ -136,7 +141,20 @@ def scan_source(build_dir, root_dir, thisbuild):
             d = f.read(1024)
         return bool(d.translate(None, textchars))
 
-    gradle_compile_commands = get_gradle_compile_commands(thisbuild)
+    # False positives patterns for files that are binary and executable.
+    safe_paths = [re.compile(r) for r in [
+        r".*/drawable[^/]*/.*\.png$",  # png drawables
+        r".*/mipmap[^/]*/.*\.png$",    # png mipmaps
+        ]
+    ]
+
+    def safe_path(path):
+        for sp in safe_paths:
+            if sp.match(path):
+                return True
+        return False
+
+    gradle_compile_commands = get_gradle_compile_commands(build)
 
     def is_used_by_gradle(line):
         return any(command.match(line) for command in gradle_compile_commands)
@@ -151,6 +169,9 @@ def scan_source(build_dir, root_dir, thisbuild):
 
         for curfile in f:
 
+            if curfile in ['.DS_Store']:
+                continue
+
             # Path (relative) to the file
             fp = os.path.join(r, curfile)
 
@@ -190,7 +211,7 @@ def scan_source(build_dir, root_dir, thisbuild):
                 for i, line in enumerate(lines):
                     if is_used_by_gradle(line):
                         for name in suspects_found(line):
-                            count += handleproblem('usual supect \'%s\' at line %d' % (name, i+1), fd, fp)
+                            count += handleproblem('usual supect \'%s\' at line %d' % (name, i + 1), fd, fp)
                 noncomment_lines = [l for l in lines if not common.gradle_comment.match(l)]
                 joined = re.sub(r'[\n\r\s]+', ' ', ' '.join(noncomment_lines))
                 for m in gradle_mavenrepo.finditer(joined):
@@ -203,7 +224,7 @@ def scan_source(build_dir, root_dir, thisbuild):
                     count += handleproblem('binary', fd, fp)
 
             elif is_executable(fp):
-                if is_binary(fp):
+                if is_binary(fp) and not safe_path(fd):
                     warnproblem('possible binary', fd)
 
     for p in scanignore:
@@ -216,14 +237,6 @@ def scan_source(build_dir, root_dir, thisbuild):
             logging.error('Unused scandelete path: %s' % p)
             count += 1
 
-    # Presence of a jni directory without buildjni=yes might
-    # indicate a problem (if it's not a problem, explicitly use
-    # buildjni=no to bypass this check)
-    if (os.path.exists(os.path.join(root_dir, 'jni')) and
-            not thisbuild['buildjni']):
-        logging.error('Found jni directory, but buildjni is not enabled. Set it to \'no\' to ignore.')
-        count += 1
-
     return count
 
 
@@ -254,10 +267,10 @@ def main():
 
     for appid, app in apps.iteritems():
 
-        if app['Disabled']:
+        if app.Disabled:
             logging.info("Skipping %s: disabled" % appid)
             continue
-        if not app['builds']:
+        if not app.builds:
             logging.info("Skipping %s: no builds specified" % appid)
             continue
 
@@ -265,32 +278,32 @@ def main():
 
         try:
 
-            if app['Repo Type'] == 'srclib':
-                build_dir = os.path.join('build', 'srclib', app['Repo'])
+            if app.RepoType == 'srclib':
+                build_dir = os.path.join('build', 'srclib', app.Repo)
             else:
                 build_dir = os.path.join('build', appid)
 
             # Set up vcs interface and make sure we have the latest code...
-            vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
+            vcs = common.getvcs(app.RepoType, app.Repo, build_dir)
 
-            for thisbuild in app['builds']:
+            for build in app.builds:
 
-                if thisbuild['disable']:
+                if build.disable:
                     logging.info("...skipping version %s - %s" % (
-                        thisbuild['version'], thisbuild.get('disable', thisbuild['commit'][1:])))
+                        build.version, build.get('disable', build.commit[1:])))
                 else:
-                    logging.info("...scanning version " + thisbuild['version'])
+                    logging.info("...scanning version " + build.version)
 
                     # Prepare the source code...
-                    root_dir, _ = common.prepare_source(vcs, app, thisbuild,
+                    root_dir, _ = common.prepare_source(vcs, app, build,
                                                         build_dir, srclib_dir,
                                                         extlib_dir, False)
 
                     # Do the scan...
-                    count = scan_source(build_dir, root_dir, thisbuild)
+                    count = scan_source(build_dir, root_dir, build)
                     if count > 0:
                         logging.warn('Scanner found %d problems in %s (%s)' % (
-                            count, appid, thisbuild['vercode']))
+                            count, appid, build.vercode))
                         probcount += count
 
         except BuildException as be:
@@ -306,7 +319,7 @@ def main():
             probcount += 1
 
     logging.info("Finished:")
-    print "%d problems found" % probcount
+    print("%d problems found" % probcount)
 
 if __name__ == "__main__":
     main()