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