~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).
5
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
6
# pylint: disable-msg=W0403
9641.1.7 by Gary Poster
fix scripts to work
7
import _pythonpath
1424 by Canonical.com Patch Queue Manager
Script to import Sourcepackage and Binarypackage names implemented
8
9
import sys
10
import re
11
from optparse import OptionParser
12
13
from canonical.lp import initZopeless
11869.8.6 by Jonathan Lange
Turns out I missed a whole lot more.
14
from lp.soyuz.model.binarypackagename import BinaryPackageName
1424 by Canonical.com Patch Queue Manager
Script to import Sourcepackage and Binarypackage names implemented
15
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
16
1424 by Canonical.com Patch Queue Manager
Script to import Sourcepackage and Binarypackage names implemented
17
class BaseNameList:
18
    """Base for Packages name list"""
19
20
    def __init__(self, filename):
21
        self.filename = filename
22
        self.list = []
23
        self._buildlist()
24
        self.list.sort()
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
25
1424 by Canonical.com Patch Queue Manager
Script to import Sourcepackage and Binarypackage names implemented
26
    def _buildlist(self):
27
        try:
28
            f = open(self.filename)
29
        except IOError:
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
30
            print 'file %s not found. Exiting...' % self.filename
1424 by Canonical.com Patch Queue Manager
Script to import Sourcepackage and Binarypackage names implemented
31
            sys.exit(1)
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
32
1424 by Canonical.com Patch Queue Manager
Script to import Sourcepackage and Binarypackage names implemented
33
        for line in f:
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
34
            line = self._check_format(line.strip())
1424 by Canonical.com Patch Queue Manager
Script to import Sourcepackage and Binarypackage names implemented
35
            if line:
36
                if not self._valid_name(line):
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
37
                    print ' - Invalid package name: %s' % line
1424 by Canonical.com Patch Queue Manager
Script to import Sourcepackage and Binarypackage names implemented
38
                    continue
39
                self.list.append(line)
40
41
    def _check_format(self, name):
42
        assert isinstance(name, basestring), repr(name)
43
        try:
44
            # check that this is unicode data
45
            name.decode("utf-8").encode("utf-8")
46
            return name
47
        except UnicodeError:
48
            # check that this is latin-1 data
49
            s = name.decode("latin-1").encode("utf-8")
50
            s.decode("utf-8")
51
            return s
52
53
    def _valid_name(self, name):
54
        pat = r"^[a-z0-9][a-z0-9\\+\\.\\-]+$"
55
        if re.match(pat, name):
56
            return True
57
58
class SourcePackageNameList(BaseNameList):
59
    """Build a sourcepackagename list from a given file"""
60
61
class BinaryPackageNameList(BaseNameList):
62
    """Build a binarypackagename list from a given file"""
63
64
class Counter:
65
    def __init__(self, interval):
66
        self._count = 0
67
        self.interval = interval
68
69
        if not interval:
70
            setattr(self, 'step', self._fake_step)
71
        else:
72
            setattr(self, 'step', self._step)
73
74
    def _step(self):
75
        self._count += 1
76
        if self._count > self.interval:
77
            self.reset()
78
            return True
79
80
    def _fake_step(self):
81
        return
82
83
    def reset(self):
84
        self._count = 0
85
86
class ProcessNames:
87
    def __init__(self, source_list, binary_list, commit_interval=0):
88
        self.source_list = source_list
89
        self.binary_list = binary_list
90
        self.ztm = initZopeless()
91
        self.interval = commit_interval
92
        self.count = Counter(commit_interval)
93
94
    def commit(self):
95
        print '\t\t@ Commiting...'
96
        self.ztm.commit()
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
97
98
1424 by Canonical.com Patch Queue Manager
Script to import Sourcepackage and Binarypackage names implemented
99
    def processSource(self):
8971.22.10 by Guilherme Salgado
Fix a couple remaining tests
100
        from lp.registry.model.sourcepackagename import SourcePackageName
1424 by Canonical.com Patch Queue Manager
Script to import Sourcepackage and Binarypackage names implemented
101
        if not self.source_list:
102
            return
103
104
        spnl = SourcePackageNameList(self.source_list).list
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
105
1424 by Canonical.com Patch Queue Manager
Script to import Sourcepackage and Binarypackage names implemented
106
        for name in spnl:
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
107
            print '\t@ Evaluationg SourcePackageName %s' % name
1424 by Canonical.com Patch Queue Manager
Script to import Sourcepackage and Binarypackage names implemented
108
            SourcePackageName.ensure(name)
109
            if self.count.step():
110
                self.commit()
111
112
        if self.interval:
113
            self.commit()
114
        self.count.reset()
115
116
    def processBinary(self):
117
        if not self.binary_list:
118
            return
119
120
        bpnl = BinaryPackageNameList(self.binary_list).list
121
122
        for name in bpnl:
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
123
            print '\t@ Evaluationg BinaryPackageName %s' % name
1424 by Canonical.com Patch Queue Manager
Script to import Sourcepackage and Binarypackage names implemented
124
            BinaryPackageName.ensure(name)
125
            if self.count.step():
126
                self.commit()
127
128
        if self.interval:
129
            self.commit()
130
        self.count.reset()
131
132
if __name__ == '__main__':
133
134
    parser = OptionParser()
135
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
136
    parser.add_option(
137
        "-s", "--source-file", dest="source_file",
138
        help="SourcePackageName list file")
139
140
    parser.add_option(
141
        "-b", "--binary-file", dest="binary_file",
142
        help="BinaryPackageName list file")
143
144
    parser.add_option(
145
        "-c", "--commit-interval", dest="commit_interval", default=0,
146
        help="DB commit interval. Default 0 performs not commit.")
147
148
    (options, args) = parser.parse_args()
149
150
    processor = ProcessNames(
151
        options.source_file, options.binary_file,
152
        int(options.commit_interval))
1424 by Canonical.com Patch Queue Manager
Script to import Sourcepackage and Binarypackage names implemented
153
154
    processor.processSource()
155
    processor.processBinary()