changeset 4:907fa3e7cc2b

Add fallback to pkg_resources requirement format
author Cédric Krier <ced@b2ck.com>
date Sat, 21 Feb 2015 23:16:43 +0100
parents 1bd78eb130e3
children f800e5584b03
files farmhouse.py
diffstat 1 files changed, 21 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/farmhouse.py	Sat Feb 21 19:32:20 2015 +0100
+++ b/farmhouse.py	Sat Feb 21 23:16:43 2015 +0100
@@ -12,6 +12,10 @@
 import xmlrpc.client
 from datetime import datetime
 from distutils.versionpredicate import VersionPredicate
+try:
+    from pkg_resources import parse_requirements
+except ImportError:
+    parse_requirements = None
 
 from flask import Flask, request, render_template, redirect, url_for, flash
 from flask.ext.bootstrap import Bootstrap
@@ -30,9 +34,9 @@
     if not requirements:
         return render_template('index.html')
     packages = []
-    for package in requirements.splitlines():
+    for req in requirements.splitlines():
         try:
-            predicate = VersionPredicate(package)
+            predicate = get_predicate(req)
         except ValueError as exception:
             flash(str(exception))
             return render_template('index.html')
@@ -44,6 +48,21 @@
         return redirect(url_for('atom', packages=packages))
 
 
+def get_predicate(req):
+    try:
+        return VersionPredicate(req)
+    except ValueError as exception:
+        if not parse_requirements:
+            raise exception
+        try:
+            req, = parse_requirements(req)
+        except ValueError:
+            raise exception
+        req = '%s (%s)' % (
+            req.project_name, ','.join(''.join(s) for s in req.specs))
+        return VersionPredicate(req)
+
+
 @app.route('/atom')
 @app.route('/atom/<package>')
 @cache.cached(timeout=2 * 60 * 60)