Browse Source

fix(dev): also patch invalid escape sequences in selenium.* (#34577)

asottile-sentry 2 years ago
parent
commit
535fcc791d

+ 1 - 0
.pre-commit-config.yaml

@@ -82,4 +82,5 @@ repos:
       exclude_types: [svg]
     - id: trailing-whitespace
       exclude_types: [svg]
+      exclude: ^scripts/patches/
     - id: debug-statements

+ 1 - 6
scripts/lib.sh

@@ -127,12 +127,7 @@ install-py-dev() {
 patch-selenium() {
     # XXX: getsentry repo calls this!
     # This hack is until we can upgrade to a newer version of Selenium
-    fx_profile=.venv/lib/python3.8/site-packages/selenium/webdriver/firefox/firefox_profile.py
-    # Remove this block when upgrading the selenium package
-    if grep -q "or setting is" "${fx_profile}"; then
-        echo "We are patching ${fx_profile}. You will see this message only once."
-        patch -p0 <scripts/patches/firefox_profile.diff
-    fi
+    python -S -m tools.patch_selenium
 }
 
 setup-git-config() {

+ 11 - 0
scripts/patches/chrome_options.diff

@@ -0,0 +1,11 @@
+--- .venv/lib/python3.8/site-packages/selenium/webdriver/chrome/options.py	2022-05-12 17:07:24.000000000 -0400
++++ .venv/lib/python3.8/site-packages/selenium/webdriver/chrome/options.py.bak	2022-05-12 17:08:17.000000000 -0400
+@@ -121,7 +121,7 @@
+         to the ChromeDriver
+ 
+         :Args:
+-         - extension: path to the \*.crx file
++         - extension: path to the \\*.crx file
+         """
+         if extension:
+             extension_to_add = os.path.abspath(os.path.expanduser(extension))

+ 38 - 0
scripts/patches/remote_webdriver.diff

@@ -0,0 +1,38 @@
+--- .venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py	2022-05-12 16:53:37.000000000 -0400
++++ .venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py.bak	2022-05-12 16:54:12.000000000 -0400
+@@ -343,7 +343,7 @@
+         return resp['value'] if resp['value'] is not None else ""
+ 
+     def find_element_by_id(self, id_):
+-        """Finds an element by id.
++        r"""Finds an element by id.
+ 
+         :Args:
+          - id\_ - The id of the element to be found.
+@@ -360,7 +360,7 @@
+         return self.find_element(by=By.ID, value=id_)
+ 
+     def find_elements_by_id(self, id_):
+-        """
++        r"""
+         Finds multiple elements by id.
+ 
+         :Args:
+@@ -614,7 +614,7 @@
+         return self.find_elements(by=By.CSS_SELECTOR, value=css_selector)
+ 
+     def execute_script(self, script, *args):
+-        """
++        r"""
+         Synchronously Executes JavaScript in the current window/frame.
+ 
+         :Args:
+@@ -641,7 +641,7 @@
+ 
+         :Args:
+          - script: The JavaScript to execute.
+-         - \*args: Any applicable arguments for your JavaScript.
++         - \\*args: Any applicable arguments for your JavaScript.
+ 
+         :Usage:
+             script = "var callback = arguments[arguments.length - 1]; " \

+ 20 - 0
scripts/patches/remote_webelement.diff

@@ -0,0 +1,20 @@
+--- .venv/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py	2022-05-12 17:08:49.000000000 -0400
++++ .venv/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py.bak	2022-05-12 17:09:05.000000000 -0400
+@@ -159,7 +159,7 @@
+         return self._execute(Command.IS_ELEMENT_ENABLED)['value']
+ 
+     def find_element_by_id(self, id_):
+-        """Finds element within this element's children by ID.
++        r"""Finds element within this element's children by ID.
+ 
+         :Args:
+          - id\_ - ID of child element to locate.
+@@ -176,7 +176,7 @@
+         return self.find_element(by=By.ID, value=id_)
+ 
+     def find_elements_by_id(self, id_):
+-        """Finds a list of elements within this element's children by ID.
++        r"""Finds a list of elements within this element's children by ID.
+         Will return a list of webelements if found, or an empty list if not.
+ 
+         :Args:

+ 11 - 0
scripts/patches/support_wait.diff

@@ -0,0 +1,11 @@
+--- .venv/lib/python3.8/site-packages/selenium/webdriver/support/wait.py	2022-05-12 17:10:07.000000000 -0400
++++ .venv/lib/python3.8/site-packages/selenium/webdriver/support/wait.py.bak	2022-05-12 17:10:13.000000000 -0400
+@@ -38,7 +38,7 @@
+            Example:
+             from selenium.webdriver.support.ui import WebDriverWait \n
+             element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId")) \n
+-            is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\ \n
++            is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\\ \n
+                         until_not(lambda x: x.find_element_by_id("someId").is_displayed())
+         """
+         self._driver = driver

+ 50 - 0
tools/patch_selenium.py

@@ -0,0 +1,50 @@
+import re
+import subprocess
+
+PATCH_FILE_PATTERN = (
+    (
+        "scripts/patches/chrome_options.diff",
+        ".venv/lib/python3.8/site-packages/selenium/webdriver/chrome/options.py",
+        re.compile(r"path to the \\\*\.crx file"),
+    ),
+    (
+        "scripts/patches/firefox_profile.diff",
+        ".venv/lib/python3.8/site-packages/selenium/webdriver/firefox/firefox_profile.py",
+        re.compile(r"setting is ''"),
+    ),
+    (
+        "scripts/patches/remote_webdriver.diff",
+        ".venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py",
+        re.compile(r' """Finds an element by id'),
+    ),
+    (
+        "scripts/patches/remote_webelement.diff",
+        ".venv/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py",
+        re.compile(r' """Finds element within this element\'s children by ID'),
+    ),
+    (
+        "scripts/patches/support_wait.diff",
+        ".venv/lib/python3.8/site-packages/selenium/webdriver/support/wait.py",
+        re.compile(r"\.\\ \\n"),
+    ),
+)
+
+
+def main() -> int:
+    for patch, filename, pattern in PATCH_FILE_PATTERN:
+        with open(filename) as f:
+            for line in f:
+                if pattern.search(line):
+                    break
+            else:  # did not find the pattern
+                continue
+
+        print(f"patching {filename}, you will only see this once")
+        if subprocess.call(("patch", "-f", "-p0", "-i", patch)):
+            return 1
+
+    return 0
+
+
+if __name__ == "__main__":
+    raise SystemExit(main())