Datasets:
Download code/remote_files.py from 0721alex/a-plan-is-not-a-path: direct link, hf CLI and curl.
- Browser
- Download file 5.55 kB
-
https://huggingface.co/datasets/0721alex/a-plan-is-not-a-path/resolve/main/code/remote_files.py
- Command line
-
hf download hf://datasets/0721alex/a-plan-is-not-a-path/code/remote_files.py
-
curl -L -o remote_files.py https://huggingface.co/datasets/0721alex/a-plan-is-not-a-path/resolve/main/code/remote_files.py
5.55 kB
| """Push/pull experiment files through the skill's marimo scratchpad client. | |
| This helper does not launch experiments or retry execution. Model downloads | |
| remain remote. Transfers are ordinary notebook code, with no SSH or service. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import base64 | |
| import io | |
| import json | |
| import os | |
| from pathlib import Path | |
| import shutil | |
| import subprocess | |
| import sys | |
| import zipfile | |
| SKILL_REMOTE = (Path(os.environ.get("CODEX_HOME", str(Path.home() / ".codex"))) | |
| / "skills/molab-marimo/scripts/molab_remote.py") | |
| def execute(code, state_file, capture=False): | |
| command = [sys.executable, str(SKILL_REMOTE), "exec", "--state-file", state_file] | |
| return subprocess.run(command, input=code, text=True, capture_output=capture, check=True) | |
| def pull_archive(paths, destination, args): | |
| code = f'''import base64, io, zipfile | |
| from pathlib import Path | |
| _root = Path({args.remote_root!r}).resolve() | |
| _buffer = io.BytesIO() | |
| with zipfile.ZipFile(_buffer, "w", compression=zipfile.ZIP_DEFLATED) as _archive: | |
| for _name in {paths!r}: | |
| _path = _root / _name | |
| if _path.is_dir(): | |
| for _item in sorted(_path.rglob("*")): | |
| if _item.is_file(): | |
| _archive.write(_item, str(_item.relative_to(_root))) | |
| else: | |
| _archive.write(_path, _name) | |
| print("MOLAB_FILES_BEGIN", flush=True) | |
| _encoded = base64.b64encode(_buffer.getvalue()).decode("ascii") | |
| for _offset in range(0, len(_encoded), 32768): | |
| print(_encoded[_offset:_offset + 32768], flush=True) | |
| print("MOLAB_FILES_END", flush=True) | |
| ''' | |
| result = execute(code, args.state_file, capture=True) | |
| encoded = result.stdout.split("MOLAB_FILES_BEGIN\n", 1)[1].split("\nMOLAB_FILES_END", 1)[0] | |
| encoded = "".join(encoded.split()) | |
| destination.mkdir(parents=True, exist_ok=True) | |
| with zipfile.ZipFile(io.BytesIO(base64.b64decode(encoded))) as archive: | |
| archive.extractall(destination) | |
| print("Retrieved", len(archive.namelist()), "files into", destination, flush=True) | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--state-file", default="work/molab-state.json") | |
| parser.add_argument("--remote-root", default="molab_runs/resource_planning") | |
| commands = parser.add_subparsers(dest="command", required=True) | |
| push = commands.add_parser("push") | |
| push.add_argument("paths", nargs="+") | |
| pull = commands.add_parser("pull") | |
| pull.add_argument("paths", nargs="+") | |
| pull.add_argument("--destination", default="work/remote") | |
| pull_jsonl = commands.add_parser("pull-jsonl") | |
| pull_jsonl.add_argument("path") | |
| pull_jsonl.add_argument("--destination", default="work/remote") | |
| pull_jsonl.add_argument("--lines-per-part", type=int, default=64) | |
| args = parser.parse_args() | |
| if args.command == "push": | |
| buffer = io.BytesIO() | |
| with zipfile.ZipFile(buffer, "w", compression=zipfile.ZIP_DEFLATED) as archive: | |
| for raw_path in args.paths: | |
| path = Path(raw_path) | |
| if path.is_dir(): | |
| for item in sorted(path.rglob("*")): | |
| if item.is_file() and "__pycache__" not in item.parts: | |
| archive.write(item, str(item)) | |
| else: | |
| archive.write(path, str(path)) | |
| encoded = base64.b64encode(buffer.getvalue()).decode("ascii") | |
| code = f'''import base64, io, zipfile | |
| from pathlib import Path | |
| _root = Path({args.remote_root!r}).resolve() | |
| _root.mkdir(parents=True, exist_ok=True) | |
| with zipfile.ZipFile(io.BytesIO(base64.b64decode({encoded!r}))) as _archive: | |
| _archive.extractall(_root) | |
| print("Transferred", len(_archive.namelist()), "files to", str(_root), flush=True) | |
| ''' | |
| execute(code, args.state_file) | |
| elif args.command == "pull": | |
| pull_archive(args.paths, Path(args.destination), args) | |
| else: | |
| if args.lines_per_part < 1: | |
| raise ValueError("lines-per-part must be positive") | |
| part_root = str(Path(".transfer_parts") / args.path) | |
| code = f'''import json | |
| from pathlib import Path | |
| _root = Path({args.remote_root!r}).resolve() | |
| _parts = _root / {part_root!r} | |
| _parts.mkdir(parents=True, exist_ok=True) | |
| _paths = [] | |
| _part = None | |
| try: | |
| with (_root / {args.path!r}).open("rb") as _source: | |
| for _index, _line in enumerate(_source): | |
| if _index % {args.lines_per_part} == 0: | |
| if _part is not None: | |
| _part.close() | |
| _path = _parts / ("part%05d" % len(_paths)) | |
| _paths.append(str(_path.relative_to(_root))) | |
| _part = _path.open("wb") | |
| _part.write(_line) | |
| finally: | |
| if _part is not None: | |
| _part.close() | |
| print("MOLAB_PARTS " + json.dumps(_paths), flush=True) | |
| ''' | |
| result = execute(code, args.state_file, capture=True) | |
| paths = json.loads(result.stdout.split("MOLAB_PARTS ", 1)[1].splitlines()[0]) | |
| destination = Path(args.destination) | |
| for path in paths: | |
| pull_archive([path], destination, args) | |
| canonical = destination / args.path | |
| canonical.parent.mkdir(parents=True, exist_ok=True) | |
| with canonical.open("wb") as output: | |
| for path in paths: | |
| part = destination / path | |
| with part.open("rb") as source: | |
| shutil.copyfileobj(source, output) | |
| part.unlink() | |
| print("Retrieved complete JSONL into", canonical, flush=True) | |
| if __name__ == "__main__": | |
| main() | |