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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461 | @define
class ProjectManager:
path: Path
name: str = "Unknown"
cli: BrokenTyper = None
# # Main entry point
def main(self, ctx: Context) -> None:
self.cli = BrokenTyper(help=False)
self.cli.command(self.update)
self.cli.command(self.compile)
self.cli.command(self.run, context=True)
with BrokenPath.pushd(self.path, echo=False):
self.cli(*ctx.args)
# # Initialization
def __attrs_post_init__(self):
self.name = self.path.name
def __eq__(self, other: Self) -> bool:
return self.path == other.path
# # Utility Attributes
@property
def version(self) -> str:
import arrow
now = arrow.utcnow().format("YYYY.M.D")
return self.config.setdefault("version", now)
@property
def description(self) -> str:
description = ""
# Read Python's pyproject.toml
if (config := self.path/"pyproject.toml").exists():
description = (
toml.loads(config.read_text())
.get("project", {})
.get("description", "")
)
# Read Rust's Cargo.toml
elif (config := self.path/"Cargo.toml").exists():
description = (
toml.loads(config.read_text())
.get("package", {})
.get("description", "")
)
return description
@property
def languages(self) -> set[ProjectLanguage]:
languages = set()
# Best attempts to detect language
if (self.path/"pyproject.toml").exists():
languages.add(ProjectLanguage.Python)
elif (self.path/"Cargo.toml").exists():
languages.add(ProjectLanguage.Rust)
elif (self.path/"meson.build").exists():
languages.add(ProjectLanguage.CPP)
else:
languages.add(ProjectLanguage.Unknown)
return languages
@property
def pyproject(self) -> DotMap:
return DotMap(toml.loads((self.path/"pyproject.toml").read_text()))
@property
def cargo_toml(self) -> DotMap:
return DotMap(toml.loads((self.path/"Cargo.toml").read_text()))
@property
def _pretty_language(self) -> str:
if self.is_python: return f"🐍 (Python) {self.description}"
if self.is_nodejs: return f"🟢 (NodeJS) {self.description}"
if self.is_rust: return f"🦀 (Rust ) {self.description}"
if self.is_cpp: return f"🌀 (C/C++ ) {self.description}"
return self.description
# Shorthands for project language
@property
def is_known(self) -> bool:
return ProjectLanguage.Unknown not in self.languages
@property
def is_python(self) -> bool:
return ProjectLanguage.Python in self.languages
@property
def is_nodejs(self) -> bool:
return ProjectLanguage.NodeJS in self.languages
@property
def is_rust(self) -> bool:
return ProjectLanguage.Rust in self.languages
@property
def is_cpp(self) -> bool:
return ProjectLanguage.CPP in self.languages
# # Commands
def update(self) -> None:
"""✨ Update this project's dependencies"""
if self.is_python:
outdated = shell("uv", "pip", "list", "--outdated", "--format=json", output=True)
pyproject = (self.path/"pyproject.toml").read_text("utf8")
# Replaces any package version of '~=', '>=', '^=' with latest
for package in map(DotMap, json.loads(outdated)):
pyproject = re.sub(
rf'({re.escape(package.name)}(?:\[[^\]]+\])?\s*(?:~=|>=|\^))\s*([^\"]*)"',
rf'\g<1>{package.latest_version}"',
pyproject
)
# Write changes
(self.path/"pyproject.toml").write_text(pyproject, "utf8")
shell("uv", "sync", "--all-packages")
if self.is_nodejs:
shell("pnpm", "update")
if self.is_rust:
shell("cargo", "update")
if self.is_cpp:
log.error("C++ projects are not supported yet")
def run(self, ctx: Context,
loop: Annotated[bool, Option("--loop", help="Press Enter after each run to run again")]=False,
clear: Annotated[bool, Option("--clear", help="Clear terminal before running")]=False,
debug: Annotated[bool, Option("--debug", help="Debug mode for Rust projects")]=False,
) -> None:
"""🔥 Run this project with all arguments that follow"""
while True:
BrokenPlatform.clear_terminal() if clear else None
if self.is_python:
log.info(f"Hey! Just type '{self.name.lower()}' to run the project directly, it's faster 😉")
return
elif self.is_rust:
raise RuntimeError(log.error("Rust projects are not supported yet"))
_status = shell(
"cargo", "run",
"--bin", self.name,
["--profile", "release"] if not debug else [],
"--features", self.rust_features,
"--", ctx.args
)
elif self.is_cpp:
BUILD_DIR = BROKEN.DIRECTORIES.REPO_BUILD/self.name
if shell("meson", BUILD_DIR, "--reconfigure", "--buildtype", "release").returncode != 0:
exit(log.error(f"Could not build project ({self.name})") or 1)
if shell("ninja", "-C", BUILD_DIR).returncode != 0:
exit(log.error(f"Could not build project ({self.name})") or 1)
binary = next(BUILD_DIR.glob(f"{self.name.lower()}"))
shell(binary, ctx.args)
if not loop:
break
import rich.prompt
log.success(f"Project ({self.name}) finished successfully")
if not rich.prompt.Confirm.ask("(Infinite mode) Press Enter to run again", default=True):
break
# # Python shenanigans
def compile(self,
target: Annotated[list[PlatformEnum],
Option("--target", "-t",
help="Target platforms to build binaries for"
)] = [BrokenPlatform.Host],
tarball: Annotated[bool,
Option("--tarball", "-z",
help="Create a compressed tarball archive for unix releases",
)] = False,
standalone: Annotated[bool,
Option("--standalone", "-s",
help="(Standalone) Create self-contained distributions with all dependencies",
)] = False,
torch: Annotated[Optional[TorchRelease],
Option("--torch", "-r",
help="(Standalone) Bundle a specific PyTorch version with the project"
)] = None,
) -> None:
"""
📦 Release the Project as a distributable binary
Note:
- Requires mingw packages for Windows cross compilation from Linux
"""
# Recurse on each target item
if isinstance(target, list):
for target in flatten(map(PlatformEnum.get_all, target)):
ProjectManager.compile(**locals())
return None
# Filter invalid host -> target combinations of all targets
if BrokenPlatform.OnLinux and (target.system == SystemEnum.MacOS):
return log.skip(f"Linux can't [italic]easily[/] compile for {target.system}")
elif BrokenPlatform.OnMacOS and (target.system != SystemEnum.MacOS):
return log.skip("macOS can only [italic]easily[/] compile for itself")
elif BrokenPlatform.OnWindows and (target.system != SystemEnum.Windows):
return log.skip("Windows can only [italic]easily[/] compile for itself")
elif (target == PlatformEnum.WindowsARM64):
return log.skip("Windows on ARM is not widely supported")
# Automatically bundle some torch on projects that needs it
if (self.name == "DepthFlow"):
torch = (torch or SimpleTorch.CPU.value)
# Non-macOS ARM builds can be unstable/not tested, disable on CI
if (target.arch.is_arm() and (target.system != SystemEnum.MacOS)):
log.warning("ARM general support is only present in macOS")
# Fixme: Wait for uv's implementation of pip wheel for my own sanity
if (standalone and target != BrokenPlatform.Host):
log.error("Standalone releases are best built in a host matching the target platform")
log.error("• Awaiting implementation of (https://github.com/astral-sh/uv/issues/1681)")
log.error(f"• Attempted to build for '{target.value}' on '{BrokenPlatform.Host.value}'")
return
log.note("Building Project Release for", target)
if self.is_python:
BrokenManager.rust()
BUILD_DIR: Path = BROKEN.DIRECTORIES.REPO_BUILD/"Cargo"
BUILD_WHL: Path = BROKEN.DIRECTORIES.BUILD_WHEELS
PYTHON_VERSION: str = "3.12"
# Remove previous build cache for pyapp
for path in BUILD_DIR.rglob("pyapp*"):
BrokenPath.remove(path)
# Write a releases env config file
(RELEASE_ENV := BROKEN.RESOURCES.ROOT/"Release.env").write_text('\n'.join(
f"{key}={value}" for key, value in dict(
# Placeholder
).items()
))
# Build wheels, find main and extra ones
Environment.set("PYAPP_RELEASE", 1)
WHEELS = BrokenManager().pypi(all=True)
MAIN = next(WHEELS.glob("broken_source*"))
EXTRA = set(WHEELS.glob("*.whl")) - {MAIN}
if (standalone):
# Fixme: Improve this with (https://github.com/astral-sh/uv/issues/1681)
def fetch_wheel(
dependencies: Union[str, list[str]],
index: Optional[str]=None,
nodeps: bool=True,
) -> None:
if (returncode := shell(
sys.executable, "-m", "pip", "download", dependencies,
(("--platform", x) for x in target.pip_platform),
"--python-version", PYTHON_VERSION,
"--only-binary=:all:"*(not nodeps),
"--no-deps"*(nodeps),
"--prefer-binary",
every("--index", index),
"--dest", BUILD_WHL,
).returncode) != 0:
log.error(f"Failed to download dependency ({dependencies})")
exit(returncode)
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=10) as pool:
for dependency in filter(None, shell(
"uv", "export", "--all-packages",
"--format", "requirements-txt",
"--no-editable", "--no-hashes",
"--no-header", "--no-dev",
output=True
).splitlines()):
# Skip editable packages
if (dependency.startswith(".")):
continue
# Skip audioop on Python 3.13+ as it was dropped from stdlib
if (PYTHON_VERSION == "3.13") and ("audioop" in dependency):
continue
# Ignore platform constraints
dependency = dependency.split(";")[0]
pool.submit(fetch_wheel, dependency)
# Add all dependencies wheels and sdists to the extra list
EXTRA |= set(BUILD_WHL.glob("*.whl")) - (EXTRA | {MAIN})
EXTRA |= set(BUILD_WHL.glob("*.tar.gz"))
# Why PyTorch can't be normal?
if bool(torch):
# Help the linker deal with 3.2 GB Torch CUDA binaries..
Environment.append("RUSTFLAGS", "-C code-model=large")
fetch_wheel(
dependencies=torch.packages,
index=torch.index,
nodeps=False
)
# Remove new duplicate and list them on extra wheels
for file in set(BUILD_WHL.iterdir()) - (EXTRA | {MAIN}):
# Note: Need case insensitive enabled due shit like this:
# - https://pypi.org/project/Jinja2/3.1.4/#jinja2-3.1.4-py3-none-any.whl
# - https://download.pytorch.org/whl/Jinja2-3.1.4-py3-none-any.whl
duplicates = list(BUILD_WHL.glob(
pattern=f"{file.name.split("-")[0]}-*",
case_sensitive=False
))
if len(duplicates) > 1:
log.info(f"Removing duplicate: {file}")
file.unlink()
continue
EXTRA |= {file}
# Pyapp configuration
Environment.update(
PYAPP_PROJECT_PATH=str(MAIN),
PYAPP_EXTRA_WHEELS=";".join(map(str, EXTRA)),
PYAPP_PIP_EXTRA_ARGS=("--no-deps"*standalone),
PYAPP_PYTHON_VERSION=PYTHON_VERSION,
PYAPP_EXEC_MODULE=self.name,
PYAPP_DISTRIBUTION_EMBED=1,
PYAPP_PASS_LOCATION=1,
PYAPP_UV_ENABLED=1,
PYAPP_UV_EMBED=1,
)
# Rust configuration
Environment.update(
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=shutil.which("aarch64-linux-gnu-gcc"),
)
# Cache Rust compilation across projects
Environment.set("CARGO_HOME", BUILD_DIR)
shell("rustup", "target", "add", target.triple)
# Cargo warning: We're not 'installing' a utility
BrokenPath.add_to_path(BUILD_DIR/"bin")
if (_PYAPP_FORK := True):
if not (fork := BROKEN.DIRECTORIES.REPO_BUILD/"PyApp").exists():
shell("git", "clone", "https://github.com/BrokenSource/PyApp", fork, "-b", "custom")
embed = (fork/"src"/"embed")
# Remove previous embeddings if any
for file in embed.glob("*.whl"):
file.unlink()
for file in embed.glob("*.tar.gz"):
file.unlink()
# Actually compile it
if shell(
"cargo", "install",
"--path", fork, "--force",
"--root", BUILD_DIR,
"--target", target.triple,
).returncode != 0:
raise RuntimeError(log.error("Failed to compile PyApp"))
else:
if shell(
"cargo", "install",
"pyapp", "--force",
"--root", BUILD_DIR,
"--target", target.triple,
).returncode != 0:
raise RuntimeError(log.error("Failed to compile PyApp"))
RELEASE_ENV.unlink()
# Find the compiled binary
binary = next((BUILD_DIR/"bin").glob("pyapp*"))
log.info(f"Compiled Pyapp binary at ({binary})")
BrokenPath.make_executable(binary)
# Rename the compiled binary to the final release name
release_path = BROKEN.DIRECTORIES.REPO_RELEASES / ''.join((
f"{self.name.lower()}",
f"-{target.value}",
f"-v{BROKEN.VERSION}",
f"-{torch.flavor}" if torch else "",
"-standalone"*standalone,
f"{target.extension}",
))
BrokenPath.copy(src=binary, dst=release_path)
BrokenPath.make_executable(release_path)
# Release a tar.gz to keep chmod +x attributes
if tarball and ("windows" not in target.name):
release_path = BrokenPath.gzip(release_path, remove=True)
log.success(f"Built Project Release at ({release_path})")
|