chiark / gitweb /
checkupdates: use html.unescape instead of HTMLParser.unescape
[fdroidserver.git] / fdroidserver / checkupdates.py
index 7f5314b8d72b44ee754fcffcf6a43665f9866849..217139d19a956fb052d5ac5f5365e89615ba5f49 100644 (file)
@@ -17,7 +17,6 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-import sys
 import os
 import re
 import urllib.request
@@ -26,15 +25,14 @@ import time
 import subprocess
 from argparse import ArgumentParser
 import traceback
-from html.parser import HTMLParser
+import html
 from distutils.version import LooseVersion
 import logging
 import copy
 
 from . import common
 from . import metadata
-from .common import VCSException, FDroidException
-from .metadata import MetaDataException
+from .exception import VCSException, FDroidException, MetaDataException
 
 
 # Check for a new version by looking at a document retrieved via HTTP.
@@ -59,7 +57,7 @@ def check_http(app):
             m = re.search(codeex, page)
             if not m:
                 raise FDroidException("No RE match for version code")
-            vercode = m.group(1)
+            vercode = m.group(1).strip()
 
         version = "??"
         if len(urlver) > 0:
@@ -243,7 +241,7 @@ def check_repomanifest(app, branch=None):
         return (None, msg)
 
 
-def check_repotrunk(app, branch=None):
+def check_repotrunk(app):
 
     try:
         if app.RepoType == 'srclib':
@@ -281,7 +279,7 @@ def check_gplay(app):
     req = urllib.request.Request(url, None, headers)
     try:
         resp = urllib.request.urlopen(req, None, 20)
-        page = resp.read()
+        page = resp.read().decode()
     except urllib.error.HTTPError as e:
         return (None, str(e.code))
     except Exception as e:
@@ -291,8 +289,7 @@ def check_gplay(app):
 
     m = re.search('itemprop="softwareVersion">[ ]*([^<]+)[ ]*</div>', page)
     if m:
-        html_parser = HTMLParser()
-        version = html_parser.unescape(m.group(1))
+        version = html.unescape(m.group(1))
 
     if version == 'Varies with device':
         return (None, 'Device-variable version, cannot use this method')
@@ -372,7 +369,7 @@ def fetch_autoname(app, tag):
     return commitmsg
 
 
-def checkupdates_app(app, first=True):
+def checkupdates_app(app):
 
     # If a change is made, commitmsg should be set to a description of it.
     # Only if this is set will changes be written back to the metadata.
@@ -432,6 +429,8 @@ def checkupdates_app(app, first=True):
     elif vercode == app.CurrentVersionCode:
         logging.info("...up to date")
     else:
+        logging.debug("...updating - old vercode={0}, new vercode={1}".format(
+            app.CurrentVersionCode, vercode))
         app.CurrentVersion = version
         app.CurrentVersionCode = str(int(vercode))
         updating = True
@@ -462,22 +461,22 @@ def checkupdates_app(app, first=True):
             gotcur = False
             latest = None
             for build in app.builds:
-                if int(build.vercode) >= int(app.CurrentVersionCode):
+                if int(build.versionCode) >= int(app.CurrentVersionCode):
                     gotcur = True
-                if not latest or int(build.vercode) > int(latest.vercode):
+                if not latest or int(build.versionCode) > int(latest.versionCode):
                     latest = build
 
-            if int(latest.vercode) > int(app.CurrentVersionCode):
+            if int(latest.versionCode) > int(app.CurrentVersionCode):
                 logging.info("Refusing to auto update, since the latest build is newer")
 
             if not gotcur:
                 newbuild = copy.deepcopy(latest)
                 newbuild.disable = False
-                newbuild.vercode = app.CurrentVersionCode
-                newbuild.version = app.CurrentVersion + suffix
-                logging.info("...auto-generating build for " + newbuild.version)
-                commit = pattern.replace('%v', newbuild.version)
-                commit = commit.replace('%c', newbuild.vercode)
+                newbuild.versionCode = app.CurrentVersionCode
+                newbuild.versionName = app.CurrentVersion + suffix
+                logging.info("...auto-generating build for " + newbuild.versionName)
+                commit = pattern.replace('%v', newbuild.versionName)
+                commit = commit.replace('%c', newbuild.versionCode)
                 newbuild.commit = commit
                 app.builds.append(newbuild)
                 name = common.getappname(app)
@@ -496,8 +495,7 @@ def checkupdates_app(app, first=True):
                 gitcmd.extend(['--author', config['auto_author']])
             gitcmd.extend(["--", metadatapath])
             if subprocess.call(gitcmd) != 0:
-                logging.error("Git commit failed")
-                sys.exit(1)
+                raise FDroidException("Git commit failed")
 
 
 config = None
@@ -532,7 +530,7 @@ def main():
     apps = common.read_app_args(options.appid, allapps, False)
 
     if options.gplay:
-        for app in apps:
+        for appid, app in apps.items():
             version, reason = check_gplay(app)
             if version is None:
                 if reason == '404':
@@ -564,7 +562,10 @@ def main():
 
         logging.info("Processing " + appid + '...')
 
-        checkupdates_app(app)
+        try:
+            checkupdates_app(app)
+        except Exception as e:
+            logging.error("...checkupdate failed for {0} : {1}".format(appid, e))
 
     logging.info("Finished.")