Skip to content

Commit c668167

Browse files
authored
[CHANGED FILE/DIR: python/cargo_install.py] Added more commentaries and recursive searching Cargo.toml file (#52)
2 parents e071fbc + c53a425 commit c668167

File tree

1 file changed

+53
-19
lines changed

1 file changed

+53
-19
lines changed

python/cargo_install.py

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,66 @@
55
from subprocess import run
66
from subprocess import CalledProcessError
77

8+
def find_cargo_toml(start_dir='.'):
9+
"""Search for Cargo.toml starting from specified directory"""
10+
# Check current directory first
11+
cargo_path = os.path.join(start_dir, 'Cargo.toml')
12+
if os.path.exists(cargo_path):
13+
return cargo_path
14+
15+
# If not found, search parent directories
16+
current_dir = os.path.abspath(start_dir)
17+
while True:
18+
parent_dir = os.path.dirname(current_dir)
19+
if parent_dir == current_dir: # Reached root
20+
break
21+
22+
cargo_path = os.path.join(parent_dir, 'Cargo.toml')
23+
if os.path.exists(cargo_path):
24+
return cargo_path
25+
26+
current_dir = parent_dir
27+
28+
return None
829

930
def cargo_install(lib_name):
10-
cargo_path = "Cargo.toml"
11-
if not os.path.exists(cargo_path):
12-
print("❌ Cargo.toml not found in expected location.")
31+
# Find Cargo.toml
32+
cargo_path = find_cargo_toml()
33+
if not cargo_path:
34+
print("❌ Cargo.toml not found in current or parent directories.")
1335
return
1436

15-
# Start installing from Cargo
16-
print(f"🔧 Running cargo add {lib_name} ...")
17-
try:
18-
result = run(
19-
["cargo", "add", lib_name],
20-
check=True,
21-
capture_output=True,
22-
text=True,
23-
)
24-
print("📦 Cargo output:\n", result.stdout)
37+
# Get absolute paths for better error reporting
38+
abs_cargo_path = os.path.abspath(cargo_path)
39+
current_dir = os.getcwd()
40+
41+
print(f"📁 Found Cargo.toml at: {abs_cargo_path}")
42+
print(f"📍 Current directory: {current_dir}")
2543

26-
except CalledProcessError as e:
27-
print(f"❌ Failed to install {lib_name}")
28-
print("🔻 stdout:\n", e.stdout)
29-
print("🔻 stderr:\n", e.stderr)
44+
# Change to Cargo.toml's directory temporarily
45+
original_dir = os.getcwd()
46+
try:
47+
os.chdir(os.path.dirname(abs_cargo_path))
48+
49+
# Install the dependency
50+
print(f"🔧 Running cargo add {lib_name} ...")
51+
try:
52+
result = run(
53+
["cargo", "add", lib_name],
54+
check=True,
55+
capture_output=True,
56+
text=True,
57+
)
58+
print("📦 Cargo output:\n", result.stdout)
3059

31-
print("🔚 Return code:", e.returncode)
60+
except CalledProcessError as e:
61+
print(f"❌ Failed to install {lib_name}")
62+
print("🔻 stdout:\n", e.stdout)
63+
print("🔻 stderr:\n", e.stderr)
64+
print("🔚 Return code:", e.returncode)
3265

66+
finally:
67+
os.chdir(original_dir) # Restore original directory
3368

3469
def main():
3570
if len(sys.argv) < 2:
@@ -39,6 +74,5 @@ def main():
3974
for lib in sys.argv[1:]:
4075
cargo_install(lib)
4176

42-
4377
if __name__ == "__main__":
4478
main()

0 commit comments

Comments
 (0)