Paper2Agent / utils.py
jcmiao's picture
Upload 18 files
51f80a0 verified
raw
history blame
3.55 kB
import anyio
import argparse
import shutil
import subprocess
import os
import sys
from pathlib import Path
def copy_project_resources(FOLDER_NAME):
"""
Step 1: Copy .claude, templates, tools to the project directory.
"""
MAIN_DIR = FOLDER_NAME or "."
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
copy_targets = [
(".claude", "01: .claude already exists or source missing"),
("templates", "01: templates already exists or source missing"),
("tools", "01: tools already exists or source missing"),
]
for subdir, error_msg in copy_targets:
src = os.path.join(SCRIPT_DIR, subdir)
dst = os.path.join(MAIN_DIR, subdir)
if not os.path.isdir(dst) and os.path.isdir(src):
try:
shutil.copytree(src, dst)
except Exception as copy_exc:
print(f"01: failed to copy {subdir}: {copy_exc}", file=sys.stderr)
else:
print(error_msg, file=sys.stderr)
def clone_github_repo(GITHUB_REPO_URL, repo_name):
"""
Step 3: Clone the GitHub repository into the repo directory.
"""
REPO_PARENT = os.path.join(os.getcwd(), "repo")
REPO_DIR = os.path.join(REPO_PARENT, repo_name) if repo_name else ""
GIT_URL = GITHUB_REPO_URL
print(f"03: clone target={GIT_URL} into {REPO_DIR}", file=sys.stderr)
os.makedirs(REPO_PARENT, exist_ok=True)
if repo_name and os.path.isdir(REPO_DIR):
print("03: repo dir already exists: {}".format(REPO_DIR), file=sys.stderr)
elif repo_name and GIT_URL:
clone_attempts = [
(["git", "clone", "--recurse-submodules", GIT_URL, REPO_DIR], "02: cloned with submodules"),
(["git", "clone", "--depth=1", GIT_URL, REPO_DIR], "02: shallow clone ok"),
(["git", "clone", GIT_URL, REPO_DIR], None),
]
for i, (cmd, success_msg) in enumerate(clone_attempts):
if i == 0:
print("03: starting git clone with submodules...", file=sys.stderr)
elif i == 1:
print("03: main clone failed, trying --depth=1", file=sys.stderr)
elif i == 2:
print("03: shallow clone failed, trying plain clone", file=sys.stderr)
try:
rc = subprocess.call(cmd)
except Exception as e:
print(f"03: clone exception: {e}", file=sys.stderr)
rc = 1
if rc == 0:
if success_msg:
print(success_msg, file=sys.stderr)
break
else:
print("03: failed: all clone attempts failed", file=sys.stderr)
def prepare_folder_structure(main_dir):
"""
Prepare the required folder structure for the pipeline under main_dir.
Args:
main_dir (str): The main project directory where folders will be created.
"""
print(f"02: preparing folder structure under {main_dir}", file=sys.stderr)
dirs_to_make = [
os.path.join(main_dir, "reports"),
os.path.join(main_dir, "src", "tools"),
os.path.join(main_dir, "tests", "code"),
os.path.join(main_dir, "tests", "data"),
os.path.join(main_dir, "notebooks"),
os.path.join(main_dir, "tests", "results"),
os.path.join(main_dir, "tests", "logs"),
os.path.join(main_dir, "tests", "summary"),
os.path.join(main_dir, "tmp", "inputs"),
os.path.join(main_dir, "tmp", "outputs"),
]
for d in dirs_to_make:
os.makedirs(d, exist_ok=True)