Skip to content

Commit

Permalink
Add revision flag for installation
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielRyanSmith committed May 4, 2022
1 parent 4a888b7 commit 597f808
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
58 changes: 33 additions & 25 deletions tools/wpt/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,36 +674,40 @@ def install_mojojs(self, dest, browser_binary):
self.logger.error(f"Cannot enable MojoJS: {e}")
return None

def install_webdriver(self, dest=None, channel=None, browser_binary=None):
def install_webdriver(self, dest=None, channel=None, browser_binary=None, revision=None):
if dest is None:
dest = os.pwd

# A browser binary is needed so that the version can be detected.
# The ChromeDriver that is installed will match this version.
if browser_binary is None:
# If a browser binary path was not given, detect a valid path.
browser_binary = self.find_binary(channel=channel)
# We need a browser to version match, so if a browser binary path
# was not given and cannot be detected, raise an error.
if revision is None:
# If a revision was not given, we will need to detect the browser version.
# The ChromeDriver that is installed will match this version.
if browser_binary is None:
raise FileNotFoundError("No browser binary detected. "
"Cannot install ChromeDriver without a browser version.")
# If a browser binary path was not given, detect a valid path.
browser_binary = self.find_binary(channel=channel)
# We need a browser to version match, so if a browser binary path
# was not given and cannot be detected, raise an error.
if browser_binary is None:
raise FileNotFoundError("No browser binary detected. "
"Cannot install ChromeDriver without a browser version.")

version = self.version(browser_binary)
if version is None:
raise ValueError("Unable to detect browser version from binary at "
f"{browser_binary}. Cannot install ChromeDriver "
"without a valid version to match.")
else:
version = None

version = self.version(browser_binary)
if version is None:
raise ValueError(f"Unable to detect browser version from binary at {browser_binary}. "
"Cannot install ChromeDriver without a valid version to match.")
chromedriver_path = self.install_webdriver_by_version(version, dest, revision)

chromedriver_path = self.install_webdriver_by_version(version, dest)
return chromedriver_path

def install_webdriver_by_version(self, version, dest, channel=None):
def install_webdriver_by_version(self, version, dest, revision=None):
dest = os.path.join(dest, self.product)
self._remove_existing_chromedriver_binary(dest)

# _get_webdriver_url is implemented differently for Chrome and Chromium because
# they download their respective versions of ChromeDriver from different sources.
url = self._get_webdriver_url(version)
url = self._get_webdriver_url(version, revision)
self.logger.info(f"Downloading ChromeDriver from {url}")
unzip(get(url).raw, dest)

Expand Down Expand Up @@ -786,7 +790,7 @@ def _find_binary_in_directory(self, directory):
# find_executable will add .exe on Windows automatically.
return find_executable("chrome", os.path.join(directory, self._chromium_package_name))

def _get_webdriver_url(self, version):
def _get_webdriver_url(self, version, revision=None):
"""Get Chromium Snapshots url to download Chromium ChromeDriver."""
filename = f"chromedriver_{self._chromedriver_platform_string}.zip"

Expand All @@ -795,15 +799,19 @@ def _get_webdriver_url(self, version):
# that url takes priority over trying to form another.
if hasattr(self, "last_revision_used") and self.last_revision_used is not None:
return self._build_snapshots_url(self.last_revision_used, filename)
revision = self._get_chromium_revision(filename, version)
if revision is None:
revision = self._get_chromium_revision(filename, version)
return self._build_snapshots_url(revision, filename)

def download(self, dest=None, channel=None, rename=None, version=None):
def download(self, dest=None, channel=None, rename=None, version=None, revision=None):
if dest is None:
dest = self._get_browser_binary_dir(None, channel)

filename = f"{self._chromium_package_name}.zip"
revision = self._get_chromium_revision(filename, version)

if revision is None:
revision = self._get_chromium_revision(filename, version)

url = self._build_snapshots_url(revision, filename)
self.logger.info(f"Downloading Chromium from {url}")
resp = get(url)
Expand All @@ -818,9 +826,9 @@ def download(self, dest=None, channel=None, rename=None, version=None):
def find_binary(self, venv_path=None, channel=None):
return self._find_binary_in_directory(self._get_browser_binary_dir(venv_path, channel))

def install(self, dest=None, channel=None, version=None):
def install(self, dest=None, channel=None, version=None, revision=None):
dest = self._get_browser_binary_dir(dest, channel)
installer_path = self.download(dest, channel, version=version)
installer_path = self.download(dest, channel, version=version, revision=revision)
with open(installer_path, "rb") as f:
unzip(f, dest)
os.remove(installer_path)
Expand Down Expand Up @@ -863,7 +871,7 @@ class Chrome(ChromeChromiumBase):

product = "chrome"

def _get_webdriver_url(self, version):
def _get_webdriver_url(self, version, revision=None):
"""Get a ChromeDriver API URL to download a version of ChromeDriver that matches
the browser binary version. Version selection is described here:
https://chromedriver.chromium.org/downloads/version-selection"""
Expand Down
13 changes: 11 additions & 2 deletions tools/wpt/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def get_parser():
"(only with --download-only)")
parser.add_argument('-d', '--destination',
help='filesystem directory to place the component')
parser.add_argument('--revision', default=None,
help='Chromium revision to install from snapshots')
return parser


Expand Down Expand Up @@ -86,12 +88,16 @@ def run(venv, **kwargs):
raise argparse.ArgumentError(None,
"No --destination argument, and no default for the environment")

if kwargs["revision"] is not None and browser != "chromium":
raise argparse.ArgumentError(None, "--revision flag cannot be used for non-Chromium browsers.")

install(browser, kwargs["component"], destination, channel, logger=logger,
download_only=kwargs["download_only"], rename=kwargs["rename"])
download_only=kwargs["download_only"], rename=kwargs["rename"],
revision=kwargs["revision"])


def install(name, component, destination, channel="nightly", logger=None, download_only=False,
rename=None):
rename=None, revision=None):
if logger is None:
import logging
logger = logging.getLogger("install")
Expand All @@ -106,6 +112,9 @@ def install(name, component, destination, channel="nightly", logger=None, downlo
kwargs = {}
if download_only and rename:
kwargs["rename"] = rename
if revision:
kwargs["revision"] = revision

path = getattr(browser_cls(logger), method)(dest=destination, channel=channel, **kwargs)
if path:
logger.info('Binary %s as %s', "downloaded" if download_only else "installed", path)

0 comments on commit 597f808

Please sign in to comment.