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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
#!/usr/bin/python2
# import MySQL module
#jm
import MySQLdb
import sys
import getopt
import socket
import os , re
import time
import string
import glob
from string import letters
from string import digits
class v4l_tuners():
def __init__(self, device):
self.device = device
self.full_attribs = self.find_full_attribs()
self.full_udev_attribs = self.find_full_udev_attribs()
self.description = self.find_description()
self.udev_props = self.parse_full_udev()
self.tuner_hash = self.find_tuner_hash()
self.udev_rule = self.create_udev_rule()
def find_full_attribs(self):
cmd = 'v4l2-ctl -D -d' + self.device
return os.popen(cmd).readlines()
def find_full_udev_attribs(self):
cmd = 'udevadm info -a -p $(udevadm info -q path -n %s)' %self.device
return os.popen(cmd).readlines()
def find_description(self):
for line in self.full_attribs:
#print line
pos = string.find(line,"Driver name")
if pos >=0:
splitline= line.split(':')
Driver=splitline[1].strip()
pos = string.find(line,"Card type")
if pos >=0:
splitline= line.split(':')
Cardtype=splitline[1].strip()
pos = string.find(line,"Bus info")
if pos >=0:
splitline= line.split(':',1)
Businfo=splitline[1].strip()
return Cardtype
def parse_full_udev(self):
udev_props = []
#ATTRS{serial}=="00A2023E"\n',
#SUBSYSTEMS=="usb"\n',
#SUBSYSTEM=="video4linux"\n',
#KERNELS=="0000:08:08.0"\n',
#DRIVERS=="hdpvr"\n'
#DRIVERS=="ivtv"\n',
#ATTR{index}=="0"
match = ['SUBSYSTEM',
'SUBSYSTEMS',
'DRIVERS',
'KERNELS',
'ATTRS{serial}',
'ATTR{name}',
'ATTR{index}'
]
for item in match:
regex = re.compile('%s.*$' %item)
for line in self.full_udev_attribs:
m = regex.search(line.strip())
if m:
#print line
udev_props.append(line.strip())
break
#throw out kernels for usb subsystems.
if 'SUBSYSTEMS=="usb"' in udev_props:
#print "found usb"
new_udev_props=[]
for index, item in enumerate(udev_props):
#print item,index
if item.startswith("DRIVERS=="):
continue
elif item.startswith("KERNELS=="):
continue
else:
new_udev_props.append(item)
udev_props = new_udev_props
return udev_props
def is_hdpvr(self):
if self.description == "Hauppauge HD PVR":
return True
return False
def find_tuner_hash(self):
#tuner hash will be driven by card_type pci id and index
i=''
k=''
tuner_hash=''
card_type = self.get_card_type()
if card_type == "v4l_hdpvr":
for index, item in enumerate(self.udev_props):
if item.startswith("ATTRS{serial}=="):
hex_serial=item.split("==")[1].strip('''"''')
serial = int(hex_serial, 16)
tuner_hash="hdpvr_%s" %serial
else:
for index, item in enumerate(self.udev_props):
if item.startswith("KERNELS=="):
k=item.split("==")[1].strip('''"''')
k=k.replace(":", "_")
k=k.replace("0", "")
k=k.replace(".", "")
if item.startswith("ATTR{index}=="):
i=item.split("==")[1].strip('''"''')
tuner_hash="%s%s%s" %(card_type,k,i)
return tuner_hash
def escapeSpecialCharacters ( self, text, characters ):
for character in characters:
text = text.replace( character, '\\' + character )
return text
def create_udev_rule(self):
#DRIVERS=="ivtv", ATTR{name}=="ivtv? encoder MPG", KERNELS=="0000:08:09.0" SYMLINK+="myth/aivtv2"
line = ','.join(self.udev_props)
line += ' SYMLINK+="vstatic/%s"' %(self.get_tuner_hash())
return self.escapeSpecialCharacters(line,'[]')
def get_dev_node(self):
return self.device
def get_full_attribs(self):
return self.full_attribs
def get_full_udev_attribs(self):
return self.full_udev_attribs
def get_card_type(self):
if self.is_hdpvr() == True :
rc = "v4l_hdpvr"
else:
rc = "v4l"
return rc
def get_description(self):
return self.description
def get_udev_probs(self):
return self.udev_props
def get_tuner_hash(self):
return self.tuner_hash
def get_udev_rule(self):
return self.udev_rule
#-----
class hdhr_tuners():
def __init__(self, device):
self.device = device
self.description = self.find_description()
def find_description(self):
command = '/usr/bin/hdhomerun_config %s get /sys/model' %self.device
results=os.popen(command,'r')
return results.readline().strip()
def get_dev_node(self):
return self.device
def get_card_type(self):
return "hdhr"
def get_description(self):
return self.description
def get_udev_rule(self):
return
#----------
class dvb_tuners():
def __init__(self, device):
self.device = device
self.dvb_number = self.find_dvb_number()
self.description = self.find_description()
self.full_udev_attribs = self.find_full_udev_attribs()
self.udev_props = self.parse_full_udev()
self.tuner_hash = self.find_tuner_hash()
self.udev_rule = self.create_udev_rule()
def find_description(self):
command = '/usr/bin/dvb-fe-tool -g -a %s' %self.dvb_number
results=os.popen(command,'r')
line = results.readline().strip()
d = line.split('''(''')
return d[0]
def find_full_udev_attribs(self):
cmd = 'udevadm info -a -p $(udevadm info -q path -n %s)' %self.device
return os.popen(cmd).readlines()
def find_dvb_number(self):
#/dev/dvb/adapter2/frontend0
d=self.device.split("/")[3]
dvb_number=d.partition("adapter")[2]
return dvb_number
def parse_full_udev(self):
udev_props = ['KERNEL=="dvb?.frontend?"']
#ATTRS{serial}=="00A2023E"\n',
#SUBSYSTEMS=="usb"\n',
#SUBSYSTEM=="video4linux"\n',
#KERNELS=="0000:08:08.0"\n',
#DRIVERS=="hdpvr"\n'
#DRIVERS=="ivtv"\n',
#ATTR{index}=="0"
#KERNEL=="dvb1.frontend0"
match = ['SUBSYSTEM',
'SUBSYSTEMS',
'DRIVERS',
'KERNELS',
'ATTRS{serial}',
'ATTR{name}',
'ATTR{index}'
]
for item in match:
regex = re.compile('%s.*$' %item)
for line in self.full_udev_attribs:
m = regex.search(line.strip())
if m:
#print line
udev_props.append(line.strip())
break
#throw out kernels for usb subsystems.
if 'SUBSYSTEMS=="usb"' in udev_props:
#print "found usb"
new_udev_props=[]
for index, item in enumerate(udev_props):
#print item,index
if item.startswith("DRIVERS=="):
continue
elif item.startswith("KERNELS=="):
continue
else:
new_udev_props.append(item)
udev_props = new_udev_props
return udev_props
def find_tuner_hash(self):
#tuner hash will be driven by card_type pci id and index
i=''
k=''
tuner_hash=''
card_type = self.get_card_type()
if card_type == "v4l_hdpvr":
for index, item in enumerate(self.udev_props):
if item.startswith("ATTRS{serial}=="):
hex_serial=item.split("==")[1].strip('''"''')
serial = int(hex_serial, 16)
tuner_hash="hdpvr_%s" %serial
else:
for index, item in enumerate(self.udev_props):
if item.startswith("KERNELS=="):
k=item.split("==")[1].strip('''"''')
k=k.replace(":", "_")
k=k.replace("0", "")
k=k.replace(".", "")
if item.startswith("ATTR{index}=="):
i=item.split("==")[1].strip('''"''')
tuner_hash="%s%s%s" %(card_type,k,i)
return tuner_hash
def escapeSpecialCharacters ( self, text, characters ):
for character in characters:
text = text.replace( character, '\\' + character )
return text
def create_udev_rule(self):
#ACTION=="add",KERNEL=="dvb?.frontend?",SUBSYSTEM=="dvb",SUBSYSTEMS=="pci",DRIVERS=="saa7164",KERNELS=="0000:05:00.0",RUN+="/tmp/udev_link.sh dvb_5 $env{DEVNAME}"
line = ','.join(self.udev_props)
line += ' RUN+="/usr/MythVantage/bin/udev_link.sh %s $env{DEVNAME} "' %(self.get_tuner_hash())
return self.escapeSpecialCharacters(line,'[]')
def get_dev_node(self):
return self.device
def get_card_type(self):
return "dvb"
def get_description(self):
return self.description
def get_full_udev_attribs(self):
return self.full_udev_attribs
def get_tuner_hash(self):
return self.tuner_hash
def get_udev_rule(self):
return self.udev_rule
#------------
def gather_hdhr(tuner_list):
command="/usr/bin/hdhomerun_config --discover"
results=os.popen(command,'r')
lines=results.readlines()
try:
if lines[0].strip().split()[0] == "no":
print "HDHOMERUN not detected"
else:
for line in lines:
hdrdevice=line.strip().split()[2]
tuner_list.append(hdhr_tuners(hdrdevice))
except:
print "Error finding HDHOMERUN"
return tuner_list
def gather_v4l(tuner_list):
try:
filelist = os.listdir('/dev/v4l/by-path/')
except OSError:
pass
#fakelist=['/dev/v4l/video3', 'ivtv', 'WinTV PVR 500 (unit #2)', '0000:04:09.0']
#cardlist.append(fakelist)
try:
p = re.compile('^/dev/video?\d$')
filelist = glob.glob("/dev/v4l/by-path/*video*")
for device in filelist:
Device=os.path.realpath(device)
if not p.search(Device):
continue
#print "real device node"
#print Device
tuner_list.append(v4l_tuners(Device))
except IOError:
print "no v4l cards found"
return tuner_list
def gather_dvb(tuner_list):
try:
filelist = os.listdir('/dev/dvb/')
except OSError:
pass
try:
for d in filelist:
if os.path.islink("/dev/dvb/"+d):
continue
Device=os.path.realpath("/dev/dvb/"+d+"/frontend0")
tuner_list.append(dvb_tuners(Device))
except:
print "no dvb cards found"
return tuner_list
def gather_tuners():
tuner_list = []
tuner_list = gather_hdhr(tuner_list)
tuner_list = gather_v4l(tuner_list)
tuner_list = gather_dvb(tuner_list)
#tuner_list = gather_ceton(tuner_list)
#for i in tuner_list:
#if i.get_card_type() == "hdhr":
#pass
#print "hdhr : %s " %(i.get_dev_node())
#print "\t" , i.get_description()
#elif i.get_card_type() == "v4l":
#pass
#print "V4L : %s " %(i.get_dev_node())
#print "\t" , i.get_description()
##print "\t" , i.get_udev_probs()
#print "\t" , i.get_tuner_hash()
##print "\t" , i.get_udev_rule()
##print "\t" , i.get_full_udev_attribs()
#elif i.get_card_type() == "v4l_hdpvr":
#pass
#print "hdpvr : %s " %(i.get_dev_node())
#print "\t" , i.get_description()
##print "\t" , i.get_udev_probs()
#print "\t" , i.get_tuner_hash()
##print "\t" , i.get_udev_rule()
##print "\t" , i.get_full_udev_attribs()
#elif i.get_card_type() == "dvb":
#pass
#print "dvb : %s " %(i.get_dev_node())
#print "\t" , i.get_description()
#print "\t" , i.get_tuner_hash()
##print "\t" , i.get_full_udev_attribs()
##for y in i.get_full_udev_attribs():
## print y
##print "\t" , i.get_udev_rule()
#elif i.get_card_type() == "ceton":
#print "ceton : %s " %(i.get_dev_node())
#else:
#print i
return tuner_list
def write_udev_file(rule_list):
udevfile = '/etc/udev/rules.d/99-mv-persistent-video.rules'
try:
f = open(udevfile, 'w')
line = "#Do not edit this file, it is auto generated by autocard.py \n\n"
f.write(line)
for rule in rule_list:
if rule:
line = "%s \n" %(rule)
f.write(line)
f.close()
except:
print "Error creating %s" %udevfile
def write_udev_map(tuner_list):
udevfile = '/etc/udev/mv-persistent-video.description'
try:
f = open(udevfile, 'w')
line = "#Do not edit this file, it is auto generated by autocard.py \n"
f.write(line)
line = "#This file is used to generate the static tuner card web page\n"
f.write(line)
for i in tuner_list:
if i.get_card_type() == "hdhr":
pass
line="hdhr:%s:%s" %(i.get_description(),i.get_dev_node())
f.write(line)
f.write("\n")
elif i.get_card_type() == "v4l":
pass
line="V4L:%s:/dev/vstatic/%s " %(i.get_description(),i.get_tuner_hash())
f.write(line)
f.write("\n")
elif i.get_card_type() == "v4l_hdpvr":
pass
line="hdpvr:%s:/dev/vstatic/%s " %(i.get_description(),i.get_tuner_hash())
f.write(line)
f.write("\n")
elif i.get_card_type() == "dvb":
line="dvb:%s:/dev/dvb/adapter_static_%s " %(i.get_description(),i.get_tuner_hash())
f.write(line)
f.write("\n")
elif i.get_card_type() == "ceton":
line="ceton:%s:/dev/vstatic/%s " %(i.get_description(),i.get_tuner_hash())
f.write(line)
f.write("\n")
else:
print i
line = i
f.write(line)
f.write("\n")
except:
print "Error creating %s" %udevfile
f.close()
def main(argv):
tuner_list = gather_tuners()
rule_list = []
for i in tuner_list:
rule_list.append(i.get_udev_rule())
write_udev_file(rule_list)
write_udev_map(tuner_list)
if __name__ == "__main__":
main(sys.argv[1:])
|