summaryrefslogtreecommitdiffstats
path: root/lldb/third_party/Python/module/pexpect-2.4/examples
diff options
context:
space:
mode:
Diffstat (limited to 'lldb/third_party/Python/module/pexpect-2.4/examples')
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/astat.py27
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/bd_client.py22
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/bd_serv.py161
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/chess.py151
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/chess2.py208
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/chess3.py177
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/df.py9
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/fix_cvs_files.py111
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/ftp.py10
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/hive.py159
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/monitor.py104
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/passmass.py45
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/python.py7
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/rippy.py843
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/script.py50
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/ssh_session.py45
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/ssh_tunnel.py50
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/sshls.py27
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/topip.py102
-rw-r--r--lldb/third_party/Python/module/pexpect-2.4/examples/uptime.py15
20 files changed, 1398 insertions, 925 deletions
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/astat.py b/lldb/third_party/Python/module/pexpect-2.4/examples/astat.py
index 82fa3c68b70..a5d1b3514bc 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/astat.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/astat.py
@@ -13,30 +13,42 @@ Example:
"""
-import os, sys, time, re, getopt, getpass
+import os
+import sys
+import time
+import re
+import getopt
+import getpass
import traceback
-import pexpect, pxssh
+import pexpect
+import pxssh
+
def exit_with_usage():
print globals()['__doc__']
os._exit(1)
+
def main():
######################################################################
- ## Parse the options, arguments, get ready, etc.
+ # Parse the options, arguments, get ready, etc.
######################################################################
try:
- optlist, args = getopt.getopt(sys.argv[1:], 'h?s:u:p:', ['help','h','?'])
- except Exception, e:
+ optlist, args = getopt.getopt(
+ sys.argv[
+ 1:], 'h?s:u:p:', [
+ 'help', 'h', '?'])
+ except Exception as e:
print str(e)
exit_with_usage()
options = dict(optlist)
if len(args) > 1:
exit_with_usage()
- if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]:
+ if [elem for elem in options if elem in [
+ '-h', '--h', '-?', '--?', '--help']]:
print "Help:"
exit_with_usage()
@@ -67,8 +79,7 @@ def main():
if __name__ == "__main__":
try:
main()
- except Exception, e:
+ except Exception as e:
print str(e)
traceback.print_exc()
os._exit(1)
-
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/bd_client.py b/lldb/third_party/Python/module/pexpect-2.4/examples/bd_client.py
index 564739a0aad..a941bc93555 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/bd_client.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/bd_client.py
@@ -4,35 +4,37 @@
for testing rather than normal use. See bd_serv.py """
import socket
-import sys, time, select
+import sys
+import time
+import select
+
def recv_wrapper(s):
- r,w,e = select.select([s.fileno()],[],[], 2)
+ r, w, e = select.select([s.fileno()], [], [], 2)
if not r:
return ''
#cols = int(s.recv(4))
#rows = int(s.recv(4))
cols = 80
rows = 24
- packet_size = cols * rows * 2 # double it for good measure
+ packet_size = cols * rows * 2 # double it for good measure
return s.recv(packet_size)
-#HOST = '' #'localhost' # The remote host
-#PORT = 1664 # The same port as used by the server
+# HOST = '' #'localhost' # The remote host
+# PORT = 1664 # The same port as used by the server
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
-s.connect(sys.argv[1])#(HOST, PORT))
+s.connect(sys.argv[1]) # (HOST, PORT))
time.sleep(1)
-#s.setblocking(0)
+# s.setblocking(0)
#s.send('COMMAND' + '\x01' + sys.argv[1])
s.send(':sendline ' + sys.argv[2])
print recv_wrapper(s)
s.close()
sys.exit()
-#while True:
+# while True:
# data = recv_wrapper(s)
# if data == '':
# break
# sys.stdout.write (data)
# sys.stdout.flush()
-#s.close()
-
+# s.close()
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/bd_serv.py b/lldb/third_party/Python/module/pexpect-2.4/examples/bd_serv.py
index b7def9e1402..aa6a9ddb2c3 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/bd_serv.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/bd_serv.py
@@ -14,20 +14,30 @@ This exposes an shell terminal on a socket.
# Having the password on the command line is not a good idea, but
# then this entire project is probably not the most security concious thing
# I've ever built. This should be considered an experimental tool -- at best.
-import pxssh, pexpect, ANSI
-import time, sys, os, getopt, getpass, traceback, threading, socket
+import pxssh
+import pexpect
+import ANSI
+import time
+import sys
+import os
+import getopt
+import getpass
+import traceback
+import threading
+import socket
+
def exit_with_usage(exit_code=1):
print globals()['__doc__']
os._exit(exit_code)
+
class roller (threading.Thread):
"""This runs a function in a loop in a thread."""
def __init__(self, interval, function, args=[], kwargs={}):
-
"""The interval parameter defines time between each call to the function.
"""
@@ -39,7 +49,6 @@ class roller (threading.Thread):
self.finished = threading.Event()
def cancel(self):
-
"""Stop the roller."""
self.finished.set()
@@ -50,8 +59,8 @@ class roller (threading.Thread):
# self.finished.wait(self.interval)
self.function(*self.args, **self.kwargs)
-def endless_poll (child, prompt, screen, refresh_timeout=0.1):
+def endless_poll(child, prompt, screen, refresh_timeout=0.1):
"""This keeps the screen updated with the output of the child. This runs in
a separate thread. See roller(). """
@@ -61,7 +70,7 @@ def endless_poll (child, prompt, screen, refresh_timeout=0.1):
screen.write(s)
except:
pass
- #while True:
+ # while True:
# #child.prompt (timeout=refresh_timeout)
# try:
# #child.read_nonblocking(1,timeout=refresh_timeout)
@@ -69,14 +78,14 @@ def endless_poll (child, prompt, screen, refresh_timeout=0.1):
# except:
# pass
-def daemonize (stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
+def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
'''This forks the current process into a daemon. Almost none of this is
necessary (or advisable) if your daemon is being started by inetd. In that
case, stdin, stdout and stderr are all set up for you to refer to the
network connection, and the fork()s and session manipulation should not be
done (to avoid confusing inetd). Only the chdir() and umask() steps remain
- as useful.
+ as useful.
References:
UNIX Programming FAQ
@@ -94,30 +103,30 @@ def daemonize (stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
expect. '''
# Do first fork.
- try:
- pid = os.fork()
+ try:
+ pid = os.fork()
if pid > 0:
sys.exit(0) # Exit first parent.
- except OSError, e:
- sys.stderr.write ("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror) )
+ except OSError as e:
+ sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
sys.exit(1)
# Decouple from parent environment.
- os.chdir("/")
- os.umask(0)
- os.setsid()
+ os.chdir("/")
+ os.umask(0)
+ os.setsid()
# Do second fork.
- try:
- pid = os.fork()
+ try:
+ pid = os.fork()
if pid > 0:
sys.exit(0) # Exit second parent.
- except OSError, e:
- sys.stderr.write ("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror) )
+ except OSError as e:
+ sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
sys.exit(1)
# Now I am a daemon!
-
+
# Redirect standard file descriptors.
si = open(stdin, 'r')
so = open(stdout, 'a+')
@@ -129,25 +138,32 @@ def daemonize (stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
# I now return as the daemon
return 0
-def add_cursor_blink (response, row, col):
- i = (row-1) * 80 + col
- return response[:i]+'<img src="http://www.noah.org/cursor.gif">'+response[i:]
+def add_cursor_blink(response, row, col):
+
+ i = (row - 1) * 80 + col
+ return response[:i] + \
+ '<img src="http://www.noah.org/cursor.gif">' + response[i:]
-def main ():
+
+def main():
try:
- optlist, args = getopt.getopt(sys.argv[1:], 'h?d', ['help','h','?', 'hostname=', 'username=', 'password=', 'port=', 'watch'])
- except Exception, e:
+ optlist, args = getopt.getopt(
+ sys.argv[
+ 1:], 'h?d', [
+ 'help', 'h', '?', 'hostname=', 'username=', 'password=', 'port=', 'watch'])
+ except Exception as e:
print str(e)
exit_with_usage()
command_line_options = dict(optlist)
options = dict(optlist)
# There are a million ways to cry for help. These are but a few of them.
- if [elem for elem in command_line_options if elem in ['-h','--h','-?','--?','--help']]:
+ if [elem for elem in command_line_options if elem in [
+ '-h', '--h', '-?', '--?', '--help']]:
exit_with_usage(0)
-
+
hostname = "127.0.0.1"
port = 1664
username = os.getenv('USER')
@@ -170,38 +186,39 @@ def main ():
password = options['--password']
else:
password = getpass.getpass('password: ')
-
- if daemon_mode:
+
+ if daemon_mode:
print "daemonizing server"
daemonize()
- #daemonize('/dev/null','/tmp/daemon.log','/tmp/daemon.log')
-
- sys.stdout.write ('server started with pid %d\n' % os.getpid() )
+ # daemonize('/dev/null','/tmp/daemon.log','/tmp/daemon.log')
+
+ sys.stdout.write('server started with pid %d\n' % os.getpid())
- virtual_screen = ANSI.ANSI (24,80)
+ virtual_screen = ANSI.ANSI(24, 80)
child = pxssh.pxssh()
- child.login (hostname, username, password)
+ child.login(hostname, username, password)
print 'created shell. command line prompt is', child.PROMPT
#child.sendline ('stty -echo')
- #child.setecho(False)
- virtual_screen.write (child.before)
- virtual_screen.write (child.after)
+ # child.setecho(False)
+ virtual_screen.write(child.before)
+ virtual_screen.write(child.after)
- if os.path.exists("/tmp/mysock"): os.remove("/tmp/mysock")
+ if os.path.exists("/tmp/mysock"):
+ os.remove("/tmp/mysock")
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
localhost = '127.0.0.1'
s.bind('/tmp/mysock')
- os.chmod('/tmp/mysock',0777)
+ os.chmod('/tmp/mysock', 0o777)
print 'Listen'
s.listen(1)
print 'Accept'
#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#localhost = '127.0.0.1'
#s.bind((localhost, port))
- #print 'Listen'
- #s.listen(1)
+ # print 'Listen'
+ # s.listen(1)
- r = roller (0.01, endless_poll, (child, child.PROMPT, virtual_screen))
+ r = roller(0.01, endless_poll, (child, child.PROMPT, virtual_screen))
r.start()
print "screen poll updater started in background thread"
sys.stdout.flush()
@@ -211,12 +228,12 @@ def main ():
conn, addr = s.accept()
print 'Connected by', addr
data = conn.recv(1024)
- if data[0]!=':':
+ if data[0] != ':':
cmd = ':sendline'
arg = data.strip()
else:
request = data.split(' ', 1)
- if len(request)>1:
+ if len(request) > 1:
cmd = request[0].strip()
arg = request[1].strip()
else:
@@ -225,45 +242,50 @@ def main ():
r.cancel()
break
elif cmd == ':sendline':
- child.sendline (arg)
- #child.prompt(timeout=2)
+ child.sendline(arg)
+ # child.prompt(timeout=2)
time.sleep(0.2)
shell_window = str(virtual_screen)
- elif cmd == ':send' or cmd==':xsend':
- if cmd==':xsend':
+ elif cmd == ':send' or cmd == ':xsend':
+ if cmd == ':xsend':
arg = arg.decode("hex")
- child.send (arg)
+ child.send(arg)
time.sleep(0.2)
shell_window = str(virtual_screen)
elif cmd == ':cursor':
- shell_window = '%x%x' % (virtual_screen.cur_r, virtual_screen.cur_c)
+ shell_window = '%x%x' % (
+ virtual_screen.cur_r, virtual_screen.cur_c)
elif cmd == ':refresh':
shell_window = str(virtual_screen)
response = []
- response.append (shell_window)
+ response.append(shell_window)
#response = add_cursor_blink (response, row, col)
sent = conn.send('\n'.join(response))
- if watch_mode: print '\n'.join(response)
- if sent < len (response):
+ if watch_mode:
+ print '\n'.join(response)
+ if sent < len(response):
print "Sent is too short. Some data was cut off."
conn.close()
finally:
r.cancel()
print "cleaning up socket"
s.close()
- if os.path.exists("/tmp/mysock"): os.remove("/tmp/mysock")
+ if os.path.exists("/tmp/mysock"):
+ os.remove("/tmp/mysock")
print "done!"
-def pretty_box (rows, cols, s):
+def pretty_box(rows, cols, s):
"""This puts an ASCII text box around the given string, s.
"""
- top_bot = '+' + '-'*cols + '+\n'
- return top_bot + '\n'.join(['|'+line+'|' for line in s.split('\n')]) + '\n' + top_bot
-
-def error_response (msg):
+ top_bot = '+' + '-' * cols + '+\n'
+ return top_bot + \
+ '\n'.join(['|' + line + '|' for line in s.split('\n')]) + '\n' + top_bot
+
+
+def error_response(msg):
response = []
response.append ("""All commands start with :
@@ -280,11 +302,11 @@ Example:
is equivalent to:
:sendline ls -l
""")
- response.append (msg)
+ response.append(msg)
return '\n'.join(response)
-def parse_host_connect_string (hcs):
+def parse_host_connect_string(hcs):
"""This parses a host connection string in the form
username:password@hostname:port. All fields are options expcet hostname. A
dictionary is returned with all four keys. Keys that were not included are
@@ -292,14 +314,16 @@ def parse_host_connect_string (hcs):
then you must backslash escape it. """
if '@' in hcs:
- p = re.compile (r'(?P<username>[^@:]*)(:?)(?P<password>.*)(?!\\)@(?P<hostname>[^:]*):?(?P<port>[0-9]*)')
+ p = re.compile(
+ r'(?P<username>[^@:]*)(:?)(?P<password>.*)(?!\\)@(?P<hostname>[^:]*):?(?P<port>[0-9]*)')
else:
- p = re.compile (r'(?P<username>)(?P<password>)(?P<hostname>[^:]*):?(?P<port>[0-9]*)')
- m = p.search (hcs)
+ p = re.compile(
+ r'(?P<username>)(?P<password>)(?P<hostname>[^:]*):?(?P<port>[0-9]*)')
+ m = p.search(hcs)
d = m.groupdict()
- d['password'] = d['password'].replace('\\@','@')
+ d['password'] = d['password'].replace('\\@', '@')
return d
-
+
if __name__ == "__main__":
try:
@@ -309,8 +333,7 @@ if __name__ == "__main__":
print time.asctime()
print "TOTAL TIME IN MINUTES:",
print (time.time() - start_time) / 60.0
- except Exception, e:
+ except Exception as e:
print str(e)
tb_dump = traceback.format_exc()
print str(tb_dump)
-
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/chess.py b/lldb/third_party/Python/module/pexpect-2.4/examples/chess.py
index 8c32cf798f2..2e785182bd4 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/chess.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/chess.py
@@ -11,121 +11,122 @@ import ANSI
REGEX_MOVE = '(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)'
REGEX_MOVE_PART = '(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)'
+
class Chess:
- def __init__(self, engine = "/usr/local/bin/gnuchess -a -h 1"):
- self.child = pexpect.spawn (engine)
- self.term = ANSI.ANSI ()
-
- self.child.expect ('Chess')
- if self.child.after != 'Chess':
- raise IOError, 'incompatible chess program'
- self.term.process_list (self.before)
- self.term.process_list (self.after)
- self.last_computer_move = ''
- def read_until_cursor (self, r,c)
- while 1:
- self.child.read(1, 60)
- self.term.process (c)
- if self.term.cur_r == r and self.term.cur_c == c:
- return 1
-
- def do_first_move (self, move):
- self.child.expect ('Your move is')
- self.child.sendline (move)
- self.term.process_list (self.before)
- self.term.process_list (self.after)
- return move
-
- def do_move (self, move):
- read_until_cursor (19,60)
- #self.child.expect ('\[19;60H')
- self.child.sendline (move)
- print 'do_move' move
- return move
-
- def get_first_computer_move (self):
- self.child.expect ('My move is')
- self.child.expect (REGEX_MOVE)
+ def __init__(self, engine="/usr/local/bin/gnuchess -a -h 1"):
+ self.child = pexpect.spawn(engine)
+ self.term = ANSI.ANSI()
+
+ self.child.expect('Chess')
+ if self.child.after != 'Chess':
+ raise IOError, 'incompatible chess program'
+ self.term.process_list(self.before)
+ self.term.process_list(self.after)
+ self.last_computer_move = ''
+
+ def read_until_cursor(self, r, c)
+ while 1:
+ self.child.read(1, 60)
+ self.term.process(c)
+ if self.term.cur_r == r and self.term.cur_c == c:
+ return 1
+
+ def do_first_move(self, move):
+ self.child.expect('Your move is')
+ self.child.sendline(move)
+ self.term.process_list(self.before)
+ self.term.process_list(self.after)
+ return move
+
+ def do_move(self, move):
+ read_until_cursor(19, 60)
+ #self.child.expect ('\[19;60H')
+ self.child.sendline(move)
+ print 'do_move' move
+ return move
+
+ def get_first_computer_move(self):
+ self.child.expect('My move is')
+ self.child.expect(REGEX_MOVE)
# print '', self.child.after
- return self.child.after
-
- def get_computer_move (self):
- print 'Here'
- i = self.child.expect (['\[17;59H', '\[17;58H'])
- print i
- if i == 0:
- self.child.expect (REGEX_MOVE)
- if len(self.child.after) < 4:
- self.child.after = self.child.after + self.last_computer_move[3]
- if i == 1:
- self.child.expect (REGEX_MOVE_PART)
- self.child.after = self.last_computer_move[0] + self.child.after
- print '', self.child.after
- self.last_computer_move = self.child.after
- return self.child.after
-
- def switch (self):
- self.child.sendline ('switch')
-
- def set_depth (self, depth):
- self.child.sendline ('depth')
- self.child.expect ('depth=')
- self.child.sendline ('%d' % depth)
-
- def quit(self):
- self.child.sendline ('quit')
-import sys, os
+ return self.child.after
+
+ def get_computer_move(self):
+ print 'Here'
+ i = self.child.expect(['\[17;59H', '\[17;58H'])
+ print i
+ if i == 0:
+ self.child.expect(REGEX_MOVE)
+ if len(self.child.after) < 4:
+ self.child.after = self.child.after + \
+ self.last_computer_move[3]
+ if i == 1:
+ self.child.expect(REGEX_MOVE_PART)
+ self.child.after = self.last_computer_move[0] + self.child.after
+ print '', self.child.after
+ self.last_computer_move = self.child.after
+ return self.child.after
+
+ def switch(self):
+ self.child.sendline('switch')
+
+ def set_depth(self, depth):
+ self.child.sendline('depth')
+ self.child.expect('depth=')
+ self.child.sendline('%d' % depth)
+
+ def quit(self):
+ self.child.sendline('quit')
+import sys
+import os
print 'Starting...'
white = Chess()
white.child.echo = 1
-white.child.expect ('Your move is')
+white.child.expect('Your move is')
white.set_depth(2)
white.switch()
move_white = white.get_first_computer_move()
print 'first move white:', move_white
-white.do_move ('e7e5')
+white.do_move('e7e5')
move_white = white.get_computer_move()
print 'move white:', move_white
-white.do_move ('f8c5')
+white.do_move('f8c5')
move_white = white.get_computer_move()
print 'move white:', move_white
-white.do_move ('b8a6')
+white.do_move('b8a6')
move_white = white.get_computer_move()
print 'move white:', move_white
sys.exit(1)
-
black = Chess()
white = Chess()
-white.child.expect ('Your move is')
+white.child.expect('Your move is')
white.switch()
move_white = white.get_first_computer_move()
print 'first move white:', move_white
-black.do_first_move (move_white)
+black.do_first_move(move_white)
move_black = black.get_first_computer_move()
print 'first move black:', move_black
-white.do_move (move_black)
+white.do_move(move_black)
done = 0
while not done:
move_white = white.get_computer_move()
print 'move white:', move_white
- black.do_move (move_white)
+ black.do_move(move_white)
move_black = black.get_computer_move()
print 'move black:', move_black
-
- white.do_move (move_black)
+
+ white.do_move(move_black)
print 'tail of loop'
g.quit()
-
-
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/chess2.py b/lldb/third_party/Python/module/pexpect-2.4/examples/chess2.py
index c62d5ce37a0..c75b6d8493f 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/chess2.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/chess2.py
@@ -7,125 +7,129 @@ It starts two instances of gnuchess and then pits them against each other.
import pexpect
import string
import ANSI
-import sys, os, time
+import sys
+import os
+import time
+
class Chess:
- def __init__(self, engine = "/usr/local/bin/gnuchess -a -h 1"):
- self.child = pexpect.spawn (engine)
- self.term = ANSI.ANSI ()
-
- #self.child.expect ('Chess')
- #if self.child.after != 'Chess':
- # raise IOError, 'incompatible chess program'
- #self.term.process_list (self.child.before)
- #self.term.process_list (self.child.after)
-
- self.last_computer_move = ''
-
- def read_until_cursor (self, r,c, e=0):
- '''Eventually something like this should move into the screen class or
- a subclass. Maybe a combination of pexpect and screen...
- '''
- fout = open ('log','a')
- while self.term.cur_r != r or self.term.cur_c != c:
- try:
- k = self.child.read(1, 10)
- except Exception, e:
- print 'EXCEPTION, (r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c)
- sys.stdout.flush()
- self.term.process (k)
- fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
- fout.flush()
- if e:
- sys.stdout.write (k)
- sys.stdout.flush()
- if self.term.cur_r == r and self.term.cur_c == c:
- fout.close()
- return 1
- print 'DIDNT EVEN HIT.'
- fout.close()
- return 1
-
- def expect_region (self):
- '''This is another method that would be moved into the
- screen class.
- '''
- pass
- def do_scan (self):
- fout = open ('log','a')
- while 1:
- c = self.child.read(1,10)
- self.term.process (c)
- fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
- fout.flush()
- sys.stdout.write (c)
+ def __init__(self, engine="/usr/local/bin/gnuchess -a -h 1"):
+ self.child = pexpect.spawn(engine)
+ self.term = ANSI.ANSI()
+
+ #self.child.expect ('Chess')
+ # if self.child.after != 'Chess':
+ # raise IOError, 'incompatible chess program'
+ #self.term.process_list (self.child.before)
+ #self.term.process_list (self.child.after)
+
+ self.last_computer_move = ''
+
+ def read_until_cursor(self, r, c, e=0):
+ '''Eventually something like this should move into the screen class or
+ a subclass. Maybe a combination of pexpect and screen...
+ '''
+ fout = open('log', 'a')
+ while self.term.cur_r != r or self.term.cur_c != c:
+ try:
+ k = self.child.read(1, 10)
+ except Exception as e:
+ print 'EXCEPTION, (r,c):(%d,%d)\n' % (self.term.cur_r, self.term.cur_c)
sys.stdout.flush()
-
- def do_move (self, move, e = 0):
- time.sleep(1)
- self.read_until_cursor (19,60, e)
- self.child.sendline (move)
-
- def wait (self, color):
- while 1:
- r = self.term.get_region (14,50,14,60)[0]
- r = r.strip()
- if r == color:
- return
- time.sleep (1)
-
- def parse_computer_move (self, s):
- i = s.find ('is: ')
- cm = s[i+3:i+9]
- return cm
- def get_computer_move (self, e = 0):
- time.sleep(1)
- self.read_until_cursor (19,60, e)
- time.sleep(1)
- r = self.term.get_region (17,50,17,62)[0]
- cm = self.parse_computer_move (r)
- return cm
-
- def switch (self):
- print 'switching'
- self.child.sendline ('switch')
-
- def set_depth (self, depth):
- self.child.sendline ('depth')
- self.child.expect ('depth=')
- self.child.sendline ('%d' % depth)
-
- def quit(self):
- self.child.sendline ('quit')
-
-def LOG (s):
+ self.term.process(k)
+ fout.write('(r,c):(%d,%d)\n' % (self.term.cur_r, self.term.cur_c))
+ fout.flush()
+ if e:
+ sys.stdout.write(k)
+ sys.stdout.flush()
+ if self.term.cur_r == r and self.term.cur_c == c:
+ fout.close()
+ return 1
+ print 'DIDNT EVEN HIT.'
+ fout.close()
+ return 1
+
+ def expect_region(self):
+ '''This is another method that would be moved into the
+ screen class.
+ '''
+ pass
+
+ def do_scan(self):
+ fout = open('log', 'a')
+ while True:
+ c = self.child.read(1, 10)
+ self.term.process(c)
+ fout.write('(r,c):(%d,%d)\n' % (self.term.cur_r, self.term.cur_c))
+ fout.flush()
+ sys.stdout.write(c)
+ sys.stdout.flush()
+
+ def do_move(self, move, e=0):
+ time.sleep(1)
+ self.read_until_cursor(19, 60, e)
+ self.child.sendline(move)
+
+ def wait(self, color):
+ while True:
+ r = self.term.get_region(14, 50, 14, 60)[0]
+ r = r.strip()
+ if r == color:
+ return
+ time.sleep(1)
+
+ def parse_computer_move(self, s):
+ i = s.find('is: ')
+ cm = s[i + 3:i + 9]
+ return cm
+
+ def get_computer_move(self, e=0):
+ time.sleep(1)
+ self.read_until_cursor(19, 60, e)
+ time.sleep(1)
+ r = self.term.get_region(17, 50, 17, 62)[0]
+ cm = self.parse_computer_move(r)
+ return cm
+
+ def switch(self):
+ print 'switching'
+ self.child.sendline('switch')
+
+ def set_depth(self, depth):
+ self.child.sendline('depth')
+ self.child.expect('depth=')
+ self.child.sendline('%d' % depth)
+
+ def quit(self):
+ self.child.sendline('quit')
+
+
+def LOG(s):
print s
- sys.stdout.flush ()
- fout = open ('moves.log', 'a')
- fout.write (s + '\n')
+ sys.stdout.flush()
+ fout = open('moves.log', 'a')
+ fout.write(s + '\n')
fout.close()
print 'Starting...'
black = Chess()
white = Chess()
-white.read_until_cursor (19,60,1)
+white.read_until_cursor(19, 60, 1)
white.switch()
done = 0
while not done:
- white.wait ('Black')
+ white.wait('Black')
move_white = white.get_computer_move(1)
- LOG ( 'move white:'+ move_white )
+ LOG('move white:' + move_white)
- black.do_move (move_white)
- black.wait ('White')
+ black.do_move(move_white)
+ black.wait('White')
move_black = black.get_computer_move()
- LOG ( 'move black:'+ move_black )
-
- white.do_move (move_black, 1)
-
-g.quit()
+ LOG('move black:' + move_black)
+ white.do_move(move_black, 1)
+g.quit()
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/chess3.py b/lldb/third_party/Python/module/pexpect-2.4/examples/chess3.py
index 44044420a1c..2fa7fb62722 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/chess3.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/chess3.py
@@ -11,128 +11,129 @@ import ANSI
REGEX_MOVE = '(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)'
REGEX_MOVE_PART = '(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)'
+
class Chess:
- def __init__(self, engine = "/usr/local/bin/gnuchess -a -h 1"):
- self.child = pexpect.spawn (engine)
- self.term = ANSI.ANSI ()
-
+ def __init__(self, engine="/usr/local/bin/gnuchess -a -h 1"):
+ self.child = pexpect.spawn(engine)
+ self.term = ANSI.ANSI()
+
# self.child.expect ('Chess')
- # if self.child.after != 'Chess':
- # raise IOError, 'incompatible chess program'
- # self.term.process_list (self.before)
- # self.term.process_list (self.after)
- self.last_computer_move = ''
- def read_until_cursor (self, r,c):
- fout = open ('log','a')
- while 1:
- k = self.child.read(1, 10)
- self.term.process (k)
- fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
- fout.flush()
- if self.term.cur_r == r and self.term.cur_c == c:
- fout.close()
- return 1
- sys.stdout.write (k)
- sys.stdout.flush()
-
- def do_scan (self):
- fout = open ('log','a')
- while 1:
- c = self.child.read(1,10)
- self.term.process (c)
- fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
- fout.flush()
- sys.stdout.write (c)
- sys.stdout.flush()
-
- def do_move (self, move):
- self.read_until_cursor (19,60)
- self.child.sendline (move)
- return move
-
- def get_computer_move (self):
- print 'Here'
- i = self.child.expect (['\[17;59H', '\[17;58H'])
- print i
- if i == 0:
- self.child.expect (REGEX_MOVE)
- if len(self.child.after) < 4:
- self.child.after = self.child.after + self.last_computer_move[3]
- if i == 1:
- self.child.expect (REGEX_MOVE_PART)
- self.child.after = self.last_computer_move[0] + self.child.after
- print '', self.child.after
- self.last_computer_move = self.child.after
- return self.child.after
-
- def switch (self):
- self.child.sendline ('switch')
-
- def set_depth (self, depth):
- self.child.sendline ('depth')
- self.child.expect ('depth=')
- self.child.sendline ('%d' % depth)
-
- def quit(self):
- self.child.sendline ('quit')
-import sys, os
+ # if self.child.after != 'Chess':
+ # raise IOError, 'incompatible chess program'
+ # self.term.process_list (self.before)
+ # self.term.process_list (self.after)
+ self.last_computer_move = ''
+
+ def read_until_cursor(self, r, c):
+ fout = open('log', 'a')
+ while True:
+ k = self.child.read(1, 10)
+ self.term.process(k)
+ fout.write('(r,c):(%d,%d)\n' % (self.term.cur_r, self.term.cur_c))
+ fout.flush()
+ if self.term.cur_r == r and self.term.cur_c == c:
+ fout.close()
+ return 1
+ sys.stdout.write(k)
+ sys.stdout.flush()
+
+ def do_scan(self):
+ fout = open('log', 'a')
+ while True:
+ c = self.child.read(1, 10)
+ self.term.process(c)
+ fout.write('(r,c):(%d,%d)\n' % (self.term.cur_r, self.term.cur_c))
+ fout.flush()
+ sys.stdout.write(c)
+ sys.stdout.flush()
+
+ def do_move(self, move):
+ self.read_until_cursor(19, 60)
+ self.child.sendline(move)
+ return move
+
+ def get_computer_move(self):
+ print 'Here'
+ i = self.child.expect(['\[17;59H', '\[17;58H'])
+ print i
+ if i == 0:
+ self.child.expect(REGEX_MOVE)
+ if len(self.child.after) < 4:
+ self.child.after = self.child.after + \
+ self.last_computer_move[3]
+ if i == 1:
+ self.child.expect(REGEX_MOVE_PART)
+ self.child.after = self.last_computer_move[0] + self.child.after
+ print '', self.child.after
+ self.last_computer_move = self.child.after
+ return self.child.after
+
+ def switch(self):
+ self.child.sendline('switch')
+
+ def set_depth(self, depth):
+ self.child.sendline('depth')
+ self.child.expect('depth=')
+ self.child.sendline('%d' % depth)
+
+ def quit(self):
+ self.child.sendline('quit')
+import sys
+import os
print 'Starting...'
white = Chess()
white.do_move('b2b4')
-white.read_until_cursor (19,60)
-c1 = white.term.get_abs(17,58)
-c2 = white.term.get_abs(17,59)
-c3 = white.term.get_abs(17,60)
-c4 = white.term.get_abs(17,61)
-fout = open ('log','a')
-fout.write ('Computer:%s%s%s%s\n' %(c1,c2,c3,c4))
+white.read_until_cursor(19, 60)
+c1 = white.term.get_abs(17, 58)
+c2 = white.term.get_abs(17, 59)
+c3 = white.term.get_abs(17, 60)
+c4 = white.term.get_abs(17, 61)
+fout = open('log', 'a')
+fout.write('Computer:%s%s%s%s\n' % (c1, c2, c3, c4))
fout.close()
white.do_move('c2c4')
-white.read_until_cursor (19,60)
-c1 = white.term.get_abs(17,58)
-c2 = white.term.get_abs(17,59)
-c3 = white.term.get_abs(17,60)
-c4 = white.term.get_abs(17,61)
-fout = open ('log','a')
-fout.write ('Computer:%s%s%s%s\n' %(c1,c2,c3,c4))
+white.read_until_cursor(19, 60)
+c1 = white.term.get_abs(17, 58)
+c2 = white.term.get_abs(17, 59)
+c3 = white.term.get_abs(17, 60)
+c4 = white.term.get_abs(17, 61)
+fout = open('log', 'a')
+fout.write('Computer:%s%s%s%s\n' % (c1, c2, c3, c4))
fout.close()
-white.do_scan ()
+white.do_scan()
#white.do_move ('b8a6')
#move_white = white.get_computer_move()
-#print 'move white:', move_white
+# print 'move white:', move_white
sys.exit(1)
-
black = Chess()
white = Chess()
-white.child.expect ('Your move is')
+white.child.expect('Your move is')
white.switch()
move_white = white.get_first_computer_move()
print 'first move white:', move_white
-black.do_first_move (move_white)
+black.do_first_move(move_white)
move_black = black.get_first_computer_move()
print 'first move black:', move_black
-white.do_move (move_black)
+white.do_move(move_black)
done = 0
while not done:
move_white = white.get_computer_move()
print 'move white:', move_white
- black.do_move (move_white)
+ black.do_move(move_white)
move_black = black.get_computer_move()
print 'move black:', move_black
-
- white.do_move (move_black)
+
+ white.do_move(move_black)
print 'tail of loop'
g.quit()
-
-
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/df.py b/lldb/third_party/Python/module/pexpect-2.4/examples/df.py
index 64bbf93a1b2..e6c06a8fb13 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/df.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/df.py
@@ -9,15 +9,15 @@ Apple OSX. """
import pexpect
-child = pexpect.spawn ('df')
+child = pexpect.spawn('df')
# parse 'df' output into a list.
pattern = "\n(\S+).*?([0-9]+)%"
filesystem_list = []
-for dummy in range (0, 1000):
- i = child.expect ([pattern, pexpect.EOF])
+for dummy in range(0, 1000):
+ i = child.expect([pattern, pexpect.EOF])
if i == 0:
- filesystem_list.append (child.match.groups())
+ filesystem_list.append(child.match.groups())
else:
break
@@ -31,4 +31,3 @@ for m in filesystem_list:
else:
s = ' ' + s
print s
-
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/fix_cvs_files.py b/lldb/third_party/Python/module/pexpect-2.4/examples/fix_cvs_files.py
index e75a1496abd..6fecf7cfbbe 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/fix_cvs_files.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/fix_cvs_files.py
@@ -17,79 +17,82 @@ Noah Spurrier
20030426
"""
-import os, sys, time
+import os
+import sys
+import time
import pexpect
VERBOSE = 1
-def is_binary (filename):
+def is_binary(filename):
"""Assume that any file with a character where the 8th bit is set is
binary. """
- fin = open(filename, 'rb')
- wholething = fin.read()
- fin.close()
- for c in wholething:
- if ord(c) & 0x80:
- return 1
- return 0
+ fin = open(filename, 'rb')
+ wholething = fin.read()
+ fin.close()
+ for c in wholething:
+ if ord(c) & 0x80:
+ return 1
+ return 0
-def is_kb_sticky (filename):
+def is_kb_sticky(filename):
"""This checks if 'cvs status' reports '-kb' for Sticky options. If the
Sticky Option status is '-ks' then this returns 1. If the status is
'Unknown' then it returns 1. Otherwise 0 is returned. """
- try:
- s = pexpect.spawn ('cvs status %s' % filename)
- i = s.expect (['Sticky Options:\s*(.*)\r\n', 'Status: Unknown'])
- if i==1 and VERBOSE:
- print 'File not part of CVS repository:', filename
- return 1 # Pretend it's OK.
- if s.match.group(1) == '-kb':
- return 1
- s = None
- except:
- print 'Something went wrong trying to run external cvs command.'
- print ' cvs status %s' % filename
- print 'The cvs command returned:'
- print s.before
- return 0
-
-def cvs_admin_kb (filename):
-
+ try:
+ s = pexpect.spawn('cvs status %s' % filename)
+ i = s.expect(['Sticky Options:\s*(.*)\r\n', 'Status: Unknown'])
+ if i == 1 and VERBOSE:
+ print 'File not part of CVS repository:', filename
+ return 1 # Pretend it's OK.
+ if s.match.group(1) == '-kb':
+ return 1
+ s = None
+ except:
+ print 'Something went wrong trying to run external cvs command.'
+ print ' cvs status %s' % filename
+ print 'The cvs command returned:'
+ print s.before
+ return 0
+
+
+def cvs_admin_kb(filename):
"""This uses 'cvs admin' to set the '-kb' sticky option. """
- s = pexpect.run ('cvs admin -kb %s' % filename)
- # There is a timing issue. If I run 'cvs admin' too quickly
- # cvs sometimes has trouble obtaining the directory lock.
- time.sleep(1)
-
-def walk_and_clean_cvs_binaries (arg, dirname, names):
+ s = pexpect.run('cvs admin -kb %s' % filename)
+ # There is a timing issue. If I run 'cvs admin' too quickly
+ # cvs sometimes has trouble obtaining the directory lock.
+ time.sleep(1)
+
+def walk_and_clean_cvs_binaries(arg, dirname, names):
"""This contains the logic for processing files. This is the os.path.walk
callback. This skips dirnames that end in CVS. """
- if len(dirname)>3 and dirname[-3:]=='CVS':
- return
- for n in names:
- fullpath = os.path.join (dirname, n)
- if os.path.isdir(fullpath) or os.path.islink(fullpath):
- continue
- if is_binary(fullpath):
- if not is_kb_sticky (fullpath):
- if VERBOSE: print fullpath
- cvs_admin_kb (fullpath)
-
-def main ():
-
- if len(sys.argv) == 1:
- root = '.'
- else:
- root = sys.argv[1]
- os.path.walk (root, walk_and_clean_cvs_binaries, None)
+ if len(dirname) > 3 and dirname[-3:] == 'CVS':
+ return
+ for n in names:
+ fullpath = os.path.join(dirname, n)
+ if os.path.isdir(fullpath) or os.path.islink(fullpath):
+ continue
+ if is_binary(fullpath):
+ if not is_kb_sticky(fullpath):
+ if VERBOSE:
+ print fullpath
+ cvs_admin_kb(fullpath)
-if __name__ == '__main__':
- main ()
+def main():
+
+ if len(sys.argv) == 1:
+ root = '.'
+ else:
+ root = sys.argv[1]
+ os.path.walk(root, walk_and_clean_cvs_binaries, None)
+
+if __name__ == '__main__':
+ main()
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/ftp.py b/lldb/third_party/Python/module/pexpect-2.4/examples/ftp.py
index 89a502e1b8f..c859632709b 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/ftp.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/ftp.py
@@ -24,9 +24,9 @@ child.expect('ftp> ')
child.sendline('pwd')
child.expect('ftp> ')
print("Escape character is '^]'.\n")
-sys.stdout.write (child.after)
+sys.stdout.write(child.after)
sys.stdout.flush()
-child.interact() # Escape character defaults to ^]
+child.interact() # Escape character defaults to ^]
# At this point this script blocks until the user presses the escape character
# or until the child exits. The human user and the child should be talking
# to each other now.
@@ -35,13 +35,13 @@ child.interact() # Escape character defaults to ^]
print 'Left interactve mode.'
# The rest is not strictly necessary. This just demonstrates a few functions.
-# This makes sure the child is dead; although it would be killed when Python exits.
+# This makes sure the child is dead; although it would be killed when
+# Python exits.
if child.isalive():
- child.sendline('bye') # Try to ask ftp child to exit.
+ child.sendline('bye') # Try to ask ftp child to exit.
child.close()
# Print the final state of the child. Normally isalive() should be FALSE.
if child.isalive():
print 'Child did not exit gracefully.'
else:
print 'Child exited gracefully.'
-
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/hive.py b/lldb/third_party/Python/module/pexpect-2.4/examples/hive.py
index fcb75bcac67..d2b13d4f081 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/hive.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/hive.py
@@ -12,7 +12,7 @@ Example:
$ hive.py --sameuser --samepass host1.example.com host2.example.net
username: myusername
- password:
+ password:
connecting to host1.example.com - OK
connecting to host2.example.net - OK
targetting hosts: 192.168.1.104 192.168.1.107
@@ -66,18 +66,27 @@ $Id: hive.py 509 2008-01-05 21:27:47Z noah $
# TODO add feature to support username:password@host combination
# TODO add feature to log each host output in separate file
-import sys, os, re, optparse, traceback, types, time, getpass
-import pexpect, pxssh
-import readline, atexit
+import sys
+import os
+import re
+import optparse
+import traceback
+import types
+import time
+import getpass
+import pexpect
+import pxssh
+import readline
+import atexit
#histfile = os.path.join(os.environ["HOME"], ".hive_history")
-#try:
+# try:
# readline.read_history_file(histfile)
-#except IOError:
+# except IOError:
# pass
#atexit.register(readline.write_history_file, histfile)
-CMD_HELP="""Hive commands are preceded by a colon : (just think of vi).
+CMD_HELP = """Hive commands are preceded by a colon : (just think of vi).
:target name1 name2 name3 ...
@@ -85,7 +94,7 @@ CMD_HELP="""Hive commands are preceded by a colon : (just think of vi).
:target all
- reset list of hosts to target all hosts in the hive.
+ reset list of hosts to target all hosts in the hive.
:to name command
@@ -146,21 +155,23 @@ CMD_HELP="""Hive commands are preceded by a colon : (just think of vi).
"""
-def login (args, cli_username=None, cli_password=None):
+
+def login(args, cli_username=None, cli_password=None):
# I have to keep a separate list of host names because Python dicts are not ordered.
# I want to keep the same order as in the args list.
host_names = []
hive_connect_info = {}
hive = {}
- # build up the list of connection information (hostname, username, password, port)
+ # build up the list of connection information (hostname, username,
+ # password, port)
for host_connect_string in args:
- hcd = parse_host_connect_string (host_connect_string)
+ hcd = parse_host_connect_string(host_connect_string)
hostname = hcd['hostname']
- port = hcd['port']
+ port = hcd['port']
if port == '':
port = None
- if len(hcd['username']) > 0:
+ if len(hcd['username']) > 0:
username = hcd['username']
elif cli_username is not None:
username = cli_username
@@ -178,20 +189,21 @@ def login (args, cli_username=None, cli_password=None):
for hostname in host_names:
print 'connecting to', hostname
try:
- fout = file("log_"+hostname, "w")
+ fout = file("log_" + hostname, "w")
hive[hostname] = pxssh.pxssh()
hive[hostname].login(*hive_connect_info[hostname])
print hive[hostname].before
hive[hostname].logfile = fout
print '- OK'
- except Exception, e:
+ except Exception as e:
print '- ERROR',
print str(e)
print 'Skipping', hostname
hive[hostname] = None
return host_names, hive
-def main ():
+
+def main():
global options, args, CMD_HELP
@@ -204,7 +216,7 @@ def main ():
cli_password = getpass.getpass('password: ')
else:
cli_password = None
-
+
host_names, hive = login(args, cli_username, cli_password)
synchronous_mode = True
@@ -213,11 +225,11 @@ def main ():
while True:
cmd = raw_input('CMD (? for help) > ')
cmd = cmd.strip()
- if cmd=='?' or cmd==':help' or cmd==':h':
+ if cmd == '?' or cmd == ':help' or cmd == ':h':
print CMD_HELP
continue
- elif cmd==':refresh':
- refresh (hive, target_hostnames, timeout=0.5)
+ elif cmd == ':refresh':
+ refresh(hive, target_hostnames, timeout=0.5)
for hostname in target_hostnames:
if hive[hostname] is None:
print '/============================================================================='
@@ -230,8 +242,8 @@ def main ():
print hive[hostname].before
print '=============================================================================='
continue
- elif cmd==':resync':
- resync (hive, target_hostnames, timeout=0.5)
+ elif cmd == ':resync':
+ resync(hive, target_hostnames, timeout=0.5)
for hostname in target_hostnames:
if hive[hostname] is None:
print '/============================================================================='
@@ -244,62 +256,62 @@ def main ():
print hive[hostname].before
print '=============================================================================='
continue
- elif cmd==':sync':
+ elif cmd == ':sync':
synchronous_mode = True
- resync (hive, target_hostnames, timeout=0.5)
+ resync(hive, target_hostnames, timeout=0.5)
continue
- elif cmd==':async':
+ elif cmd == ':async':
synchronous_mode = False
continue
- elif cmd==':prompt':
+ elif cmd == ':prompt':
for hostname in target_hostnames:
try:
if hive[hostname] is not None:
hive[hostname].set_unique_prompt()
- except Exception, e:
+ except Exception as e:
print "Had trouble communicating with %s, so removing it from the target list." % hostname
print str(e)
hive[hostname] = None
continue
elif cmd[:5] == ':send':
- cmd, txt = cmd.split(None,1)
+ cmd, txt = cmd.split(None, 1)
for hostname in target_hostnames:
try:
if hive[hostname] is not None:
hive[hostname].send(txt)
- except Exception, e:
+ except Exception as e:
print "Had trouble communicating with %s, so removing it from the target list." % hostname
print str(e)
hive[hostname] = None
continue
elif cmd[:3] == ':to':
- cmd, hostname, txt = cmd.split(None,2)
+ cmd, hostname, txt = cmd.split(None, 2)
if hive[hostname] is None:
print '/============================================================================='
print '| ' + hostname + ' is DEAD'
print '\\-----------------------------------------------------------------------------'
continue
try:
- hive[hostname].sendline (txt)
+ hive[hostname].sendline(txt)
hive[hostname].prompt(timeout=2)
print '/============================================================================='
print '| ' + hostname
print '\\-----------------------------------------------------------------------------'
print hive[hostname].before
- except Exception, e:
+ except Exception as e:
print "Had trouble communicating with %s, so removing it from the target list." % hostname
print str(e)
hive[hostname] = None
continue
elif cmd[:7] == ':expect':
- cmd, pattern = cmd.split(None,1)
+ cmd, pattern = cmd.split(None, 1)
print 'looking for', pattern
try:
for hostname in target_hostnames:
if hive[hostname] is not None:
hive[hostname].expect(pattern)
print hive[hostname].before
- except Exception, e:
+ except Exception as e:
print "Had trouble communicating with %s, so removing it from the target list." % hostname
print str(e)
hive[hostname] = None
@@ -312,9 +324,9 @@ def main ():
continue
elif cmd == ':exit' or cmd == ':q' or cmd == ':quit':
break
- elif cmd[:8] == ':control' or cmd[:5] == ':ctrl' :
- cmd, c = cmd.split(None,1)
- if ord(c)-96 < 0 or ord(c)-96 > 255:
+ elif cmd[:8] == ':control' or cmd[:5] == ':ctrl':
+ cmd, c = cmd.split(None, 1)
+ if ord(c) - 96 < 0 or ord(c) - 96 > 255:
print '/============================================================================='
print '| Invalid character. Must be [a-zA-Z], @, [, ], \\, ^, _, or ?'
print '\\-----------------------------------------------------------------------------'
@@ -323,7 +335,7 @@ def main ():
try:
if hive[hostname] is not None:
hive[hostname].sendcontrol(c)
- except Exception, e:
+ except Exception as e:
print "Had trouble communicating with %s, so removing it from the target list." % hostname
print str(e)
hive[hostname] = None
@@ -339,8 +351,8 @@ def main ():
for hostname in target_hostnames:
try:
if hive[hostname] is not None:
- hive[hostname].sendline (cmd)
- except Exception, e:
+ hive[hostname].sendline(cmd)
+ except Exception as e:
print "Had trouble communicating with %s, so removing it from the target list." % hostname
print str(e)
hive[hostname] = None
@@ -361,23 +373,23 @@ def main ():
print '| ' + hostname
print '\\-----------------------------------------------------------------------------'
print hive[hostname].before
- except Exception, e:
+ except Exception as e:
print "Had trouble communicating with %s, so removing it from the target list." % hostname
print str(e)
hive[hostname] = None
print '=============================================================================='
-
-def refresh (hive, hive_names, timeout=0.5):
+
+def refresh(hive, hive_names, timeout=0.5):
"""This waits for the TIMEOUT on each host.
"""
# TODO This is ideal for threading.
for hostname in hive_names:
- hive[hostname].expect([pexpect.TIMEOUT,pexpect.EOF],timeout=timeout)
+ hive[hostname].expect([pexpect.TIMEOUT, pexpect.EOF], timeout=timeout)
-def resync (hive, hive_names, timeout=2, max_attempts=5):
+def resync(hive, hive_names, timeout=2, max_attempts=5):
"""This waits for the shell prompt for each host in an effort to try to get
them all to the same state. The timeout is set low so that hosts that are
already at the prompt will not slow things down too much. If a prompt match
@@ -393,8 +405,8 @@ def resync (hive, hive_names, timeout=2, max_attempts=5):
if not hive[hostname].prompt(timeout=timeout):
break
-def parse_host_connect_string (hcs):
+def parse_host_connect_string(hcs):
"""This parses a host connection string in the form
username:password@hostname:port. All fields are options expcet hostname. A
dictionary is returned with all four keys. Keys that were not included are
@@ -402,35 +414,58 @@ def parse_host_connect_string (hcs):
then you must backslash escape it. """
if '@' in hcs:
- p = re.compile (r'(?P<username>[^@:]*)(:?)(?P<password>.*)(?!\\)@(?P<hostname>[^:]*):?(?P<port>[0-9]*)')
+ p = re.compile(
+ r'(?P<username>[^@:]*)(:?)(?P<password>.*)(?!\\)@(?P<hostname>[^:]*):?(?P<port>[0-9]*)')
else:
- p = re.compile (r'(?P<username>)(?P<password>)(?P<hostname>[^:]*):?(?P<port>[0-9]*)')
- m = p.search (hcs)
+ p = re.compile(
+ r'(?P<username>)(?P<password>)(?P<hostname>[^:]*):?(?P<port>[0-9]*)')
+ m = p.search(hcs)
d = m.groupdict()
- d['password'] = d['password'].replace('\\@','@')
+ d['password'] = d['password'].replace('\\@', '@')
return d
if __name__ == '__main__':
try:
start_time = time.time()
- parser = optparse.OptionParser(formatter=optparse.TitledHelpFormatter(), usage=globals()['__doc__'], version='$Id: hive.py 509 2008-01-05 21:27:47Z noah $',conflict_handler="resolve")
- parser.add_option ('-v', '--verbose', action='store_true', default=False, help='verbose output')
- parser.add_option ('--samepass', action='store_true', default=False, help='Use same password for each login.')
- parser.add_option ('--sameuser', action='store_true', default=False, help='Use same username for each login.')
+ parser = optparse.OptionParser(
+ formatter=optparse.TitledHelpFormatter(),
+ usage=globals()['__doc__'],
+ version='$Id: hive.py 509 2008-01-05 21:27:47Z noah $',
+ conflict_handler="resolve")
+ parser.add_option(
+ '-v',
+ '--verbose',
+ action='store_true',
+ default=False,
+ help='verbose output')
+ parser.add_option(
+ '--samepass',
+ action='store_true',
+ default=False,
+ help='Use same password for each login.')
+ parser.add_option(
+ '--sameuser',
+ action='store_true',
+ default=False,
+ help='Use same username for each login.')
(options, args) = parser.parse_args()
if len(args) < 1:
- parser.error ('missing argument')
- if options.verbose: print time.asctime()
+ parser.error('missing argument')
+ if options.verbose:
+ print time.asctime()
main()
- if options.verbose: print time.asctime()
- if options.verbose: print 'TOTAL TIME IN MINUTES:',
- if options.verbose: print (time.time() - start_time) / 60.0
+ if options.verbose:
+ print time.asctime()
+ if options.verbose:
+ print 'TOTAL TIME IN MINUTES:',
+ if options.verbose:
+ print (time.time() - start_time) / 60.0
sys.exit(0)
- except KeyboardInterrupt, e: # Ctrl-C
+ except KeyboardInterrupt as e: # Ctrl-C
raise e
- except SystemExit, e: # sys.exit()
+ except SystemExit as e: # sys.exit()
raise e
- except Exception, e:
+ except Exception as e:
print 'ERROR, UNEXPECTED EXCEPTION'
print str(e)
traceback.print_exc()
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/monitor.py b/lldb/third_party/Python/module/pexpect-2.4/examples/monitor.py
index e31b51b6d21..6ed6589d670 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/monitor.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/monitor.py
@@ -23,40 +23,53 @@ It works like this:
Exit the remote host.
"""
-import os, sys, time, re, getopt, getpass
+import os
+import sys
+import time
+import re
+import getopt
+import getpass
import traceback
import pexpect
#
# Some constants.
#
-COMMAND_PROMPT = '[#$] ' ### This is way too simple for industrial use -- we will change is ASAP.
+# This is way too simple for industrial use -- we will change is ASAP.
+COMMAND_PROMPT = '[#$] '
TERMINAL_PROMPT = '(?i)terminal type\?'
TERMINAL_TYPE = 'vt100'
-# This is the prompt we get if SSH does not have the remote host's public key stored in the cache.
+# This is the prompt we get if SSH does not have the remote host's public
+# key stored in the cache.
SSH_NEWKEY = '(?i)are you sure you want to continue connecting'
+
def exit_with_usage():
print globals()['__doc__']
os._exit(1)
+
def main():
global COMMAND_PROMPT, TERMINAL_PROMPT, TERMINAL_TYPE, SSH_NEWKEY
######################################################################
- ## Parse the options, arguments, get ready, etc.
+ # Parse the options, arguments, get ready, etc.
######################################################################
try:
- optlist, args = getopt.getopt(sys.argv[1:], 'h?s:u:p:', ['help','h','?'])
- except Exception, e:
+ optlist, args = getopt.getopt(
+ sys.argv[
+ 1:], 'h?s:u:p:', [
+ 'help', 'h', '?'])
+ except Exception as e:
print str(e)
exit_with_usage()
options = dict(optlist)
if len(args) > 1:
exit_with_usage()
- if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]:
+ if [elem for elem in options if elem in [
+ '-h', '--h', '-?', '--?', '--help']]:
print "Help:"
exit_with_usage()
@@ -76,16 +89,17 @@ def main():
#
# Login via SSH
#
- child = pexpect.spawn('ssh -l %s %s'%(user, host))
- i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, COMMAND_PROMPT, '(?i)password'])
- if i == 0: # Timeout
+ child = pexpect.spawn('ssh -l %s %s' % (user, host))
+ i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY,
+ COMMAND_PROMPT, '(?i)password'])
+ if i == 0: # Timeout
print 'ERROR! could not login with SSH. Here is what SSH said:'
print child.before, child.after
print str(child)
- sys.exit (1)
- if i == 1: # In this case SSH does not have the public key cached.
- child.sendline ('yes')
- child.expect ('(?i)password')
+ sys.exit(1)
+ if i == 1: # In this case SSH does not have the public key cached.
+ child.sendline('yes')
+ child.expect('(?i)password')
if i == 2:
# This may happen if a public key was setup to automatically login.
# But beware, the COMMAND_PROMPT at this point is very trivial and
@@ -95,25 +109,25 @@ def main():
child.sendline(password)
# Now we are either at the command prompt or
# the login process is asking for our terminal type.
- i = child.expect ([COMMAND_PROMPT, TERMINAL_PROMPT])
+ i = child.expect([COMMAND_PROMPT, TERMINAL_PROMPT])
if i == 1:
- child.sendline (TERMINAL_TYPE)
- child.expect (COMMAND_PROMPT)
+ child.sendline(TERMINAL_TYPE)
+ child.expect(COMMAND_PROMPT)
#
# Set command prompt to something more unique.
#
COMMAND_PROMPT = "\[PEXPECT\]\$ "
- child.sendline ("PS1='[PEXPECT]\$ '") # In case of sh-style
- i = child.expect ([pexpect.TIMEOUT, COMMAND_PROMPT], timeout=10)
+ child.sendline("PS1='[PEXPECT]\$ '") # In case of sh-style
+ i = child.expect([pexpect.TIMEOUT, COMMAND_PROMPT], timeout=10)
if i == 0:
print "# Couldn't set sh-style prompt -- trying csh-style."
- child.sendline ("set prompt='[PEXPECT]\$ '")
- i = child.expect ([pexpect.TIMEOUT, COMMAND_PROMPT], timeout=10)
+ child.sendline("set prompt='[PEXPECT]\$ '")
+ i = child.expect([pexpect.TIMEOUT, COMMAND_PROMPT], timeout=10)
if i == 0:
print "Failed to set command prompt using sh or csh style."
print "Response was:"
print child.before
- sys.exit (1)
+ sys.exit(1)
# Now we should be at the command prompt and ready to run some commands.
print '---------------------------------------'
@@ -121,8 +135,8 @@ def main():
print '---------------------------------------'
# Run uname.
- child.sendline ('uname -a')
- child.expect (COMMAND_PROMPT)
+ child.sendline('uname -a')
+ child.expect(COMMAND_PROMPT)
print child.before
if 'linux' in child.before.lower():
LINUX_MODE = 1
@@ -130,51 +144,52 @@ def main():
LINUX_MODE = 0
# Run and parse 'uptime'.
- child.sendline ('uptime')
- child.expect('up\s+(.*?),\s+([0-9]+) users?,\s+load averages?: ([0-9]+\.[0-9][0-9]),?\s+([0-9]+\.[0-9][0-9]),?\s+([0-9]+\.[0-9][0-9])')
+ child.sendline('uptime')
+ child.expect(
+ 'up\s+(.*?),\s+([0-9]+) users?,\s+load averages?: ([0-9]+\.[0-9][0-9]),?\s+([0-9]+\.[0-9][0-9]),?\s+([0-9]+\.[0-9][0-9])')
duration, users, av1, av5, av15 = child.match.groups()
days = '0'
hours = '0'
mins = '0'
if 'day' in duration:
- child.match = re.search('([0-9]+)\s+day',duration)
+ child.match = re.search('([0-9]+)\s+day', duration)
days = str(int(child.match.group(1)))
if ':' in duration:
- child.match = re.search('([0-9]+):([0-9]+)',duration)
+ child.match = re.search('([0-9]+):([0-9]+)', duration)
hours = str(int(child.match.group(1)))
mins = str(int(child.match.group(2)))
if 'min' in duration:
- child.match = re.search('([0-9]+)\s+min',duration)
+ child.match = re.search('([0-9]+)\s+min', duration)
mins = str(int(child.match.group(1)))
print
print 'Uptime: %s days, %s users, %s (1 min), %s (5 min), %s (15 min)' % (
duration, users, av1, av5, av15)
- child.expect (COMMAND_PROMPT)
+ child.expect(COMMAND_PROMPT)
# Run iostat.
- child.sendline ('iostat')
- child.expect (COMMAND_PROMPT)
+ child.sendline('iostat')
+ child.expect(COMMAND_PROMPT)
print child.before
# Run vmstat.
- child.sendline ('vmstat')
- child.expect (COMMAND_PROMPT)
+ child.sendline('vmstat')
+ child.expect(COMMAND_PROMPT)
print child.before
# Run free.
if LINUX_MODE:
- child.sendline ('free') # Linux systems only.
- child.expect (COMMAND_PROMPT)
+ child.sendline('free') # Linux systems only.
+ child.expect(COMMAND_PROMPT)
print child.before
# Run df.
- child.sendline ('df')
- child.expect (COMMAND_PROMPT)
+ child.sendline('df')
+ child.expect(COMMAND_PROMPT)
print child.before
-
+
# Run lsof.
- child.sendline ('lsof')
- child.expect (COMMAND_PROMPT)
+ child.sendline('lsof')
+ child.expect(COMMAND_PROMPT)
print child.before
# # Run netstat
@@ -191,9 +206,9 @@ def main():
# print child.before
# Now exit the remote host.
- child.sendline ('exit')
+ child.sendline('exit')
index = child.expect([pexpect.EOF, "(?i)there are stopped jobs"])
- if index==1:
+ if index == 1:
child.sendline("exit")
child.expect(EOF)
@@ -201,8 +216,7 @@ if __name__ == "__main__":
try:
main()
- except Exception, e:
+ except Exception as e:
print str(e)
traceback.print_exc()
os._exit(1)
-
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/passmass.py b/lldb/third_party/Python/module/pexpect-2.4/examples/passmass.py
index b1e17b9cb03..3d129b67d77 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/passmass.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/passmass.py
@@ -4,7 +4,8 @@
Note that login shell prompt on remote machine must end in # or $. """
import pexpect
-import sys, getpass
+import sys
+import getpass
USAGE = '''passmass host1 host2 host3 . . .'''
COMMAND_PROMPT = '[$#] '
@@ -12,38 +13,42 @@ TERMINAL_PROMPT = r'Terminal type\?'
TERMINAL_TYPE = 'vt100'
SSH_NEWKEY = r'Are you sure you want to continue connecting \(yes/no\)\?'
+
def login(host, user, password):
- child = pexpect.spawn('ssh -l %s %s'%(user, host))
- fout = file ("LOG.TXT","wb")
- child.setlog (fout)
+ child = pexpect.spawn('ssh -l %s %s' % (user, host))
+ fout = file("LOG.TXT", "wb")
+ child.setlog(fout)
i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, '[Pp]assword: '])
- if i == 0: # Timeout
+ if i == 0: # Timeout
print 'ERROR!'
print 'SSH could not login. Here is what SSH said:'
print child.before, child.after
- sys.exit (1)
- if i == 1: # SSH does not have the public key. Just accept it.
- child.sendline ('yes')
- child.expect ('[Pp]assword: ')
+ sys.exit(1)
+ if i == 1: # SSH does not have the public key. Just accept it.
+ child.sendline('yes')
+ child.expect('[Pp]assword: ')
child.sendline(password)
# Now we are either at the command prompt or
# the login process is asking for our terminal type.
- i = child.expect (['Permission denied', TERMINAL_PROMPT, COMMAND_PROMPT])
+ i = child.expect(['Permission denied', TERMINAL_PROMPT, COMMAND_PROMPT])
if i == 0:
print 'Permission denied on host:', host
- sys.exit (1)
+ sys.exit(1)
if i == 1:
- child.sendline (TERMINAL_TYPE)
- child.expect (COMMAND_PROMPT)
+ child.sendline(TERMINAL_TYPE)
+ child.expect(COMMAND_PROMPT)
return child
# (current) UNIX password:
+
+
def change_password(child, user, oldpassword, newpassword):
- child.sendline('passwd')
- i = child.expect(['[Oo]ld [Pp]assword', '.current.*password', '[Nn]ew [Pp]assword'])
+ child.sendline('passwd')
+ i = child.expect(
+ ['[Oo]ld [Pp]assword', '.current.*password', '[Nn]ew [Pp]assword'])
# Root does not require old password, so it gets to bypass the next step.
if i == 0 or i == 1:
child.sendline(oldpassword)
@@ -53,11 +58,12 @@ def change_password(child, user, oldpassword, newpassword):
if i == 0:
print 'Host did not like new password. Here is what it said...'
print child.before
- child.send (chr(3)) # Ctrl-C
- child.sendline('') # This should tell remote passwd command to quit.
+ child.send(chr(3)) # Ctrl-C
+ child.sendline('') # This should tell remote passwd command to quit.
return
child.sendline(newpassword)
+
def main():
if len(sys.argv) <= 1:
@@ -74,7 +80,7 @@ def main():
for host in sys.argv[1:]:
child = login(host, user, password)
- if child == None:
+ if child is None:
print 'Could not login to host:', host
continue
print 'Changing password on host:', host
@@ -85,6 +91,5 @@ def main():
if __name__ == '__main__':
try:
main()
- except pexpect.ExceptionPexpect, e:
+ except pexpect.ExceptionPexpect as e:
print str(e)
-
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/python.py b/lldb/third_party/Python/module/pexpect-2.4/examples/python.py
index d8c986652e6..e7ef462ba68 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/python.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/python.py
@@ -7,10 +7,10 @@ the user interactive control over the session. Why? For fun... """
# c = pexpect.spawn ('/usr/bin/env python ./python.py')
import pexpect
-c = pexpect.spawn ('/usr/bin/env python')
-c.expect ('>>>')
+c = pexpect.spawn('/usr/bin/env python')
+c.expect('>>>')
print 'And now for something completely different...'
-f = lambda s:s and f(s[1:])+s[0] # Makes a function to reverse a string.
+f = lambda s: s and f(s[1:]) + s[0] # Makes a function to reverse a string.
print f(c.before)
print 'Yes, it\'s python, but it\'s backwards.'
print
@@ -19,4 +19,3 @@ print c.after,
c.interact()
c.kill(1)
print 'is alive:', c.isalive()
-
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/rippy.py b/lldb/third_party/Python/module/pexpect-2.4/examples/rippy.py
index e89c314c323..8967821b1fe 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/rippy.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/rippy.py
@@ -69,7 +69,15 @@ Noah Spurrier
$Id: rippy.py 517 2008-08-18 22:23:56Z noah $
"""
-import sys, os, re, math, stat, getopt, traceback, types, time
+import sys
+import os
+import re
+import math
+import stat
+import getopt
+import traceback
+import types
+import time
import pexpect
__version__ = '1.2'
@@ -77,7 +85,7 @@ __revision__ = '$Revision: 11 $'
__all__ = ['main', __version__, __revision__]
GLOBAL_LOGFILE_NAME = "rippy_%d.log" % os.getpid()
-GLOBAL_LOGFILE = open (GLOBAL_LOGFILE_NAME, "wb")
+GLOBAL_LOGFILE = open(GLOBAL_LOGFILE_NAME, "wb")
###############################################################################
# This giant section defines the prompts and defaults used in interactive mode.
@@ -85,32 +93,32 @@ GLOBAL_LOGFILE = open (GLOBAL_LOGFILE_NAME, "wb")
# Python dictionaries are unordered, so
# I have this list that maintains the order of the keys.
prompts_key_order = (
-'verbose_flag',
-'dry_run_flag',
-'video_source_filename',
-'video_chapter',
-'video_final_filename',
-'video_length',
-'video_aspect_ratio',
-'video_scale',
-'video_encode_passes',
-'video_codec',
-'video_fourcc_override',
-'video_bitrate',
-'video_bitrate_overhead',
-'video_target_size',
-'video_deinterlace_flag',
-'video_crop_area',
-'video_gray_flag',
-'subtitle_id',
-'audio_id',
-'audio_codec',
-'audio_raw_filename',
-'audio_volume_boost',
-'audio_sample_rate',
-'audio_bitrate',
-#'audio_lowpass_filter',
-'delete_tmp_files_flag'
+ 'verbose_flag',
+ 'dry_run_flag',
+ 'video_source_filename',
+ 'video_chapter',
+ 'video_final_filename',
+ 'video_length',
+ 'video_aspect_ratio',
+ 'video_scale',
+ 'video_encode_passes',
+ 'video_codec',
+ 'video_fourcc_override',
+ 'video_bitrate',
+ 'video_bitrate_overhead',
+ 'video_target_size',
+ 'video_deinterlace_flag',
+ 'video_crop_area',
+ 'video_gray_flag',
+ 'subtitle_id',
+ 'audio_id',
+ 'audio_codec',
+ 'audio_raw_filename',
+ 'audio_volume_boost',
+ 'audio_sample_rate',
+ 'audio_bitrate',
+ #'audio_lowpass_filter',
+ 'delete_tmp_files_flag'
)
#
# The 'prompts' dictionary holds all the messages shown to the user in
@@ -118,58 +126,58 @@ prompts_key_order = (
# prompt_key : ( default value, prompt string, help string, level of difficulty (0,1,2) )
#
prompts = {
-'video_source_filename':("dvd://1", 'video source filename?', """This is the filename of the video that you want to convert from.
+ 'video_source_filename': ("dvd://1", 'video source filename?', """This is the filename of the video that you want to convert from.
It can be any file that mencoder supports.
You can also choose a DVD device using the dvd://1 syntax.
-Title 1 is usually the main title on a DVD.""",0),
-'video_chapter':("none",'video chapter?',"""This is the chapter number. Usually disks such as TV series seasons will be divided into chapters. Maybe be set to none.""",0),
-'video_final_filename':("video_final.avi", "video final filename?", """This is the name of the final video.""",0),
-'audio_raw_filename':("audiodump.wav", "audio raw filename?", """This is the audio raw PCM filename. This is prior to compression.
-Note that mplayer automatically names this audiodump.wav, so don't change this.""",1000),
-#'audio_compressed_filename':("audiodump.mp3","Audio compressed filename?", """This is the name of the compressed audio that will be mixed
-#into the final video. Normally you don't need to change this.""",2),
-'video_length':("none","video length in seconds?","""This sets the length of the video in seconds. This is used to estimate the
+Title 1 is usually the main title on a DVD.""", 0),
+ 'video_chapter': ("none", 'video chapter?', """This is the chapter number. Usually disks such as TV series seasons will be divided into chapters. Maybe be set to none.""", 0),
+ 'video_final_filename': ("video_final.avi", "video final filename?", """This is the name of the final video.""", 0),
+ 'audio_raw_filename': ("audiodump.wav", "audio raw filename?", """This is the audio raw PCM filename. This is prior to compression.
+Note that mplayer automatically names this audiodump.wav, so don't change this.""", 1000),
+ #'audio_compressed_filename':("audiodump.mp3","Audio compressed filename?", """This is the name of the compressed audio that will be mixed
+ # into the final video. Normally you don't need to change this.""",2),
+ 'video_length': ("none", "video length in seconds?", """This sets the length of the video in seconds. This is used to estimate the
bitrate for a target video file size. Set to 'calc' to have Rippy calculate
the length. Set to 'none' if you don't want rippy to estimate the bitrate --
-you will have to manually specify bitrate.""",1),
-'video_aspect_ratio':("calc","aspect ratio?","""This sets the aspect ratio of the video. Most DVDs are 16/9 or 4/3.""",1),
-'video_scale':("none","video scale?","""This scales the video to the given output size. The default is to do no scaling.
+you will have to manually specify bitrate.""", 1),
+ 'video_aspect_ratio': ("calc", "aspect ratio?", """This sets the aspect ratio of the video. Most DVDs are 16/9 or 4/3.""", 1),
+ 'video_scale': ("none", "video scale?", """This scales the video to the given output size. The default is to do no scaling.
You may type in a resolution such as 320x240 or you may use presets.
qntsc: 352x240 (NTSC quarter screen)
qpal: 352x288 (PAL quarter screen)
ntsc: 720x480 (standard NTSC)
pal: 720x576 (standard PAL)
sntsc: 640x480 (square pixel NTSC)
- spal: 768x576 (square pixel PAL)""",1),
-'video_codec':("mpeg4","video codec?","""This is the video compression to use. This is passed directly to mencoder, so
+ spal: 768x576 (square pixel PAL)""", 1),
+ 'video_codec': ("mpeg4", "video codec?", """This is the video compression to use. This is passed directly to mencoder, so
any format that it recognizes should work. For XviD or DivX use mpeg4.
Almost all MS Windows systems support wmv2 out of the box.
Some common codecs include:
mjpeg, h263, h263p, h264, mpeg4, msmpeg4, wmv1, wmv2, mpeg1video, mpeg2video, huffyuv, ffv1.
-""",2),
-'audio_codec':("mp3","audio codec?","""This is the audio compression to use. This is passed directly to mencoder, so
+""", 2),
+ 'audio_codec': ("mp3", "audio codec?", """This is the audio compression to use. This is passed directly to mencoder, so
any format that it recognizes will work.
Some common codecs include:
mp3, mp2, aac, pcm
-See mencoder manual for details.""",2),
-'video_fourcc_override':("XVID","force fourcc code?","""This forces the fourcc codec to the given value. XVID is safest for Windows.
+See mencoder manual for details.""", 2),
+ 'video_fourcc_override': ("XVID", "force fourcc code?", """This forces the fourcc codec to the given value. XVID is safest for Windows.
The following are common fourcc values:
FMP4 - This is the mencoder default. This is the "real" value.
XVID - used by Xvid (safest)
DX50 -
- MP4S - Microsoft""",2),
-'video_encode_passes':("1","number of encode passes?","""This sets how many passes to use to encode the video. You can choose 1 or 2.
+ MP4S - Microsoft""", 2),
+ 'video_encode_passes': ("1", "number of encode passes?", """This sets how many passes to use to encode the video. You can choose 1 or 2.
Using two pases takes twice as long as one pass, but produces a better
-quality video. I found that the improvement is not that impressive.""",1),
-'verbose_flag':("Y","verbose output?","""This sets verbose output. If true then all commands and arguments are printed
-before they are run. This is useful to see exactly how commands are run.""",1),
-'dry_run_flag':("N","dry run?","""This sets 'dry run' mode. If true then commands are not run. This is useful
-if you want to see what would the script would do.""",1),
-'video_bitrate':("calc","video bitrate?","""This sets the video bitrate. This overrides video_target_size.
+quality video. I found that the improvement is not that impressive.""", 1),
+ 'verbose_flag': ("Y", "verbose output?", """This sets verbose output. If true then all commands and arguments are printed
+before they are run. This is useful to see exactly how commands are run.""", 1),
+ 'dry_run_flag': ("N", "dry run?", """This sets 'dry run' mode. If true then commands are not run. This is useful
+if you want to see what would the script would do.""", 1),
+ 'video_bitrate': ("calc", "video bitrate?", """This sets the video bitrate. This overrides video_target_size.
Set to 'calc' to automatically estimate the bitrate based on the
video final target size. If you set video_length to 'none' then
-you will have to specify this video_bitrate.""",1),
-'video_target_size':("737280000","video final target size?","""This sets the target video size that you want to end up with.
+you will have to specify this video_bitrate.""", 1),
+ 'video_target_size': ("737280000", "video final target size?", """This sets the target video size that you want to end up with.
This is over-ridden by video_bitrate. In other words, if you specify
video_bitrate then video_target_size is ignored.
Due to the unpredictable nature of VBR compression the final video size
@@ -177,44 +185,46 @@ may not exactly match. The following are common CDR sizes:
180MB CDR (21 minutes) holds 193536000 bytes
550MB CDR (63 minutes) holds 580608000 bytes
650MB CDR (74 minutes) holds 681984000 bytes
- 700MB CDR (80 minutes) holds 737280000 bytes""",0),
-'video_bitrate_overhead':("1.0","bitrate overhead factor?","""Adjust this value if you want to leave more room for
+ 700MB CDR (80 minutes) holds 737280000 bytes""", 0),
+ 'video_bitrate_overhead': ("1.0", "bitrate overhead factor?", """Adjust this value if you want to leave more room for
other files such as subtitle files.
-If you specify video_bitrate then this value is ignored.""",2),
-'video_crop_area':("detect","crop area?","""This sets the crop area to remove black bars from the top or sides of the video.
+If you specify video_bitrate then this value is ignored.""", 2),
+ 'video_crop_area': ("detect", "crop area?", """This sets the crop area to remove black bars from the top or sides of the video.
This helps save space. Set to 'detect' to automatically detect the crop area.
-Set to 'none' to not crop the video. Normally you don't need to change this.""",1),
-'video_deinterlace_flag':("N","is the video interlaced?","""This sets the deinterlace flag. If set then mencoder will be instructed
-to filter out interlace artifacts (using '-vf pp=md').""",1),
-'video_gray_flag':("N","is the video black and white (gray)?","""This improves output for black and white video.""",1),
-'subtitle_id':("None","Subtitle ID stream?","""This selects the subtitle stream to extract from the source video.
+Set to 'none' to not crop the video. Normally you don't need to change this.""", 1),
+ 'video_deinterlace_flag': ("N", "is the video interlaced?", """This sets the deinterlace flag. If set then mencoder will be instructed
+to filter out interlace artifacts (using '-vf pp=md').""", 1),
+ 'video_gray_flag': ("N", "is the video black and white (gray)?", """This improves output for black and white video.""", 1),
+ 'subtitle_id': ("None", "Subtitle ID stream?", """This selects the subtitle stream to extract from the source video.
Normally, 0 is the English subtitle stream for a DVD.
-Subtitles IDs with higher numbers may be other languages.""",1),
-'audio_id':("128","audio ID stream?","""This selects the audio stream to extract from the source video.
+Subtitles IDs with higher numbers may be other languages.""", 1),
+ 'audio_id': ("128", "audio ID stream?", """This selects the audio stream to extract from the source video.
If your source is a VOB file (DVD) then stream IDs start at 128.
Normally, 128 is the main audio track for a DVD.
-Tracks with higher numbers may be other language dubs or audio commentary.""",1),
-'audio_sample_rate':("32000","audio sample rate (Hz) 48000, 44100, 32000, 24000, 12000","""This sets the rate at which the compressed audio will be resampled.
+Tracks with higher numbers may be other language dubs or audio commentary.""", 1),
+ 'audio_sample_rate': ("32000", "audio sample rate (Hz) 48000, 44100, 32000, 24000, 12000", """This sets the rate at which the compressed audio will be resampled.
DVD audio is 48 kHz whereas music CDs use 44.1 kHz. The higher the sample rate
the more space the audio track will take. That will leave less space for video.
-32 kHz is a good trade-off if you are trying to fit a video onto a CD.""",1),
-'audio_bitrate':("96","audio bitrate (kbit/s) 192, 128, 96, 64?","""This sets the bitrate for MP3 audio compression.
+32 kHz is a good trade-off if you are trying to fit a video onto a CD.""", 1),
+ 'audio_bitrate': ("96", "audio bitrate (kbit/s) 192, 128, 96, 64?", """This sets the bitrate for MP3 audio compression.
The higher the bitrate the more space the audio track will take.
That will leave less space for video. Most people find music to be acceptable
-at 128 kBitS. 96 kBitS is a good trade-off if you are trying to fit a video onto a CD.""",1),
-'audio_volume_boost':("none","volume dB boost?","""Many DVDs have very low audio volume. This sets an audio volume boost in Decibels.
-Values of 6 to 10 usually adjust quiet DVDs to a comfortable level.""",1),
-#'audio_lowpass_filter':("16","audio lowpass filter (kHz)?","""This sets the low-pass filter for the audio.
-#Normally this should be half of the audio sample rate.
-#This improves audio compression and quality.
-#Normally you don't need to change this.""",1),
-'delete_tmp_files_flag':("N","delete temporary files when finished?","""If Y then %s, audio_raw_filename, and 'divx2pass.log' will be deleted at the end."""%GLOBAL_LOGFILE_NAME,1)
+at 128 kBitS. 96 kBitS is a good trade-off if you are trying to fit a video onto a CD.""", 1),
+ 'audio_volume_boost': ("none", "volume dB boost?", """Many DVDs have very low audio volume. This sets an audio volume boost in Decibels.
+Values of 6 to 10 usually adjust quiet DVDs to a comfortable level.""", 1),
+ #'audio_lowpass_filter':("16","audio lowpass filter (kHz)?","""This sets the low-pass filter for the audio.
+ # Normally this should be half of the audio sample rate.
+ # This improves audio compression and quality.
+ # Normally you don't need to change this.""",1),
+ 'delete_tmp_files_flag': ("N", "delete temporary files when finished?", """If Y then %s, audio_raw_filename, and 'divx2pass.log' will be deleted at the end.""" % GLOBAL_LOGFILE_NAME, 1)
}
##############################################################################
# This is the important convert control function
##############################################################################
-def convert (options):
+
+
+def convert(options):
"""This is the heart of it all -- this performs an end-to-end conversion of
a video from one format to another. It requires a dictionary of options.
The conversion process will also add some keys to the dictionary
@@ -224,7 +234,7 @@ def convert (options):
"""
if options['subtitle_id'] is not None:
print "# extract subtitles"
- apply_smart (extract_subtitles, options)
+ apply_smart(extract_subtitles, options)
else:
print "# do not extract subtitles."
@@ -233,47 +243,52 @@ def convert (options):
# selected 'calc' for video_bitrate
# or
# selected 'detect' for video_crop_area.
- if options['video_bitrate']=='calc' or options['video_crop_area']=='detect':
+ if options['video_bitrate'] == 'calc' or options[
+ 'video_crop_area'] == 'detect':
# As strange as it seems, the only reliable way to calculate the length
# of a video (in seconds) is to extract the raw, uncompressed PCM audio stream
# and then calculate the length of that. This is because MP4 video is VBR, so
# you cannot get exact time based on compressed size.
- if options['video_length']=='calc':
+ if options['video_length'] == 'calc':
print "# extract PCM raw audio to %s" % (options['audio_raw_filename'])
- apply_smart (extract_audio, options)
- options['video_length'] = apply_smart (get_length, options)
- print "# Length of raw audio file : %d seconds (%0.2f minutes)" % (options['video_length'], float(options['video_length'])/60.0)
- if options['video_bitrate']=='calc':
- options['video_bitrate'] = options['video_bitrate_overhead'] * apply_smart (calc_video_bitrate, options)
+ apply_smart(extract_audio, options)
+ options['video_length'] = apply_smart(get_length, options)
+ print "# Length of raw audio file : %d seconds (%0.2f minutes)" % (options['video_length'], float(options['video_length']) / 60.0)
+ if options['video_bitrate'] == 'calc':
+ options['video_bitrate'] = options[
+ 'video_bitrate_overhead'] * apply_smart(calc_video_bitrate, options)
print "# video bitrate : " + str(options['video_bitrate'])
- if options['video_crop_area']=='detect':
- options['video_crop_area'] = apply_smart (crop_detect, options)
+ if options['video_crop_area'] == 'detect':
+ options['video_crop_area'] = apply_smart(crop_detect, options)
print "# crop area : " + str(options['video_crop_area'])
print "# compression estimate"
- print apply_smart (compression_estimate, options)
+ print apply_smart(compression_estimate, options)
print "# compress video"
- apply_smart (compress_video, options)
+ apply_smart(compress_video, options)
'audio_volume_boost',
print "# delete temporary files:",
if options['delete_tmp_files_flag']:
print "yes"
- apply_smart (delete_tmp_files, options)
+ apply_smart(delete_tmp_files, options)
else:
print "no"
- # Finish by saving options to rippy.conf and
+ # Finish by saving options to rippy.conf and
# calclating if final_size is less than target_size.
o = ["# options used to create video\n"]
- video_actual_size = get_filesize (options['video_final_filename'])
+ video_actual_size = get_filesize(options['video_final_filename'])
if options['video_target_size'] != 'none':
- revised_bitrate = calculate_revised_bitrate (options['video_bitrate'], options['video_target_size'], video_actual_size)
+ revised_bitrate = calculate_revised_bitrate(
+ options['video_bitrate'],
+ options['video_target_size'],
+ video_actual_size)
o.append("# revised video_bitrate : %d\n" % revised_bitrate)
- for k,v in options.iteritems():
- o.append (" %30s : %s\n" % (k, v))
+ for k, v in options.iteritems():
+ o.append(" %30s : %s\n" % (k, v))
print '# '.join(o)
- fout = open("rippy.conf","wb").write(''.join(o))
+ fout = open("rippy.conf", "wb").write(''.join(o))
print "# final actual video size = %d" % video_actual_size
if options['video_target_size'] != 'none':
if video_actual_size > options['video_target_size']:
@@ -289,13 +304,15 @@ def convert (options):
##############################################################################
+
def exit_with_usage(exit_code=1):
print globals()['__doc__']
print 'version:', globals()['__version__']
sys.stdout.flush()
os._exit(exit_code)
-def check_missing_requirements ():
+
+def check_missing_requirements():
"""This list of missing requirements (mencoder, mplayer, lame, and mkvmerge).
Returns None if all requirements are in the execution path.
"""
@@ -307,21 +324,22 @@ def check_missing_requirements ():
cmd = "mencoder -oac help"
(command_output, exitstatus) = run(cmd)
ar = re.findall("(mp3lame)", command_output)
- if len(ar)==0:
+ if len(ar) == 0:
missing.append("Mencoder was not compiled with mp3lame support.")
-
- #if pexpect.which("lame") is None:
+
+ # if pexpect.which("lame") is None:
# missing.append("lame")
- #if pexpect.which("mkvmerge") is None:
+ # if pexpect.which("mkvmerge") is None:
# missing.append("mkvmerge")
- if len(missing)==0:
+ if len(missing) == 0:
return None
return missing
-def input_option (message, default_value="", help=None, level=0, max_level=0):
+
+def input_option(message, default_value="", help=None, level=0, max_level=0):
"""This is a fancy raw_input function.
If the user enters '?' then the contents of help is printed.
-
+
The 'level' and 'max_level' are used to adjust which advanced options
are printed. 'max_level' is the level of options that the user wants
to see. 'level' is the level of difficulty for this particular option.
@@ -333,53 +351,63 @@ def input_option (message, default_value="", help=None, level=0, max_level=0):
message = "%s [%s] " % (message, default_value)
if level > max_level:
return default_value
- while 1:
- user_input = raw_input (message)
- if user_input=='?':
+ while True:
+ user_input = raw_input(message)
+ if user_input == '?':
print help
- elif user_input=='':
+ elif user_input == '':
return default_value
else:
break
return user_input
-def progress_callback (d=None):
+
+def progress_callback(d=None):
"""This callback simply prints a dot to show activity.
This is used when running external commands with pexpect.run.
"""
- sys.stdout.write (".")
+ sys.stdout.write(".")
sys.stdout.flush()
+
def run(cmd):
global GLOBAL_LOGFILE
print >>GLOBAL_LOGFILE, cmd
- (command_output, exitstatus) = pexpect.run(cmd, events={pexpect.TIMEOUT:progress_callback}, timeout=5, withexitstatus=True, logfile=GLOBAL_LOGFILE)
+ (command_output,
+ exitstatus) = pexpect.run(cmd,
+ events={pexpect.TIMEOUT: progress_callback},
+ timeout=5,
+ withexitstatus=True,
+ logfile=GLOBAL_LOGFILE)
if exitstatus != 0:
print "RUN FAILED. RETURNED EXIT STATUS:", exitstatus
print >>GLOBAL_LOGFILE, "RUN FAILED. RETURNED EXIT STATUS:", exitstatus
return (command_output, exitstatus)
-def apply_smart (func, args):
- """This is similar to func(**args), but this won't complain about
- extra keys in 'args'. This ignores keys in 'args' that are
+
+def apply_smart(func, args):
+ """This is similar to func(**args), but this won't complain about
+ extra keys in 'args'. This ignores keys in 'args' that are
not required by 'func'. This passes None to arguments that are
not defined in 'args'. That's fine for arguments with a default valeue, but
that's a bug for required arguments. I should probably raise a TypeError.
The func parameter can be a function reference or a string.
If it is a string then it is converted to a function reference.
"""
- if type(func) is type(''):
+ if isinstance(func, type('')):
if func in globals():
func = globals()[func]
else:
raise NameError("name '%s' is not defined" % func)
- if hasattr(func,'im_func'): # Handle case when func is a class method.
+ if hasattr(func, 'im_func'): # Handle case when func is a class method.
func = func.im_func
argcount = func.func_code.co_argcount
- required_args = dict([(k,args.get(k)) for k in func.func_code.co_varnames[:argcount]])
+ required_args = dict([(k, args.get(k))
+ for k in func.func_code.co_varnames[:argcount]])
return func(**required_args)
-def count_unique (items):
+
+def count_unique(items):
"""This takes a list and returns a sorted list of tuples with a count of each unique item in the list.
Example 1:
count_unique(['a','b','c','a','c','c','a','c','c'])
@@ -396,20 +424,25 @@ def count_unique (items):
stats[i] = stats[i] + 1
else:
stats[i] = 1
- stats = [(v, k) for k, v in stats.items()]
- stats.sort()
+ stats = sorted([(v, k) for k, v in stats.items()])
stats.reverse()
return stats
-def calculate_revised_bitrate (video_bitrate, video_target_size, video_actual_size):
+
+def calculate_revised_bitrate(
+ video_bitrate,
+ video_target_size,
+ video_actual_size):
"""This calculates a revised video bitrate given the video_bitrate used,
the actual size that resulted, and the video_target_size.
This can be used if you want to compress the video all over again in an
attempt to get closer to the video_target_size.
"""
- return int(math.floor(video_bitrate * (float(video_target_size) / float(video_actual_size))))
+ return int(math.floor(video_bitrate * \
+ (float(video_target_size) / float(video_actual_size))))
+
-def get_aspect_ratio (video_source_filename):
+def get_aspect_ratio(video_source_filename):
"""This returns the aspect ratio of the original video.
This is usualy 1.78:1(16/9) or 1.33:1(4/3).
This function is very lenient. It basically guesses 16/9 whenever
@@ -417,8 +450,10 @@ def get_aspect_ratio (video_source_filename):
"""
cmd = "mplayer '%s' -vo png -ao null -frames 1" % video_source_filename
(command_output, exitstatus) = run(cmd)
- ar = re.findall("Movie-Aspect is ([0-9]+\.?[0-9]*:[0-9]+\.?[0-9]*)", command_output)
- if len(ar)==0:
+ ar = re.findall(
+ "Movie-Aspect is ([0-9]+\.?[0-9]*:[0-9]+\.?[0-9]*)",
+ command_output)
+ if len(ar) == 0:
return '16/9'
if ar[0] == '1.78:1':
return '16/9'
@@ -426,58 +461,70 @@ def get_aspect_ratio (video_source_filename):
return '4/3'
return '16/9'
#idh = re.findall("ID_VIDEO_HEIGHT=([0-9]+)", command_output)
- #if len(idw)==0 or len(idh)==0:
+ # if len(idw)==0 or len(idh)==0:
# print 'WARNING!'
# print 'Could not get aspect ration. Assuming 1.78:1 (16/9).'
# return 1.78
- #return float(idw[0])/float(idh[0])
-#ID_VIDEO_WIDTH=720
-#ID_VIDEO_HEIGHT=480
-#Movie-Aspect is 1.78:1 - prescaling to correct movie aspect.
+ # return float(idw[0])/float(idh[0])
+# ID_VIDEO_WIDTH=720
+# ID_VIDEO_HEIGHT=480
+# Movie-Aspect is 1.78:1 - prescaling to correct movie aspect.
-def get_aid_list (video_source_filename):
+def get_aid_list(video_source_filename):
"""This returns a list of audio ids in the source video file.
TODO: Also extract ID_AID_nnn_LANG to associate language. Not all DVDs include this.
"""
cmd = "mplayer '%s' -vo null -ao null -frames 0 -identify" % video_source_filename
(command_output, exitstatus) = run(cmd)
- idl = re.findall("ID_AUDIO_ID=([0-9]+)", command_output)
- idl.sort()
+ idl = sorted(re.findall("ID_AUDIO_ID=([0-9]+)", command_output))
return idl
-def get_sid_list (video_source_filename):
+
+def get_sid_list(video_source_filename):
"""This returns a list of subtitle ids in the source video file.
TODO: Also extract ID_SID_nnn_LANG to associate language. Not all DVDs include this.
"""
cmd = "mplayer '%s' -vo null -ao null -frames 0 -identify" % video_source_filename
(command_output, exitstatus) = run(cmd)
- idl = re.findall("ID_SUBTITLE_ID=([0-9]+)", command_output)
- idl.sort()
+ idl = sorted(re.findall("ID_SUBTITLE_ID=([0-9]+)", command_output))
return idl
-
-def extract_audio (video_source_filename, audio_id=128, verbose_flag=0, dry_run_flag=0):
+
+
+def extract_audio(
+ video_source_filename,
+ audio_id=128,
+ verbose_flag=0,
+ dry_run_flag=0):
"""This extracts the given audio_id track as raw uncompressed PCM from the given source video.
Note that mplayer always saves this to audiodump.wav.
At this time there is no way to set the output audio name.
"""
#cmd = "mplayer %(video_source_filename)s -vc null -vo null -aid %(audio_id)s -ao pcm:fast -noframedrop" % locals()
cmd = "mplayer -quiet '%(video_source_filename)s' -vc dummy -vo null -aid %(audio_id)s -ao pcm:fast -noframedrop" % locals()
- if verbose_flag: print cmd
+ if verbose_flag:
+ print cmd
if not dry_run_flag:
run(cmd)
print
-def extract_subtitles (video_source_filename, subtitle_id=0, verbose_flag=0, dry_run_flag=0):
+
+def extract_subtitles(
+ video_source_filename,
+ subtitle_id=0,
+ verbose_flag=0,
+ dry_run_flag=0):
"""This extracts the given subtitle_id track as VOBSUB format from the given source video.
"""
cmd = "mencoder -quiet '%(video_source_filename)s' -o /dev/null -nosound -ovc copy -vobsubout subtitles -vobsuboutindex 0 -sid %(subtitle_id)s" % locals()
- if verbose_flag: print cmd
+ if verbose_flag:
+ print cmd
if not dry_run_flag:
run(cmd)
print
-def get_length (audio_raw_filename):
+
+def get_length(audio_raw_filename):
"""This attempts to get the length of the media file (length is time in seconds).
This should not be confused with size (in bytes) of the file data.
This is best used on a raw PCM AUDIO file because mplayer cannot get an accurate
@@ -487,8 +534,7 @@ def get_length (audio_raw_filename):
"""
cmd = "mplayer %s -vo null -ao null -frames 0 -identify" % audio_raw_filename
(command_output, exitstatus) = run(cmd)
- idl = re.findall("ID_LENGTH=([0-9.]*)", command_output)
- idl.sort()
+ idl = sorted(re.findall("ID_LENGTH=([0-9.]*)", command_output))
if len(idl) != 1:
print "ERROR: cannot get length of raw audio file."
print "command_output of mplayer identify:"
@@ -498,11 +544,18 @@ def get_length (audio_raw_filename):
return -1
return float(idl[0])
-def get_filesize (filename):
+
+def get_filesize(filename):
"""This returns the number of bytes a file takes on storage."""
return os.stat(filename)[stat.ST_SIZE]
-def calc_video_bitrate (video_target_size, audio_bitrate, video_length, extra_space=0, dry_run_flag=0):
+
+def calc_video_bitrate(
+ video_target_size,
+ audio_bitrate,
+ video_length,
+ extra_space=0,
+ dry_run_flag=0):
"""This gives an estimate of the video bitrate necessary to
fit the final target size. This will take into account room to
fit the audio and extra space if given (for container overhead or whatnot).
@@ -517,13 +570,15 @@ def calc_video_bitrate (video_target_size, audio_bitrate, video_length, extra_sp
"""
if dry_run_flag:
return -1
- if extra_space is None: extra_space = 0
+ if extra_space is None:
+ extra_space = 0
#audio_size = os.stat(audio_compressed_filename)[stat.ST_SIZE]
audio_size = (audio_bitrate * video_length * 1000) / 8.0
video_target_size = video_target_size - audio_size - extra_space
- return (int)(calc_video_kbitrate (video_target_size, video_length))
+ return (int)(calc_video_kbitrate(video_target_size, video_length))
+
-def calc_video_kbitrate (target_size, length_secs):
+def calc_video_kbitrate(target_size, length_secs):
"""Given a target byte size free for video data, this returns the bitrate in kBit/S.
For mencoder vbitrate 1 kBit = 1000 Bits -- not 1024 bits.
target_size = bitrate * 1000 * length_secs / 8
@@ -532,18 +587,24 @@ def calc_video_kbitrate (target_size, length_secs):
"""
return int(target_size / (125.0 * length_secs))
-def crop_detect (video_source_filename, video_length, dry_run_flag=0):
+
+def crop_detect(video_source_filename, video_length, dry_run_flag=0):
"""This attempts to figure out the best crop for the given video file.
Basically it runs crop detect for 10 seconds on five different places in the video.
It picks the crop area that was most often detected.
"""
- skip = int(video_length/9) # offset to skip (-ss option in mencoder)
+ skip = int(video_length / 9) # offset to skip (-ss option in mencoder)
sample_length = 10
- cmd1 = "mencoder '%s' -quiet -ss %d -endpos %d -o /dev/null -nosound -ovc lavc -vf cropdetect" % (video_source_filename, skip, sample_length)
- cmd2 = "mencoder '%s' -quiet -ss %d -endpos %d -o /dev/null -nosound -ovc lavc -vf cropdetect" % (video_source_filename, 2*skip, sample_length)
- cmd3 = "mencoder '%s' -quiet -ss %d -endpos %d -o /dev/null -nosound -ovc lavc -vf cropdetect" % (video_source_filename, 4*skip, sample_length)
- cmd4 = "mencoder '%s' -quiet -ss %d -endpos %d -o /dev/null -nosound -ovc lavc -vf cropdetect" % (video_source_filename, 6*skip, sample_length)
- cmd5 = "mencoder '%s' -quiet -ss %d -endpos %d -o /dev/null -nosound -ovc lavc -vf cropdetect" % (video_source_filename, 8*skip, sample_length)
+ cmd1 = "mencoder '%s' -quiet -ss %d -endpos %d -o /dev/null -nosound -ovc lavc -vf cropdetect" % (
+ video_source_filename, skip, sample_length)
+ cmd2 = "mencoder '%s' -quiet -ss %d -endpos %d -o /dev/null -nosound -ovc lavc -vf cropdetect" % (
+ video_source_filename, 2 * skip, sample_length)
+ cmd3 = "mencoder '%s' -quiet -ss %d -endpos %d -o /dev/null -nosound -ovc lavc -vf cropdetect" % (
+ video_source_filename, 4 * skip, sample_length)
+ cmd4 = "mencoder '%s' -quiet -ss %d -endpos %d -o /dev/null -nosound -ovc lavc -vf cropdetect" % (
+ video_source_filename, 6 * skip, sample_length)
+ cmd5 = "mencoder '%s' -quiet -ss %d -endpos %d -o /dev/null -nosound -ovc lavc -vf cropdetect" % (
+ video_source_filename, 8 * skip, sample_length)
if dry_run_flag:
return "0:0:0:0"
(command_output1, exitstatus1) = run(cmd1)
@@ -552,36 +613,61 @@ def crop_detect (video_source_filename, video_length, dry_run_flag=0):
(command_output4, exitstatus4) = run(cmd4)
(command_output5, exitstatus5) = run(cmd5)
idl = re.findall("-vf crop=([0-9]+:[0-9]+:[0-9]+:[0-9]+)", command_output1)
- idl = idl + re.findall("-vf crop=([0-9]+:[0-9]+:[0-9]+:[0-9]+)", command_output2)
- idl = idl + re.findall("-vf crop=([0-9]+:[0-9]+:[0-9]+:[0-9]+)", command_output3)
- idl = idl + re.findall("-vf crop=([0-9]+:[0-9]+:[0-9]+:[0-9]+)", command_output4)
- idl = idl + re.findall("-vf crop=([0-9]+:[0-9]+:[0-9]+:[0-9]+)", command_output5)
+ idl = idl + \
+ re.findall("-vf crop=([0-9]+:[0-9]+:[0-9]+:[0-9]+)", command_output2)
+ idl = idl + \
+ re.findall("-vf crop=([0-9]+:[0-9]+:[0-9]+:[0-9]+)", command_output3)
+ idl = idl + \
+ re.findall("-vf crop=([0-9]+:[0-9]+:[0-9]+:[0-9]+)", command_output4)
+ idl = idl + \
+ re.findall("-vf crop=([0-9]+:[0-9]+:[0-9]+:[0-9]+)", command_output5)
items_count = count_unique(idl)
return items_count[0][1]
-def build_compression_command (video_source_filename, video_final_filename, video_target_size, audio_id=128, video_bitrate=1000, video_codec='mpeg4', audio_codec='mp3', video_fourcc_override='FMP4', video_gray_flag=0, video_crop_area=None, video_aspect_ratio='16/9', video_scale=None, video_encode_passes=2, video_deinterlace_flag=0, audio_volume_boost=None, audio_sample_rate=None, audio_bitrate=None, seek_skip=None, seek_length=None, video_chapter=None):
-#Notes:For DVD, VCD, and SVCD use acodec=mp2 and vcodec=mpeg2video:
-#mencoder movie.avi -o movie.VOB -ovc lavc -oac lavc -lavcopts acodec=mp2:abitrate=224:vcodec=mpeg2video:vbitrate=2000
+def build_compression_command(
+ video_source_filename,
+ video_final_filename,
+ video_target_size,
+ audio_id=128,
+ video_bitrate=1000,
+ video_codec='mpeg4',
+ audio_codec='mp3',
+ video_fourcc_override='FMP4',
+ video_gray_flag=0,
+ video_crop_area=None,
+ video_aspect_ratio='16/9',
+ video_scale=None,
+ video_encode_passes=2,
+ video_deinterlace_flag=0,
+ audio_volume_boost=None,
+ audio_sample_rate=None,
+ audio_bitrate=None,
+ seek_skip=None,
+ seek_length=None,
+ video_chapter=None):
+ # Notes:For DVD, VCD, and SVCD use acodec=mp2 and vcodec=mpeg2video:
+ # mencoder movie.avi -o movie.VOB -ovc lavc -oac lavc -lavcopts
+ # acodec=mp2:abitrate=224:vcodec=mpeg2video:vbitrate=2000
#
- # build video filter (-vf) argument
+ # build video filter (-vf) argument
#
video_filter = ''
- if video_crop_area and video_crop_area.lower()!='none':
+ if video_crop_area and video_crop_area.lower() != 'none':
video_filter = video_filter + 'crop=%s' % video_crop_area
if video_deinterlace_flag:
if video_filter != '':
video_filter = video_filter + ','
video_filter = video_filter + 'pp=md'
- if video_scale and video_scale.lower()!='none':
+ if video_scale and video_scale.lower() != 'none':
if video_filter != '':
video_filter = video_filter + ','
video_filter = video_filter + 'scale=%s' % video_scale
# optional video rotation -- were you holding your camera sideways?
- #if video_filter != '':
+ # if video_filter != '':
# video_filter = video_filter + ','
- #video_filter = video_filter + 'rotate=2'
+ #video_filter = video_filter + 'rotate=2'
if video_filter != '':
video_filter = '-vf ' + video_filter
@@ -589,7 +675,7 @@ def build_compression_command (video_source_filename, video_final_filename, vide
# build chapter argument
#
if video_chapter is not None:
- chapter = '-chapter %d-%d' %(video_chapter,video_chapter)
+ chapter = '-chapter %d-%d' % (video_chapter, video_chapter)
else:
chapter = ''
# chapter = '-chapter 2-2'
@@ -601,15 +687,15 @@ def build_compression_command (video_source_filename, video_final_filename, vide
if audio_sample_rate:
if audio_filter != '':
audio_filter = audio_filter + ','
- audio_filter = audio_filter + 'lavcresample=%s' % audio_sample_rate
+ audio_filter = audio_filter + 'lavcresample=%s' % audio_sample_rate
if audio_volume_boost is not None:
if audio_filter != '':
audio_filter = audio_filter + ','
- audio_filter = audio_filter + 'volume=%0.1f:1'%audio_volume_boost
+ audio_filter = audio_filter + 'volume=%0.1f:1' % audio_volume_boost
if audio_filter != '':
audio_filter = '-af ' + audio_filter
#
- #if audio_sample_rate:
+ # if audio_sample_rate:
# audio_filter = ('-srate %d ' % audio_sample_rate) + audio_filter
#
@@ -630,26 +716,164 @@ def build_compression_command (video_source_filename, video_final_filename, vide
cmd = "mencoder -quiet -info comment='Arkivist' '%(video_source_filename)s' %(seek_filter)s %(chapter)s -aid %(audio_id)s -o '%(video_final_filename)s' -ffourcc %(video_fourcc_override)s -ovc lavc -oac mp3lame %(lavcopts)s %(video_filter)s %(audio_filter)s" % locals()
return cmd
-def compression_estimate (video_length, video_source_filename, video_final_filename, video_target_size, audio_id=128, video_bitrate=1000, video_codec='mpeg4', audio_codec='mp3', video_fourcc_override='FMP4', video_gray_flag=0, video_crop_area=None, video_aspect_ratio='16/9', video_scale=None, video_encode_passes=2, video_deinterlace_flag=0, audio_volume_boost=None, audio_sample_rate=None, audio_bitrate=None):
+
+def compression_estimate(
+ video_length,
+ video_source_filename,
+ video_final_filename,
+ video_target_size,
+ audio_id=128,
+ video_bitrate=1000,
+ video_codec='mpeg4',
+ audio_codec='mp3',
+ video_fourcc_override='FMP4',
+ video_gray_flag=0,
+ video_crop_area=None,
+ video_aspect_ratio='16/9',
+ video_scale=None,
+ video_encode_passes=2,
+ video_deinterlace_flag=0,
+ audio_volume_boost=None,
+ audio_sample_rate=None,
+ audio_bitrate=None):
"""This attempts to figure out the best compression ratio for a given set of compression options.
"""
# TODO Need to account for AVI overhead.
- skip = int(video_length/9) # offset to skip (-ss option in mencoder)
+ skip = int(video_length / 9) # offset to skip (-ss option in mencoder)
sample_length = 10
- cmd1 = build_compression_command (video_source_filename, "compression_test_1.avi", video_target_size, audio_id, video_bitrate, video_codec, audio_codec, video_fourcc_override, video_gray_flag, video_crop_area, video_aspect_ratio, video_scale, video_encode_passes, video_deinterlace_flag, audio_volume_boost, audio_sample_rate, audio_bitrate, skip, sample_length)
- cmd2 = build_compression_command (video_source_filename, "compression_test_2.avi", video_target_size, audio_id, video_bitrate, video_codec, audio_codec, video_fourcc_override, video_gray_flag, video_crop_area, video_aspect_ratio, video_scale, video_encode_passes, video_deinterlace_flag, audio_volume_boost, audio_sample_rate, audio_bitrate, skip*2, sample_length)
- cmd3 = build_compression_command (video_source_filename, "compression_test_3.avi", video_target_size, audio_id, video_bitrate, video_codec, audio_codec, video_fourcc_override, video_gray_flag, video_crop_area, video_aspect_ratio, video_scale, video_encode_passes, video_deinterlace_flag, audio_volume_boost, audio_sample_rate, audio_bitrate, skip*4, sample_length)
- cmd4 = build_compression_command (video_source_filename, "compression_test_4.avi", video_target_size, audio_id, video_bitrate, video_codec, audio_codec, video_fourcc_override, video_gray_flag, video_crop_area, video_aspect_ratio, video_scale, video_encode_passes, video_deinterlace_flag, audio_volume_boost, audio_sample_rate, audio_bitrate, skip*6, sample_length)
- cmd5 = build_compression_command (video_source_filename, "compression_test_5.avi", video_target_size, audio_id, video_bitrate, video_codec, audio_codec, video_fourcc_override, video_gray_flag, video_crop_area, video_aspect_ratio, video_scale, video_encode_passes, video_deinterlace_flag, audio_volume_boost, audio_sample_rate, audio_bitrate, skip*8, sample_length)
+ cmd1 = build_compression_command(
+ video_source_filename,
+ "compression_test_1.avi",
+ video_target_size,
+ audio_id,
+ video_bitrate,
+ video_codec,
+ audio_codec,
+ video_fourcc_override,
+ video_gray_flag,
+ video_crop_area,
+ video_aspect_ratio,
+ video_scale,
+ video_encode_passes,
+ video_deinterlace_flag,
+ audio_volume_boost,
+ audio_sample_rate,
+ audio_bitrate,
+ skip,
+ sample_length)
+ cmd2 = build_compression_command(
+ video_source_filename,
+ "compression_test_2.avi",
+ video_target_size,
+ audio_id,
+ video_bitrate,
+ video_codec,
+ audio_codec,
+ video_fourcc_override,
+ video_gray_flag,
+ video_crop_area,
+ video_aspect_ratio,
+ video_scale,
+ video_encode_passes,
+ video_deinterlace_flag,
+ audio_volume_boost,
+ audio_sample_rate,
+ audio_bitrate,
+ skip * 2,
+ sample_length)
+ cmd3 = build_compression_command(
+ video_source_filename,
+ "compression_test_3.avi",
+ video_target_size,
+ audio_id,
+ video_bitrate,
+ video_codec,
+ audio_codec,
+ video_fourcc_override,
+ video_gray_flag,
+ video_crop_area,
+ video_aspect_ratio,
+ video_scale,
+ video_encode_passes,
+ video_deinterlace_flag,
+ audio_volume_boost,
+ audio_sample_rate,
+ audio_bitrate,
+ skip * 4,
+ sample_length)
+ cmd4 = build_compression_command(
+ video_source_filename,
+ "compression_test_4.avi",
+ video_target_size,
+ audio_id,
+ video_bitrate,
+ video_codec,
+ audio_codec,
+ video_fourcc_override,
+ video_gray_flag,
+ video_crop_area,
+ video_aspect_ratio,
+ video_scale,
+ video_encode_passes,
+ video_deinterlace_flag,
+ audio_volume_boost,
+ audio_sample_rate,
+ audio_bitrate,
+ skip * 6,
+ sample_length)
+ cmd5 = build_compression_command(
+ video_source_filename,
+ "compression_test_5.avi",
+ video_target_size,
+ audio_id,
+ video_bitrate,
+ video_codec,
+ audio_codec,
+ video_fourcc_override,
+ video_gray_flag,
+ video_crop_area,
+ video_aspect_ratio,
+ video_scale,
+ video_encode_passes,
+ video_deinterlace_flag,
+ audio_volume_boost,
+ audio_sample_rate,
+ audio_bitrate,
+ skip * 8,
+ sample_length)
run(cmd1)
run(cmd2)
run(cmd3)
run(cmd4)
run(cmd5)
- size = get_filesize ("compression_test_1.avi")+get_filesize ("compression_test_2.avi")+get_filesize ("compression_test_3.avi")+get_filesize ("compression_test_4.avi")+get_filesize ("compression_test_5.avi")
+ size = get_filesize("compression_test_1.avi") + get_filesize("compression_test_2.avi") + get_filesize(
+ "compression_test_3.avi") + get_filesize("compression_test_4.avi") + get_filesize("compression_test_5.avi")
return (size / 5.0)
-def compress_video (video_source_filename, video_final_filename, video_target_size, audio_id=128, video_bitrate=1000, video_codec='mpeg4', audio_codec='mp3', video_fourcc_override='FMP4', video_gray_flag=0, video_crop_area=None, video_aspect_ratio='16/9', video_scale=None, video_encode_passes=2, video_deinterlace_flag=0, audio_volume_boost=None, audio_sample_rate=None, audio_bitrate=None, seek_skip=None, seek_length=None, video_chapter=None, verbose_flag=0, dry_run_flag=0):
+
+def compress_video(
+ video_source_filename,
+ video_final_filename,
+ video_target_size,
+ audio_id=128,
+ video_bitrate=1000,
+ video_codec='mpeg4',
+ audio_codec='mp3',
+ video_fourcc_override='FMP4',
+ video_gray_flag=0,
+ video_crop_area=None,
+ video_aspect_ratio='16/9',
+ video_scale=None,
+ video_encode_passes=2,
+ video_deinterlace_flag=0,
+ audio_volume_boost=None,
+ audio_sample_rate=None,
+ audio_bitrate=None,
+ seek_skip=None,
+ seek_length=None,
+ video_chapter=None,
+ verbose_flag=0,
+ dry_run_flag=0):
"""This compresses the video and audio of the given source video filename to the transcoded filename.
This does a two-pass compression (I'm assuming mpeg4, I should probably make this smarter for other formats).
"""
@@ -658,18 +882,39 @@ def compress_video (video_source_filename, video_final_filename, video_target_si
#
#cmd = "mencoder -quiet '%(video_source_filename)s' -ss 65 -endpos 20 -aid %(audio_id)s -o '%(video_final_filename)s' -ffourcc %(video_fourcc_override)s -ovc lavc -oac lavc %(lavcopts)s %(video_filter)s %(audio_filter)s" % locals()
- cmd = build_compression_command (video_source_filename, video_final_filename, video_target_size, audio_id, video_bitrate, video_codec, audio_codec, video_fourcc_override, video_gray_flag, video_crop_area, video_aspect_ratio, video_scale, video_encode_passes, video_deinterlace_flag, audio_volume_boost, audio_sample_rate, audio_bitrate, seek_skip, seek_length, video_chapter)
- if verbose_flag: print cmd
+ cmd = build_compression_command(
+ video_source_filename,
+ video_final_filename,
+ video_target_size,
+ audio_id,
+ video_bitrate,
+ video_codec,
+ audio_codec,
+ video_fourcc_override,
+ video_gray_flag,
+ video_crop_area,
+ video_aspect_ratio,
+ video_scale,
+ video_encode_passes,
+ video_deinterlace_flag,
+ audio_volume_boost,
+ audio_sample_rate,
+ audio_bitrate,
+ seek_skip,
+ seek_length,
+ video_chapter)
+ if verbose_flag:
+ print cmd
if not dry_run_flag:
run(cmd)
print
# If not doing two passes then return early.
- if video_encode_passes!='2':
+ if video_encode_passes != '2':
return
if verbose_flag:
- video_actual_size = get_filesize (video_final_filename)
+ video_actual_size = get_filesize(video_final_filename)
if video_actual_size > video_target_size:
print "======================================================="
print "WARNING!"
@@ -681,18 +926,27 @@ def compress_video (video_source_filename, video_final_filename, video_target_si
#
# do the second pass video compression
#
- cmd = cmd.replace ('vpass=1', 'vpass=2')
- if verbose_flag: print cmd
+ cmd = cmd.replace('vpass=1', 'vpass=2')
+ if verbose_flag:
+ print cmd
if not dry_run_flag:
run(cmd)
print
return
-def compress_audio (audio_raw_filename, audio_compressed_filename, audio_lowpass_filter=None, audio_sample_rate=None, audio_bitrate=None, verbose_flag=0, dry_run_flag=0):
+
+def compress_audio(
+ audio_raw_filename,
+ audio_compressed_filename,
+ audio_lowpass_filter=None,
+ audio_sample_rate=None,
+ audio_bitrate=None,
+ verbose_flag=0,
+ dry_run_flag=0):
"""This is depricated.
This compresses the raw audio file to the compressed audio filename.
"""
- cmd = 'lame -h --athaa-sensitivity 1' # --cwlimit 11"
+ cmd = 'lame -h --athaa-sensitivity 1' # --cwlimit 11"
if audio_lowpass_filter:
cmd = cmd + ' --lowpass ' + audio_lowpass_filter
if audio_bitrate:
@@ -701,30 +955,62 @@ def compress_audio (audio_raw_filename, audio_compressed_filename, audio_lowpass
if audio_sample_rate:
cmd = cmd + ' --resample ' + audio_sample_rate
cmd = cmd + ' ' + audio_raw_filename + ' ' + audio_compressed_filename
- if verbose_flag: print cmd
+ if verbose_flag:
+ print cmd
if not dry_run_flag:
(command_output, exitstatus) = run(cmd)
print
if exitstatus != 0:
raise Exception('ERROR: lame failed to compress raw audio file.')
-def mux (video_final_filename, video_transcoded_filename, audio_compressed_filename, video_container_format, verbose_flag=0, dry_run_flag=0):
+
+def mux(
+ video_final_filename,
+ video_transcoded_filename,
+ audio_compressed_filename,
+ video_container_format,
+ verbose_flag=0,
+ dry_run_flag=0):
"""This is depricated. I used to use a three-pass encoding where I would mix the audio track separately, but
this never worked very well (loss of audio sync)."""
- if video_container_format.lower() == 'mkv': # Matroska
- mux_mkv (video_final_filename, video_transcoded_filename, audio_compressed_filename, verbose_flag, dry_run_flag)
+ if video_container_format.lower() == 'mkv': # Matroska
+ mux_mkv(
+ video_final_filename,
+ video_transcoded_filename,
+ audio_compressed_filename,
+ verbose_flag,
+ dry_run_flag)
if video_container_format.lower() == 'avi':
- mux_avi (video_final_filename, video_transcoded_filename, audio_compressed_filename, verbose_flag, dry_run_flag)
-
-def mux_mkv (video_final_filename, video_transcoded_filename, audio_compressed_filename, verbose_flag=0, dry_run_flag=0):
+ mux_avi(
+ video_final_filename,
+ video_transcoded_filename,
+ audio_compressed_filename,
+ verbose_flag,
+ dry_run_flag)
+
+
+def mux_mkv(
+ video_final_filename,
+ video_transcoded_filename,
+ audio_compressed_filename,
+ verbose_flag=0,
+ dry_run_flag=0):
"""This is depricated."""
- cmd = 'mkvmerge -o %s --noaudio %s %s' % (video_final_filename, video_transcoded_filename, audio_compressed_filename)
- if verbose_flag: print cmd
+ cmd = 'mkvmerge -o %s --noaudio %s %s' % (
+ video_final_filename, video_transcoded_filename, audio_compressed_filename)
+ if verbose_flag:
+ print cmd
if not dry_run_flag:
run(cmd)
print
-def mux_avi (video_final_filename, video_transcoded_filename, audio_compressed_filename, verbose_flag=0, dry_run_flag=0):
+
+def mux_avi(
+ video_final_filename,
+ video_transcoded_filename,
+ audio_compressed_filename,
+ verbose_flag=0,
+ dry_run_flag=0):
"""This is depricated."""
pass
# cmd = "mencoder -quiet -oac copy -ovc copy -o '%s' -audiofile %s '%s'" % (video_final_filename, audio_compressed_filename, video_transcoded_filename)
@@ -733,19 +1019,24 @@ def mux_avi (video_final_filename, video_transcoded_filename, audio_compressed_f
# run(cmd)
# print
-def delete_tmp_files (audio_raw_filename, verbose_flag=0, dry_run_flag=0):
+
+def delete_tmp_files(audio_raw_filename, verbose_flag=0, dry_run_flag=0):
global GLOBAL_LOGFILE_NAME
- file_list = ' '.join([GLOBAL_LOGFILE_NAME, 'divx2pass.log', audio_raw_filename ])
+ file_list = ' '.join(
+ [GLOBAL_LOGFILE_NAME, 'divx2pass.log', audio_raw_filename])
cmd = 'rm -f ' + file_list
- if verbose_flag: print cmd
+ if verbose_flag:
+ print cmd
if not dry_run_flag:
run(cmd)
print
-
+
##############################################################################
# This is the interactive Q&A that is used if a conf file was not given.
##############################################################################
-def interactive_convert ():
+
+
+def interactive_convert():
global prompts, prompts_key_order
@@ -755,10 +1046,10 @@ def interactive_convert ():
print " Enter '?' at any question to get extra help."
print "=============================================="
print
-
- # Ask for the level of options the user wants.
+
+ # Ask for the level of options the user wants.
# A lot of code just to print a string!
- level_sort = {0:'', 1:'', 2:''}
+ level_sort = {0: '', 1: '', 2: ''}
for k in prompts:
level = prompts[k][3]
if level < 0 or level > 2:
@@ -775,11 +1066,16 @@ def interactive_convert ():
for k in prompts_key_order:
if k == 'video_aspect_ratio':
guess_aspect = get_aspect_ratio(options['video_source_filename'])
- options[k] = input_option (prompts[k][1], guess_aspect, prompts[k][2], prompts[k][3], max_prompt_level)
+ options[k] = input_option(
+ prompts[k][1],
+ guess_aspect,
+ prompts[k][2],
+ prompts[k][3],
+ max_prompt_level)
elif k == 'audio_id':
- aid_list = get_aid_list (options['video_source_filename'])
+ aid_list = get_aid_list(options['video_source_filename'])
default_id = '128'
- if max_prompt_level>=prompts[k][3]:
+ if max_prompt_level >= prompts[k][3]:
if len(aid_list) > 1:
print "This video has more than one audio stream. The following stream audio IDs were found:"
for aid in aid_list:
@@ -791,11 +1087,16 @@ def interactive_convert ():
print "If reading directly from a DVD then the DVD device might be busy."
print "Using a default setting of stream id 128 (main audio on most DVDs)."
default_id = '128'
- options[k] = input_option (prompts[k][1], default_id, prompts[k][2], prompts[k][3], max_prompt_level)
+ options[k] = input_option(
+ prompts[k][1],
+ default_id,
+ prompts[k][2],
+ prompts[k][3],
+ max_prompt_level)
elif k == 'subtitle_id':
- sid_list = get_sid_list (options['video_source_filename'])
+ sid_list = get_sid_list(options['video_source_filename'])
default_id = 'None'
- if max_prompt_level>=prompts[k][3]:
+ if max_prompt_level >= prompts[k][3]:
if len(sid_list) > 0:
print "This video has one or more subtitle streams. The following stream subtitle IDs were found:"
for sid in sid_list:
@@ -807,24 +1108,45 @@ def interactive_convert ():
print "Unable to get the list of subtitle streams from this video. It may have none."
print "Setting default to None."
default_id = 'None'
- options[k] = input_option (prompts[k][1], default_id, prompts[k][2], prompts[k][3], max_prompt_level)
+ options[k] = input_option(
+ prompts[k][1],
+ default_id,
+ prompts[k][2],
+ prompts[k][3],
+ max_prompt_level)
elif k == 'audio_lowpass_filter':
- lowpass_default = "%.1f" % (math.floor(float(options['audio_sample_rate']) / 2.0))
- options[k] = input_option (prompts[k][1], lowpass_default, prompts[k][2], prompts[k][3], max_prompt_level)
+ lowpass_default = "%.1f" % (math.floor(
+ float(options['audio_sample_rate']) / 2.0))
+ options[k] = input_option(
+ prompts[k][1],
+ lowpass_default,
+ prompts[k][2],
+ prompts[k][3],
+ max_prompt_level)
elif k == 'video_bitrate':
if options['video_length'].lower() == 'none':
- options[k] = input_option (prompts[k][1], '1000', prompts[k][2], prompts[k][3], max_prompt_level)
+ options[k] = input_option(prompts[k][1], '1000', prompts[k][
+ 2], prompts[k][3], max_prompt_level)
else:
- options[k] = input_option (prompts[k][1], prompts[k][0], prompts[k][2], prompts[k][3], max_prompt_level)
+ options[k] = input_option(prompts[k][1], prompts[k][0], prompts[
+ k][2], prompts[k][3], max_prompt_level)
else:
- # don't bother asking for video_target_size or video_bitrate_overhead if video_bitrate was set
- if (k=='video_target_size' or k=='video_bitrate_overhead') and options['video_bitrate']!='calc':
+ # don't bother asking for video_target_size or
+ # video_bitrate_overhead if video_bitrate was set
+ if (k == 'video_target_size' or k == 'video_bitrate_overhead') and options[
+ 'video_bitrate'] != 'calc':
continue
# don't bother with crop area if video length is none
- if k == 'video_crop_area' and options['video_length'].lower() == 'none':
+ if k == 'video_crop_area' and options[
+ 'video_length'].lower() == 'none':
options['video_crop_area'] = 'none'
continue
- options[k] = input_option (prompts[k][1], prompts[k][0], prompts[k][2], prompts[k][3], max_prompt_level)
+ options[k] = input_option(
+ prompts[k][1],
+ prompts[k][0],
+ prompts[k][2],
+ prompts[k][3],
+ max_prompt_level)
#options['video_final_filename'] = options['video_final_filename'] + "." + options['video_container_format']
@@ -832,7 +1154,7 @@ def interactive_convert ():
print "Ready to Rippy!"
print
print "The following options will be used:"
- for k,v in options.iteritems():
+ for k, v in options.iteritems():
print "%27s : %s" % (k, v)
print
@@ -843,7 +1165,8 @@ def interactive_convert ():
os._exit(1)
return options
-def clean_options (d):
+
+def clean_options(d):
"""This validates and cleans up the options dictionary.
After reading options interactively or from a conf file
we need to make sure that the values make sense and are
@@ -859,14 +1182,14 @@ def clean_options (d):
d[k] = d[k].strip()
# convert all flag options to 0 or 1
if '_flag' in k:
- if type(d[k]) is types.StringType:
- if d[k].strip().lower()[0] in 'yt1': #Yes, True, 1
+ if isinstance(d[k], types.StringType):
+ if d[k].strip().lower()[0] in 'yt1': # Yes, True, 1
d[k] = 1
else:
d[k] = 0
d['video_bitrate'] = d['video_bitrate'].lower()
- if d['video_bitrate'][0]=='c':
- d['video_bitrate']='calc'
+ if d['video_bitrate'][0] == 'c':
+ d['video_bitrate'] = 'calc'
else:
d['video_bitrate'] = int(float(d['video_bitrate']))
try:
@@ -892,7 +1215,7 @@ def clean_options (d):
d['subtitle_id'] = int(d['subtitle_id'])
except:
d['subtitle_id'] = None
-
+
try:
d['video_bitrate_overhead'] = float(d['video_bitrate_overhead'])
except:
@@ -901,45 +1224,47 @@ def clean_options (d):
d['audio_bitrate'] = int(d['audio_bitrate'])
d['audio_sample_rate'] = int(d['audio_sample_rate'])
d['audio_volume_boost'] = d['audio_volume_boost'].lower()
- if d['audio_volume_boost'][0]=='n':
+ if d['audio_volume_boost'][0] == 'n':
d['audio_volume_boost'] = None
else:
- d['audio_volume_boost'] = d['audio_volume_boost'].replace('db','')
+ d['audio_volume_boost'] = d['audio_volume_boost'].replace('db', '')
d['audio_volume_boost'] = float(d['audio_volume_boost'])
# assert (d['video_bitrate']=='calc' and d['video_target_size']!='none')
# or (d['video_bitrate']!='calc' and d['video_target_size']=='none')
d['video_scale'] = d['video_scale'].lower()
- if d['video_scale'][0]=='n':
- d['video_scale']='none'
+ if d['video_scale'][0] == 'n':
+ d['video_scale'] = 'none'
else:
al = re.findall("([0-9]+).*?([0-9]+)", d['video_scale'])
- d['video_scale']=al[0][0]+':'+al[0][1]
+ d['video_scale'] = al[0][0] + ':' + al[0][1]
d['video_crop_area'] = d['video_crop_area'].lower()
- if d['video_crop_area'][0]=='n':
- d['video_crop_area']='none'
+ if d['video_crop_area'][0] == 'n':
+ d['video_crop_area'] = 'none'
d['video_length'] = d['video_length'].lower()
- if d['video_length'][0]=='c':
- d['video_length']='calc'
- elif d['video_length'][0]=='n':
- d['video_length']='none'
+ if d['video_length'][0] == 'c':
+ d['video_length'] = 'calc'
+ elif d['video_length'][0] == 'n':
+ d['video_length'] = 'none'
else:
d['video_length'] = int(float(d['video_length']))
- if d['video_length']==0:
+ if d['video_length'] == 0:
d['video_length'] = 'none'
- assert (not (d['video_length']=='none' and d['video_bitrate']=='calc'))
+ assert (not (d['video_length'] == 'none' and d['video_bitrate'] == 'calc'))
return d
-def main ():
+
+def main():
try:
- optlist, args = getopt.getopt(sys.argv[1:], 'h?', ['help','h','?'])
- except Exception, e:
+ optlist, args = getopt.getopt(sys.argv[1:], 'h?', ['help', 'h', '?'])
+ except Exception as e:
print str(e)
exit_with_usage()
command_line_options = dict(optlist)
# There are a million ways to cry for help. These are but a few of them.
- if [elem for elem in command_line_options if elem in ['-h','--h','-?','--?','--help']]:
+ if [elem for elem in command_line_options if elem in [
+ '-h', '--h', '-?', '--?', '--help']]:
exit_with_usage(0)
missing = check_missing_requirements()
@@ -959,16 +1284,21 @@ def main ():
os._exit(1)
if len(args) > 0:
- # cute one-line string-to-dictionary parser (two-lines if you count this comment):
- options = dict(re.findall('([^: \t\n]*)\s*:\s*(".*"|[^ \t\n]*)', file(args[0]).read()))
+ # cute one-line string-to-dictionary parser (two-lines if you count
+ # this comment):
+ options = dict(
+ re.findall(
+ '([^: \t\n]*)\s*:\s*(".*"|[^ \t\n]*)',
+ file(
+ args[0]).read()))
options = clean_options(options)
- convert (options)
+ convert(options)
else:
- options = interactive_convert ()
+ options = interactive_convert()
options = clean_options(options)
- convert (options)
+ convert(options)
print "# Done!"
-
+
if __name__ == "__main__":
try:
start_time = time.time()
@@ -977,7 +1307,7 @@ if __name__ == "__main__":
print time.asctime()
print "TOTAL TIME IN MINUTES:",
print (time.time() - start_time) / 60.0
- except Exception, e:
+ except Exception as e:
tb_dump = traceback.format_exc()
print "=========================================================================="
print "ERROR -- Unexpected exception in script."
@@ -990,4 +1320,3 @@ if __name__ == "__main__":
print >>GLOBAL_LOGFILE, str(tb_dump)
print >>GLOBAL_LOGFILE, "=========================================================================="
exit_with_usage(3)
-
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/script.py b/lldb/third_party/Python/module/pexpect-2.4/examples/script.py
index 908b91241a6..41e2ea81428 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/script.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/script.py
@@ -18,33 +18,45 @@ Example:
"""
-import os, sys, time, getopt
-import signal, fcntl, termios, struct
+import os
+import sys
+import time
+import getopt
+import signal
+import fcntl
+import termios
+import struct
import traceback
import pexpect
-global_pexpect_instance = None # Used by signal handler
+global_pexpect_instance = None # Used by signal handler
+
def exit_with_usage():
print globals()['__doc__']
os._exit(1)
+
def main():
######################################################################
# Parse the options, arguments, get ready, etc.
######################################################################
try:
- optlist, args = getopt.getopt(sys.argv[1:], 'h?ac:', ['help','h','?'])
- except Exception, e:
+ optlist, args = getopt.getopt(
+ sys.argv[
+ 1:], 'h?ac:', [
+ 'help', 'h', '?'])
+ except Exception as e:
print str(e)
exit_with_usage()
options = dict(optlist)
if len(args) > 1:
exit_with_usage()
-
- if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]:
+
+ if [elem for elem in options if elem in [
+ '-h', '--h', '-?', '--?', '--help']]:
print "Help:"
exit_with_usage()
@@ -53,17 +65,17 @@ def main():
else:
script_filename = "script.log"
if '-a' in options:
- fout = file (script_filename, "ab")
+ fout = file(script_filename, "ab")
else:
- fout = file (script_filename, "wb")
+ fout = file(script_filename, "wb")
if '-c' in options:
command = options['-c']
else:
command = "sh"
# Begin log with date/time in the form CCCCyymm.hhmmss
- fout.write ('# %4d%02d%02d.%02d%02d%02d \n' % time.localtime()[:-3])
-
+ fout.write('# %4d%02d%02d.%02d%02d%02d \n' % time.localtime()[:-3])
+
######################################################################
# Start the interactive session
######################################################################
@@ -78,26 +90,26 @@ def main():
fout.close()
return 0
-def sigwinch_passthrough (sig, data):
+
+def sigwinch_passthrough(sig, data):
# Check for buggy platforms (see pexpect.setwinsize()).
if 'TIOCGWINSZ' in dir(termios):
TIOCGWINSZ = termios.TIOCGWINSZ
else:
- TIOCGWINSZ = 1074295912 # assume
- s = struct.pack ("HHHH", 0, 0, 0, 0)
- a = struct.unpack ('HHHH', fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ , s))
+ TIOCGWINSZ = 1074295912 # assume
+ s = struct.pack("HHHH", 0, 0, 0, 0)
+ a = struct.unpack('HHHH', fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ, s))
global global_pexpect_instance
- global_pexpect_instance.setwinsize(a[0],a[1])
+ global_pexpect_instance.setwinsize(a[0], a[1])
if __name__ == "__main__":
try:
main()
- except SystemExit, e:
+ except SystemExit as e:
raise e
- except Exception, e:
+ except Exception as e:
print "ERROR"
print str(e)
traceback.print_exc()
os._exit(1)
-
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/ssh_session.py b/lldb/third_party/Python/module/pexpect-2.4/examples/ssh_session.py
index 4d0e228a7e3..222a72c1693 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/ssh_session.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/ssh_session.py
@@ -5,10 +5,12 @@
# April 2003
#
from pexpect import *
-import os, sys
+import os
+import sys
import getpass
import time
-
+
+
class ssh_session:
"Session with extra state including the password to be used."
@@ -25,30 +27,29 @@ class ssh_session:
'@@@@@@@@@@@@',
'Command not found.',
EOF,
- ]
-
- self.f = open('ssh.out','w')
-
+ ]
+
+ self.f = open('ssh.out', 'w')
+
def __repr__(self):
- outl = 'class :'+self.__class__.__name__
+ outl = 'class :' + self.__class__.__name__
for attr in self.__dict__:
if attr == 'password':
- outl += '\n\t'+attr+' : '+'*'*len(self.password)
+ outl += '\n\t' + attr + ' : ' + '*' * len(self.password)
else:
- outl += '\n\t'+attr+' : '+str(getattr(self, attr))
+ outl += '\n\t' + attr + ' : ' + str(getattr(self, attr))
return outl
def __exec(self, command):
-
"Execute a command on the remote host. Return the output."
child = spawn(command,
- #timeout=10,
- )
+ # timeout=10,
+ )
if self.verbose:
sys.stderr.write("-> " + command + "\n")
seen = child.expect(self.keys)
- self.f.write(str(child.before) + str(child.after)+'\n')
+ self.f.write(str(child.before) + str(child.after) + '\n')
if seen == 0:
child.sendline('yes')
seen = child.expect(self.keys)
@@ -61,13 +62,13 @@ class ssh_session:
# Added to allow the background running of remote process
if not child.isalive():
seen = child.expect(self.keys)
- if seen == 2:
+ if seen == 2:
lines = child.readlines()
self.f.write(lines)
if self.verbose:
sys.stderr.write("<- " + child.before + "|\n")
try:
- self.f.write(str(child.before) + str(child.after)+'\n')
+ self.f.write(str(child.before) + str(child.after) + '\n')
except:
pass
self.f.close()
@@ -75,20 +76,18 @@ class ssh_session:
def ssh(self, command):
- return self.__exec("ssh -l %s %s \"%s\"" \
- % (self.user,self.host,command))
+ return self.__exec("ssh -l %s %s \"%s\""
+ % (self.user, self.host, command))
def scp(self, src, dst):
- return self.__exec("scp %s %s@%s:%s" \
- % (src, session.user, session.host, dst))
+ return self.__exec("scp %s %s@%s:%s"
+ % (src, session.user, session.host, dst))
def exists(self, file):
-
"Retrieve file permissions of specified remote file."
seen = self.ssh("/bin/ls -ld %s" % file)
if string.find(seen, "No such file") > -1:
- return None # File doesn't exist
+ return None # File doesn't exist
else:
- return seen.split()[0] # Return permission field of listing.
-
+ return seen.split()[0] # Return permission field of listing.
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/ssh_tunnel.py b/lldb/third_party/Python/module/pexpect-2.4/examples/ssh_tunnel.py
index 3c8bc09514b..ae1ce46faee 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/ssh_tunnel.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/ssh_tunnel.py
@@ -23,50 +23,54 @@ host = raw_input('Hostname: ')
user = raw_input('Username: ')
X = getpass.getpass('Password: ')
-def get_process_info ():
- # This seems to work on both Linux and BSD, but should otherwise be considered highly UNportable.
+def get_process_info():
- ps = pexpect.run ('ps ax -O ppid')
+ # This seems to work on both Linux and BSD, but should otherwise be
+ # considered highly UNportable.
+
+ ps = pexpect.run('ps ax -O ppid')
pass
-def start_tunnel ():
+
+
+def start_tunnel():
try:
- ssh_tunnel = pexpect.spawn (tunnel_command % globals())
- ssh_tunnel.expect ('password:')
- time.sleep (0.1)
- ssh_tunnel.sendline (X)
- time.sleep (60) # Cygwin is slow to update process status.
- ssh_tunnel.expect (pexpect.EOF)
+ ssh_tunnel = pexpect.spawn(tunnel_command % globals())
+ ssh_tunnel.expect('password:')
+ time.sleep(0.1)
+ ssh_tunnel.sendline(X)
+ time.sleep(60) # Cygwin is slow to update process status.
+ ssh_tunnel.expect(pexpect.EOF)
except Exception, e:
print str(e)
-def main ():
+
+def main():
while True:
- ps = pexpect.spawn ('ps')
- time.sleep (1)
- index = ps.expect (['/usr/bin/ssh', pexpect.EOF, pexpect.TIMEOUT])
+ ps = pexpect.spawn('ps')
+ time.sleep(1)
+ index = ps.expect(['/usr/bin/ssh', pexpect.EOF, pexpect.TIMEOUT])
if index == 2:
print 'TIMEOUT in ps command...'
print str(ps)
- time.sleep (13)
+ time.sleep(13)
if index == 1:
print time.asctime(),
print 'restarting tunnel'
- start_tunnel ()
- time.sleep (11)
- print 'tunnel OK'
+ start_tunnel()
+ time.sleep(11)
+ print 'tunnel OK'
else:
# print 'tunnel OK'
- time.sleep (7)
+ time.sleep(7)
if __name__ == '__main__':
- main ()
+ main()
# This was for older SSH versions that didn't have -f option
#tunnel_command = 'ssh -C -n -L 25:%(host)s:25 -L 110:%(host)s:110 %(user)s@%(host)s -f nothing.sh'
-#nothing_script = """#!/bin/sh
-#while true; do sleep 53; done
+# nothing_script = """#!/bin/sh
+# while true; do sleep 53; done
#"""
-
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/sshls.py b/lldb/third_party/Python/module/pexpect-2.4/examples/sshls.py
index ef1ab9c23cd..8e4909ac48c 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/sshls.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/sshls.py
@@ -7,10 +7,11 @@ $Id: sshls.py 489 2007-11-28 23:40:34Z noah $
"""
import pexpect
-import getpass, os
+import getpass
+import os
-def ssh_command (user, host, password, command):
+def ssh_command(user, host, password, command):
"""This runs a command on the remote host. This could also be done with the
pxssh class, but this demonstrates what that class does at a simpler level.
This returns a pexpect.spawn object. This handles the case when you try to
@@ -18,39 +19,39 @@ connect to a new host and ssh asks you if you want to accept the public key
fingerprint and continue connecting. """
ssh_newkey = 'Are you sure you want to continue connecting'
- child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command))
+ child = pexpect.spawn('ssh -l %s %s %s' % (user, host, command))
i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])
- if i == 0: # Timeout
+ if i == 0: # Timeout
print 'ERROR!'
print 'SSH could not login. Here is what SSH said:'
print child.before, child.after
return None
- if i == 1: # SSH does not have the public key. Just accept it.
- child.sendline ('yes')
- child.expect ('password: ')
+ if i == 1: # SSH does not have the public key. Just accept it.
+ child.sendline('yes')
+ child.expect('password: ')
i = child.expect([pexpect.TIMEOUT, 'password: '])
- if i == 0: # Timeout
+ if i == 0: # Timeout
print 'ERROR!'
print 'SSH could not login. Here is what SSH said:'
print child.before, child.after
- return None
+ return None
child.sendline(password)
return child
-def main ():
+
+def main():
host = raw_input('Hostname: ')
user = raw_input('User: ')
password = getpass.getpass('Password: ')
- child = ssh_command (user, host, password, '/bin/ls -l')
+ child = ssh_command(user, host, password, '/bin/ls -l')
child.expect(pexpect.EOF)
print child.before
if __name__ == '__main__':
try:
main()
- except Exception, e:
+ except Exception as e:
print str(e)
traceback.print_exc()
os._exit(1)
-
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/topip.py b/lldb/third_party/Python/module/pexpect-2.4/examples/topip.py
index 5bd63e2ef22..498f9a3b516 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/topip.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/topip.py
@@ -46,21 +46,30 @@ Noah Spurrier
$Id: topip.py 489 2007-11-28 23:40:34Z noah $
"""
-import pexpect, pxssh # See http://pexpect.sourceforge.net/
-import os, sys, time, re, getopt, pickle, getpass, smtplib
+import pexpect
+import pxssh # See http://pexpect.sourceforge.net/
+import os
+import sys
+import time
+import re
+import getopt
+import pickle
+import getpass
+import smtplib
import traceback
from pprint import pprint
TOPIP_LOG_FILE = '/var/log/topip.log'
TOPIP_LAST_RUN_STATS = '/var/run/topip.last'
+
def exit_with_usage():
print globals()['__doc__']
os._exit(1)
-def stats(r):
+def stats(r):
"""This returns a dict of the median, average, standard deviation, min and max of the given sequence.
>>> from topip import stats
@@ -75,30 +84,35 @@ def stats(r):
"""
total = sum(r)
- avg = float(total)/float(len(r))
- sdsq = sum([(i-avg)**2 for i in r])
- s = list(r)
- s.sort()
- return dict(zip(['med', 'avg', 'stddev', 'min', 'max'] , (s[len(s)//2], avg, (sdsq/len(r))**.5, min(r), max(r))))
+ avg = float(total) / float(len(r))
+ sdsq = sum([(i - avg)**2 for i in r])
+ s = sorted(r)
+ return dict(zip(['med', 'avg', 'stddev', 'min', 'max'],
+ (s[len(s) // 2], avg, (sdsq / len(r))**.5, min(r), max(r))))
-def send_alert (message, subject, addr_from, addr_to, smtp_server='localhost'):
+def send_alert(message, subject, addr_from, addr_to, smtp_server='localhost'):
"""This sends an email alert.
"""
- message = 'From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n' % (addr_from, addr_to, subject) + message
+ message = 'From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n' % (
+ addr_from, addr_to, subject) + message
server = smtplib.SMTP(smtp_server)
server.sendmail(addr_from, addr_to, message)
server.quit()
+
def main():
######################################################################
- ## Parse the options, arguments, etc.
+ # Parse the options, arguments, etc.
######################################################################
try:
- optlist, args = getopt.getopt(sys.argv[1:], 'h?valqs:u:p:n:', ['help','h','?','ipv6','stddev='])
- except Exception, e:
+ optlist, args = getopt.getopt(
+ sys.argv[
+ 1:], 'h?valqs:u:p:n:', [
+ 'help', 'h', '?', 'ipv6', 'stddev='])
+ except Exception as e:
print str(e)
exit_with_usage()
options = dict(optlist)
@@ -119,7 +133,8 @@ def main():
print args, len(args)
return 0
exit_with_usage()
- if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]:
+ if [elem for elem in options if elem in [
+ '-h', '--h', '-?', '--?', '--help']]:
print 'Help:'
exit_with_usage()
if '-s' in options:
@@ -183,10 +198,11 @@ def main():
p.sendline('netstat -n -t')
PROMPT = p.PROMPT
- # loop through each matching netstat_pattern and put the ip address in the list.
+ # loop through each matching netstat_pattern and put the ip address in the
+ # list.
ip_list = {}
try:
- while 1:
+ while True:
i = p.expect([PROMPT, netstat_pattern])
if i == 0:
break
@@ -199,21 +215,25 @@ def main():
pass
# remove a few common, uninteresting addresses from the dictionary.
- ip_list = dict([ (key,value) for key,value in ip_list.items() if '192.168.' not in key])
- ip_list = dict([ (key,value) for key,value in ip_list.items() if '127.0.0.1' not in key])
+ ip_list = dict([(key, value)
+ for key, value in ip_list.items() if '192.168.' not in key])
+ ip_list = dict([(key, value)
+ for key, value in ip_list.items() if '127.0.0.1' not in key])
# sort dict by value (count)
#ip_list = sorted(ip_list.iteritems(),lambda x,y:cmp(x[1], y[1]),reverse=True)
ip_list = ip_list.items()
if len(ip_list) < 1:
- if verbose: print 'Warning: no networks connections worth looking at.'
+ if verbose:
+ print 'Warning: no networks connections worth looking at.'
return 0
- ip_list.sort(lambda x,y:cmp(y[1],x[1]))
+ ip_list.sort(lambda x, y: cmp(y[1], x[1]))
# generate some stats for the ip addresses found.
if average_n <= 1:
average_n = None
- s = stats(zip(*ip_list[0:average_n])[1]) # The * unary operator treats the list elements as arguments
+ # The * unary operator treats the list elements as arguments
+ s = stats(zip(*ip_list[0:average_n])[1])
s['maxip'] = ip_list[0]
# print munin-style or verbose results for the stats.
@@ -223,33 +243,44 @@ def main():
print 'connections_stddev.value', s['stddev']
return 0
if verbose:
- pprint (s)
+ pprint(s)
print
- pprint (ip_list[0:average_n])
+ pprint(ip_list[0:average_n])
# load the stats from the last run.
try:
last_stats = pickle.load(file(TOPIP_LAST_RUN_STATS))
except:
- last_stats = {'maxip':None}
+ last_stats = {'maxip': None}
- if s['maxip'][1] > (s['stddev'] * stddev_trigger) and s['maxip']==last_stats['maxip']:
- if verbose: print 'The maxip has been above trigger for two consecutive samples.'
+ if s['maxip'][1] > (
+ s['stddev'] *
+ stddev_trigger) and s['maxip'] == last_stats['maxip']:
+ if verbose:
+ print 'The maxip has been above trigger for two consecutive samples.'
if alert_flag:
- if verbose: print 'SENDING ALERT EMAIL'
- send_alert(str(s), 'ALERT on %s' % hostname, alert_addr_from, alert_addr_to)
+ if verbose:
+ print 'SENDING ALERT EMAIL'
+ send_alert(
+ str(s),
+ 'ALERT on %s' %
+ hostname,
+ alert_addr_from,
+ alert_addr_to)
if log_flag:
- if verbose: print 'LOGGING THIS EVENT'
- fout = file(TOPIP_LOG_FILE,'a')
+ if verbose:
+ print 'LOGGING THIS EVENT'
+ fout = file(TOPIP_LOG_FILE, 'a')
#dts = time.strftime('%Y:%m:%d:%H:%M:%S', time.localtime())
dts = time.asctime()
- fout.write ('%s - %d connections from %s\n' % (dts,s['maxip'][1],str(s['maxip'][0])))
+ fout.write('%s - %d connections from %s\n' %
+ (dts, s['maxip'][1], str(s['maxip'][0])))
fout.close()
# save state to TOPIP_LAST_RUN_STATS
try:
- pickle.dump(s, file(TOPIP_LAST_RUN_STATS,'w'))
- os.chmod (TOPIP_LAST_RUN_STATS, 0664)
+ pickle.dump(s, file(TOPIP_LAST_RUN_STATS, 'w'))
+ os.chmod(TOPIP_LAST_RUN_STATS, 0o664)
except:
pass
# p.logout()
@@ -258,10 +289,9 @@ if __name__ == '__main__':
try:
main()
sys.exit(0)
- except SystemExit, e:
+ except SystemExit as e:
raise e
- except Exception, e:
+ except Exception as e:
print str(e)
traceback.print_exc()
os._exit(1)
-
diff --git a/lldb/third_party/Python/module/pexpect-2.4/examples/uptime.py b/lldb/third_party/Python/module/pexpect-2.4/examples/uptime.py
index f5018dfe0c1..65cfc6cd711 100644
--- a/lldb/third_party/Python/module/pexpect-2.4/examples/uptime.py
+++ b/lldb/third_party/Python/module/pexpect-2.4/examples/uptime.py
@@ -29,29 +29,30 @@ import re
# 6:08PM up 4 days, 22:26, 1 user, load averages: 0.13, 0.09, 0.08
# This parses uptime output into the major groups using regex group matching.
-p = pexpect.spawn ('uptime')
-p.expect('up\s+(.*?),\s+([0-9]+) users?,\s+load averages?: ([0-9]+\.[0-9][0-9]),?\s+([0-9]+\.[0-9][0-9]),?\s+([0-9]+\.[0-9][0-9])')
+p = pexpect.spawn('uptime')
+p.expect(
+ 'up\s+(.*?),\s+([0-9]+) users?,\s+load averages?: ([0-9]+\.[0-9][0-9]),?\s+([0-9]+\.[0-9][0-9]),?\s+([0-9]+\.[0-9][0-9])')
duration, users, av1, av5, av15 = p.match.groups()
# The duration is a little harder to parse because of all the different
# styles of uptime. I'm sure there is a way to do this all at once with
# one single regex, but I bet it would be hard to read and maintain.
-# If anyone wants to send me a version using a single regex I'd be happy to see it.
+# If anyone wants to send me a version using a single regex I'd be happy
+# to see it.
days = '0'
hours = '0'
mins = '0'
if 'day' in duration:
- p.match = re.search('([0-9]+)\s+day',duration)
+ p.match = re.search('([0-9]+)\s+day', duration)
days = str(int(p.match.group(1)))
if ':' in duration:
- p.match = re.search('([0-9]+):([0-9]+)',duration)
+ p.match = re.search('([0-9]+):([0-9]+)', duration)
hours = str(int(p.match.group(1)))
mins = str(int(p.match.group(2)))
if 'min' in duration:
- p.match = re.search('([0-9]+)\s+min',duration)
+ p.match = re.search('([0-9]+)\s+min', duration)
mins = str(int(p.match.group(1)))
# Print the parsed fields in CSV format.
print 'days, hours, minutes, users, cpu avg 1 min, cpu avg 5 min, cpu avg 15 min'
print '%s, %s, %s, %s, %s, %s, %s' % (days, hours, mins, users, av1, av5, av15)
-
OpenPOWER on IntegriCloud