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