|
5 | 5 | """ |
6 | 6 | import argparse |
7 | 7 | import os |
| 8 | +import platform |
8 | 9 | import shutil |
9 | 10 | import stat |
10 | 11 |
|
@@ -63,24 +64,57 @@ def init_workspace(source, target): |
63 | 64 | dir_path = os.path.join(target, d) |
64 | 65 | print(f"Creating empty directory: {dir_path}") |
65 | 66 | os.makedirs(dir_path) |
66 | | - |
| 67 | + # 3. Create activation script |
| 68 | + is_windows = platform.system() == "Windows" |
| 69 | + script_ext = ".ps1" if is_windows else ".sh" |
| 70 | + script_name = "run_env" + script_ext |
| 71 | + run_env_path = os.path.join(target, script_name) |
| 72 | + |
| 73 | + with open(run_env_path, "w", encoding="utf-8") as f: |
| 74 | + if is_windows: |
| 75 | + f.write("# run this file to activate the workspace\n\n") |
| 76 | + f.write('$env:QLIB_WORKSPACE_DIR = Split-Path -Parent $MyInvocation.MyCommand.Path\n') |
| 77 | + f.write("# Uncomment and modify to use a custom Qlib data directory:\n") |
| 78 | + f.write('# $env:QLIB_DATA_DIR = "D:\\data\\cn_data"\n') |
| 79 | + f.write('# $env:QLIB_REGION = "cn"\n\n') |
| 80 | + f.write('Write-Host "Workspace activated: $env:QLIB_WORKSPACE_DIR"\n') |
| 81 | + else: |
| 82 | + f.write("#!/bin/bash\n") |
| 83 | + f.write("# Source this file to activate the workspace\n") |
| 84 | + f.write('export QLIB_WORKSPACE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"\n') |
| 85 | + f.write("# Uncomment and modify to use a custom Qlib data directory:\n") |
| 86 | + f.write('# export QLIB_DATA_DIR="~/.qlib/qlib_data/cn_data"\n') |
| 87 | + f.write('# export QLIB_REGION="cn"\n') |
| 88 | + f.write('echo "Workspace activated: $QLIB_WORKSPACE_DIR"\n') |
| 89 | + |
| 90 | + # Make it executable for Linux/macOS |
| 91 | + if not is_windows: |
| 92 | + os.chmod(run_env_path, os.stat(run_env_path).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) |
| 93 | + |
| 94 | + print("\nWorkspace initialization complete!") |
| 95 | + if is_windows: |
| 96 | + print(f"To use this workspace, run: .\\{script_name}") |
| 97 | + print(f"Or in PowerShell: $env:QLIB_WORKSPACE_DIR='{target}'; python ...") |
| 98 | + else: |
| 99 | + print(f"To use this workspace, run: source {run_env_path}") |
| 100 | + print(f"Or prepend commands with: QLIB_WORKSPACE_DIR={target} python ...") |
67 | 101 | # 3. Create run_env.sh |
68 | | - run_env_path = os.path.join(target, "run_env.sh") |
69 | | - with open(run_env_path, "w") as f: |
70 | | - f.write("#!/bin/bash\n") |
71 | | - f.write("# Source this file to activate the workspace\n") |
72 | | - f.write('export QLIB_WORKSPACE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"\n') |
73 | | - f.write("# Uncomment and modify to use a custom Qlib data directory:\n") |
74 | | - f.write('# export QLIB_DATA_DIR="~/.qlib/qlib_data/cn_data"\n') |
75 | | - f.write('# export QLIB_REGION="cn"\n') |
76 | | - f.write("echo \"Workspace activated: $QLIB_WORKSPACE_DIR\"\n") |
| 102 | + # run_env_path = os.path.join(target, "run_env.sh") |
| 103 | + # with open(run_env_path, "w") as f: |
| 104 | + # f.write("#!/bin/bash\n") |
| 105 | + # f.write("# Source this file to activate the workspace\n") |
| 106 | + # f.write('export QLIB_WORKSPACE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"\n') |
| 107 | + # f.write("# Uncomment and modify to use a custom Qlib data directory:\n") |
| 108 | + # f.write('# export QLIB_DATA_DIR="~/.qlib/qlib_data/cn_data"\n') |
| 109 | + # f.write('# export QLIB_REGION="cn"\n') |
| 110 | + # f.write("echo \"Workspace activated: $QLIB_WORKSPACE_DIR\"\n") |
77 | 111 |
|
78 | 112 | # Make it executable just in case, though it should be sourced |
79 | | - os.chmod(run_env_path, os.stat(run_env_path).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) |
| 113 | + # os.chmod(run_env_path, os.stat(run_env_path).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) |
80 | 114 |
|
81 | | - print("\\nWorkspace initialization complete!") |
82 | | - print(f"To use this workspace, run: source {run_env_path}") |
83 | | - print(f"Or prepend commands with: QLIB_WORKSPACE_DIR={target} python ...") |
| 115 | + # print("\\nWorkspace initialization complete!") |
| 116 | + # print(f"To use this workspace, run: source {run_env_path}") |
| 117 | + # print(f"Or prepend commands with: QLIB_WORKSPACE_DIR={target} python ...") |
84 | 118 |
|
85 | 119 | def main(): |
86 | 120 | parser = argparse.ArgumentParser(description="Initialize a new Qlib Workspace") |
|
0 commit comments