Skip to content

Commit e9c35be

Browse files
committedJun 14, 2014
Add network import
Also add --anonymize option to get better turnaround times. Inserting person info takes ages.
1 parent 4b3e266 commit e9c35be

File tree

1 file changed

+184
-30
lines changed

1 file changed

+184
-30
lines changed
 

‎convert_graz.py

+184-30
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737

3838
class Convert (object) :
3939

40-
person_dupes = \
41-
{ 23 : 895 # nick ähnlich, gleicher Name?
4240
# 54 : 655 # nick == email vorn?
41+
person_dupes = \
42+
{ 71 : 72
4343
, 140 : 460
4444
, 179 : 124
4545
, 267 : 322
@@ -70,7 +70,70 @@ class Convert (object) :
7070
, 973 : 544
7171
}
7272

73+
person_ignore = dict.fromkeys \
74+
(( 11
75+
, 15
76+
, 41
77+
, 42
78+
, 62
79+
, 63
80+
, 64
81+
, 66
82+
, 70
83+
, 93
84+
, 100
85+
, 106
86+
, 108
87+
, 112
88+
, 118
89+
, 123
90+
, 132
91+
, 136
92+
, 142
93+
, 172
94+
, 174
95+
, 177
96+
, 199
97+
, 209
98+
, 242
99+
, 244
100+
, 245
101+
, 254
102+
, 263
103+
, 266
104+
, 270
105+
, 287
106+
, 313
107+
, 320
108+
, 324
109+
, 338
110+
, 937
111+
# Lastname missing:
112+
, 34
113+
, 69
114+
, 107
115+
, 166
116+
, 168
117+
, 200
118+
, 251
119+
, 260
120+
, 323
121+
# Bogus, only initials etc, ignore if no node:
122+
, 102
123+
, 103
124+
, 185
125+
, 233
126+
, 299
127+
, 455
128+
, 530
129+
, 845
130+
, 872
131+
, 919
132+
, 955
133+
))
134+
73135
def __init__ (self, cmd, scope, debug = False) :
136+
self.anonymize = cmd.anonymize
74137
self.pers_exception = {}
75138
try :
76139
pf = open ('pers.csv', 'r')
@@ -100,37 +163,57 @@ def __init__ (self, cmd, scope, debug = False) :
100163
self.node_by_id = {}
101164
self.ffm_node = {}
102165
self.phone_ids = {}
166+
self.net_by_id = {}
103167
# end def __init__
104168

105169
def create (self) :
106-
self.create_persons ()
107-
self.create_networks ()
170+
if self.anonymize :
171+
self.fake_persons ()
172+
else :
173+
self.create_persons ()
174+
self.create_nettypes ()
108175
self.scope.commit ()
109176
self.create_nodes ()
110177
self.scope.commit ()
178+
self.create_networks ()
179+
self.scope.commit ()
111180
self.create_devices ()
112181
self.scope.commit ()
113182
# end def create
114183

115184
def create_devices (self) :
116185
# ignore snmp_ip and snmp_lastseen (only used by three nodes)
117186
dt = self.ffm.Net_Device_Type.instance (name = 'Generic', raw = True)
118-
for d in self.contents ['node'] :
119-
if d.location_id not in self.node_by_id :
120-
print "WARN: Ignoring device (node) %s with location_id %s" \
121-
% (d.id, d.location_id)
122-
continue
123-
n = self.node_by_id [d.location_id]
124-
if d.person_id and d.person_id != n.person_id :
125-
print "WARN: Device (node) %s: person mismatch d:%s n:%s" \
126-
% (d.id, d.person_id, n.person_id)
127-
if d.location_id not in self.ffm_node :
128-
print "WARN: Node (location) %s for device (node) %s missing" \
187+
for d in sorted (self.contents ['node'], key = lambda x : x.id) :
188+
print "INFO: Dev: %s Node: %s" % (d.id, d.location_id)
189+
node = None
190+
n = self.node_by_id.get (d.location_id)
191+
if n :
192+
if d.person_id and d.person_id != n.person_id :
193+
print \
194+
( "WARN: Device (node) %s, (loc) %s: "
195+
"person mismatch d:%s n:%s"
196+
% (d.id, n.id, d.person_id, n.person_id)
197+
)
198+
node = self.ffm_node.get (d.location_id)
199+
if not node :
200+
print "WARN: Node (location) %s for dev (node) %s missing" \
201+
% (d.location_id, d.id)
202+
continue
203+
else :
204+
mgr = self.person_by_id.get (d.person_id) or self.graz_admin
205+
node = self.ffm.Node \
206+
( name = d.name
207+
, desc = 'Auto-created node (id: %s)' % d.location_id
208+
, show_in_map = True
209+
, manager = mgr
210+
, raw = True
211+
)
212+
print "WARN: Manufacturing Node (loc: %s) for dev (node) %s" \
129213
% (d.location_id, d.id)
130-
continue
131214
dev = self.ffm.Net_Device \
132215
( left = dt
133-
, node = self.ffm_node [d.location_id]
216+
, node = node
134217
, name = d.name
135218
, desc = d.comment
136219
, raw = True
@@ -140,31 +223,72 @@ def create_devices (self) :
140223
self.scope.commit ()
141224
# end def create_devices
142225

143-
def create_networks (self) :
226+
def create_nettypes (self) :
144227
""" Network ranges for reservation
145228
"""
146229
by_mask = {}
147230
# first group by netmask
148231
for nw in self.contents ['nettype'] :
149232
if not nw.comment :
150-
print 'WARN: Ignoring nettype "%s"' % nw.name
233+
print 'WARN: Ignoring nettype %s "%s"' % (nw.id, nw.name)
151234
continue
152235
for net_ip in nw.comment.split (',') :
153236
ip = IP4_Address (net_ip)
154237
if ip.mask not in by_mask :
155238
by_mask [ip.mask] = []
156-
by_mask [ip.mask].append ((ip, nw.name))
239+
by_mask [ip.mask].append ((ip, nw.name, nw.id))
157240
typ = self.ffm.IP4_Network
158241
for mask in sorted (by_mask) :
159-
for ip, name in by_mask [mask] :
242+
for ip, name, id in by_mask [mask] :
160243
r = typ.query \
161244
( Q.net_address.CONTAINS (ip)
162245
, sort_key = TFL.Sorted_By ("-net_address.mask_len")
163246
).first ()
164247
reserver = r.reserve if r else typ
165248
network = reserver (ip, owner = self.graz_admin)
249+
self.net_by_id [id] = network
166250
if name :
167251
network.set_raw (desc = name)
252+
# end def create_nettypes
253+
254+
def create_networks (self) :
255+
for net in self.contents ['net'] :
256+
parent = self.net_by_id.get (net.nettype_id)
257+
node = self.ffm_node.get (net.location_id)
258+
ip = IP4_Address (net.netip, net.netmask)
259+
if node :
260+
owner = node.owner
261+
else :
262+
print "WARN: Network %s Location %s missing" \
263+
% (net.id, net.location_id)
264+
owner = self.graz_admin
265+
if not parent or ip not in parent.net_address :
266+
print "No parent: %s ip: %s" % (parent, ip)
267+
parent = None
268+
for p in self.net_by_id.itervalues () :
269+
if ip in p.net_address :
270+
parent = p
271+
print "Got parent in net_by_id: %s" % parent
272+
break
273+
else :
274+
parent = self.ffm.IP4_Network.query \
275+
( Q.net_address.CONTAINS (ip)
276+
, sort_key = TFL.Sorted_By ("-net_address.mask_len")
277+
).first ()
278+
if parent :
279+
print "Got parent by network query: %s" % parent
280+
if parent :
281+
reserver = parent.reserve
282+
else :
283+
print "No parent: new network: %s" % ip
284+
reserver = self.ffm.IP4_Network
285+
network = reserver (ip, owner = owner)
286+
if node :
287+
network.set (node = node)
288+
if net.comment :
289+
network.set_raw (desc = net.comment)
290+
if len (self.scope.uncommitted_changes) > 10 :
291+
self.scope.commit ()
168292
# end def create_networks
169293

170294
def create_nodes (self) :
@@ -177,12 +301,14 @@ def create_nodes (self) :
177301
for n in sorted (self.contents ['location'], key = lambda x : x.id) :
178302
self.node_by_id [n.id] = n
179303
person_id = self.person_dupes.get (n.person_id, n.person_id)
180-
if person_id not in self.person_by_id :
304+
if person_id != 0 and person_id not in self.person_by_id :
305+
# should not happen now
181306
print "WARN: Location %s owner %s missing" % (n.id, person_id)
182307
continue
183-
person = self.person_by_id [person_id]
184-
185-
308+
if person_id == 0 :
309+
person = self.graz_admin
310+
else :
311+
person = self.person_by_id [person_id]
186312
lat = lon = None
187313
if n.pixel_x is not None and n.pixel_y is not None :
188314
lon = "%f" % (x_lon + (n.pixel_x - x_start) / dx_lon)
@@ -218,9 +344,32 @@ def create_nodes (self) :
218344
self.scope.commit ()
219345
# end def create_nodes
220346

347+
def fake_persons (self) :
348+
self.graz_admin = self.pap.Person \
349+
( first_name = 'Graz'
350+
, last_name = 'Admin'
351+
, raw = True
352+
)
353+
mail = 'admin@graz.funkfeuer.at'
354+
email = self.pap.Email (address = mail)
355+
self.pap.Person_has_Email (self.graz_admin, email)
356+
auth = self.scope.Auth.Account.create_new_account_x \
357+
( mail
358+
, enabled = True
359+
, suspended = True
360+
, password = uuid.uuid4 ().hex
361+
)
362+
self.pap.Person_has_Account (self.graz_admin, auth)
363+
for m in sorted (self.contents ['person'], key = lambda x : x.id) :
364+
self.person_by_id [m.id] = self.graz_admin
365+
# end def fake_persons
366+
221367
def create_persons (self) :
222368
for m in sorted (self.contents ['person'], key = lambda x : x.id) :
223369
#print "%s: %r %r" % (m.id, m.firstname, m.lastname)
370+
if m.id in self.person_ignore :
371+
print "INFO: Ignoring anonymous without location: %s" % m.id
372+
continue
224373
if m.id in self.pers_exception :
225374
pe = self.pers_exception [m.id]
226375
fn, ln = (x.decode ('utf-8') for x in pe)
@@ -235,11 +384,13 @@ def create_persons (self) :
235384
print >> sys.stderr, "WARN: name missing: %s (%r/%r)" \
236385
% (m.id, m.firstname, m.lastname)
237386
if not fn and not ln :
238-
print >> sys.stderr, "WARN: ignoring person %s" % m.id
239-
continue
240-
else :
241-
fn = fn or '?'
242-
ln = ln or '?'
387+
if m.nick :
388+
fn = m.nick
389+
else :
390+
fn = m.email.split ('@') [0]
391+
fn = fn or '?'
392+
ln = ln or '?'
393+
print "Person: %s %r/%r" % (m.id, fn, ln)
243394
person = self.pap.Person \
244395
( first_name = fn
245396
, last_name = ln
@@ -280,6 +431,8 @@ def create_persons (self) :
280431
self.try_insert_nick (d.nick, m_id, person)
281432
if d.tel :
282433
self.try_insert_phone (d.tel, m_id, person)
434+
if len (self.scope.uncommitted_changes) > 10 :
435+
self.scope.commit ()
283436
# end def create_persons
284437

285438
def set_creation (self, obj, create_time) :
@@ -347,6 +500,7 @@ def _main (cmd) :
347500
)
348501
, opts =
349502
( "verbose:B"
503+
, "anonymize:B"
350504
, "create:B"
351505
) + model.opts
352506
, min_args = 1

0 commit comments

Comments
 (0)
Please sign in to comment.