removed some debugging prints no longer needed
[pyvconv.git] / command_executer.py
blob8e9f54a283a28c1ec3096ca888eaa9ed31fe26c3
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 re
17 import os
18 import time
19 import datetime
20 import subprocess
21 import thread
22 import threading
23 import signal
24 from commands import Variable
26 class CommandExecuterView:
27 def __init__(self):
28 pass
30 def recieve_executor(self, executor):
31 pass
33 def starting_conversion(self, infile, index, total, outfile):
34 pass
36 def update_progress(self, logline):
37 pass
39 def error(self):
40 pass
42 def finished_all(self):
43 pass
45 class CommandExecuter:
46 def mk_unique_subdir(self, parentdir):
47 p = re.compile("[\ ,\/,\(,\)]")
48 namepart = p.sub("_", self.command.get_name())
50 t = datetime.datetime.now()
51 newdir = os.path.join(parentdir, namepart + "_" + str(time.mktime(t.timetuple())))
53 if os.path.isdir(newdir):
54 raise OSError("Unable to make up unique name for new folder")
55 elif os.path.isfile(newdir):
56 raise OSError("a file with the same name as the desired " \
57 "dir, '%s', already exists." % newdir)
58 else:
59 os.mkdir(newdir)
60 return newdir
62 def __init__(self, cmdexecview, command, infilelist, outdir):
63 self.cmdexecview = cmdexecview
64 self.command = command
65 self.infilelist = infilelist
66 self.outdir = self.mk_unique_subdir(outdir)
68 self.please_abort = False
70 self.shared_vars_lock = threading.Lock()
71 thread.start_new_thread(self._run_conversion, ())
73 def abort(self):
74 self.shared_vars_lock.acquire()
75 self.please_abort = True
76 self.shared_vars_lock.release()
78 def _escape_filepath(self, path):
79 return "\"" + path + "\""
81 def _run_conversion(self):
82 index = 0
83 total = len(self.infilelist)
85 for infile in self.infilelist:
86 newbase = os.path.basename(infile)
87 newbase = newbase[0: min(newbase.rfind("."), len(newbase))] + "." + self.command.get_outfile_extension()
89 outfile = os.path.join(self.outdir, newbase)
91 index = index + 1
93 self.command.put_var(Variable("in", self._escape_filepath(infile)))
94 self.command.put_var(Variable("out", self._escape_filepath(outfile)))
96 self.cmdexecview.starting_conversion(infile, index, total, outfile)
97 p = subprocess.Popen(str(self.command), shell = True, universal_newlines=True, stderr = subprocess.PIPE)
98 ostream = p.stderr
100 self.cmdexecview.recieve_executor(self)
102 newline = str(chr(32) * 4)
103 buff = ""
104 c = 1
105 iskip = 10
106 skipc = 0
108 while c:
109 c = ostream.read(1)
111 buff = buff + c
112 if buff.endswith(newline):
113 skipc = (skipc + 1) % iskip
114 if skipc == 0:
115 self.cmdexecview.update_progress(buff.strip())
116 buff = ""
118 self.shared_vars_lock.acquire()
119 if self.please_abort == True:
120 ostream.close()
121 os.kill(p.pid, signal.SIGTERM)
122 return
123 self.shared_vars_lock.release()
125 ostream.close()
127 res = p.wait()
129 if res < 0:
130 self.cmdexecview.error()
131 return
133 self.cmdexecview.finished_all()