diff options
-rw-r--r-- | cnucnu/__init__.py | 130 | ||||
-rwxr-xr-x | cnucnu/package_list.py | 134 | ||||
-rwxr-xr-x | cnucnu/tests/aliases_test.py | 2 |
3 files changed, 134 insertions, 132 deletions
diff --git a/cnucnu/__init__.py b/cnucnu/__init__.py index 650e42d..2ed5c5a 100644 --- a/cnucnu/__init__.py +++ b/cnucnu/__init__.py @@ -14,3 +14,133 @@ # You should have received a copy of the GNU General Public License # along with cnucnu. If not, see <http://www.gnu.org/licenses/>. # }}} +import re +import urllib + + +def restore_underscore(name): + return name.replace("-", "_") + + +ALIASES = { + "CPAN-DEFAULT": { + "prefix": "perl-", + "url": "http://search.cpan.org/dist/{name}/", + }, + "DEBIAN-DEFAULT": { + "url": "http://ftp.debian.org/debian/pool/main/{name[0]}/{name}/", + }, + "DEFAULT": { + "regex": + r"(?i)" # ignore case + r"\b{name}[-_]" # word-boundary, name and dash/underscore + r"(?:(?:src|source)[-_])?" # optional src or source string + r"([^-/_\s]*?" # + r"\d" + r"[^-/_\s]*?)" + r"(?:[-_.](?:src|source|orig))?" + r"\.(?:[jt]ar|t[bglx]z|tbz2|zip)\b" + }, + "DIR-LISTING-DEFAULT": { + "regex": 'href="([0-9][0-9.]*)/"' + }, + "DRUPAL-DEFAULT": { + "prefix": ["drupal6-", "drupal7-"], + "regex": "(?s)Recommended releases.*?>{raw_name[6]}.x-([^<]*)", + "url": "http://drupal.org/project/{name}", + }, + "FM-DEFAULT": { + "regex": '<a href="/projects/[^/]*/releases/[0-9]*">([^<]*)</a>', + "url": "http://freshmeat.net/projects/{name}", + }, + "GNU-DEFAULT": { + "url": "http://ftp.gnu.org/gnu/{name}/" + }, + "GNOME-DEFAULT": { + "url": "http://download.gnome.org/sources/{name}/*/", + }, + "GOOGLE-DEFAULT": { + "url": "http://code.google.com/p/{name}/downloads/list" + }, + "HACKAGE-DEFAULT": { + "prefix": "ghc-", + "url": "http://hackage.haskell.org/package/{name}", + }, + "LP-DEFAULT": { + "url": "https://launchpad.net/{name}/+download" + }, + "NPM-DEFAULT": { + "prefix": "nodejs-", + "regex": '"version":"([0-9.]*?)"', + "url": "http://registry.npmjs.org/{name}", + }, + "PEAR-DEFAULT": { + "name_modifiers": [restore_underscore], + "prefix": "php-pear-", + "url": "http://pear.php.net/package/{name}/download", + }, + "PECL-DEFAULT": { + "name_modifiers": [restore_underscore], + "prefix": "php-pecl-", + "url": "http://pecl.php.net/package/{name}/download", + }, + "PYPI-DEFAULT": { + "url": "https://pypi.python.org/packages/source/{name[0]}/{name}/", + }, + "RUBYGEMS-DEFAULT": { + "prefix": "rubygem-", + "regex": + '"gem_uri":"http:\/\/rubygems.org\/gems\/{name}-([0-9.]*?)\.gem"', + "url": "http://rubygems.org/api/v1/gems/{name}.json", + }, + "SF-DEFAULT": { + "url": "http://sourceforge.net/api/file/index/project-name/{name}/" + "mtime/desc/limit/200/rss" + }, +} + + +def unalias(name, value, what): + """ Unalias `value` for package `name`. + :param what: "regex" or "url" + :returns: Unaliased value + """ + + raw_name = name + + # allow name override with e.g. DEFAULT:othername + if value and ":" in value: + alias, name_override = value.split(":", 1) + if alias in ALIASES.keys(): + value = alias + name = name_override + else: + name_override = False + + # Use while loop to allow to fall back to DEFAULT value + while value in ALIASES.keys(): + if not name_override: + prefixes = ALIASES[value].get("prefix", []) + if isinstance(prefixes, basestring): + prefixes = [prefixes] + for prefix in prefixes: + if name.startswith(prefix): + name = name[len(prefix):] + + name_modifiers = ALIASES[value].get("name_modifiers", []) + for modifier in name_modifiers: + name = modifier(name) + + # Use DEFAULT regex if None is defined + value = ALIASES[value].get(what, "DEFAULT") + if what == "regex": + format_values = {"name": re.escape(name), + "raw_name": re.escape(raw_name)} + elif what == "url": + format_values = {"name": urllib.quote(name, safe=""), + "raw_name": urllib.quote(raw_name, safe="")} + else: + raise NotImplementedError("what needs ot be 'regex' or 'url'") + + value = value.format(**format_values) + return value diff --git a/cnucnu/package_list.py b/cnucnu/package_list.py index 07bd723..ac7a98e 100755 --- a/cnucnu/package_list.py +++ b/cnucnu/package_list.py @@ -29,13 +29,13 @@ import re import sre_constants import string import subprocess -import urllib #extra modules import pycurl from fedora.client.pkgdb import PackageDB # cnucnu modules +import cnucnu from cnucnu.bugzilla_reporter import BugzillaReporter from cnucnu.config import global_config import cnucnu.errors as cc_errors @@ -46,134 +46,6 @@ from cnucnu.scm import SCM from cnucnu.wiki import MediaWiki -def restore_underscore(name): - return name.replace("-", "_") - - -ALIASES = { - "CPAN-DEFAULT": { - "prefix": "perl-", - "url": "http://search.cpan.org/dist/{name}/", - }, - "DEBIAN-DEFAULT": { - "url": "http://ftp.debian.org/debian/pool/main/{name[0]}/{name}/", - }, - "DEFAULT": { - "regex": - r"(?i)" # ignore case - r"\b{name}[-_]" # word-boundary, name and dash/underscore - r"(?:(?:src|source)[-_])?" # optional src or source string - r"([^-/_\s]*?" # - r"\d" - r"[^-/_\s]*?)" - r"(?:[-_.](?:src|source|orig))?" - r"\.(?:[jt]ar|t[bglx]z|tbz2|zip)\b" - }, - "DIR-LISTING-DEFAULT": { - "regex": 'href="([0-9][0-9.]*)/"' - }, - "DRUPAL-DEFAULT": { - "prefix": ["drupal6-", "drupal7-"], - "regex": "(?s)Recommended releases.*?>{raw_name[6]}.x-([^<]*)", - "url": "http://drupal.org/project/{name}", - }, - "FM-DEFAULT": { - "regex": '<a href="/projects/[^/]*/releases/[0-9]*">([^<]*)</a>', - "url": "http://freshmeat.net/projects/{name}", - }, - "GNU-DEFAULT": { - "url": "http://ftp.gnu.org/gnu/{name}/" - }, - "GNOME-DEFAULT": { - "url": "http://download.gnome.org/sources/{name}/*/", - }, - "GOOGLE-DEFAULT": { - "url": "http://code.google.com/p/{name}/downloads/list" - }, - "HACKAGE-DEFAULT": { - "prefix": "ghc-", - "url": "http://hackage.haskell.org/package/{name}", - }, - "LP-DEFAULT": { - "url": "https://launchpad.net/{name}/+download" - }, - "NPM-DEFAULT": { - "prefix": "nodejs-", - "regex": '"version":"([0-9.]*?)"', - "url": "http://registry.npmjs.org/{name}", - }, - "PEAR-DEFAULT": { - "name_modifiers": [restore_underscore], - "prefix": "php-pear-", - "url": "http://pear.php.net/package/{name}/download", - }, - "PECL-DEFAULT": { - "name_modifiers": [restore_underscore], - "prefix": "php-pecl-", - "url": "http://pecl.php.net/package/{name}/download", - }, - "PYPI-DEFAULT": { - "url": "https://pypi.python.org/packages/source/{name[0]}/{name}/", - }, - "RUBYGEMS-DEFAULT": { - "prefix": "rubygem-", - "regex": - '"gem_uri":"http:\/\/rubygems.org\/gems\/{name}-([0-9.]*?)\.gem"', - "url": "http://rubygems.org/api/v1/gems/{name}.json", - }, - "SF-DEFAULT": { - "url": "http://sourceforge.net/api/file/index/project-name/{name}/" - "mtime/desc/limit/200/rss" - }, -} - - -def unalias(name, value, what): - """ Unalias `value` for package `name`. - :param what: "regex" or "url" - :returns: Unaliased value - """ - - raw_name = name - - # allow name override with e.g. DEFAULT:othername - if value and ":" in value: - alias, name_override = value.split(":", 1) - if alias in ALIASES.keys(): - value = alias - name = name_override - else: - name_override = False - - # Use while loop to allow to fall back to DEFAULT value - while value in ALIASES.keys(): - if not name_override: - prefixes = ALIASES[value].get("prefix", []) - if isinstance(prefixes, basestring): - prefixes = [prefixes] - for prefix in prefixes: - if name.startswith(prefix): - name = name[len(prefix):] - - name_modifiers = ALIASES[value].get("name_modifiers", []) - for modifier in name_modifiers: - name = modifier(name) - - # Use DEFAULT regex if None is defined - value = ALIASES[value].get(what, "DEFAULT") - if what == "regex": - format_values = {"name": re.escape(name), - "raw_name": re.escape(raw_name)} - elif what == "url": - format_values = {"name": urllib.quote(name, safe=""), - "raw_name": urllib.quote(raw_name, safe="")} - else: - raise NotImplementedError("what needs ot be 'regex' or 'url'") - - value = value.format(**format_values) - return value - - class Repository: def __init__(self, name="", path=""): if not (name and path): @@ -273,7 +145,7 @@ class Package(object): def set_regex(self, regex): self.raw_regex = regex - regex = unalias(self.name, regex, "regex") + regex = cnucnu.unalias(self.name, regex, "regex") self.__regex = regex self._invalidate_caches() @@ -281,7 +153,7 @@ class Package(object): def set_url(self, url): self.raw_url = url - url = unalias(self.name, url, "url") + url = cnucnu.unalias(self.name, url, "url") self.__url = url self.html = None diff --git a/cnucnu/tests/aliases_test.py b/cnucnu/tests/aliases_test.py index 31d9e34..dbe5e38 100755 --- a/cnucnu/tests/aliases_test.py +++ b/cnucnu/tests/aliases_test.py @@ -21,7 +21,7 @@ import unittest import sys sys.path.insert(0, '../..') -from cnucnu.package_list import unalias, ALIASES +from cnucnu import unalias, ALIASES class AliasTest(unittest.TestCase): |