chiark / gitweb /
Adapt scanner, fix some other issues
[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, metadata
25 from common import BuildException
26 from common import VCSException
27
28 config = None
29 options = None
30
31 def main():
32
33     global config, options
34
35     # Parse command line...
36     parser = OptionParser(usage="Usage: %prog [options] [APPID[:VERCODE] [APPID[:VERCODE] ...]]")
37     parser.add_option("-v", "--verbose", action="store_true", default=False,
38                       help="Spew out even more information than normal")
39     parser.add_option("-p", "--package", default=None,
40                       help="Scan only the specified package")
41     parser.add_option("--nosvn", action="store_true", default=False,
42                       help="Skip svn repositories - for test purposes, because they are too slow.")
43     (options, args) = parser.parse_args()
44
45     config = common.read_config(options)
46
47     # Get all apps...
48     allapps = metadata.read_metadata()
49     apps = common.read_app_args(args, allapps, True)
50
51     problems = []
52
53     build_dir = 'build'
54     if not os.path.isdir(build_dir):
55         print "Creating build directory"
56         os.makedirs(build_dir)
57     srclib_dir = os.path.join(build_dir, 'srclib')
58     extlib_dir = os.path.join(build_dir, 'extlib')
59
60     for app in apps:
61
62         skip = False
63         if app['Disabled']:
64             print "Skipping %s: disabled" % app['id']
65             continue
66         if not app['builds']:
67             print "Skipping %s: no builds specified" % app['id']
68             continue
69         elif options.nosvn and app['Repo Type'] == 'svn':
70             continue
71
72         print "Processing " + app['id']
73
74         try:
75
76             build_dir = 'build/' + app['id']
77
78             # Set up vcs interface and make sure we have the latest code...
79             vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
80
81             for thisbuild in app['builds']:
82
83                 if 'disable' in thisbuild:
84                     print ("..skipping version " + thisbuild['version'] + " - " +
85                             thisbuild.get('disable', thisbuild['commit'][1:]))
86                 else:
87                     print "..scanning version " + thisbuild['version']
88
89                     # Prepare the source code...
90                     root_dir, _ = common.prepare_source(vcs, app, thisbuild,
91                             build_dir, srclib_dir, extlib_dir, False)
92
93                     # Do the scan...
94                     buildprobs = common.scan_source(build_dir, root_dir, thisbuild)
95                     for problem in buildprobs:
96                         problems.append(problem + 
97                             ' in ' + app['id'] + ' ' + thisbuild['version'])
98
99         except BuildException as be:
100             msg = "Could not scan app %s due to BuildException: %s" % (app['id'], be)
101             problems.append(msg)
102         except VCSException as vcse:
103             msg = "VCS error while scanning app %s: %s" % (app['id'], vcse)
104             problems.append(msg)
105         except Exception:
106             msg = "Could not scan app %s due to unknown error: %s" % (app['id'], traceback.format_exc())
107             problems.append(msg)
108
109     print "Finished:"
110     for problem in problems:
111         print problem
112     print str(len(problems)) + ' problems.'
113
114 if __name__ == "__main__":
115     main()
116