Skip to content

Commit 6ba5746

Browse files
committed
wip
1 parent a20534f commit 6ba5746

7 files changed

Lines changed: 74 additions & 23 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,4 @@ dmypy.json
132132
.vscode/
133133

134134
test
135+
.DS_Store

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
include CHANGELOG.md
2+
include src/dotenvx/bin/dotenvx

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ Install and use it in code just like `python-dotenv`.
2525
```sh
2626
pip install python-dotenvx
2727
```
28+
29+
Then run `dotenvx-postinstall` to install the `dotenvx` binary.
30+
31+
```sh
32+
dotenvx-postinstall
33+
```
34+
2835
```python
2936
# main.py
3037
import os

setup.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import os
2-
import subprocess
32
from setuptools import setup, find_packages
4-
from setuptools.command.install import install
53

64
src = {}
75
dir = os.path.abspath(os.path.dirname(__file__))
@@ -17,25 +15,6 @@ def read_files(files):
1715

1816
readme = read_files(['README.md', 'CHANGELOG.md'])
1917

20-
class InstallBinary(install):
21-
def run(self):
22-
print("installing dotenvx........")
23-
24-
bin_dir = os.path.join(dir, 'src', 'dotenvx', 'bin')
25-
os.makedirs(bin_dir, exist_ok=True)
26-
27-
# install dotenvx binary using your install script into bin/
28-
subprocess.run(
29-
['sh', '-c', f'curl -sfS "https://dotenvx.sh?directory={bin_dir}" | sh'],
30-
check=True,
31-
stdout=None,
32-
stderr=None
33-
)
34-
35-
print("installed dotenvx........")
36-
37-
super().run()
38-
3918
setup(
4019
name='python-dotenvx',
4120
description=src['__description__'],
@@ -61,5 +40,9 @@ def run(self):
6140
],
6241
install_requires=[
6342
],
64-
cmdclass={'install': InstallBinary},
43+
entry_points={
44+
'console_scripts': [
45+
'dotenvx-postinstall=dotenvx.main:postinstall',
46+
],
47+
}
6548
)

src/dotenvx/bin/.gitkeep

Whitespace-only changes.

src/dotenvx/bin/dotenvx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
echo "📦 Self-boostrapping dotenvx binary..."
3+
4+
# Directory of this script (e.g. site-packages/dotenvx/bin)
5+
SELF_PATH="$(cd "$(dirname "$0")" && pwd)"
6+
TARGET="$SELF_PATH/dotenvx"
7+
8+
# Install binary
9+
curl -sfS "https://dotenvx.sh?directory=$SELF_PATH" | sh
10+
11+
# Make sure it's executable
12+
chmod +x "$TARGET"
13+
14+
# Re-exec with original arguments
15+
exec "$TARGET" "$@"

src/dotenvx/main.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,46 @@
1+
import os
2+
import subprocess
3+
4+
from dotenvx import __file__ as package_path
5+
16
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

Comments
 (0)