summaryrefslogtreecommitdiffstats
path: root/cnucnu/__init__.py
blob: 0c817cb83ca6edc8cddcdeeb1fcc8770d3bda61b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# {{{ License header: GPLv2+
#    This file is part of cnucnu.
#
#    Cnucnu is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.
#
#    Cnucnu is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    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/All",
    },
    "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
            name_override = True
        else:
            name_override = False
    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