added DV-support
[pyvconv.git] / command_executer.py
blob9764981c5e6efec7ebca1cbe110218115051b5cb
1 # Pyvconv - A simple frontend for ffmpeg/mencoder
2 # Copyright (C) 2008, Kristian Rumberg (kristianrumberg@gmail.com)
4 # Permission to use, copy, modify, and/or distribute this software for any
5 # purpose with or without fee is hereby granted, provided that the above
6 # copyright notice and this permission notice appear in all copies.
8 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 import os
17 import time
18 import datetime
19 import subprocess
20 import thread
21 import threading
22 import signal
23 from commands import Variable
25 class CommandExecuterView:
26 def __init__(self):
27 pass
29 def recieve_executor(self, executor):
30 pass
32 def starting_conversion(self, infile, index, total, outfile):
33 pass
35 def update_progress(self, logline):
36 pass
38 def error(self):
39 pass
41 def finished_all(self):
42 pass
44 class CommandExecuter:
45 def mk_unique_subdir(self, parentdir):
46 t = datetime.datetime.now()
47 newdir = parentdir + "/" + self.command.get_name().replace("/", "_") + "_" + str(time.mktime(t.timetuple()))
49 if os.path.isdir(newdir):
50 raise OSError("Unable to make up unique name for new folder")
51 elif os.path.isfile(newdir):
52 raise OSError("a file with the same name as the desired " \
53 "dir, '%s', already exists." % newdir)
54 else:
55 os.mkdir(newdir)
56 return newdir
58 def __init__(self, cmdexecview, command, infilelist, outdir):
59 self.cmdexecview = cmdexecview
60 self.command = command
61 self.infilelist = infilelist
62 self.outdir = self.mk_unique_subdir(outdir)
64 self.please_abort = False
66 self.shared_vars_lock = threading.Lock()
67 thread.start_new_thread(self._run_conversion, ())
69 def abort(self):
70 self.shared_vars_lock.acquire()
71 self.please_abort = True
72 self.shared_vars_lock.release()
74 def _run_conversion(self):
75 index = 0
76 total = len(self.infilelist)
78 for infile in self.infilelist:
79 outfile = self.outdir + "/" + os.path.basename(infile) + "_converted"
81 index = index + 1
83 self.command.put_var(Variable("in", infile))
84 self.command.put_var(Variable("out", outfile))
86 self.cmdexecview.starting_conversion(infile, index, total, outfile)
88 p = subprocess.Popen(str(self.command), shell = True, universal_newlines=True, stderr = subprocess.PIPE)
89 ostream = p.stderr
91 self.cmdexecview.recieve_executor(self)
93 newline = str(chr(32) * 4)
94 buff = ""
95 c = 1
96 iskip = 10
97 skipc = 0
99 while c:
100 c = ostream.read(1)
101 buff = buff + c
102 if buff.endswith(newline):
103 skipc = (skipc + 1) % iskip
104 if skipc == 0:
105 self.cmdexecview.update_progress(buff.strip())
106 buff = ""
108 self.shared_vars_lock.acquire()
109 if self.please_abort == True:
110 ostream.close()
111 os.kill(p.pid, signal.SIGTERM)
112 return
113 self.shared_vars_lock.release()
115 ostream.close()
117 res = p.wait()
119 if res < 0:
120 self.cmdexecview.error()
121 return
123 self.cmdexecview.finished_all()