|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import click |
| 16 | +import shutil |
| 17 | +import sys |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | + |
| 21 | +def get_rl_template_root() -> Path: |
| 22 | + """Get absolute path of RL scaffold template root (cli/templates/rl/)""" |
| 23 | + current_file = Path(__file__).resolve() |
| 24 | + cli_dir = current_file.parent |
| 25 | + rl_template_root = cli_dir / "templates" / "rl" |
| 26 | + return rl_template_root |
| 27 | + |
| 28 | + |
| 29 | +@click.group(name="rl", help="RL related commands") |
| 30 | +def rl_group(): |
| 31 | + pass |
| 32 | + |
| 33 | + |
| 34 | +@rl_group.command( |
| 35 | + name="init", help="Initialize RL scaffold project (specify platform/workspace)" |
| 36 | +) |
| 37 | +@click.option( |
| 38 | + "--platform", |
| 39 | + "-p", |
| 40 | + required=True, |
| 41 | + type=click.Choice(["ark"], case_sensitive=False), |
| 42 | + help="Scaffold platform type (only support for now: ark)", |
| 43 | +) |
| 44 | +@click.option( |
| 45 | + "--workspace", "-w", required=True, type=str, help="Target workspace directory name" |
| 46 | +) |
| 47 | +@click.option( |
| 48 | + "--overwrite", |
| 49 | + "-f", |
| 50 | + is_flag=True, |
| 51 | + help="Force overwrite existing workspace (default: false)", |
| 52 | +) |
| 53 | +def rl_init(platform: str, workspace: str, overwrite: bool): |
| 54 | + """ |
| 55 | + Initialize RL scaffold project for ark platform |
| 56 | + Example: veadk rl init --platform ark --workspace veadk_rl_ark_project |
| 57 | + """ |
| 58 | + # Locate template directory |
| 59 | + rl_template_root = get_rl_template_root() |
| 60 | + platform_template_dir = rl_template_root / platform.lower() |
| 61 | + |
| 62 | + # Validate template directory |
| 63 | + if not platform_template_dir.exists(): |
| 64 | + click.secho(f"Error: Scaffold template for {platform} not found!", fg="red") |
| 65 | + click.secho(f" Expected path: {platform_template_dir}", fg="yellow") |
| 66 | + click.secho( |
| 67 | + f" Supported platforms: {[d.name for d in rl_template_root.glob('*') if d.is_dir()]}", |
| 68 | + fg="blue", |
| 69 | + ) |
| 70 | + sys.exit(1) |
| 71 | + |
| 72 | + # Target workspace path |
| 73 | + target_workspace = Path.cwd() / workspace |
| 74 | + |
| 75 | + # Handle existing directory |
| 76 | + if target_workspace.exists(): |
| 77 | + if not overwrite: |
| 78 | + click.secho( |
| 79 | + f"\nWarning: Target directory {target_workspace} already exists!", |
| 80 | + fg="yellow", |
| 81 | + ) |
| 82 | + if not click.confirm("Overwrite?"): |
| 83 | + click.secho("Operation cancelled", fg="red") |
| 84 | + sys.exit(0) |
| 85 | + shutil.rmtree(target_workspace) |
| 86 | + click.secho(f"Cleared existing directory: {target_workspace}", fg="green") |
| 87 | + |
| 88 | + # Copy scaffold files |
| 89 | + try: |
| 90 | + shutil.copytree( |
| 91 | + src=platform_template_dir, |
| 92 | + dst=target_workspace, |
| 93 | + ignore=None, |
| 94 | + dirs_exist_ok=False, |
| 95 | + ) |
| 96 | + click.secho("\nRL scaffold initialized successfully!", fg="green") |
| 97 | + click.secho(f" - Project path: {target_workspace.absolute()}", fg="green") |
| 98 | + except PermissionError: |
| 99 | + click.secho( |
| 100 | + f"Error: Permission denied to write to {target_workspace}", fg="red" |
| 101 | + ) |
| 102 | + sys.exit(1) |
| 103 | + except Exception as e: |
| 104 | + click.secho(f"Error: Failed to copy scaffold - {str(e)}", fg="red") |
| 105 | + sys.exit(1) |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + rl_group() |
0 commit comments