Add series task branch flow
This commit is contained in:
@@ -111,9 +111,9 @@ def load_config(config_path: Path) -> WorkspaceConfig:
|
||||
git = GitConfig(
|
||||
base_url=git_raw.get("base_url", "http://77.90.8.171:3001").rstrip("/"),
|
||||
source_org=git_raw.get("source_org", "edu-inf"),
|
||||
answer_org=git_raw.get("answer_org", "zsl-inf"),
|
||||
answer_org=git_raw.get("answer_org", "c2025-1a-inf"),
|
||||
source_remote=git_raw.get("source_remote", "r1"),
|
||||
answer_remote=git_raw.get("answer_remote", "a1"),
|
||||
answer_remote=git_raw.get("answer_remote", "r1a"),
|
||||
origin_remote=git_raw.get("origin_remote", "origin"),
|
||||
fallback_branch=git_raw.get("fallback_branch", "build"),
|
||||
)
|
||||
@@ -134,8 +134,7 @@ def load_config(config_path: Path) -> WorkspaceConfig:
|
||||
|
||||
|
||||
def series_directories(config: WorkspaceConfig) -> list[Path]:
|
||||
if not config.series_root.is_dir():
|
||||
raise SystemExit(f"Missing series root: {config.series_root}")
|
||||
config.series_root.mkdir(parents=True, exist_ok=True)
|
||||
return sorted(path for path in config.series_root.iterdir() if path.is_dir())
|
||||
|
||||
|
||||
@@ -143,6 +142,51 @@ def card_directories(series_path: Path) -> list[Path]:
|
||||
return sorted(path for path in series_path.iterdir() if path.is_dir())
|
||||
|
||||
|
||||
def source_series_directories(config: WorkspaceConfig) -> list[Path]:
|
||||
if not config.original_series_root.is_dir():
|
||||
return []
|
||||
return sorted(path for path in config.original_series_root.iterdir() if path.is_dir())
|
||||
|
||||
|
||||
def source_card_directories(config: WorkspaceConfig, series_name: str) -> list[Path]:
|
||||
source_series_path = config.original_series_root / series_name
|
||||
if not source_series_path.is_dir():
|
||||
return []
|
||||
return sorted(path for path in source_series_path.iterdir() if path.is_dir())
|
||||
|
||||
|
||||
def card_label_from_repo_name(repo_name: str) -> str:
|
||||
if repo_name.startswith("lab-"):
|
||||
repo_name = repo_name[4:]
|
||||
marker = "-strlen-"
|
||||
if marker in repo_name:
|
||||
return repo_name.split(marker, 1)[1].split("-", 1)[0]
|
||||
return repo_name
|
||||
|
||||
|
||||
def resolve_source_card_path(config: WorkspaceConfig, series_name: str, card_name: str) -> Path:
|
||||
cards = source_card_directories(config, series_name)
|
||||
if not cards:
|
||||
raise SystemExit(f"Missing source series: {config.original_series_root / series_name}")
|
||||
|
||||
normalized = slug_value(card_name, "card")
|
||||
matches = []
|
||||
for card_path in cards:
|
||||
repo_name = card_path.name
|
||||
label = card_label_from_repo_name(repo_name)
|
||||
candidates = {repo_name, label}
|
||||
if normalized in candidates or any(normalized in candidate for candidate in candidates):
|
||||
matches.append(card_path)
|
||||
|
||||
if not matches:
|
||||
available = ", ".join(card_label_from_repo_name(path.name) for path in cards)
|
||||
raise SystemExit(f"Missing card '{card_name}' in series '{series_name}'. Available: {available}")
|
||||
if len(matches) > 1:
|
||||
available = ", ".join(path.name for path in matches)
|
||||
raise SystemExit(f"Ambiguous card '{card_name}' in series '{series_name}'. Matches: {available}")
|
||||
return matches[0]
|
||||
|
||||
|
||||
def read_card_title(card_path: Path) -> str:
|
||||
readme_path = card_path / "README.md"
|
||||
if not readme_path.is_file():
|
||||
@@ -170,12 +214,19 @@ def resolve_selector(
|
||||
return series_value, card_value
|
||||
|
||||
if series_arg and not card_arg:
|
||||
if (config.series_root / series_arg).is_dir():
|
||||
if (config.series_root / series_arg).is_dir() or (config.original_series_root / series_arg).is_dir():
|
||||
if not default_card:
|
||||
raise SystemExit("Missing default card in config.")
|
||||
return series_arg, default_card
|
||||
if default_series and (config.series_root / default_series / series_arg).is_dir():
|
||||
return default_series, series_arg
|
||||
if default_series:
|
||||
try:
|
||||
source_card_path = resolve_source_card_path(config, default_series, series_arg)
|
||||
if workspace_card_path(config, default_series, source_card_path).is_dir():
|
||||
return default_series, series_arg
|
||||
except SystemExit:
|
||||
pass
|
||||
|
||||
series_value = series_arg or default_series
|
||||
card_value = card_arg or default_card
|
||||
@@ -186,15 +237,25 @@ def resolve_selector(
|
||||
|
||||
def resolve_card_path(config: WorkspaceConfig, series_name: str, card_name: str) -> Path:
|
||||
card_path = config.series_root / series_name / card_name
|
||||
if not card_path.is_dir():
|
||||
raise SystemExit(f"Missing card path: {card_path}")
|
||||
return card_path
|
||||
if card_path.is_dir():
|
||||
return card_path
|
||||
source_card_path = resolve_source_card_path(config, series_name, card_name)
|
||||
resolved_path = workspace_card_path(config, series_name, source_card_path)
|
||||
if resolved_path.is_dir():
|
||||
return resolved_path
|
||||
raise SystemExit(f"Missing card path: {resolved_path}")
|
||||
|
||||
|
||||
def print_series(config: WorkspaceConfig) -> None:
|
||||
for series_path in series_directories(config):
|
||||
card_count = len(card_directories(series_path))
|
||||
print(f"{series_path.name}\t{card_count}\t{series_path}")
|
||||
config.series_root.mkdir(parents=True, exist_ok=True)
|
||||
source_by_name = {path.name: path for path in source_series_directories(config)}
|
||||
workspace_by_name = {path.name: path for path in series_directories(config)}
|
||||
for series_name in sorted(set(source_by_name) | set(workspace_by_name)):
|
||||
source_path = source_by_name.get(series_name)
|
||||
workspace_path = workspace_by_name.get(series_name)
|
||||
source_count = len(source_card_directories(config, series_name)) if source_path else 0
|
||||
workspace_count = len(card_directories(workspace_path)) if workspace_path else 0
|
||||
print(f"{series_name}\t{source_count}\t{workspace_count}\t{workspace_path or ''}")
|
||||
|
||||
|
||||
def print_cards(config: WorkspaceConfig, series_arg: str | None) -> None:
|
||||
@@ -202,16 +263,23 @@ def print_cards(config: WorkspaceConfig, series_arg: str | None) -> None:
|
||||
if not series_name:
|
||||
raise SystemExit("Missing series name and no default series is configured.")
|
||||
|
||||
series_path = config.series_root / series_name
|
||||
if not series_path.is_dir():
|
||||
raise SystemExit(f"Missing series path: {series_path}")
|
||||
config.series_root.mkdir(parents=True, exist_ok=True)
|
||||
source_cards = {path.name: path for path in source_card_directories(config, series_name)}
|
||||
workspace_series_path = config.series_root / series_name
|
||||
workspace_cards = {path.name: path for path in card_directories(workspace_series_path)} if workspace_series_path.is_dir() else {}
|
||||
if not source_cards and not workspace_cards:
|
||||
raise SystemExit(f"Missing series: {series_name}")
|
||||
|
||||
for card_path in card_directories(series_path):
|
||||
title = read_card_title(card_path)
|
||||
for repo_name in sorted(set(source_cards) | set(workspace_cards)):
|
||||
source_path = source_cards.get(repo_name)
|
||||
workspace_path = workspace_cards.get(repo_name)
|
||||
title = read_card_title(workspace_path or source_path) if (workspace_path or source_path) else ""
|
||||
label = card_label_from_repo_name(repo_name)
|
||||
status = "workspace" if workspace_path else "source"
|
||||
if title:
|
||||
print(f"{card_path.name}\t{title}\t{card_path}")
|
||||
print(f"{label}\t{repo_name}\t{status}\t{title}")
|
||||
else:
|
||||
print(f"{card_path.name}\t{card_path}")
|
||||
print(f"{label}\t{repo_name}\t{status}")
|
||||
|
||||
|
||||
def slug_value(raw_value: str, field_name: str) -> str:
|
||||
@@ -221,6 +289,13 @@ def slug_value(raw_value: str, field_name: str) -> str:
|
||||
return slug
|
||||
|
||||
|
||||
def branch_value(raw_value: str, field_name: str = "branch") -> str:
|
||||
branch_name = re.sub(r"[^A-Za-z0-9._-]+", "-", raw_value.strip()).strip("-")
|
||||
if not branch_name:
|
||||
raise SystemExit(f"Empty {field_name} after branch conversion: {raw_value!r}")
|
||||
return branch_name
|
||||
|
||||
|
||||
def git_capture(repo_path: Path, git_args: list[str]) -> str | None:
|
||||
result = subprocess.run(
|
||||
["git", "-C", str(repo_path)] + git_args,
|
||||
@@ -2388,13 +2463,17 @@ def repo_name_from_url(remote_url: str) -> str:
|
||||
|
||||
|
||||
def build_answer_repo_name(source_repo_name: str, class_name: str, date_value: str) -> str:
|
||||
return "-".join([source_repo_name, slug_value(class_name, "class"), date_value])
|
||||
return source_repo_name
|
||||
|
||||
|
||||
def build_answer_url(config: WorkspaceConfig, answer_repo_name: str) -> str:
|
||||
return f"{config.git.base_url}/{config.git.answer_org}/{answer_repo_name}.git"
|
||||
|
||||
|
||||
def answer_url_for_card(config: WorkspaceConfig, repo_name: str) -> str:
|
||||
return f"{config.git.base_url}/{config.git.answer_org}/{repo_name}.git"
|
||||
|
||||
|
||||
def set_git_remote(repo_path: Path, remote_name: str, remote_url: str) -> str:
|
||||
existing_url = git_capture(repo_path, ["remote", "get-url", remote_name])
|
||||
if existing_url == remote_url:
|
||||
@@ -2413,21 +2492,393 @@ def set_git_remote(repo_path: Path, remote_name: str, remote_url: str) -> str:
|
||||
return "added"
|
||||
|
||||
|
||||
def source_url_for_card(config: WorkspaceConfig, series_name: str, source_card_path: Path) -> str:
|
||||
source_url = git_capture(source_card_path, ["remote", "get-url", config.git.source_remote])
|
||||
if source_url:
|
||||
return source_url
|
||||
source_url = git_capture(source_card_path, ["remote", "get-url", config.git.origin_remote])
|
||||
if source_url:
|
||||
return source_url
|
||||
return f"{config.git.base_url}/{config.git.source_org}/{source_card_path.name}.git"
|
||||
|
||||
|
||||
def ensure_answer_token_from_source(
|
||||
config: WorkspaceConfig,
|
||||
source_remote: str,
|
||||
answer_remote: str,
|
||||
answer_url: str,
|
||||
dry_run: bool = False,
|
||||
) -> tuple[str, dict]:
|
||||
token_data = load_token_store(config, write_normalized=not dry_run)
|
||||
answer_server_info = server_info_from_url(config, answer_url)
|
||||
if answer_server_info is None:
|
||||
raise SystemExit(f"Only http/https remote URLs are supported: {answer_url}")
|
||||
source_token = resolve_token_from_store(token_data, answer_server_info["endpoint"], source_remote, None)
|
||||
if dry_run:
|
||||
return "dry-run", source_token
|
||||
status = register_token(
|
||||
token_data,
|
||||
answer_server_info,
|
||||
answer_remote,
|
||||
str(source_token.get("user", "")),
|
||||
str(source_token.get("value", "")),
|
||||
hydrate=False,
|
||||
reset_api_metadata=True,
|
||||
)
|
||||
write_token_store(config, token_data)
|
||||
return status, source_token
|
||||
|
||||
|
||||
def workspace_card_path(config: WorkspaceConfig, series_name: str, source_card_path: Path) -> Path:
|
||||
return config.series_root / series_name / source_card_path.name
|
||||
|
||||
|
||||
def resolve_workspace_card_path(config: WorkspaceConfig, series_name: str, card_name: str) -> Path:
|
||||
source_card_path = resolve_source_card_path(config, series_name, card_name)
|
||||
card_path = workspace_card_path(config, series_name, source_card_path)
|
||||
if not card_path.is_dir():
|
||||
raise SystemExit(f"Missing workspace card: {card_path}. Run: ./rvctl series cards fetch {series_name} {card_name}")
|
||||
return card_path
|
||||
|
||||
|
||||
def fetch_card_to_workspace(config: WorkspaceConfig, series_name: str, card_name: str, dry_run: bool = False) -> dict[str, str]:
|
||||
source_card_path = resolve_source_card_path(config, series_name, card_name)
|
||||
repo_name = source_card_path.name
|
||||
target_path = workspace_card_path(config, series_name, source_card_path)
|
||||
source_url = source_url_for_card(config, series_name, source_card_path)
|
||||
answer_url = answer_url_for_card(config, repo_name)
|
||||
source_branch = git_current_branch(config, source_card_path)
|
||||
|
||||
if not dry_run:
|
||||
target_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
if target_path.exists() and git_repo_root(target_path) is None:
|
||||
raise SystemExit(f"Workspace card path exists but is not a git repo: {target_path}")
|
||||
cloned = False
|
||||
if not target_path.exists():
|
||||
subprocess.run(["git", "clone", source_url, str(target_path)], check=True)
|
||||
cloned = True
|
||||
origin_url = git_capture(target_path, ["remote", "get-url", config.git.origin_remote])
|
||||
if origin_url and git_capture(target_path, ["remote", "get-url", config.git.source_remote]) is None:
|
||||
subprocess.run(
|
||||
["git", "-C", str(target_path), "remote", "rename", config.git.origin_remote, config.git.source_remote],
|
||||
check=True,
|
||||
)
|
||||
set_git_remote(target_path, config.git.source_remote, source_url)
|
||||
subprocess.run(["git", "-C", str(target_path), "fetch", config.git.source_remote, source_branch], check=True)
|
||||
current_branch = git_current_branch(config, target_path)
|
||||
if cloned or current_branch == source_branch:
|
||||
if current_branch != source_branch:
|
||||
local_branch_exists = subprocess.run(
|
||||
["git", "-C", str(target_path), "rev-parse", "--verify", "--quiet", source_branch],
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
check=False,
|
||||
).returncode == 0
|
||||
if local_branch_exists:
|
||||
subprocess.run(["git", "-C", str(target_path), "switch", source_branch], check=True)
|
||||
else:
|
||||
subprocess.run(
|
||||
["git", "-C", str(target_path), "switch", "--track", "-c", source_branch, f"{config.git.source_remote}/{source_branch}"],
|
||||
check=True,
|
||||
)
|
||||
subprocess.run(["git", "-C", str(target_path), "pull", "--ff-only", config.git.source_remote, source_branch], check=True)
|
||||
|
||||
token_status, source_token = ensure_answer_token_from_source(
|
||||
config,
|
||||
config.git.source_remote,
|
||||
config.git.answer_remote,
|
||||
answer_url,
|
||||
dry_run=dry_run,
|
||||
)
|
||||
answer_remote_status = "dry-run"
|
||||
if not dry_run:
|
||||
answer_remote_url = credentialed_remote_url(
|
||||
answer_url,
|
||||
str(source_token.get("user", "")),
|
||||
str(source_token.get("value", "")),
|
||||
)
|
||||
answer_remote_status = set_git_remote(target_path, config.git.answer_remote, answer_remote_url)
|
||||
|
||||
return {
|
||||
"series": series_name,
|
||||
"card": card_label_from_repo_name(repo_name),
|
||||
"repo": repo_name,
|
||||
"source": source_url,
|
||||
"workspace": str(target_path),
|
||||
"source_remote": config.git.source_remote,
|
||||
"source_branch": source_branch,
|
||||
"answer_remote": config.git.answer_remote,
|
||||
"answer_org": config.git.answer_org,
|
||||
"answer_repo": repo_name,
|
||||
"answer_url": answer_url,
|
||||
"token": config.git.answer_remote,
|
||||
"token_status": token_status,
|
||||
"answer_remote_status": answer_remote_status,
|
||||
"status": "dry-run" if dry_run else "ready",
|
||||
}
|
||||
|
||||
|
||||
def print_key_values(values: dict[str, str]) -> None:
|
||||
for key, value in values.items():
|
||||
print(f"{key}\t{value}")
|
||||
|
||||
|
||||
def task_names(card_path: Path) -> list[str]:
|
||||
task_root = card_path / "src" / "tasks"
|
||||
if not task_root.is_dir():
|
||||
return []
|
||||
names = {
|
||||
path.stem
|
||||
for path in task_root.iterdir()
|
||||
if path.is_file() and path.stem.startswith("task")
|
||||
}
|
||||
return sorted(names)
|
||||
|
||||
|
||||
def task_branch_part(task_name: str) -> str:
|
||||
match = re.search(r"(?i)task[-_]?(\d+)", task_name)
|
||||
if match is None:
|
||||
match = re.fullmatch(r"(?i)t(\d+)", task_name.strip())
|
||||
if match is None:
|
||||
match = re.search(r"(\d+)", task_name)
|
||||
if match is None:
|
||||
raise SystemExit(f"Task name does not contain a task number: {task_name}")
|
||||
return f"T{int(match.group(1))}"
|
||||
|
||||
|
||||
def resolve_submission_task(config: WorkspaceConfig, card_path: Path, task_arg: str | None) -> str:
|
||||
task_selector = task_arg or config.defaults.get("task")
|
||||
if not task_selector:
|
||||
raise SystemExit("Missing task selector. Pass --task or set defaults.task in workspace.json.")
|
||||
|
||||
names = task_names(card_path)
|
||||
if not names:
|
||||
return task_selector
|
||||
|
||||
matches = [name for name in names if name == task_selector]
|
||||
if not matches:
|
||||
try:
|
||||
selector_part = task_branch_part(task_selector)
|
||||
except SystemExit:
|
||||
selector_part = ""
|
||||
number_matches = []
|
||||
for name in names:
|
||||
try:
|
||||
if selector_part and task_branch_part(name) == selector_part:
|
||||
number_matches.append(name)
|
||||
except SystemExit:
|
||||
pass
|
||||
matches = number_matches
|
||||
if not matches:
|
||||
matches = [name for name in names if task_selector in name]
|
||||
|
||||
if not matches:
|
||||
raise SystemExit(f"Missing task '{task_selector}' in card: {card_path}")
|
||||
if len(matches) > 1:
|
||||
raise SystemExit(f"Ambiguous task '{task_selector}'. Matches: {', '.join(matches)}")
|
||||
return matches[0]
|
||||
|
||||
|
||||
def store_user_for_submission(
|
||||
config: WorkspaceConfig,
|
||||
source_url: str,
|
||||
answer_url: str,
|
||||
required: bool = True,
|
||||
) -> str:
|
||||
token_data = load_token_store(config)
|
||||
endpoints = []
|
||||
for remote_url in [source_url, answer_url, config.git.base_url]:
|
||||
server_info = server_info_from_url(config, remote_url)
|
||||
if server_info is None:
|
||||
continue
|
||||
endpoint = server_info["endpoint"]
|
||||
if endpoint not in endpoints:
|
||||
endpoints.append(endpoint)
|
||||
|
||||
for endpoint in endpoints:
|
||||
for remote_id in [config.git.source_remote, config.git.answer_remote]:
|
||||
token_record = find_token_record(token_data, endpoint, remote_id)
|
||||
if token_record and token_record.get("user"):
|
||||
return slug_value(str(token_record["user"]), "token user")
|
||||
|
||||
users = {
|
||||
str(token_record.get("user", ""))
|
||||
for token_record in token_data.get("tokens", [])
|
||||
if token_server_endpoint(token_record) in endpoints and token_record.get("user")
|
||||
}
|
||||
if len(users) == 1:
|
||||
return slug_value(next(iter(users)), "token user")
|
||||
|
||||
if required:
|
||||
raise SystemExit("Missing --nick and no unique user in tokens.json for the answer branch.")
|
||||
return ""
|
||||
|
||||
|
||||
def submission_branch_name(user_name: str, task_name: str) -> str:
|
||||
return branch_value(f"{slug_value(user_name, 'student user')}{task_branch_part(task_name)}a")
|
||||
|
||||
|
||||
def print_card_tasks(config: WorkspaceConfig, series_name: str, card_name: str) -> None:
|
||||
card_path = resolve_workspace_card_path(config, series_name, card_name)
|
||||
for task_name in task_names(card_path):
|
||||
print(f"{series_name}\t{card_label_from_repo_name(card_path.name)}\t{task_name}\t{card_path / 'src' / 'tasks'}")
|
||||
|
||||
|
||||
def print_card_task(config: WorkspaceConfig, series_name: str, card_name: str, task_name: str) -> None:
|
||||
card_path = resolve_workspace_card_path(config, series_name, card_name)
|
||||
matches = [name for name in task_names(card_path) if name == task_name or task_name in name]
|
||||
if not matches:
|
||||
raise SystemExit(f"Missing task '{task_name}' in card: {card_path}")
|
||||
if len(matches) > 1:
|
||||
raise SystemExit(f"Ambiguous task '{task_name}'. Matches: {', '.join(matches)}")
|
||||
print(f"series\t{series_name}")
|
||||
print(f"card\t{card_label_from_repo_name(card_path.name)}")
|
||||
print(f"task\t{matches[0]}")
|
||||
print(f"card_path\t{card_path}")
|
||||
print(f"task_root\t{card_path / 'src' / 'tasks'}")
|
||||
|
||||
|
||||
def switch_card_task(
|
||||
config: WorkspaceConfig,
|
||||
series_name: str,
|
||||
card_name: str,
|
||||
task_selector: str,
|
||||
branch_override: str | None = None,
|
||||
dry_run: bool = False,
|
||||
) -> dict[str, str]:
|
||||
card_path = resolve_workspace_card_path(config, series_name, card_name)
|
||||
task_name = resolve_submission_task(config, card_path, task_selector)
|
||||
source_url = discover_source_url(config, card_path, series_name, card_name)
|
||||
source_repo_name = repo_name_from_url(source_url)
|
||||
answer_url = build_answer_url(config, source_repo_name)
|
||||
student_user = store_user_for_submission(config, source_url, answer_url, required=branch_override is None)
|
||||
branch_name = branch_value(branch_override) if branch_override else submission_branch_name(student_user, task_name)
|
||||
current_branch = git_current_branch(config, card_path)
|
||||
branch_exists = subprocess.run(
|
||||
["git", "-C", str(card_path), "rev-parse", "--verify", "--quiet", branch_name],
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
check=False,
|
||||
).returncode == 0
|
||||
|
||||
if dry_run:
|
||||
status = "dry-run"
|
||||
elif current_branch == branch_name:
|
||||
status = "unchanged"
|
||||
elif branch_exists:
|
||||
subprocess.run(["git", "-C", str(card_path), "switch", branch_name], check=True)
|
||||
status = "switched"
|
||||
else:
|
||||
subprocess.run(["git", "-C", str(card_path), "switch", "-c", branch_name], check=True)
|
||||
status = "created"
|
||||
|
||||
return {
|
||||
"series": series_name,
|
||||
"card": card_label_from_repo_name(card_path.name),
|
||||
"task": task_name,
|
||||
"student_user": student_user,
|
||||
"branch": branch_name,
|
||||
"workspace": str(card_path),
|
||||
"status": status,
|
||||
}
|
||||
|
||||
|
||||
def print_series_details(config: WorkspaceConfig, series_name: str) -> None:
|
||||
config.series_root.mkdir(parents=True, exist_ok=True)
|
||||
source_path = config.original_series_root / series_name
|
||||
workspace_path = config.series_root / series_name
|
||||
cards = source_card_directories(config, series_name)
|
||||
if not cards and not workspace_path.is_dir():
|
||||
raise SystemExit(f"Missing series: {series_name}")
|
||||
print(f"series\t{series_name}")
|
||||
print(f"source_path\t{source_path if source_path.is_dir() else ''}")
|
||||
print(f"workspace_path\t{workspace_path}")
|
||||
print(f"cards_count\t{len(cards)}")
|
||||
print(f"cards\t{' '.join(card_label_from_repo_name(path.name) for path in cards)}")
|
||||
|
||||
|
||||
def print_card_details(config: WorkspaceConfig, series_name: str, card_name: str) -> None:
|
||||
source_card_path = resolve_source_card_path(config, series_name, card_name)
|
||||
target_path = workspace_card_path(config, series_name, source_card_path)
|
||||
repo_name = source_card_path.name
|
||||
print(f"series\t{series_name}")
|
||||
print(f"card\t{card_label_from_repo_name(repo_name)}")
|
||||
print(f"repo\t{repo_name}")
|
||||
print(f"source_path\t{source_card_path}")
|
||||
print(f"workspace_path\t{target_path}")
|
||||
print(f"source_remote\t{config.git.source_remote}")
|
||||
print(f"source_url\t{source_url_for_card(config, series_name, source_card_path)}")
|
||||
print(f"answer_remote\t{config.git.answer_remote}")
|
||||
print(f"answer_url\t{answer_url_for_card(config, repo_name)}")
|
||||
if target_path.is_dir():
|
||||
print(f"tasks\t{' '.join(task_names(target_path))}")
|
||||
|
||||
|
||||
def run_series(config: WorkspaceConfig, args: argparse.Namespace) -> None:
|
||||
if args.series_command == "list":
|
||||
print_series(config)
|
||||
return
|
||||
if args.series_command == "show":
|
||||
print_series_details(config, args.series)
|
||||
return
|
||||
if args.series_command == "fetch":
|
||||
for source_card_path in source_card_directories(config, args.series):
|
||||
result = fetch_card_to_workspace(config, args.series, source_card_path.name, dry_run=args.dry_run)
|
||||
print_key_values(result)
|
||||
print()
|
||||
return
|
||||
if args.series_command != "cards":
|
||||
raise SystemExit(f"Unsupported series command: {args.series_command}")
|
||||
|
||||
if args.cards_command == "list":
|
||||
print_cards(config, args.series)
|
||||
return
|
||||
if args.cards_command == "show":
|
||||
print_card_details(config, args.series, args.card)
|
||||
return
|
||||
if args.cards_command == "fetch":
|
||||
print_key_values(fetch_card_to_workspace(config, args.series, args.card, dry_run=args.dry_run))
|
||||
return
|
||||
if args.cards_command == "submission":
|
||||
print_submission_plan(config, args)
|
||||
return
|
||||
if args.cards_command != "tasks":
|
||||
raise SystemExit(f"Unsupported series cards command: {args.cards_command}")
|
||||
|
||||
if args.tasks_command == "list":
|
||||
print_card_tasks(config, args.series, args.card)
|
||||
return
|
||||
if args.tasks_command == "show":
|
||||
print_card_task(config, args.series, args.card, args.task)
|
||||
return
|
||||
if args.tasks_command == "switch":
|
||||
print_key_values(switch_card_task(config, args.series, args.card, args.task, args.branch, args.dry_run))
|
||||
return
|
||||
raise SystemExit(f"Unsupported series cards tasks command: {args.tasks_command}")
|
||||
|
||||
|
||||
def print_submission_plan(config: WorkspaceConfig, args: argparse.Namespace) -> None:
|
||||
series_name, card_name = resolve_selector(config, args.series, args.card)
|
||||
selector = f"{series_name}/{card_name}"
|
||||
card_path = resolve_card_path(config, series_name, card_name)
|
||||
sync_tokens_from_repo(config, card_path)
|
||||
class_name = slug_value(args.class_name, "class")
|
||||
branch_name = slug_value(args.branch or args.nick, "branch")
|
||||
source_url = args.source_url or discover_source_url(config, card_path, series_name, card_name)
|
||||
source_repo_name = repo_name_from_url(source_url)
|
||||
answer_repo_name = build_answer_repo_name(source_repo_name, class_name, args.date)
|
||||
answer_url = build_answer_url(config, answer_repo_name)
|
||||
source_branch = git_current_branch(config, card_path)
|
||||
task_name = resolve_submission_task(config, card_path, args.task)
|
||||
student_user = (
|
||||
slug_value(args.nick, "student user")
|
||||
if args.nick
|
||||
else store_user_for_submission(config, source_url, answer_url, required=args.branch is None)
|
||||
)
|
||||
branch_name = branch_value(args.branch) if args.branch else submission_branch_name(student_user, task_name)
|
||||
|
||||
print(f"selector\t{selector}")
|
||||
print(f"card_path\t{card_path}")
|
||||
print(f"task\t{task_name}")
|
||||
print(f"source_repo\t{config.git.source_org}/{source_repo_name}")
|
||||
print(f"source_remote\t{config.git.source_remote}")
|
||||
print(f"source_url\t{source_url}")
|
||||
@@ -2435,12 +2886,28 @@ def print_submission_plan(config: WorkspaceConfig, args: argparse.Namespace) ->
|
||||
print(f"answer_remote\t{config.git.answer_remote}")
|
||||
print(f"answer_repo\t{config.git.answer_org}/{answer_repo_name}")
|
||||
print(f"answer_url\t{answer_url}")
|
||||
print(f"student_user\t{student_user}")
|
||||
print(f"student_branch\t{branch_name}")
|
||||
|
||||
if args.apply:
|
||||
source_status = set_git_remote(card_path, config.git.source_remote, source_url)
|
||||
answer_status = set_git_remote(card_path, config.git.answer_remote, answer_url)
|
||||
token_status, source_token = ensure_answer_token_from_source(
|
||||
config,
|
||||
config.git.source_remote,
|
||||
config.git.answer_remote,
|
||||
answer_url,
|
||||
)
|
||||
answer_status = set_git_remote(
|
||||
card_path,
|
||||
config.git.answer_remote,
|
||||
credentialed_remote_url(
|
||||
answer_url,
|
||||
str(source_token.get("user", "")),
|
||||
str(source_token.get("value", "")),
|
||||
),
|
||||
)
|
||||
print(f"apply_{config.git.source_remote}\t{source_status}")
|
||||
print(f"apply_token_{config.git.answer_remote}\t{token_status}")
|
||||
print(f"apply_{config.git.answer_remote}\t{answer_status}")
|
||||
|
||||
print("commands")
|
||||
@@ -2621,10 +3088,11 @@ def print_main_overview(config_path: Path) -> None:
|
||||
["command", "arguments", "purpose"],
|
||||
[
|
||||
["show-config", "", "show resolved paths and Git defaults"],
|
||||
["list-series", "", "list series from workspace"],
|
||||
["list-cards", "[series]", "list cards in a selected series"],
|
||||
["series", "list|cards|fetch|tasks", "work with series, cards and tasks"],
|
||||
["list-series", "", "compat alias for series list"],
|
||||
["list-cards", "[series]", "compat alias for series cards list"],
|
||||
["tmux-container", "[series] [card]", "start tmux with container in pane 0"],
|
||||
["submission", "[series] [card] --class K --nick N", "prepare answer repo and student branch"],
|
||||
["submission", "[series] [card] --class K [--task T]", "prepare answer repo and student branch"],
|
||||
["tokens", "list|compare|cmp|sync|update|remove|rm|add|read|stats|write", "manage tokens.json and Git remote credentials"],
|
||||
],
|
||||
)
|
||||
@@ -2634,10 +3102,10 @@ def print_main_overview(config_path: Path) -> None:
|
||||
["step", "command", "result"],
|
||||
[
|
||||
["1", "./rvctl tokens compare", "compare repo remotes with tokens.json"],
|
||||
["2", "./rvctl list-series", "choose a series"],
|
||||
["3", "./rvctl list-cards inf", "choose a card"],
|
||||
["4", "./rvctl tmux-container inf <card>", "start the working container"],
|
||||
["5", "./rvctl submission inf <card> --class 4i --nick u1", "prepare answer remote and branch"],
|
||||
["2", "./rvctl series list", "choose a series"],
|
||||
["3", "./rvctl series cards list inf", "choose a card"],
|
||||
["4", "./rvctl series cards fetch inf bss", "fetch card and prepare r1a"],
|
||||
["5", "./rvctl series cards tasks list inf bss", "list card tasks"],
|
||||
],
|
||||
)
|
||||
print()
|
||||
@@ -2686,8 +3154,8 @@ def print_tokens_overview() -> None:
|
||||
["write store r1 to remote", "./rvctl tokens sync store r1 --repo PATH"],
|
||||
["remove r1 from store", "./rvctl tokens rm store r1"],
|
||||
["remove Git remote r1", "./rvctl tokens rm remote r1"],
|
||||
["compare selected card", "./rvctl tokens cmp --repo ~/dev/workspace/rv/series/inf/03"],
|
||||
["stats selected card", "./rvctl tokens stats --repo ~/dev/workspace/rv/series/inf/03"],
|
||||
["compare selected card", "./rvctl tokens cmp --repo ~/dev/workspace/rv/series/inf/lab-rv32i-strlen-bss-data-stack"],
|
||||
["stats selected card", "./rvctl tokens stats --repo ~/dev/workspace/rv/series/inf/lab-rv32i-strlen-bss-data-stack"],
|
||||
["write auth to r1", "./rvctl tokens write --repo PATH --remote r1 --server URL"],
|
||||
],
|
||||
)
|
||||
@@ -2741,8 +3209,8 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
"tmux-container",
|
||||
help="Create a tmux session with pane 0 running the container shell for a selected card.",
|
||||
)
|
||||
tmux_parser.add_argument("series", nargs="?", help="Series id or full selector like 'inf/03'.")
|
||||
tmux_parser.add_argument("card", nargs="?", help="Card id, for example '03'.")
|
||||
tmux_parser.add_argument("series", nargs="?", help="Series id or full selector like 'inf/bss'.")
|
||||
tmux_parser.add_argument("card", nargs="?", help="Card id or unique fragment, for example 'bss'.")
|
||||
tmux_parser.add_argument("--session", help="tmux session name.")
|
||||
tmux_parser.add_argument("--window", help="tmux window name.")
|
||||
tmux_parser.add_argument("--instance", help="RV_INSTANCE passed to the tool launcher.")
|
||||
@@ -2753,22 +3221,23 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
"submission",
|
||||
help="Print or apply source/answer git remotes for a selected card.",
|
||||
)
|
||||
submission_parser.add_argument("series", nargs="?", help="Series id or full selector like 'inf/03'.")
|
||||
submission_parser.add_argument("card", nargs="?", help="Card id, for example '03'.")
|
||||
submission_parser.add_argument("--class", dest="class_name", required=True, help="Class id, for example '4i'.")
|
||||
submission_parser.add_argument("--nick", required=True, help="Student nick used as the branch name.")
|
||||
submission_parser.add_argument("series", nargs="?", help="Series id or full selector like 'inf/bss'.")
|
||||
submission_parser.add_argument("card", nargs="?", help="Card id or unique fragment, for example 'bss'.")
|
||||
submission_parser.add_argument("--class", dest="class_name", required=True, help="Class id, for example 'c2025-1a-inf'.")
|
||||
submission_parser.add_argument("--nick", help="Student nick. Defaults to the user from tokens.json.")
|
||||
submission_parser.add_argument("--task", help="Task name or unique fragment. Defaults to defaults.task.")
|
||||
submission_parser.add_argument(
|
||||
"--branch",
|
||||
help="Override the submission branch name. Defaults to the value of --nick.",
|
||||
help="Override the submission branch name. Defaults to <store-user>T<task-number>a.",
|
||||
)
|
||||
submission_parser.add_argument(
|
||||
"--date",
|
||||
default=dt.date.today().isoformat(),
|
||||
help="Lesson date used in the answer repo name. Default: today in YYYY-MM-DD.",
|
||||
help="Lesson date reported in the submission plan. Default: today in YYYY-MM-DD.",
|
||||
)
|
||||
submission_parser.add_argument(
|
||||
"--source-url",
|
||||
help="Override the source repo URL. By default launcher reads the current origin from the card repo.",
|
||||
help="Override the source repo URL. By default launcher reads r1 or origin from the card repo.",
|
||||
)
|
||||
submission_parser.add_argument(
|
||||
"--apply",
|
||||
@@ -2776,6 +3245,63 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
help="Write remotes into the selected card repo instead of only printing the plan.",
|
||||
)
|
||||
|
||||
series_parser = subparsers.add_parser("series", help="Work with series, cards and tasks.")
|
||||
series_subparsers = series_parser.add_subparsers(dest="series_command", required=True)
|
||||
|
||||
series_subparsers.add_parser("list", help="List available series.")
|
||||
|
||||
series_show_parser = series_subparsers.add_parser("show", help="Show one series.")
|
||||
series_show_parser.add_argument("series", help="Series id, for example 'inf'.")
|
||||
|
||||
series_fetch_parser = series_subparsers.add_parser("fetch", help="Fetch all cards in a series.")
|
||||
series_fetch_parser.add_argument("series", help="Series id, for example 'inf'.")
|
||||
series_fetch_parser.add_argument("--dry-run", action="store_true", help="Print plan without cloning.")
|
||||
|
||||
cards_group_parser = series_subparsers.add_parser("cards", help="Work with cards in a series.")
|
||||
cards_subparsers = cards_group_parser.add_subparsers(dest="cards_command", required=True)
|
||||
|
||||
cards_list_parser = cards_subparsers.add_parser("list", help="List cards in a series.")
|
||||
cards_list_parser.add_argument("series", help="Series id, for example 'inf'.")
|
||||
|
||||
cards_show_parser = cards_subparsers.add_parser("show", help="Show one card.")
|
||||
cards_show_parser.add_argument("series", help="Series id, for example 'inf'.")
|
||||
cards_show_parser.add_argument("card", help="Card id or unique fragment, for example 'bss'.")
|
||||
|
||||
cards_fetch_parser = cards_subparsers.add_parser("fetch", help="Fetch one card into workspace.")
|
||||
cards_fetch_parser.add_argument("series", help="Series id, for example 'inf'.")
|
||||
cards_fetch_parser.add_argument("card", help="Card id or unique fragment, for example 'bss'.")
|
||||
cards_fetch_parser.add_argument("--dry-run", action="store_true", help="Print plan without cloning.")
|
||||
|
||||
cards_submission_parser = cards_subparsers.add_parser("submission", help="Print/apply answer remotes for a card.")
|
||||
cards_submission_parser.add_argument("series", help="Series id, for example 'inf'.")
|
||||
cards_submission_parser.add_argument("card", help="Card id or unique fragment, for example 'bss'.")
|
||||
cards_submission_parser.add_argument("--class", dest="class_name", required=True, help="Class id.")
|
||||
cards_submission_parser.add_argument("--nick", help="Student nick. Defaults to the user from tokens.json.")
|
||||
cards_submission_parser.add_argument("--task", help="Task name or unique fragment. Defaults to defaults.task.")
|
||||
cards_submission_parser.add_argument("--branch", help="Override the submission branch name.")
|
||||
cards_submission_parser.add_argument("--date", default=dt.date.today().isoformat(), help="Lesson date in YYYY-MM-DD.")
|
||||
cards_submission_parser.add_argument("--source-url", help="Override source repo URL.")
|
||||
cards_submission_parser.add_argument("--apply", action="store_true", help="Write remotes into the selected card repo.")
|
||||
|
||||
tasks_group_parser = cards_subparsers.add_parser("tasks", help="Work with tasks in a card.")
|
||||
tasks_subparsers = tasks_group_parser.add_subparsers(dest="tasks_command", required=True)
|
||||
|
||||
tasks_list_parser = tasks_subparsers.add_parser("list", help="List tasks in a card.")
|
||||
tasks_list_parser.add_argument("series", help="Series id, for example 'inf'.")
|
||||
tasks_list_parser.add_argument("card", help="Card id or unique fragment, for example 'bss'.")
|
||||
|
||||
tasks_show_parser = tasks_subparsers.add_parser("show", help="Show one task.")
|
||||
tasks_show_parser.add_argument("series", help="Series id, for example 'inf'.")
|
||||
tasks_show_parser.add_argument("card", help="Card id or unique fragment, for example 'bss'.")
|
||||
tasks_show_parser.add_argument("task", help="Task name or unique fragment.")
|
||||
|
||||
tasks_switch_parser = tasks_subparsers.add_parser("switch", help="Create or switch to the answer branch for a task.")
|
||||
tasks_switch_parser.add_argument("series", help="Series id, for example 'inf'.")
|
||||
tasks_switch_parser.add_argument("card", help="Card id or unique fragment, for example 'bss'.")
|
||||
tasks_switch_parser.add_argument("task", help="Task name or unique fragment, for example 'task4' or '4'.")
|
||||
tasks_switch_parser.add_argument("--branch", help="Override the branch name.")
|
||||
tasks_switch_parser.add_argument("--dry-run", action="store_true", help="Print plan without switching branches.")
|
||||
|
||||
tokens_parser = subparsers.add_parser("tokens", help="Manage local token store and repo remotes.")
|
||||
tokens_subparsers = tokens_parser.add_subparsers(dest="tokens_command", required=True)
|
||||
|
||||
@@ -2919,6 +3445,9 @@ def main() -> int:
|
||||
if args.command == "submission":
|
||||
print_submission_plan(config, args)
|
||||
return 0
|
||||
if args.command == "series":
|
||||
run_series(config, args)
|
||||
return 0
|
||||
if args.command == "tokens":
|
||||
if args.tokens_command in {"compare", "cmp"}:
|
||||
run_tokens_compare(config, args)
|
||||
|
||||
Reference in New Issue
Block a user