1818import sys
1919import tempfile
2020import time
21- import uuid
2221from pathlib import Path
2322import pytest
2423
3534
3635# Module-level cache for binary path
3736_forumdl_binary_path = None
38- _forumdl_lib_root = None
3937
4038
4139def require_forumdl_binary () -> str :
@@ -49,95 +47,41 @@ def require_forumdl_binary() -> str:
4947 return binary_path
5048
5149
52- def get_forumdl_binary_path ():
53- """Get the installed forum-dl binary path from cache or by running installation ."""
50+ def get_forumdl_binary_path () -> str | None :
51+ """Get forum-dl binary path, installing via abx_pkg if needed ."""
5452 global _forumdl_binary_path
5553 if _forumdl_binary_path :
5654 return _forumdl_binary_path
5755
58- # Try to find forum-dl binary using abx-pkg
5956 from abx_pkg import Binary , PipProvider , EnvProvider
6057
61- try :
62- binary = Binary (
63- name = "forum-dl" , binproviders = [PipProvider (), EnvProvider ()]
64- ).load ()
65-
66- if binary and binary .abspath :
67- _forumdl_binary_path = str (binary .abspath )
68- return _forumdl_binary_path
69- except Exception :
70- pass
71-
72- # If not found, try to install via pip using the crawl hook overrides
73- pip_hook = PLUGINS_ROOT / "pip" / "on_Binary__11_pip_install.py"
74- crawl_hook = next (PLUGIN_DIR .glob ("on_Crawl__25_forumdl_install*.py" ), None )
75- if pip_hook .exists ():
76- binary_id = str (uuid .uuid4 ())
77- machine_id = str (uuid .uuid4 ())
78- overrides = None
79-
80- if crawl_hook and crawl_hook .exists ():
81- crawl_result = subprocess .run (
82- [str (crawl_hook )],
83- capture_output = True ,
84- text = True ,
85- timeout = 30 ,
86- )
87- for crawl_line in crawl_result .stdout .strip ().split ("\n " ):
88- if crawl_line .strip ().startswith ("{" ):
89- try :
90- crawl_record = json .loads (crawl_line )
91- if (
92- crawl_record .get ("type" ) == "Binary"
93- and crawl_record .get ("name" ) == "forum-dl"
94- ):
95- overrides = crawl_record .get ("overrides" )
96- break
97- except json .JSONDecodeError :
98- continue
99-
100- # Create a persistent temp HOME for default LIB_DIR usage
101- global _forumdl_lib_root
102- if not _forumdl_lib_root :
103- _forumdl_lib_root = tempfile .mkdtemp (prefix = "forumdl-lib-" )
104- env = os .environ .copy ()
105- env ["HOME" ] = str (_forumdl_lib_root )
106- env ["SNAP_DIR" ] = str (Path (_forumdl_lib_root ) / "data" )
107- env .pop ("LIB_DIR" , None )
108-
109- cmd = [str (pip_hook ),
110- "--binary-id" ,
111- binary_id ,
112- "--machine-id" ,
113- machine_id ,
114- "--name" ,
115- "forum-dl" ,
116- ]
117- if overrides :
118- cmd .append (f"--overrides={ json .dumps (overrides )} " )
119-
120- install_result = subprocess .run (
121- cmd ,
122- capture_output = True ,
123- text = True ,
124- timeout = 300 ,
125- env = env ,
126- )
127-
128- # Parse Binary from pip installation
129- for install_line in install_result .stdout .strip ().split ("\n " ):
130- if install_line .strip ():
131- try :
132- install_record = json .loads (install_line )
133- if (
134- install_record .get ("type" ) == "Binary"
135- and install_record .get ("name" ) == "forum-dl"
136- ):
137- _forumdl_binary_path = install_record .get ("abspath" )
138- return _forumdl_binary_path
139- except json .JSONDecodeError :
140- pass
58+ binary = Binary (
59+ name = "forum-dl" ,
60+ binproviders = [PipProvider (), EnvProvider ()],
61+ overrides = {
62+ "pip" : {
63+ "install_args" : [
64+ "--no-deps" ,
65+ "--prefer-binary" ,
66+ "forum-dl" ,
67+ "chardet==5.2.0" ,
68+ "beautifulsoup4" ,
69+ "soupsieve" ,
70+ "lxml" ,
71+ "requests" ,
72+ "urllib3" ,
73+ "tenacity" ,
74+ "python-dateutil" ,
75+ "six" ,
76+ "html2text" ,
77+ "warcio" ,
78+ ]
79+ }
80+ },
81+ ).load_or_install ()
82+ if binary and binary .abspath :
83+ _forumdl_binary_path = str (binary .abspath )
84+ return _forumdl_binary_path
14185
14286 return None
14387
0 commit comments