Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

automatic discovery of latest development version

  • Loading branch information...
commit 64594c8b45c49c3858e27aad7f78d8fa5ec16479 1 parent 58d2910
@brosner brosner authored
View
8 pinaxsite_project/apps/downloads/urls.py
@@ -1,10 +1,6 @@
-from django.conf.urls.defaults import *
-from django.views.generic.simple import direct_to_template
-
+from django.conf.urls.defaults import patterns, url
urlpatterns = patterns("",
- url(r"^$", direct_to_template, {
- "template": "downloads/home.html",
- }, name="downloads_home"),
+ url(r"^$", "downloads.views.download_index", name="download_index"),
)
View
87 pinaxsite_project/apps/downloads/utils.py
@@ -0,0 +1,87 @@
+import posixpath
+import re
+import urllib2
+import urlparse
+
+from downloads.verlib import NormalizedVersion
+
+
+_href_re = re.compile('href=(?:"([^"]*)"|\'([^\']*)\'|([^>\\s\\n]*))', re.I|re.S)
+_clean_re = re.compile(r'[^a-z0-9$&+,/:;=?@.#%_\\|-]', re.I)
+_egg_info_re = re.compile(r'([a-z0-9_.]+)-([a-z0-9_.-]+)', re.I)
+
+
+class Link(object):
+
+ @classmethod
+ def dirty(cls, url):
+ url = _clean_re.sub(lambda m: "%%%2x" % ord(m.group(0)), url)
+ return cls(url)
+
+ def __init__(self, url):
+ self.url = url
+
+ @property
+ def path(self):
+ return urlparse.urlsplit(self.url)[2]
+
+ def splitext(self):
+ path = posixpath.basename(self.path.rstrip("/"))
+ base, ext = posixpath.splitext(path)
+ if base.lower().endswith(".tar"):
+ ext = base[-4:] + ext
+ base = base[:-4]
+ return base, ext
+
+ _egg_fragment_re = re.compile(r'#egg=([^&]*)')
+
+ @property
+ def egg_fragment(self):
+ m = self._egg_fragment_re.search(self.url)
+ if not m:
+ return None
+ return m.group(1)
+
+
+def parse_versions(distribution, content):
+ """
+ Given a bunch of HTML (content) work out what versions are on the page.
+ """
+ search_name = distribution
+ links = []
+ for match in _href_re.finditer(content):
+ url = match.group(1) or match.group(2) or match.group(3)
+ links.append(Link.dirty(url))
+ for link in links:
+ if link.egg_fragment:
+ egg_info = link.egg_fragment
+ else:
+ egg_info, ext = link.splitext()
+ if not ext:
+ continue
+ if egg_info.endswith(".tar"):
+ # Special double-extension case:
+ egg_info = egg_info[:-4]
+ ext = ".tar" + ext
+ if ext not in [".tar.gz", ".tar.bz2", ".tar", ".tgz", ".zip"]:
+ continue
+ m = _egg_info_re.search(egg_info)
+ if not match:
+ version = None
+ name = m.group(0).lower()
+ # to match the "safe" name that pkg_resources creates
+ name = name.replace("_", "-")
+ if name.startswith(search_name.lower()):
+ version = m.group(0)[len(search_name):].lstrip("-")
+ else:
+ version = None
+ if version is None:
+ continue
+ yield NormalizedVersion(version)
+
+
+def latest_version(distribution, index_url):
+ url = posixpath.join(index_url, distribution)
+ content = urllib2.urlopen(url, timeout=1).read()
+ versions = parse_versions(distribution, content)
+ return sorted(versions, reverse=True)[0]
View
10 pinaxsite_project/apps/downloads/views.py
@@ -0,0 +1,10 @@
+from django.shortcuts import render
+
+from downloads.utils import latest_version
+
+
+def download_index(request):
+ ctx = {
+ "latest_dev_version": latest_version("Pinax", "http://dist.pinaxproject.com/dev/"),
+ }
+ return render(request, "downloads/index.html", ctx)
View
2  pinaxsite_project/templates/_nav.html
@@ -2,7 +2,7 @@
<nav>
<ul class="nav">
- <li><a href="{% url downloads_home %}" id="download-nav" class="first">Downloads</a></li>
+ <li><a href="{% url download_index %}" id="download-nav" class="first">Downloads</a></li>
<li><a href="http://pinax.readthedocs.org/" id="documentation-nav">Documentation</a></li>
<li><a href="http://code.pinaxproject.com/" id="development-nav">Development</a></li>
<li><a href="{% url packages_list %}" id="ecosystem-nav">Ecosystem</a></li>
View
2  pinaxsite_project/templates/downloads/home.html → pinaxsite_project/templates/downloads/index.html
@@ -10,7 +10,7 @@
<hr />
<div class="col-wrapper">
<div style="display:none"><a href="http://dist.pinaxproject.com/dev/Pinax/Pinax-0.9a1.tar.gz#egg=Pinax-0.9a1">0.9a1</a></div>
- <div style="display:none"><a href="http://dist.pinaxproject.com/dev/Pinax/Pinax-0.9b1.dev2.tar.gz#egg=Pinax-dev">dev</a></div>
+ <div style="display:none"><a href="http://dist.pinaxproject.com/dev/Pinax/Pinax-{{ latest_dev_version }}.tar.gz#egg=Pinax-dev">dev</a></div>
<section class="col1">
<p>
Pinax is open source software under an MIT license. It requires
View
6 pinaxsite_project/templates/homepage.html
@@ -66,19 +66,19 @@
</article>
<article class="box right2 columns">
- <h2><a href="{% url downloads_home %}">Download</a></h2>
+ <h2><a href="{% url download_index %}">Download</a></h2>
<hr />
{% latest_stable_release as release %}
{% if release %}
<div class="column-content">
<p class="main">
- <a href="{% url downloads_home %}" id="btn-release">
+ <a href="{% url download_index %}" id="btn-release">
Latest stable release: {{ release.version }}
</a>
</p>
</div>
- <p><a href="{% url downloads_home %}" class="more">more downloads &rarr;</a></p>
+ <p><a href="{% url download_index %}" class="more">more downloads &rarr;</a></p>
{% else %}
<p class="main">
There are currently no stable releases.
Please sign in to comment.
Something went wrong with that request. Please try again.