|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | + |
| 4 | +from dotenvx import __file__ as package_path |
| 5 | + |
1 | 6 | def load_dotenvx(): |
2 | | - raise NotImplementedError("go to [github.com/dotenvx/dotenvx] and follow python directions there") |
| 7 | + binary = dotenvx_binary() |
| 8 | + 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"], |
| 18 | + capture_output=True, |
| 19 | + text=True, |
| 20 | + check=True |
| 21 | + ) |
| 22 | + return result.stdout.strip() |
| 23 | + |
| 24 | +def dotenvx_binary(): |
| 25 | + 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 |
| 28 | + |
| 29 | + system_bin = shutil.which("dotenvx") |
| 30 | + if system_bin: |
| 31 | + return system_bin |
| 32 | + |
| 33 | + raise FileNotFoundError("dotenvx binary not found locally or in PATH") |
| 34 | + |
| 35 | +def postinstall(): |
| 36 | + bin_dir = os.path.join(os.path.dirname(package_path), "bin") |
| 37 | + os.makedirs(bin_dir, exist_ok=True) |
| 38 | + |
| 39 | + try: |
| 40 | + subprocess.run( |
| 41 | + ["sh", "-c", f"curl -sfS https://dotenvx.sh?directory={bin_dir} | sh"], |
| 42 | + check=True |
| 43 | + ) |
| 44 | + except subprocess.CalledProcessError as e: |
| 45 | + print("❌ Failed to install dotenvx binary.") |
| 46 | + raise SystemExit(e.returncode) |
0 commit comments