37
37
38
38
class Convert (object ) :
39
39
40
- person_dupes = \
41
- { 23 : 895 # nick ähnlich, gleicher Name?
42
40
# 54 : 655 # nick == email vorn?
41
+ person_dupes = \
42
+ { 71 : 72
43
43
, 140 : 460
44
44
, 179 : 124
45
45
, 267 : 322
@@ -70,7 +70,70 @@ class Convert (object) :
70
70
, 973 : 544
71
71
}
72
72
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
+
73
135
def __init__ (self , cmd , scope , debug = False ) :
136
+ self .anonymize = cmd .anonymize
74
137
self .pers_exception = {}
75
138
try :
76
139
pf = open ('pers.csv' , 'r' )
@@ -100,37 +163,57 @@ def __init__ (self, cmd, scope, debug = False) :
100
163
self .node_by_id = {}
101
164
self .ffm_node = {}
102
165
self .phone_ids = {}
166
+ self .net_by_id = {}
103
167
# end def __init__
104
168
105
169
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 ()
108
175
self .scope .commit ()
109
176
self .create_nodes ()
110
177
self .scope .commit ()
178
+ self .create_networks ()
179
+ self .scope .commit ()
111
180
self .create_devices ()
112
181
self .scope .commit ()
113
182
# end def create
114
183
115
184
def create_devices (self ) :
116
185
# ignore snmp_ip and snmp_lastseen (only used by three nodes)
117
186
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" \
129
213
% (d .location_id , d .id )
130
- continue
131
214
dev = self .ffm .Net_Device \
132
215
( left = dt
133
- , node = self . ffm_node [ d . location_id ]
216
+ , node = node
134
217
, name = d .name
135
218
, desc = d .comment
136
219
, raw = True
@@ -140,31 +223,72 @@ def create_devices (self) :
140
223
self .scope .commit ()
141
224
# end def create_devices
142
225
143
- def create_networks (self ) :
226
+ def create_nettypes (self ) :
144
227
""" Network ranges for reservation
145
228
"""
146
229
by_mask = {}
147
230
# first group by netmask
148
231
for nw in self .contents ['nettype' ] :
149
232
if not nw .comment :
150
- print 'WARN: Ignoring nettype "%s"' % nw .name
233
+ print 'WARN: Ignoring nettype %s "%s"' % ( nw .id , nw . name )
151
234
continue
152
235
for net_ip in nw .comment .split (',' ) :
153
236
ip = IP4_Address (net_ip )
154
237
if ip .mask not in by_mask :
155
238
by_mask [ip .mask ] = []
156
- by_mask [ip .mask ].append ((ip , nw .name ))
239
+ by_mask [ip .mask ].append ((ip , nw .name , nw . id ))
157
240
typ = self .ffm .IP4_Network
158
241
for mask in sorted (by_mask ) :
159
- for ip , name in by_mask [mask ] :
242
+ for ip , name , id in by_mask [mask ] :
160
243
r = typ .query \
161
244
( Q .net_address .CONTAINS (ip )
162
245
, sort_key = TFL .Sorted_By ("-net_address.mask_len" )
163
246
).first ()
164
247
reserver = r .reserve if r else typ
165
248
network = reserver (ip , owner = self .graz_admin )
249
+ self .net_by_id [id ] = network
166
250
if name :
167
251
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 ()
168
292
# end def create_networks
169
293
170
294
def create_nodes (self ) :
@@ -177,12 +301,14 @@ def create_nodes (self) :
177
301
for n in sorted (self .contents ['location' ], key = lambda x : x .id ) :
178
302
self .node_by_id [n .id ] = n
179
303
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
181
306
print "WARN: Location %s owner %s missing" % (n .id , person_id )
182
307
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 ]
186
312
lat = lon = None
187
313
if n .pixel_x is not None and n .pixel_y is not None :
188
314
lon = "%f" % (x_lon + (n .pixel_x - x_start ) / dx_lon )
@@ -218,9 +344,32 @@ def create_nodes (self) :
218
344
self .scope .commit ()
219
345
# end def create_nodes
220
346
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
+
221
367
def create_persons (self ) :
222
368
for m in sorted (self .contents ['person' ], key = lambda x : x .id ) :
223
369
#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
224
373
if m .id in self .pers_exception :
225
374
pe = self .pers_exception [m .id ]
226
375
fn , ln = (x .decode ('utf-8' ) for x in pe )
@@ -235,11 +384,13 @@ def create_persons (self) :
235
384
print >> sys .stderr , "WARN: name missing: %s (%r/%r)" \
236
385
% (m .id , m .firstname , m .lastname )
237
386
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 )
243
394
person = self .pap .Person \
244
395
( first_name = fn
245
396
, last_name = ln
@@ -280,6 +431,8 @@ def create_persons (self) :
280
431
self .try_insert_nick (d .nick , m_id , person )
281
432
if d .tel :
282
433
self .try_insert_phone (d .tel , m_id , person )
434
+ if len (self .scope .uncommitted_changes ) > 10 :
435
+ self .scope .commit ()
283
436
# end def create_persons
284
437
285
438
def set_creation (self , obj , create_time ) :
@@ -347,6 +500,7 @@ def _main (cmd) :
347
500
)
348
501
, opts =
349
502
( "verbose:B"
503
+ , "anonymize:B"
350
504
, "create:B"
351
505
) + model .opts
352
506
, min_args = 1
0 commit comments