11import os
2+ import json
3+ import shutil
24import subprocess
35
46from dotenvx import __file__ as package_path
57
8+ ERROR_MISSING_BINARY = "[MISSING_BINARY] missing dotenvx binary\n [MISSING_BINARY] https://github.com/dotenvx/dotenvx/issues/576"
9+
610def load_dotenvx ():
7- binary = dotenvx_binary ()
11+ output = get ()
12+
813 try :
9- return dotenvx_get (binary )
10- except FileNotFoundError :
11- print ("⚠️ 'dotenvx' binary not found. Attempting to install it now..." )
12- postinstall ()
13- return dotenvx_get (binary )
14-
15- def dotenvx_get (binary ):
16- result = subprocess .run (
17- [binary , "get" , "-pp" ],
14+ parsed = json .loads (output )
15+ for key , value in parsed .items ():
16+ os .environ [key ] = value
17+ return parsed
18+ except Exception as e :
19+ raise RuntimeError (f"Failed to parse dotenvx output: { e } " )
20+
21+ def get ():
22+ binpath = binary ()
23+ output = subprocess .run (
24+ [binpath , "get" , "-pp" ],
1825 capture_output = True ,
1926 text = True ,
2027 check = True
2128 )
22- return result .stdout .strip ()
29+ return output .stdout .strip ()
2330
24- def dotenvx_binary ():
31+ def binary ():
2532 local_bin = os .path .join (os .path .dirname (package_path ), "bin" , "dotenvx" )
26- if os .path .isfile (local_bin ) and os .access (local_bin , os .X_OK ):
27- return local_bin
33+ candidates = [local_bin , shutil .which ("dotenvx" )]
2834
29- system_bin = shutil .which ("dotenvx" )
30- if system_bin :
31- return system_bin
35+ for candidate in candidates :
36+ if candidate and os .path .isfile (candidate ) and os .access (candidate , os .X_OK ):
37+ if is_stub_file (candidate ):
38+ continue
39+ return candidate
3240
33- raise FileNotFoundError ("dotenvx binary not found locally or in PATH" )
41+ print ("[MISSING_BINARY] missing dotenvx binary" )
42+ print ("[MISSING_BINARY] https://github.com/dotenvx/dotenvx/issues/576" )
43+ raise SystemExit (1 )
44+
45+ def is_stub_file (path , max_stub_size = 1024 ):
46+ try :
47+ return os .path .getsize (path ) < max_stub_size
48+ except OSError :
49+ return False
3450
3551def postinstall ():
3652 bin_dir = os .path .join (os .path .dirname (package_path ), "bin" )
@@ -42,5 +58,4 @@ def postinstall():
4258 check = True
4359 )
4460 except subprocess .CalledProcessError as e :
45- print ("❌ Failed to install dotenvx binary." )
4661 raise SystemExit (e .returncode )
0 commit comments