~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python -S
8687.15.22 by Karl Fogel
Add the copyright header block to the remaining .py files.
2
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
7849.9.3 by Gavin Panella
New data fix script for product.sourceforgeproject.
5
6
import re
7
import sys
8
9
import _pythonpath
10
11
from zope.component import getUtility
12
13
from canonical.lp import initZopeless
14
from canonical.launchpad.database.product import Product
15
from canonical.launchpad.scripts import execute_zcml_for_scripts
7675.110.1 by Curtis Hovey
semi-manual changes in preperation to the the migration script. The registry
16
from canonical.launchpad.interfaces.product import (
7849.9.3 by Gavin Panella
New data fix script for product.sourceforgeproject.
17
    valid_sourceforge_project_name)
18
from canonical.launchpad.webapp.interfaces import (
19
    IStoreSelector, MAIN_STORE, MASTER_FLAVOR)
20
21
22
re_find_project_names = [
23
    re.compile(r'(?:sou?rcefor..|sf)[.]net/projects?/([^/]+)'),
24
    re.compile(r'([a-zA-Z0-9-]+)[.](?:sou?rceforge|sf)[.]net'),
25
    ]
26
27
28
def extract_project_name(project_name):
29
    # Remove whitespace and slashes.
30
    project_name = project_name.strip().strip('/')
31
    if valid_sourceforge_project_name(project_name):
32
        return project_name
33
34
    # Try to pattern match.
35
    for regex in re_find_project_names:
36
        match = regex.search(project_name)
37
        if match is not None:
38
            if valid_sourceforge_project_name(match.group(1)):
39
                return match.group(1)
40
41
    # No luck.
42
    return None
43
44
45
def main(argv):
46
    execute_zcml_for_scripts()
47
    ztm = initZopeless()
48
    store = getUtility(IStoreSelector).get(MAIN_STORE, MASTER_FLAVOR)
49
50
    # Get all products with a sourceforgeproject.
51
    products = store.find(Product,
52
                          Product.sourceforgeproject != None,
53
                          Product.sourceforgeproject != '')
54
55
    for product in products:
56
        if not valid_sourceforge_project_name(product.sourceforgeproject):
57
            extracted_project_name = (
58
                extract_project_name(product.sourceforgeproject))
59
            print '%r ==> %r' % (
60
                product.sourceforgeproject, extracted_project_name)
61
            product.sourceforgeproject = extracted_project_name
62
63
    ztm.commit()
64
65
66
if __name__ == '__main__':
67
    sys.exit(main(sys.argv))