Skip to content

Commit

Permalink
Setup Pipeline & Routing Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinyzu committed Dec 8, 2024
1 parent 6b64fac commit df93fea
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 54 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/patchright_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
python -m pip install --upgrade pip
pip install -r local-requirements.txt
pip install -e .
pip install black
pip install black toml
- name: Patch Playwright-Python Package
if: steps.version_check.outputs.proceed == 'true'
Expand All @@ -79,7 +79,9 @@ jobs:
run: |
cd playwright-python
pip install -e .
python setup.py bdist_wheel --all
for wheel in $(python setup.py --list-wheels); do
PLAYWRIGHT_TARGET_WHEEL=$wheel python -m build --wheel
done
- name: Create Empty Versioning Release
if: steps.version_check.outputs.proceed == 'true'
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<img alt="Patchright Version" src="https://img.shields.io/github/v/release/microsoft/playwright-python?display_name=release&label=Version">
</a>
<a href="https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python/releases">
<img alt="GitHub Downloads (all assets, all releases)" src="https://img.shields.io/github/downloads/Kaliiiiiiiiii-Vinyzu/patchright-python/total">
<img alt="GitHub Downloads (all assets, all releases)" src="https://img.shields.io/pepy/dt/patchright?color=red">
</a>
<a href="https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python">
<img src="https://img.shields.io/badge/Package-Python-seagreen">
Expand Down
111 changes: 60 additions & 51 deletions patch_python_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,41 @@
import glob
import os

import toml

patchright_version = os.environ.get('playwright_version')

def patch_file(file_path: str, patched_tree: ast.AST) -> None:
with open(file_path, "w") as f:
f.write(ast.unparse(ast.fix_missing_locations(patched_tree)))

# Adding _repo_version.py (Might not be intended but fixes the build)
with open("playwright-python/playwright/_repo_version.py", "w") as f:
#f.write(f"version = {os.environ.get('playwright_version')}")
f.write(f"version = '{patchright_version}'")

# Patching pyproject.toml
with open("playwright-python/pyproject.toml", "r+") as f:
pyproject_source = f.read()
pyproject_source = pyproject_source.replace('version_file = "playwright/_repo_version.py"', 'version_file = "patchright/_repo_version.py"')
pyproject_source = toml.load(f)

pyproject_source["project"]["name"] = "patchright"
pyproject_source["project"]["description"] = "Undetected Python version of the Playwright testing and automation library."
pyproject_source["project"]["authors"] = [{'name': 'Microsoft Corporation, patched by github.com/Kaliiiiiiiiii-Vinyzu/'}]

pyproject_source["project"]["urls"]["homepage"] = "https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python"
pyproject_source["project"]["urls"]["Release notes"] = "https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python/releases"
pyproject_source["project"]["urls"]["Bug Reports"] = "https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python/issues"
pyproject_source["project"]["urls"]["homeSource Codepage"] = "https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python"

del pyproject_source["project"]["scripts"]["playwright"]
pyproject_source["project"]["scripts"]["patchright"] = "patchright.__main__:main"
pyproject_source["project"]["entry-points"]["pyinstaller40"]["hook-dirs"] = "patchright._impl.__pyinstaller:get_hook_dirs"

pyproject_source["tool"]["setuptools"]["packages"] = ['patchright', 'patchright.async_api', 'patchright.sync_api', 'patchright._impl', 'patchright._impl.__pyinstaller']
pyproject_source["tool"]["setuptools_scm"] = {'version_file': 'patchright/_repo_version.py'}

f.seek(0)
f.write(pyproject_source)
f.truncate()
toml.dump(pyproject_source, f)


