diff --git a/cutekit/builder.py b/cutekit/builder.py index 8b45719..f75b1c0 100644 --- a/cutekit/builder.py +++ b/cutekit/builder.py @@ -354,6 +354,7 @@ def _(args: cli.Args): @cli.command("r", "builder/run", "Run a component") def runCmd(args: cli.Args): debug = args.consumeOpt("debug", False) is True + profile = args.consumeOpt("profile", False) is True wait = args.consumeOpt("wait", False) is True debugger = args.consumeOpt("debugger", "lldb") componentSpec = args.consumeArg() or "__main__" @@ -390,6 +391,29 @@ def runCmd(args: cli.Args): ) else: raise RuntimeError(f"Unknown debugger {debugger}") + elif profile: + # Profile the executable using perf + shell.exec( + "perf", + "record", + "-g", + "-o", + "perf.data", + "--call-graph", + "dwarf", + str(object=product.path), + *args.extra, + ) + + # Run this command using subprocess + # perf script -i perf.data | speedscope - + import subprocess + + proc = subprocess.Popen( + ["perf", "script", "-i", "perf.data"], stdout=subprocess.PIPE + ) + subprocess.run(["speedscope", "-"], stdin=proc.stdout) + proc.wait() else: shell.exec(str(product.path), *args.extra) diff --git a/cutekit/shell.py b/cutekit/shell.py index 17effa9..9c93084 100644 --- a/cutekit/shell.py +++ b/cutekit/shell.py @@ -101,7 +101,10 @@ def rmrf(path: str) -> bool: if not os.path.exists(path): return False - shutil.rmtree(path, ignore_errors=True) + if os.path.isfile(path): + os.remove(path) + else: + shutil.rmtree(path, ignore_errors=True) return True @@ -285,20 +288,49 @@ def nproc() -> int: return os.cpu_count() or 1 -def compress(path: str, dest: Optional[str] = None, format: str = "zstd") -> str: +def gzip(path: str, dest: Optional[str] = None) -> str: """ Compress a file or directory """ if dest is None: - dest = path + "." + format + dest = path + ".gz" + + with open(dest, "wb") as f: + proc = subprocess.run( + ["gzip", "-c", path], + stdout=f, + stderr=sys.stderr, + ) + + if proc.returncode != 0: + raise RuntimeError(f"gzip: Process exited with code {proc.returncode}") + + return dest + + +def compress(path: str, dest: Optional[str] = None, format: str = "zstd") -> str: + """ + Compress a file or directory + """ + + EXTS = { + "zip": "zip", + "zstd": "zst", + "gzip": "gz", + } + + if dest is None: + dest = path + "." + EXTS[format] _logger.debug(f"Compressing {path} to {dest}") if format == "zip": exec("zip", "-r", dest, path) elif format == "zstd": - exec("zsdt", "-q", "-o", dest, path) + exec("zstd", "-q", "-o", dest, path) + elif format == "gzip": + gzip(path, dest) else: raise RuntimeError(f"Unknown compression format {format}")