~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to scripts/import-packagenames.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2005-03-09 04:32:32 UTC
  • mfrom: (unknown (missing))
  • Revision ID: Arch-1:rocketfuel@canonical.com%launchpad--devel--0--patch-1393
Script to import Sourcepackage and Binarypackage names implemented
Patches applied:

 * daniel.debonzi@canonical.com/launchpad--devel--0--patch-451
   merge from rocketfuel

 * daniel.debonzi@canonical.com/launchpad--devel--0--patch-452
   Script to import Sourcepackage and Binarypackage names implemented

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
#
 
3
 
 
4
# Python imports
 
5
import sys
 
6
import re
 
7
from string import strip
 
8
from optparse import OptionParser
 
9
 
 
10
# LP imports
 
11
from canonical.lp import initZopeless
 
12
 
 
13
# launchpad imports
 
14
from canonical.launchpad.database import SourcePackageName
 
15
from canonical.launchpad.database import BinaryPackageName
 
16
 
 
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()
 
25
        
 
26
    def _buildlist(self):
 
27
        try:
 
28
            f = open(self.filename)
 
29
        except IOError:
 
30
            print 'file %s not found. Exiting...' %self.filename
 
31
            sys.exit(1)
 
32
            
 
33
        for line in f:
 
34
            line = self._check_format(strip(line))
 
35
            if line:
 
36
                if not self._valid_name(line):
 
37
                    print ' - Invalid package name: %s' %line
 
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()
 
97
        
 
98
        
 
99
    def processSource(self):
 
100
        if not self.source_list:
 
101
            return
 
102
 
 
103
        spnl = SourcePackageNameList(self.source_list).list
 
104
        
 
105
        for name in spnl:
 
106
            print '\t@ Evaluationg SourcePackageName %s' %name
 
107
            SourcePackageName.ensure(name)
 
108
            if self.count.step():
 
109
                self.commit()
 
110
 
 
111
        if self.interval:
 
112
            self.commit()
 
113
        self.count.reset()
 
114
 
 
115
    def processBinary(self):
 
116
        if not self.binary_list:
 
117
            return
 
118
 
 
119
        bpnl = BinaryPackageNameList(self.binary_list).list
 
120
 
 
121
        for name in bpnl:
 
122
            print '\t@ Evaluationg BinaryPackageName %s' %name
 
123
            BinaryPackageName.ensure(name)
 
124
            if self.count.step():
 
125
                self.commit()
 
126
 
 
127
        if self.interval:
 
128
            self.commit()
 
129
        self.count.reset()
 
130
 
 
131
if __name__ == '__main__':
 
132
 
 
133
    parser = OptionParser()
 
134
 
 
135
    parser.add_option("-s", "--source-file", dest="source_file",
 
136
                      help="SourcePackageName list file",
 
137
                      default=None)
 
138
 
 
139
    parser.add_option("-b", "--binary-file", dest="binary_file",
 
140
                      help="BinaryPackageName list file",
 
141
                      default=None)
 
142
 
 
143
    parser.add_option("-c", "--commit-interval", dest="commit_interval",
 
144
                      help="DB commit interval. Default 0 performs not commit.",
 
145
                      default=0)
 
146
 
 
147
    (options,args) = parser.parse_args()
 
148
 
 
149
 
 
150
    processor = ProcessNames(options.source_file,
 
151
                             options.binary_file,
 
152
                             int(options.commit_interval))
 
153
 
 
154
    processor.processSource()
 
155
    processor.processBinary()