-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_utils.py
More file actions
executable file
·179 lines (142 loc) · 4.53 KB
/
example_utils.py
File metadata and controls
executable file
·179 lines (142 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
"""
Example Python utilities for devbox-config environment.
This script demonstrates various Python capabilities and patterns
useful in a development environment.
"""
import json
import subprocess
import sys
from pathlib import Path
from typing import Dict, List, Optional
def run_command(cmd: List[str]) -> Dict[str, any]:
"""
Execute a shell command and return results.
Args:
cmd: Command as list of strings
Returns:
Dict with stdout, stderr, and return code
"""
try:
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=30
)
return {
"stdout": result.stdout,
"stderr": result.stderr,
"returncode": result.returncode,
"success": result.returncode == 0
}
except subprocess.TimeoutExpired:
return {
"stdout": "",
"stderr": "Command timed out",
"returncode": -1,
"success": False
}
except Exception as e:
return {
"stdout": "",
"stderr": str(e),
"returncode": -1,
"success": False
}
def check_devbox_packages() -> List[Dict[str, str]]:
"""
Read devbox.json and list installed packages.
Returns:
List of package information dictionaries
"""
devbox_json = Path(__file__).parent.parent / "devbox.json"
if not devbox_json.exists():
print(f"Error: {devbox_json} not found", file=sys.stderr)
return []
with open(devbox_json, 'r') as f:
config = json.load(f)
packages = config.get("packages", [])
return [{"name": pkg.split("@")[0], "version": pkg.split("@")[1] if "@" in pkg else "latest"}
for pkg in packages]
def find_files(directory: Path, pattern: str = "*") -> List[Path]:
"""
Recursively find files matching a pattern.
Args:
directory: Directory to search
pattern: Glob pattern to match
Returns:
List of matching file paths
"""
return list(Path(directory).rglob(pattern))
def format_size(bytes: int) -> str:
"""
Format bytes to human-readable size.
Args:
bytes: Size in bytes
Returns:
Formatted string (e.g., "1.5 MB")
"""
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
if bytes < 1024.0:
return f"{bytes:.1f} {unit}"
bytes /= 1024.0
return f"{bytes:.1f} PB"
def analyze_directory(path: str = ".") -> Dict[str, any]:
"""
Analyze a directory and return statistics.
Args:
path: Directory path to analyze
Returns:
Dictionary with directory statistics
"""
directory = Path(path)
if not directory.exists():
return {"error": "Directory does not exist"}
files = list(directory.rglob("*"))
total_size = sum(f.stat().st_size for f in files if f.is_file())
extensions = {}
for f in files:
if f.is_file():
ext = f.suffix or "no_extension"
extensions[ext] = extensions.get(ext, 0) + 1
return {
"path": str(directory.absolute()),
"total_files": len([f for f in files if f.is_file()]),
"total_dirs": len([f for f in files if f.is_dir()]),
"total_size": format_size(total_size),
"file_types": extensions
}
def main():
"""Main function demonstrating the utilities."""
print("=== Devbox Configuration Python Example ===\n")
# Example 1: Check devbox packages
print("1. Installed Devbox Packages:")
packages = check_devbox_packages()
for pkg in packages[:5]: # Show first 5
print(f" - {pkg['name']} ({pkg['version']})")
if len(packages) > 5:
print(f" ... and {len(packages) - 5} more packages")
print()
# Example 2: Run a simple command
print("2. System Information:")
result = run_command(["python", "--version"])
if result["success"]:
print(f" {result['stdout'].strip()}")
print()
# Example 3: Analyze current directory
print("3. Directory Analysis (python/):")
stats = analyze_directory("python")
if "error" not in stats:
print(f" Total files: {stats['total_files']}")
print(f" Total size: {stats['total_size']}")
print()
# Example 4: Find Python files
print("4. Finding Python files:")
py_files = find_files(Path(__file__).parent.parent, "*.py")
for py_file in py_files:
print(f" - {py_file}")
print()
print("=== Example Complete ===")
if __name__ == "__main__":
main()