Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 4 | |
michael@0 | 5 | |
michael@0 | 6 | import copy |
michael@0 | 7 | import httplib2 |
michael@0 | 8 | import os |
michael@0 | 9 | |
michael@0 | 10 | import mozfile |
michael@0 | 11 | import mozinstall |
michael@0 | 12 | from mozprofile import Profile |
michael@0 | 13 | from mozrunner import FirefoxRunner |
michael@0 | 14 | |
michael@0 | 15 | |
michael@0 | 16 | class TPSFirefoxRunner(object): |
michael@0 | 17 | |
michael@0 | 18 | PROCESS_TIMEOUT = 240 |
michael@0 | 19 | |
michael@0 | 20 | def __init__(self, binary): |
michael@0 | 21 | if binary is not None and ('http://' in binary or 'ftp://' in binary): |
michael@0 | 22 | self.url = binary |
michael@0 | 23 | self.binary = None |
michael@0 | 24 | else: |
michael@0 | 25 | self.url = None |
michael@0 | 26 | self.binary = binary |
michael@0 | 27 | |
michael@0 | 28 | self.installdir = None |
michael@0 | 29 | |
michael@0 | 30 | def __del__(self): |
michael@0 | 31 | if self.installdir: |
michael@0 | 32 | mozfile.remove(self.installdir, True) |
michael@0 | 33 | |
michael@0 | 34 | def download_url(self, url, dest=None): |
michael@0 | 35 | h = httplib2.Http() |
michael@0 | 36 | resp, content = h.request(url, 'GET') |
michael@0 | 37 | if dest == None: |
michael@0 | 38 | dest = os.path.basename(url) |
michael@0 | 39 | |
michael@0 | 40 | local = open(dest, 'wb') |
michael@0 | 41 | local.write(content) |
michael@0 | 42 | local.close() |
michael@0 | 43 | return dest |
michael@0 | 44 | |
michael@0 | 45 | def download_build(self, installdir='downloadedbuild', appname='firefox'): |
michael@0 | 46 | self.installdir = os.path.abspath(installdir) |
michael@0 | 47 | buildName = os.path.basename(self.url) |
michael@0 | 48 | pathToBuild = os.path.join(os.path.dirname(os.path.abspath(__file__)), |
michael@0 | 49 | buildName) |
michael@0 | 50 | |
michael@0 | 51 | # delete the build if it already exists |
michael@0 | 52 | if os.access(pathToBuild, os.F_OK): |
michael@0 | 53 | os.remove(pathToBuild) |
michael@0 | 54 | |
michael@0 | 55 | # download the build |
michael@0 | 56 | print 'downloading build' |
michael@0 | 57 | self.download_url(self.url, pathToBuild) |
michael@0 | 58 | |
michael@0 | 59 | # install the build |
michael@0 | 60 | print 'installing %s' % pathToBuild |
michael@0 | 61 | mozfile.remove(self.installdir, True) |
michael@0 | 62 | binary = mozinstall.install(src=pathToBuild, dest=self.installdir) |
michael@0 | 63 | |
michael@0 | 64 | # remove the downloaded archive |
michael@0 | 65 | os.remove(pathToBuild) |
michael@0 | 66 | |
michael@0 | 67 | return binary |
michael@0 | 68 | |
michael@0 | 69 | def run(self, profile=None, timeout=PROCESS_TIMEOUT, env=None, args=None): |
michael@0 | 70 | """Runs the given FirefoxRunner with the given Profile, waits |
michael@0 | 71 | for completion, then returns the process exit code |
michael@0 | 72 | """ |
michael@0 | 73 | if profile is None: |
michael@0 | 74 | profile = Profile() |
michael@0 | 75 | self.profile = profile |
michael@0 | 76 | |
michael@0 | 77 | if self.binary is None and self.url: |
michael@0 | 78 | self.binary = self.download_build() |
michael@0 | 79 | |
michael@0 | 80 | runner = FirefoxRunner(profile=self.profile, binary=self.binary, |
michael@0 | 81 | env=env, cmdargs=args) |
michael@0 | 82 | |
michael@0 | 83 | runner.start(timeout=timeout) |
michael@0 | 84 | return runner.wait() |