2 # -*- coding: utf-8 -*-
4 # stats.py - part of the FDroid server tools
5 # Copyright (C) 2010-12, Ciaran Gultnieks, ciaran@ciarang.com
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.
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.
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/>.
28 from optparse import OptionParser
35 # Read configuration...
38 execfile('config.py', globals())
41 print "Stats are disabled - check your configuration"
44 # Parse command line...
45 parser = OptionParser()
46 parser.add_option("-v", "--verbose", action="store_true", default=False,
47 help="Spew out even more information than normal")
48 parser.add_option("-d", "--download", action="store_true", default=False,
49 help="Download logs we don't have")
50 (options, args) = parser.parse_args()
52 # Get all metadata-defined apps...
53 metaapps = common.read_metadata(options.verbose)
56 logsdir = os.path.join(statsdir, 'logs')
57 logsarchivedir = os.path.join(logsdir, 'archive')
58 datadir = os.path.join(statsdir, 'data')
59 if not os.path.exists(statsdir):
61 if not os.path.exists(logsdir):
63 if not os.path.exists(datadir):
67 # Get any access logs we don't have...
71 print 'Retrieving logs'
72 ssh = paramiko.SSHClient()
73 ssh.load_system_host_keys()
74 ssh.connect('f-droid.org', username='fdroid', timeout=10,
75 key_filename=webserver_keyfile)
77 ftp.get_channel().settimeout(15)
83 if f.startswith('access-') and f.endswith('.log'):
85 destpath = os.path.join(logsdir, f)
86 archivepath = os.path.join(logsarchivedir, f + '.gz')
87 if os.path.exists(archivepath):
88 if os.path.exists(destpath):
89 # Just in case we have it archived but failed to remove
93 destsize = ftp.stat(f).st_size
94 if (not os.path.exists(destpath) or
95 os.path.getsize(destpath) != destsize):
96 print "...retrieving " + f
98 except Exception as e:
109 logexpr = '(?P<ip>[.:0-9a-fA-F]+) - - \[(?P<time>.*?)\] "GET (?P<uri>.*?) HTTP/1.\d" (?P<statuscode>\d+) \d+ "(?P<referral>.*?)" "(?P<useragent>.*?)"'
110 logsearch = re.compile(logexpr).search
113 knownapks = common.KnownApks()
114 for logfile in glob.glob(os.path.join(logsdir,'access-*.log')):
115 logdate = logfile[len(logsdir) + 1 + len('access-'):-4]
116 matches = (logsearch(line) for line in file(logfile))
117 for match in matches:
118 if match and match.group('statuscode') == '200':
119 uri = match.group('uri')
120 if uri.endswith('.apk'):
121 _, apkname = os.path.split(uri)
122 app = knownapks.getapp(apkname)
130 if not apkname in unknownapks:
131 unknownapks.append(apkname)
133 # Calculate and write stats for total downloads...
136 for app, count in apps.iteritems():
137 lst.append(app + " " + str(count))
138 alldownloads += count
139 lst.append("ALL " + str(alldownloads))
140 f = open('stats/total_downloads_app.txt', 'w')
141 f.write('# Total downloads by application, since October 2011\n')
142 for line in sorted(lst):
146 # Calculate and write stats for repo types...
149 if len(app['Repo Type']) == 0:
152 rtype = app['Repo Type']
153 if rtype in repotypes:
154 repotypes[rtype] += 1;
157 f = open('stats/repotypes.txt', 'w')
158 for rtype, count in repotypes.iteritems():
159 f.write(rtype + ' ' + str(count) + '\n')
162 # Calculate and write stats for update check modes...
165 checkmode = app['Update Check Mode']
166 if checkmode in ucms:
167 ucms[checkmode] += 1;
170 f = open('stats/update_check_modes.txt', 'w')
171 for checkmode, count in ucms.iteritems():
172 f.write(checkmode + ' ' + str(count) + '\n')
175 # Calculate and write stats for licenses...
178 license = app['License']
179 if license in licenses:
180 licenses[license] += 1;
182 licenses[license] = 1
183 f = open('stats/licenses.txt', 'w')
184 for license, count in licenses.iteritems():
185 f.write(license + ' ' + str(count) + '\n')
190 # Write list of latest apps added to the repo...
191 latest = knownapks.getlatest(10)
192 f = open('stats/latestapps.txt', 'w')
197 if len(unknownapks) > 0:
198 print '\nUnknown apks:'
199 for apk in unknownapks:
204 if __name__ == "__main__":