~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to scripts/clean-sourceforge-project-entries.py

  • Committer: Danilo Segan
  • Date: 2011-04-22 14:02:29 UTC
  • mto: This revision was merged to the branch mainline in revision 12910.
  • Revision ID: danilo@canonical.com-20110422140229-zhq4d4c2k8jpglhf
Ignore hidden files when building combined JS file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python -S
 
2
#
 
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
 
4
# GNU Affero General Public License version 3 (see the file LICENSE).
 
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
 
16
from canonical.launchpad.interfaces.product import (
 
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))