chiark / gitweb /
Switch all headers to python3
[fdroidserver.git] / fdroidserver / stats.py
index ac9896144ce55f7f5f2e6bd360bc30caceaa7787..351390fbf77bbc4dca6fc8854d655efd825829f1 100644 (file)
@@ -1,5 +1,4 @@
-#!/usr/bin/env python2
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
 #
 # stats.py - part of the FDroid server tools
 # Copyright (C) 2010-13, Ciaran Gultnieks, ciaran@ciarang.com
@@ -45,6 +44,13 @@ options = None
 config = None
 
 
+def most_common_stable(counts):
+    pairs = []
+    for s in counts:
+        pairs.append((s, counts[s]))
+    return sorted(pairs, key=lambda t: (-t[1], t[0]))
+
+
 def main():
 
     global options, config
@@ -68,8 +74,8 @@ def main():
         sys.exit(1)
 
     # Get all metadata-defined apps...
-    allmetaapps = [a for a in metadata.read_metadata().itervalues()]
-    metaapps = [a for a in allmetaapps if not a['Disabled']]
+    allmetaapps = [app for app in metadata.read_metadata().itervalues()]
+    metaapps = [app for app in allmetaapps if not app.Disabled]
 
     statsdir = 'stats'
     logsdir = os.path.join(statsdir, 'logs')
@@ -222,7 +228,7 @@ def main():
             rtype = common.getsrclibvcs(app.Repo)
         repotypes[rtype] += 1
     with open(os.path.join(statsdir, 'repotypes.txt'), 'w') as f:
-        for rtype, count in repotypes.most_common():
+        for rtype, count in most_common_stable(repotypes):
             f.write(rtype + ' ' + str(count) + '\n')
 
     # Calculate and write stats for update check modes...
@@ -236,7 +242,7 @@ def main():
             checkmode = checkmode[:4]
         ucms[checkmode] += 1
     with open(os.path.join(statsdir, 'update_check_modes.txt'), 'w') as f:
-        for checkmode, count in ucms.most_common():
+        for checkmode, count in most_common_stable(ucms):
             f.write(checkmode + ' ' + str(count) + '\n')
 
     logging.info("Processing categories...")
@@ -245,7 +251,7 @@ def main():
         for category in app.Categories:
             ctgs[category] += 1
     with open(os.path.join(statsdir, 'categories.txt'), 'w') as f:
-        for category, count in ctgs.most_common():
+        for category, count in most_common_stable(ctgs):
             f.write(category + ' ' + str(count) + '\n')
 
     logging.info("Processing antifeatures...")
@@ -256,7 +262,7 @@ def main():
         for antifeature in app.AntiFeatures:
             afs[antifeature] += 1
     with open(os.path.join(statsdir, 'antifeatures.txt'), 'w') as f:
-        for antifeature, count in afs.most_common():
+        for antifeature, count in most_common_stable(afs):
             f.write(antifeature + ' ' + str(count) + '\n')
 
     # Calculate and write stats for licenses...
@@ -266,12 +272,12 @@ def main():
         license = app.License
         licenses[license] += 1
     with open(os.path.join(statsdir, 'licenses.txt'), 'w') as f:
-        for license, count in licenses.most_common():
+        for license, count in most_common_stable(licenses):
             f.write(license + ' ' + str(count) + '\n')
 
     # Write list of disabled apps...
     logging.info("Processing disabled apps...")
-    disabled = [a['id'] for a in allmetaapps if a['Disabled']]
+    disabled = [app.id for app in allmetaapps if app.Disabled]
     with open(os.path.join(statsdir, 'disabled_apps.txt'), 'w') as f:
         for appid in sorted(disabled):
             f.write(appid + '\n')