#!/usr/bin/python # # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. import subprocess import tempfile import os import re import getpass import datetime import logging import time import sys import signal import shutil import glob def sighandler(signal, frame): print "signal %d, throwing" % signal raise Exception signal.signal(signal.SIGHUP, sighandler) ######################################################################## def start_xvnc(disp, tmpdir): xvnc_stdout = open(os.path.join(tmpdir, "xvnc.stdout"), mode="w") xvnc_stderr = open(os.path.join(tmpdir, "xvnc.stderr"), mode="w") xvnc_proc = subprocess.Popen (["Xvnc", "-fp", "/usr/share/X11/fonts/misc", "-localhost", "-SecurityTypes", "None", "-IdleTimeout", "604800", "-depth", "24", "-ac", (":%d" % disp)], cwd=tmpdir, shell=False, stdout=xvnc_stdout, stderr=xvnc_stderr) time.sleep(3) assert xvnc_proc.poll() == None logging.info("started Xvnc server (pid %d) on display :%d" % (xvnc_proc.pid, disp)) return xvnc_proc ######################################################################## def large_variant_filename(variant): return variant + "-large.swf" def large_variant_html_filename(variant): return variant + "-large.html" def small_variant_filename(variant): return variant + ".swf" def start_xvnc_recorder(vnc2swfpath, disp, variant, tmpdir): rec_stdout = open(os.path.join(tmpdir, "vnc2swf.stdout"), mode="w") rec_stderr = open(os.path.join(tmpdir, "vnc2swf.stderr"), mode="w") rec_proc = subprocess.Popen ([os.path.join(vnc2swfpath, "vnc2swf.py"), "-n", "-o", large_variant_filename(variant), "-C", "800x600+0+100", "-r", "2", "-t", "video", "localhost:%d" % disp], cwd=tmpdir, shell=False, stdout=rec_stdout, stderr=rec_stderr) time.sleep(3) assert rec_proc.poll() == None logging.info("started vnc2swf recorder (pid %d) on display :%d" % (rec_proc.pid, disp)) return rec_proc def complete_xvnc_recording(vnc2swfpath, proc, variant, tmpdir): edit_stdout = open(os.path.join(tmpdir, "edit.stdout"), mode="w") edit_stderr = open(os.path.join(tmpdir, "edit.stderr"), mode="w") logging.info("killing vnc2swf recorder (pid %d)" % proc.pid) os.kill(proc.pid, signal.SIGINT) proc.wait() logging.info("down-sampling video") subprocess.Popen([os.path.join(vnc2swfpath, "edit.py"), "-c", "-o", small_variant_filename(variant), "-t", "video", "-s", "0.5", "-r", "32", large_variant_filename(variant)], cwd=tmpdir, shell=False, stdout=edit_stdout, stderr=edit_stderr).wait() #os.unlink(large_variant_html_filename(variant)) #os.unlink(large_variant_filename(variant)) logging.info("output video is in " + small_variant_filename(variant)) ######################################################################## def write_vgopts(tmpdir, vgopts): f = open(os.path.join(tmpdir, ".valgrindrc"), "w") for i in vgopts: f.write(i + "\n") f.close() ######################################################################## class Firefox_runner: def __init__(this, batchprefix, homedir, ffdir, timestr, tmpdir, disp): this.tmpdir = tmpdir this.homedir = homedir this.ffdir = ffdir this.profile = batchprefix + timestr this.profile_dir = os.path.join(this.tmpdir, this.profile) this.bin = os.path.join(this.ffdir, "firefox-bin") this.environ = { "PATH" : os.getenv("PATH"), "HOME" : this.homedir, "LD_LIBRARY_PATH" : this.ffdir, "MOZ_NO_REMOTE" : "1", # "DISPLAY" : ":0", "DISPLAY" : (":%d" % disp), } this.kill_initial_profiles_ini() this.create_tmp_profile() this.write_proxy_pac_file() this.write_user_prefs() this.perform_initial_registration() def kill_initial_profiles_ini(this): prof = os.path.join(this.homedir, ".mozilla") shutil.rmtree(prof, True) os.mkdir(prof) def clear_cache(this): shutil.rmtree(os.path.join(this.profile_dir, "Cache"), True) def create_tmp_profile(this): subprocess.Popen ([this.bin, "-CreateProfile", (this.profile + " " + this.profile_dir)], cwd=this.tmpdir, shell=False, env=this.environ).wait() def write_proxy_pac_file(this): this.proxypac = os.path.join(this.tmpdir, "proxy.pac") p = open(this.proxypac, "w") p.write("function FindProxyForURL(url, host) {\n" " if (host == \"localhost\")\n" " return \"DIRECT\";\n" " else\n" " return \"PROXY localhost\";\n" "}\n") p.close() def write_user_prefs(this): userprefs = open(os.path.join(this.profile_dir, "user.js"), "w") userprefs.write("user_pref(\"network.proxy.autoconfig_url\", \"file://%s\");\n" % this.proxypac) userprefs.write("user_pref(\"network.proxy.type\", 2);\n") userprefs.write("user_pref(\"dom.max_script_run_time\", 0);\n") userprefs.write("user_pref(\"hangmonitor.timeout\", 0);\n"); userprefs.write("user_pref(\"dom.allow_scripts_to_close_windows\", true);\n") userprefs.close() def perform_initial_registration(this): dummy_file = os.path.join(this.tmpdir, "dummy.html") f = open(dummy_file, "w") f.write("
\n") ix.write("%s | %s |