chiark / gitweb /
`fdroid update` uses datetime instances for timestamps
authorHans-Christoph Steiner <hans@eds.org>
Mon, 28 Nov 2016 20:10:58 +0000 (21:10 +0100)
committerHans-Christoph Steiner <hans@eds.org>
Fri, 17 Mar 2017 12:55:40 +0000 (13:55 +0100)
Using datetime instances as the internal format makes it much easier to
convert between the formats needed for index.xml and index-v1.  apkcache
still uses time tuples and known_apks.txt still uses the ISO date.

examples/config.py
fdroidserver/common.py
fdroidserver/update.py

index 20405b07c2f6fb13849fcf594ad9559865f3a44d..4614e9ca8002c15ce227b2b400512d47f4d47650 100644 (file)
@@ -247,7 +247,8 @@ The repository of older versions of applications from the main demo repository.
 
 # Only set this to true when running a repository where you want to generate
 # stats, and only then on the master build servers, not a development
-# machine.
+# machine. If you want to keep the "added" and "last updated" dates for each
+# app and APK in your repo, then you should enable this.
 # update_stats = True
 
 # When used with stats, this is a list of IP addresses that are ignored for
index c84ddfff63efc97c8da82b953208c26ce89ab2e0..342677879e270e191e984447ae1b8f4e9a948c45 100644 (file)
@@ -36,6 +36,7 @@ import socket
 import base64
 import xml.etree.ElementTree as XMLElementTree
 
+from datetime import datetime
 from distutils.version import LooseVersion
 from queue import Queue
 from zipfile import ZipFile
@@ -1624,7 +1625,7 @@ class KnownApks:
                     if len(t) == 2:
                         self.apks[t[0]] = (t[1], None)
                     else:
-                        self.apks[t[0]] = (t[1], time.strptime(t[2], '%Y-%m-%d'))
+                        self.apks[t[0]] = (t[1], datetime.strptime(t[2], '%Y-%m-%d'))
         self.changed = False
 
     def writeifchanged(self):
@@ -1639,19 +1640,21 @@ class KnownApks:
             appid, added = app
             line = apk + ' ' + appid
             if added:
-                line += ' ' + time.strftime('%Y-%m-%d', added)
+                line += ' ' + added.strftime('%Y-%m-%d')
             lst.append(line)
 
         with open(self.path, 'w', encoding='utf8') as f:
             for line in sorted(lst, key=natural_key):
                 f.write(line + '\n')
 
-    # Record an apk (if it's new, otherwise does nothing)
-    # Returns the date it was added.
     def recordapk(self, apk, app, default_date=None):
+        '''
+        Record an apk (if it's new, otherwise does nothing)
+        Returns the date it was added as a datetime instance
+        '''
         if apk not in self.apks:
             if default_date is None:
-                default_date = time.gmtime(time.time())
+                default_date = datetime.utcnow()
             self.apks[apk] = (app, default_date)
             self.changed = True
         _, added = self.apks[apk]
index 6a22c302831598d8830e6ddb1a77fb6667ea6b42..913941ba12c534eb358f279e9a1e0d02fc71fa81 100644 (file)
@@ -35,7 +35,6 @@ import urllib.parse
 from datetime import datetime, timedelta
 from xml.dom.minidom import Document
 from argparse import ArgumentParser
-import time
 
 import collections
 from pyasn1.error import PyAsn1Error
@@ -117,8 +116,8 @@ def update_wiki(apps, sortedids, apks):
         wikidata += '{{App|id=%s|name=%s|added=%s|lastupdated=%s|source=%s|tracker=%s|web=%s|changelog=%s|donate=%s|flattr=%s|bitcoin=%s|litecoin=%s|license=%s|root=%s|author=%s|email=%s}}\n' % (
             appid,
             app.Name,
-            time.strftime('%Y-%m-%d', app.added) if app.added else '',
-            time.strftime('%Y-%m-%d', app.lastUpdated) if app.lastUpdated else '',
+            app.added.strftime('%Y-%m-%d') if app.added else '',
+            app.lastUpdated.strftime('%Y-%m-%d') if app.lastUpdated else '',
             app.SourceCode,
             app.IssueTracker,
             app.WebSite,
@@ -566,6 +565,13 @@ def scan_repo_files(apkcache, repodir, knownapks, use_date_from_file=False):
         usecache = False
         if name in apkcache:
             repo_file = apkcache[name]
+            # added time is cached as tuple but used here as datetime instance
+            if 'added' in repo_file:
+                a = repo_file['added']
+                if isinstance(a, datetime):
+                    repo_file['added'] = a
+                else:
+                    repo_file['added'] = datetime(*a[:6])
             if repo_file['sha256'] == shasum:
                 logging.debug("Reading " + name + " from cache")
                 usecache = True
@@ -926,7 +932,7 @@ def scan_apks(apkcache, repodir, knownapks, use_date_from_apk=False):
                                 os.path.join(get_icon_dir(repodir, '0'), iconfilename))
 
             if use_date_from_apk and manifest.date_time[1] != 0:
-                default_date_param = datetime(*manifest.date_time).utctimetuple()
+                default_date_param = datetime(*manifest.date_time)
             else:
                 default_date_param = None
 
@@ -1258,9 +1264,9 @@ def make_index_v0(apps, apks, repodir, repodict):
 
         addElement('id', app.id, doc, apel)
         if app.added:
-            addElement('added', time.strftime('%Y-%m-%d', app.added), doc, apel)
+            addElement('added', app.added.strftime('%Y-%m-%d'), doc, apel)
         if app.lastUpdated:
-            addElement('lastupdated', time.strftime('%Y-%m-%d', app.lastUpdated), doc, apel)
+            addElement('lastupdated', app.lastUpdated.strftime('%Y-%m-%d'), doc, apel)
         addElement('name', app.Name, doc, apel)
         addElement('summary', app.Summary, doc, apel)
         if app.icon:
@@ -1356,7 +1362,7 @@ def make_index_v0(apps, apks, repodir, repodict):
             addElementIfInApk('obbPatchFileSha256', apk,
                               'obbPatchFileSha256', doc, apkel)
             if 'added' in apk:
-                addElement('added', time.strftime('%Y-%m-%d', apk['added']), doc, apkel)
+                addElement('added', apk['added'].strftime('%Y-%m-%d'), doc, apkel)
 
             if file_extension == 'apk':  # sig is required for APKs, but only APKs
                 addElement('sig', apk['sig'], doc, apkel)