66from pathlib import Path
77from ctypes import c_int , c_char_p , c_void_p , CFUNCTYPE
88import ctypes .util
9+ import shutil
910
1011def _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+
1733def _get_lib_names ():
1834 """Get platform-specific library names."""
1935 system = platform .system ().lower ()
@@ -35,8 +51,18 @@ def _get_lib_names():
3551def _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
4167def _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