Debian Codegraph @

tools/KateDJ/kdj/tools.py

raw
1 import sys
2 import os
3 import subprocess
4
5 external_tools_path=['','/usr/bin/','/usr/local/bin/']
6 internal_tools_path=['../']+external_tools_path
7
8 class Tools:
9 demux_command=None
10 mux_command=None
11 codecs_command=None
12 kateenc_command=None
13 katedec_command=None
14
15 def __init__(self,on_start=None,on_end=None):
16 self.on_start=on_start
17 self.on_end=on_end
18 self.find_tools()
19
20 def probe_command(self,command,options,magic):
21 cmdline=command+' '+options
22 try:
23 popen=subprocess.Popen(cmdline,stdin=None,stderr=subprocess.PIPE,stdout=subprocess.PIPE,universal_newlines=True,shell=True)
24 except OSError as e:
25 return None
26 if not popen.stdout:
27 return None
28 if popen.stdin:
29 popen.stdin.close()
30 if popen.stderr:
31 popen.stderr.close()
32 line=popen.stdout.readline()
33 while line:
34 if magic in line:
35 popen.stdout.close()
36 return command
37 line=popen.stdout.readline()
38 popen.stdout.close()
39 return None
40
41 def probe_command_in(self,commands,options,magic,paths):
42 for command in commands:
43 for path in paths:
44 found=self.probe_command(path+command,options,magic)
45 if found!=None:
46 return found
47 return None
48
49 def check(self):
50 msg=''
51 if self.demux_command==None:
52 msg+='Failed to find the oggz-rip demuxing tool\n'
53
54 if self.mux_command==None:
55 msg+='Failed to find the oggz-merge muxing tool\n'
56
57 if self.kateenc_command==None:
58 msg+='Failed to find kateenc\n'
59
60 if self.katedec_command==None:
61 msg+='Failed to find katedec\n'
62
63 # oggz-known-codecs is ok not to be found
64
65 if msg!='':
66 raise Exception(msg)
67
68 def find_tools(self):
69 self.mux_command=self.probe_command_in(['oggz-merge','oggzmerge'],'-v','merge version',external_tools_path)
70 self.demux_command=self.probe_command_in(['oggz-rip','oggzrip'],'-v','rip version',external_tools_path)
71 self.codecs_command=self.probe_command_in(['oggz-known-codecs'],'-v','oggz-known-codecs version',external_tools_path)
72 self.kateenc_command=self.probe_command_in(['kateenc'],'-V','Kate reference encoder - libkate',internal_tools_path)
73 self.katedec_command=self.probe_command_in(['katedec'],'-V','Kate reference decoder - libkate',internal_tools_path)
74 self.check()
75
76 def run(self,params,stdin):
77 if self.on_start!=None: self.on_start()
78 try:
79 try:
80 popen=subprocess.Popen(params,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True)
81 if stdin==None:
82 (stdoutdata,stderrdata)=popen.communicate(stdin)
83 else:
84 (stdoutdata,stderrdata)=popen.communicate(stdin.encode('utf8'))
85 ret=popen.returncode
86 # oggz tools can return 0 when they fail, so do not test ret
87 msg=stderrdata.encode('utf8')
88 if msg!=None and msg!='':
89 raise Exception(msg)
90 except:
91 raise
92 finally:
93 if self.on_end!=None: self.on_end()
94
95 def run_demux(self,params,stdin=None):
96 if self.demux_command==None:
97 raise Exception('No demux command found')
98 self.run([self.demux_command]+params,stdin)
99
100 def run_mux(self,params,stdin=None):
101 if self.mux_command==None:
102 raise Exception('No mux command found')
103 self.run([self.mux_command]+params,stdin)
104
105 def run_katedec(self,params,stdin=None):
106 if self.katedec_command==None:
107 raise Exception('katedec was not found')
108 self.run([self.katedec_command]+params,stdin)
109
110 def run_kateenc(self,params,stdin=None):
111 if self.kateenc_command==None:
112 raise Exception('kateenc was not found')
113 self.run([self.kateenc_command]+params,stdin)
114
115 if __name__=='__main__':
116 tools=Tools();
117