chiark / gitweb /
Merge branch 'master' of gitorious.org:f-droid/fdroidserver
[fdroidserver.git] / fdroidserver / scanner.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3 #
4 # scanner.py - part of the FDroid server tools
5 # Copyright (C) 2010-13, Ciaran Gultnieks, ciaran@ciarang.com
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Affero General Public License for more details.
16 #
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 import sys
21 import os
22 import traceback
23 from optparse import OptionParser
24 import common
25 from common import BuildException
26 from common import VCSException
27
28 config = {}
29
30 def main():
31
32     # Read configuration...
33     common.read_config(config)
34
35
36     # Parse command line...
37     parser = OptionParser()
38     parser.add_option("-v", "--verbose", action="store_true", default=False,
39                       help="Spew out even more information than normal")
40     parser.add_option("-p", "--package", default=None,
41                       help="Scan only the specified package")
42     parser.add_option("--nosvn", action="store_true", default=False,
43                       help="Skip svn repositories - for test purposes, because they are too slow.")
44     (options, args) = parser.parse_args()
45
46     # Get all apps...
47     apps = common.read_metadata(options.verbose)
48
49     # Filter apps according to command-line options
50     if options.package:
51         apps = [app for app in apps if app['id'] == options.package]
52         if len(apps) == 0:
53             print "No such package"
54             sys.exit(1)
55
56     problems = []
57
58     build_dir = 'build'
59     if not os.path.isdir(build_dir):
60         print "Creating build directory"
61         os.makedirs(build_dir)
62     srclib_dir = os.path.join(build_dir, 'srclib')
63     extlib_dir = os.path.join(build_dir, 'extlib')
64
65     for app in apps:
66
67         skip = False
68         if app['Disabled']:
69             print "Skipping %s: disabled" % app['id']
70             skip = True
71         elif not app['builds']:
72             print "Skipping %s: no builds specified" % app['id']
73             skip = True
74         elif options.nosvn and app['Repo Type'] == 'svn':
75             skip = True
76
77         if not skip:
78
79             print "Processing " + app['id']
80
81             try:
82
83                 build_dir = 'build/' + app['id']
84
85                 # Set up vcs interface and make sure we have the latest code...
86                 vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir,
87                         config['sdk_path'])
88
89                 for thisbuild in app['builds']:
90
91                     if 'disable' in thisbuild:
92                         print ("..skipping version " + thisbuild['version'] + " - " +
93                                 thisbuild.get('disable', thisbuild['commit'][1:]))
94                     else:
95                         print "..scanning version " + thisbuild['version']
96
97                         # Prepare the source code...
98                         root_dir, _ = common.prepare_source(vcs, app, thisbuild,
99                                 build_dir, srclib_dir, extlib_dir,
100                                 config['sdk_path'], config['ndk_path'],
101                                 config['javacc_path'], config['mvn3'],
102                                 options.verbose, False)
103
104                         # Do the scan...
105                         buildprobs = common.scan_source(build_dir, root_dir, thisbuild)
106                         for problem in buildprobs:
107                             problems.append(problem + 
108                                 ' in ' + app['id'] + ' ' + thisbuild['version'])
109
110             except BuildException as be:
111                 msg = "Could not scan app %s due to BuildException: %s" % (app['id'], be)
112                 problems.append(msg)
113             except VCSException as vcse:
114                 msg = "VCS error while scanning app %s: %s" % (app['id'], vcse)
115                 problems.append(msg)
116             except Exception:
117                 msg = "Could not scan app %s due to unknown error: %s" % (app['id'], traceback.format_exc())
118                 problems.append(msg)
119
120     print "Finished:"
121     for problem in problems:
122         print problem
123     print str(len(problems)) + ' problems.'
124
125 if __name__ == "__main__":
126     main()
127