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
150
151
152
153
154
155
|
#!/usr/bin/python -S
#
# Copyright 2009 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
# pylint: disable-msg=W0403
import _pythonpath
import sys
import re
from optparse import OptionParser
from canonical.lp import initZopeless
from lp.soyuz.model.binarypackagename import BinaryPackageName
class BaseNameList:
"""Base for Packages name list"""
def __init__(self, filename):
self.filename = filename
self.list = []
self._buildlist()
self.list.sort()
def _buildlist(self):
try:
f = open(self.filename)
except IOError:
print 'file %s not found. Exiting...' % self.filename
sys.exit(1)
for line in f:
line = self._check_format(line.strip())
if line:
if not self._valid_name(line):
print ' - Invalid package name: %s' % line
continue
self.list.append(line)
def _check_format(self, name):
assert isinstance(name, basestring), repr(name)
try:
# check that this is unicode data
name.decode("utf-8").encode("utf-8")
return name
except UnicodeError:
# check that this is latin-1 data
s = name.decode("latin-1").encode("utf-8")
s.decode("utf-8")
return s
def _valid_name(self, name):
pat = r"^[a-z0-9][a-z0-9\\+\\.\\-]+$"
if re.match(pat, name):
return True
class SourcePackageNameList(BaseNameList):
"""Build a sourcepackagename list from a given file"""
class BinaryPackageNameList(BaseNameList):
"""Build a binarypackagename list from a given file"""
class Counter:
def __init__(self, interval):
self._count = 0
self.interval = interval
if not interval:
setattr(self, 'step', self._fake_step)
else:
setattr(self, 'step', self._step)
def _step(self):
self._count += 1
if self._count > self.interval:
self.reset()
return True
def _fake_step(self):
return
def reset(self):
self._count = 0
class ProcessNames:
def __init__(self, source_list, binary_list, commit_interval=0):
self.source_list = source_list
self.binary_list = binary_list
self.ztm = initZopeless()
self.interval = commit_interval
self.count = Counter(commit_interval)
def commit(self):
print '\t\t@ Commiting...'
self.ztm.commit()
def processSource(self):
from lp.registry.model.sourcepackagename import SourcePackageName
if not self.source_list:
return
spnl = SourcePackageNameList(self.source_list).list
for name in spnl:
print '\t@ Evaluationg SourcePackageName %s' % name
SourcePackageName.ensure(name)
if self.count.step():
self.commit()
if self.interval:
self.commit()
self.count.reset()
def processBinary(self):
if not self.binary_list:
return
bpnl = BinaryPackageNameList(self.binary_list).list
for name in bpnl:
print '\t@ Evaluationg BinaryPackageName %s' % name
BinaryPackageName.ensure(name)
if self.count.step():
self.commit()
if self.interval:
self.commit()
self.count.reset()
if __name__ == '__main__':
parser = OptionParser()
parser.add_option(
"-s", "--source-file", dest="source_file",
help="SourcePackageName list file")
parser.add_option(
"-b", "--binary-file", dest="binary_file",
help="BinaryPackageName list file")
parser.add_option(
"-c", "--commit-interval", dest="commit_interval", default=0,
help="DB commit interval. Default 0 performs not commit.")
(options, args) = parser.parse_args()
processor = ProcessNames(
options.source_file, options.binary_file,
int(options.commit_interval))
processor.processSource()
processor.processBinary()
|