chiark / gitweb /
stats: fix a few dict accesses left over
[fdroidserver.git] / fdroidserver / stats.py
index b320f9dbcbd470ced610f8a4f4c02c1df8605b94..f80505c354ae4610dd60477f4f4653aa33f15ee7 100644 (file)
@@ -24,7 +24,7 @@ import time
 import traceback
 import glob
 import json
-from optparse import OptionParser
+from argparse import ArgumentParser
 import paramiko
 import socket
 import logging
@@ -50,19 +50,16 @@ def main():
     global options, config
 
     # Parse command line...
-    parser = OptionParser()
-    parser.add_option("-v", "--verbose", action="store_true", default=False,
-                      help="Spew out even more information than normal")
-    parser.add_option("-q", "--quiet", action="store_true", default=False,
-                      help="Restrict output to warnings and errors")
-    parser.add_option("-d", "--download", action="store_true", default=False,
-                      help="Download logs we don't have")
-    parser.add_option("--recalc", action="store_true", default=False,
-                      help="Recalculate aggregate stats - use when changes "
-                      "have been made that would invalidate old cached data.")
-    parser.add_option("--nologs", action="store_true", default=False,
-                      help="Don't do anything logs-related")
-    (options, args) = parser.parse_args()
+    parser = ArgumentParser()
+    common.setup_global_opts(parser)
+    parser.add_argument("-d", "--download", action="store_true", default=False,
+                        help="Download logs we don't have")
+    parser.add_argument("--recalc", action="store_true", default=False,
+                        help="Recalculate aggregate stats - use when changes "
+                        "have been made that would invalidate old cached data.")
+    parser.add_argument("--nologs", action="store_true", default=False,
+                        help="Don't do anything logs-related")
+    options = parser.parse_args()
 
     config = common.read_config(options)
 
@@ -71,8 +68,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')
@@ -200,7 +197,7 @@ def main():
                             count)
             alldownloads += count
         lst.append("ALL " + str(alldownloads))
-        with open('stats/total_downloads_app.txt', 'w') as f:
+        with open(os.path.join(statsdir, 'total_downloads_app.txt'), 'w') as f:
             f.write('# Total downloads by application, since October 2011\n')
             for line in sorted(lst):
                 f.write(line + '\n')
@@ -210,7 +207,7 @@ def main():
             count = appsvercount[appver]
             lst.append(appver + " " + str(count))
 
-        with open('stats/total_downloads_app_version.txt', 'w') as f:
+        with open(os.path.join(statsdir, 'total_downloads_app_version.txt'), 'w') as f:
             f.write('# Total downloads by application and version, '
                     'since October 2011\n')
             for line in sorted(lst):
@@ -220,11 +217,11 @@ def main():
     logging.info("Processing repo types...")
     repotypes = Counter()
     for app in metaapps:
-        rtype = app['Repo Type'] or 'none'
+        rtype = app.RepoType or 'none'
         if rtype == 'srclib':
-            rtype = common.getsrclibvcs(app['Repo'])
+            rtype = common.getsrclibvcs(app.Repo)
         repotypes[rtype] += 1
-    with open('stats/repotypes.txt', 'w') as f:
+    with open(os.path.join(statsdir, 'repotypes.txt'), 'w') as f:
         for rtype, count in repotypes.most_common():
             f.write(rtype + ' ' + str(count) + '\n')
 
@@ -232,34 +229,33 @@ def main():
     logging.info("Processing update check modes...")
     ucms = Counter()
     for app in metaapps:
-        checkmode = app['Update Check Mode']
+        checkmode = app.UpdateCheckMode
         if checkmode.startswith('RepoManifest/'):
             checkmode = checkmode[:12]
         if checkmode.startswith('Tags '):
             checkmode = checkmode[:4]
         ucms[checkmode] += 1
-    with open('stats/update_check_modes.txt', 'w') as f:
+    with open(os.path.join(statsdir, 'update_check_modes.txt'), 'w') as f:
         for checkmode, count in ucms.most_common():
             f.write(checkmode + ' ' + str(count) + '\n')
 
     logging.info("Processing categories...")
     ctgs = Counter()
     for app in metaapps:
-        for category in app['Categories']:
+        for category in app.Categories:
             ctgs[category] += 1
-    with open('stats/categories.txt', 'w') as f:
+    with open(os.path.join(statsdir, 'categories.txt'), 'w') as f:
         for category, count in ctgs.most_common():
             f.write(category + ' ' + str(count) + '\n')
 
     logging.info("Processing antifeatures...")
     afs = Counter()
     for app in metaapps:
-        if app['AntiFeatures'] is None:
+        if app.AntiFeatures is None:
             continue
-        antifeatures = [a.strip() for a in app['AntiFeatures'].split(',')]
-        for antifeature in antifeatures:
+        for antifeature in app.AntiFeatures:
             afs[antifeature] += 1
-    with open('stats/antifeatures.txt', 'w') as f:
+    with open(os.path.join(statsdir, 'antifeatures.txt'), 'w') as f:
         for antifeature, count in afs.most_common():
             f.write(antifeature + ' ' + str(count) + '\n')
 
@@ -267,23 +263,23 @@ def main():
     logging.info("Processing licenses...")
     licenses = Counter()
     for app in metaapps:
-        license = app['License']
+        license = app.License
         licenses[license] += 1
-    with open('stats/licenses.txt', 'w') as f:
+    with open(os.path.join(statsdir, 'licenses.txt'), 'w') as f:
         for license, count in licenses.most_common():
             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']]
-    with open('stats/disabled_apps.txt', 'w') as f:
+    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')
 
     # Write list of latest apps added to the repo...
     logging.info("Processing latest apps...")
     latest = knownapks.getlatest(10)
-    with open('stats/latestapps.txt', 'w') as f:
+    with open(os.path.join(statsdir, 'latestapps.txt'), 'w') as f:
         for appid in latest:
             f.write(appid + '\n')