chiark / gitweb /
Revert "build: bump max_apps_per_run to 50"
[fdroidserver.git] / fdroid
1 #!/usr/bin/env python3
2 #
3 # fdroid.py - part of the FDroid server tools
4 # Copyright (C) 2010-2015, Ciaran Gultnieks, ciaran@ciarang.com
5 # Copyright (C) 2013-2014 Daniel Marti <mvdan@mvdan.cc>
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 logging
22
23 import fdroidserver.common
24 import fdroidserver.metadata
25 from fdroidserver import _
26 from argparse import ArgumentError
27 from collections import OrderedDict
28
29
30 commands = OrderedDict([
31     ("build", _("Build a package from source")),
32     ("init", _("Quickly start a new repository")),
33     ("publish", _("Sign and place packages in the repo")),
34     ("gpgsign", _("Add PGP signatures using GnuPG for packages in repo")),
35     ("update", _("Update repo information for new packages")),
36     ("verify", _("Verify the integrity of downloaded packages")),
37     ("checkupdates", _("Check for updates to applications")),
38     ("import", _("Add a new application from its source code")),
39     ("install", _("Install built packages on devices")),
40     ("readmeta", _("Read all the metadata files and exit")),
41     ("rewritemeta", _("Rewrite all the metadata files")),
42     ("lint", _("Warn about possible metadata errors")),
43     ("scanner", _("Scan the source code of a package")),
44     ("dscanner", _("Dynamically scan APKs post build")),
45     ("stats", _("Update the stats of the repo")),
46     ("server", _("Interact with the repo HTTP server")),
47     ("signindex", _("Sign indexes created using update --nosign")),
48     ("btlog", _("Update the binary transparency log for a URL")),
49     ("signatures", _("Extract signatures from APKs")),
50     ("nightly", _("Set up an app build for a nightly build repo")),
51     ("mirror", _("Download complete mirrors of small repos")),
52 ])
53
54
55 def print_help():
56     print(_("usage: ") + _("fdroid [<command>] [-h|--help|--version|<args>]"))
57     print("")
58     print(_("Valid commands are:"))
59     for cmd, summary in commands.items():
60         print("   " + cmd + ' ' * (15 - len(cmd)) + summary)
61     print("")
62
63
64 def main():
65
66     if len(sys.argv) <= 1:
67         print_help()
68         sys.exit(0)
69
70     command = sys.argv[1]
71     if command not in commands:
72         if command in ('-h', '--help'):
73             print_help()
74             sys.exit(0)
75         elif command == '--version':
76             import os.path
77             output = _('no version info found!')
78             cmddir = os.path.realpath(os.path.dirname(__file__))
79             moduledir = os.path.realpath(os.path.dirname(fdroidserver.common.__file__) + '/..')
80             if cmddir == moduledir:
81                 # running from git
82                 os.chdir(cmddir)
83                 if os.path.isdir('.git'):
84                     import subprocess
85                     try:
86                         output = subprocess.check_output(['git', 'describe'],
87                                                          stderr=subprocess.STDOUT,
88                                                          universal_newlines=True)
89                     except subprocess.CalledProcessError:
90                         output = 'git commit ' + subprocess.check_output(['git', 'rev-parse', 'HEAD'],
91                                                                          universal_newlines=True)
92                 elif os.path.exists('setup.py'):
93                     import re
94                     m = re.search(r'''.*[\s,\(]+version\s*=\s*["']([0-9a-z.]+)["'].*''',
95                                   open('setup.py').read(), flags=re.MULTILINE)
96                     if m:
97                         output = m.group(1) + '\n'
98             else:
99                 from pkg_resources import get_distribution
100                 output = get_distribution('fdroidserver').version + '\n'
101             print(output),
102             sys.exit(0)
103         else:
104             print(_("Command '%s' not recognised.\n" % command))
105             print_help()
106             sys.exit(1)
107
108     verbose = any(s in sys.argv for s in ['-v', '--verbose'])
109     quiet = any(s in sys.argv for s in ['-q', '--quiet'])
110
111     # Helpful to differentiate warnings from errors even when on quiet
112     logformat = '%(levelname)s: %(message)s'
113     loglevel = logging.INFO
114     if verbose:
115         loglevel = logging.DEBUG
116     elif quiet:
117         loglevel = logging.WARN
118
119     logging.basicConfig(format=logformat, level=loglevel)
120
121     if verbose and quiet:
122         logging.critical("Specifying --verbose and --quiet and the same time is silly")
123         sys.exit(1)
124
125     # Trick optparse into displaying the right usage when --help is used.
126     sys.argv[0] += ' ' + command
127
128     del sys.argv[1]
129     mod = __import__('fdroidserver.' + command, None, None, [command])
130
131     try:
132         mod.main()
133     # These are ours, contain a proper message and are "expected"
134     except (fdroidserver.common.FDroidException,
135             fdroidserver.metadata.MetaDataException) as e:
136         if verbose:
137             raise
138         else:
139             logging.critical(str(e))
140         sys.exit(1)
141     except ArgumentError as e:
142         logging.critical(str(e))
143         sys.exit(1)
144     except KeyboardInterrupt:
145         print('')
146         sys.exit(1)
147     # These should only be unexpected crashes due to bugs in the code
148     # str(e) often doesn't contain a reason, so just show the backtrace
149     except Exception as e:
150         logging.critical(_("Unknown exception found!"))
151         raise
152     sys.exit(0)
153
154
155 if __name__ == "__main__":
156     main()