Skip to content

Commit 2c8254f

Browse files
committed
support custom plugin download root by Environment Variables
1 parent 639d53f commit 2c8254f

File tree

2 files changed

+85
-11
lines changed

2 files changed

+85
-11
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,40 @@ Python bindings for the webview library, allowing you to create desktop applicat
1515
pip install webview_python
1616
```
1717

18+
## Environment Variables
19+
20+
Webview Python supports the following environment variables:
21+
22+
- `WEBVIEW_VERSION`: Specify the version of the webview library to use (default: "0.9.0")
23+
- `WEBVIEW_DOWNLOAD_BASE`: Specify the base URL or file path for downloading webview libraries (default: GitHub releases)
24+
- Can be a web URL: `https://internal-server.com/webview-libs`
25+
- Network share: `\\server\share\webview-libs` or `/mnt/server/webview-libs`
26+
- Local path: `/path/to/libs` or `C:\path\to\libs`
27+
28+
Example usage:
29+
```bash
30+
# Using an internal HTTP server
31+
export WEBVIEW_DOWNLOAD_BASE="http://internal-server.com/webview-libs"
32+
33+
# Using a network share on Windows
34+
set WEBVIEW_DOWNLOAD_BASE=\\\\server\\share\\webview-libs
35+
36+
# Using a mounted path on Linux
37+
export WEBVIEW_DOWNLOAD_BASE="/mnt/server/webview-libs"
38+
```
39+
40+
Note: When using a custom download location, you must organize the libraries in the same structure as the GitHub releases:
41+
```
42+
WEBVIEW_DOWNLOAD_BASE/
43+
├── 0.9.0/
44+
│ ├── webview.dll
45+
│ ├── WebView2Loader.dll
46+
│ ├── libwebview.so
47+
│ ├── libwebview.x86_64.dylib
48+
│ └── libwebview.aarch64.dylib
49+
└── other-versions/...
50+
```
51+
1852
## Usage
1953

2054
### Display Inline HTML:

src/webview/_webview_ffi.py

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pathlib import Path
77
from ctypes import c_int, c_char_p, c_void_p, CFUNCTYPE
88
import ctypes.util
9+
import shutil
910

1011
def _encode_c_string(s: str) -> bytes:
1112
return s.encode("utf-8")
@@ -14,6 +15,21 @@ def _get_webview_version():
1415
"""Get webview version from environment variable or use default"""
1516
return os.getenv("WEBVIEW_VERSION", "0.9.0")
1617

18+
def _get_download_base():
19+
"""Get download base URL or path from environment variable or use default.
20+
21+
This allows users to specify a custom location (URL or file path) to download
22+
libraries from, which is particularly useful for internal deployments or
23+
offline environments.
24+
25+
Returns:
26+
str: The base URL or path for downloading libraries
27+
"""
28+
return os.getenv(
29+
"WEBVIEW_DOWNLOAD_BASE",
30+
"https://github.com/webview/webview_deno/releases/download"
31+
)
32+
1733
def _get_lib_names():
1834
"""Get platform-specific library names."""
1935
system = platform.system().lower()
@@ -35,8 +51,18 @@ def _get_lib_names():
3551
def _get_download_urls():
3652
"""Get the appropriate download URLs based on the platform."""
3753
version = _get_webview_version()
38-
return [f"https://github.com/webview/webview_deno/releases/download/{version}/{lib_name}"
39-
for lib_name in _get_lib_names()]
54+
base_url = _get_download_base()
55+
56+
# Handle both URL and file path formats
57+
if base_url.startswith(("http://", "https://", "file://")):
58+
# For URLs, use / separator
59+
return [f"{base_url}/{version}/{lib_name}"
60+
for lib_name in _get_lib_names()]
61+
else:
62+
# For file paths, use OS-specific path handling
63+
base_path = Path(base_url)
64+
return [str(base_path / version / lib_name)
65+
for lib_name in _get_lib_names()]
4066

4167
def _be_sure_libraries():
4268
"""Ensure libraries exist and return paths."""
@@ -57,7 +83,7 @@ def _be_sure_libraries():
5783
if not missing_libs:
5884
return lib_paths
5985

60-
# Download missing libraries
86+
# Download or copy missing libraries
6187
download_urls = _get_download_urls()
6288
system = platform.system().lower()
6389

@@ -67,16 +93,30 @@ def _be_sure_libraries():
6793
if lib_path.exists():
6894
continue
6995

70-
print(f"Downloading library from {url}")
96+
print(f"Getting library from {url}")
7197
try:
72-
req = urllib.request.Request(
73-
url,
74-
headers={'User-Agent': 'Mozilla/5.0'}
75-
)
76-
with urllib.request.urlopen(req) as response, open(lib_path, 'wb') as out_file:
77-
out_file.write(response.read())
98+
# Handle different URL types
99+
if url.startswith(("http://", "https://")):
100+
# Web URL - download
101+
req = urllib.request.Request(
102+
url,
103+
headers={'User-Agent': 'Mozilla/5.0'}
104+
)
105+
with urllib.request.urlopen(req) as response, open(lib_path, 'wb') as out_file:
106+
out_file.write(response.read())
107+
elif url.startswith("file://"):
108+
# File URL - copy from local filesystem
109+
source_path = url[7:] # Strip 'file://' prefix
110+
shutil.copy2(source_path, lib_path)
111+
else:
112+
# Assume it's a filesystem path
113+
source_path = url
114+
if os.path.exists(source_path):
115+
shutil.copy2(source_path, lib_path)
116+
else:
117+
raise FileNotFoundError(f"Could not find library at {source_path}")
78118
except Exception as e:
79-
raise RuntimeError(f"Failed to download library: {e}")
119+
raise RuntimeError(f"Failed to get library from {url}: {e}")
80120

81121
return lib_paths
82122

0 commit comments

Comments
 (0)