# Patching setup.py
Expand Down Expand Up @@ -71,55 +94,11 @@ def patch_file(file_path: str, patched_tree: ast.AST) -> None:
# Modify Setup Call
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
if node.func.id == "setup":
for keyword in node.keywords:
match keyword.arg:
case "name":
keyword.value.value = "patchright"
case "author":
keyword.value.value = "Microsoft Corportation, patched by github.com/Kaliiiiiiiiii-Vinyzu/"
case "description":
keyword.value.value = "Undetected Python version of the Playwright testing and automation library. "
case "url":
keyword.value.value = "https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python"
case "project_urls":
keyword.value = ast.Dict(
keys=[
ast.Constant(value='Release notes'), ast.Constant(value='Bug Reports'), ast.Constant(value='Source Code')
],
values=[
ast.Constant(value='https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python/releases'),
ast.Constant(value='https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python/issues'),
ast.Constant(value='https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python/')
]
)
case "packages":
keyword.value.elts = [
ast.Constant(value='patchright'),
ast.Constant(value='patchright.async_api'),
ast.Constant(value='patchright.sync_api'),
ast.Constant(value='patchright._impl'),
ast.Constant(value='patchright._impl.__pyinstaller')
]
case "entry_points":
keyword.value = ast.Dict(
keys=[
ast.Constant(value='console_scripts'),
ast.Constant(value='pyinstaller40')
],
values=[
ast.List(elts=[ast.Constant(value='patchright=patchright.__main__:main')], ctx=ast.Load()),
ast.List(elts=[ast.Constant(value='hook-dirs=patchright._impl.__pyinstaller:get_hook_dirs')], ctx=ast.Load())
]
)

node.keywords.append(ast.keyword(
arg="version",
value=
# ast.parse("os.environ.get('playwright_version')'")
ast.parse("os.environ.get('playwright_version') + '.post0'")
value=ast.Constant(value=patchright_version)
))


patch_file("playwright-python/setup.py", setup_tree)

# Patching playwright/_impl/_driver.py
Expand All @@ -144,6 +123,10 @@ def patch_file(file_path: str, patched_tree: ast.AST) -> None:
if node.func.id == "Path" and node.args[0].value.id == "playwright":
node.args[0].value.id = "patchright"

elif isinstance(node, ast.Attribute) and isinstance(node.value, ast.Attribute) and isinstance(node.value.value, ast.Attribute) and isinstance(node.value.value.value, ast.Name):
if node.value.value.value.id == "playwright" and node.value.value.attr == "_impl" and node.value.attr == "_impl_to_api_mapping":
node.value.value.value.id = "patchright"

patch_file("playwright-python/playwright/_impl/_connection.py", connection_source_tree)

# Patching playwright/_impl/_js_handle.py
Expand Down Expand Up @@ -195,7 +178,20 @@ async def route_handler(route: Route) -> None:
await route.continue_()
if not self.route_injecting:
await self.route("**/*", mapping.wrap_handler(route_handler))
if self._connection._is_sync:
self._routes.insert(
0,
RouteHandler(
self._options.get("baseURL"),
"**/*",
mapping.wrap_handler(route_handler),
False,
None,
),
)
await self._update_interception_patterns()
else:
await self.route("**/*", mapping.wrap_handler(route_handler))
self.route_injecting = True""").body[0])

patch_file("playwright-python/playwright/_impl/_browser_context.py", browser_context_tree)
Expand Down Expand Up @@ -236,7 +232,20 @@ async def route_handler(route: Route) -> None:
await route.continue_()
if not self.route_injecting and not self.context.route_injecting:
await self.route("**/*", mapping.wrap_handler(route_handler))
if self._connection._is_sync:
self._routes.insert(
0,
RouteHandler(
self._options.get("baseURL"),
"**/*",
mapping.wrap_handler(route_handler),
False,
None,
),
)
await self._update_interception_patterns()
else:
await self.route("**/*", mapping.wrap_handler(route_handler))
self.route_injecting = True""").body[0])

patch_file("playwright-python/playwright/_impl/_page.py", page_tree)
Expand Down

0 comments on commit df93fea

Please sign in to comment.