chiark / gitweb /
Merge branch 'master' into logging
[fdroidserver.git] / fdroidserver / server.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3 #
4 # server.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 subprocess
23 from optparse import OptionParser
24 import logging
25 import common
26
27 config = None
28 options = None
29
30
31 def main():
32
33     global config, options
34
35     # Parse command line...
36     parser = OptionParser()
37     parser.add_option("-v", "--verbose", action="store_true", default=False,
38                       help="Spew out even more information than normal")
39     (options, args) = parser.parse_args()
40
41     config = common.read_config(options)
42
43     if len(args) != 1:
44         logging.critical("Specify a single command")
45         sys.exit(1)
46
47     if args[0] != 'init' and args[0] != 'update':
48         logging.critical("The only commands currently supported are 'init' and 'update'")
49         sys.exit(1)
50
51     serverwebroot = config['serverwebroot'].rstrip('/').replace('//', '/')
52     host, fdroiddir = serverwebroot.split(':')
53     serverrepobase = os.path.basename(fdroiddir)
54     if 'nonstandardwebroot' in config and config['nonstandardwebroot'] == True:
55         standardwebroot = False
56     else:
57         standardwebroot = True
58     if serverrepobase != 'fdroid' and standardwebroot:
59         print('ERROR: serverwebroot does not end with "fdroid", '
60               + 'perhaps you meant one of these:\n\t'
61               + serverwebroot.rstrip('/') + '/fdroid\n\t'
62               + serverwebroot.rstrip('/').rstrip(serverrepobase) + 'fdroid')
63         sys.exit(1)
64
65     repodirs = ['repo']
66     if config['archive_older'] != 0:
67         repodirs.append('archive')
68
69     for repodir in repodirs:
70         if args[0] == 'init':
71             if subprocess.call(['ssh', '-v', host,
72                                 'mkdir -p', fdroiddir + '/' + repodir]) != 0:
73                 sys.exit(1)
74         elif args[0] == 'update':
75             index = os.path.join(repodir, 'index.xml')
76             indexjar = os.path.join(repodir, 'index.jar')
77             if subprocess.call(['rsync', '-u', '-v', '-r', '--delete',
78                                 '--exclude', index, '--exclude', indexjar,
79                                 repodir, config['serverwebroot']]) != 0:
80                 sys.exit(1)
81             if subprocess.call(['rsync', '-u', '-v', '-r', '--delete',
82                                 index,
83                                 config['serverwebroot'] + '/' + repodir]) != 0:
84                 sys.exit(1)
85             if subprocess.call(['rsync', '-u', '-v', '-r', '--delete',
86                                 indexjar,
87                                 config['serverwebroot'] + '/' + repodir]) != 0:
88                 sys.exit(1)
89
90     sys.exit(0)
91
92 if __name__ == "__main__":
93     main()