|
1 | 1 | import os |
| 2 | +import shutil |
2 | 3 | import stat |
3 | 4 | import subprocess |
4 | 5 | import sys |
@@ -263,7 +264,79 @@ def setup_shell(force=False): |
263 | 264 | bashrc_path = os.path.expanduser("~/.bashrc") |
264 | 265 |
|
265 | 266 | line = f"source {get_current_shell_addins()}" |
266 | | - if not is_line_in_file(line, bashrc_path): |
| 267 | + # Detect any existing sourcing of shell_addins.sh (even from old paths) |
| 268 | + existing_lines = [] |
| 269 | + try: |
| 270 | + with open(bashrc_path, "r") as f: |
| 271 | + for file_line in f: |
| 272 | + stripped = file_line.strip() |
| 273 | + if ( |
| 274 | + stripped.startswith("source") |
| 275 | + and "/arm_cli/system/shell_scripts/shell_addins.sh" in stripped |
| 276 | + ): |
| 277 | + existing_lines.append(stripped) |
| 278 | + except FileNotFoundError: |
| 279 | + existing_lines = [] |
| 280 | + |
| 281 | + # If there are old references that don't match the current path, warn the user |
| 282 | + outdated_lines = [old_line for old_line in existing_lines if old_line != line] |
| 283 | + if outdated_lines: |
| 284 | + click.secho( |
| 285 | + "Warning: Found old shell addins entries in ~/.bashrc that reference a previous Python/site-packages path.", |
| 286 | + fg="yellow", |
| 287 | + err=True, |
| 288 | + ) |
| 289 | + for old in outdated_lines: |
| 290 | + click.secho(f" - {old}", fg="yellow", err=True) |
| 291 | + click.secho( |
| 292 | + "It is recommended to remove these old lines to avoid duplicate sourcing.", |
| 293 | + fg="yellow", |
| 294 | + err=True, |
| 295 | + ) |
| 296 | + |
| 297 | + # Offer to update outdated entries in-place to the current path |
| 298 | + if force or click.confirm( |
| 299 | + "Do you want me to update these outdated entries to the current path now? " |
| 300 | + "A backup will be saved to /tmp/.bashrc_backup." |
| 301 | + ): |
| 302 | + try: |
| 303 | + # Backup current ~/.bashrc |
| 304 | + shutil.copyfile(bashrc_path, "/tmp/.bashrc_backup") |
| 305 | + with open(bashrc_path, "r") as f: |
| 306 | + contents = f.readlines() |
| 307 | + new_contents = [] |
| 308 | + for file_line in contents: |
| 309 | + if ( |
| 310 | + file_line.strip().startswith("source") |
| 311 | + and "/arm_cli/system/shell_scripts/shell_addins.sh" in file_line |
| 312 | + and file_line.strip() != line |
| 313 | + ): |
| 314 | + new_contents.append(line + "\n") |
| 315 | + else: |
| 316 | + new_contents.append(file_line) |
| 317 | + with open(bashrc_path, "w") as f: |
| 318 | + f.writelines(new_contents) |
| 319 | + # Refresh existing_lines state after modification |
| 320 | + existing_lines = [] |
| 321 | + for file_line in new_contents: |
| 322 | + stripped = file_line.strip() |
| 323 | + if ( |
| 324 | + stripped.startswith("source") |
| 325 | + and "/arm_cli/system/shell_scripts/shell_addins.sh" in stripped |
| 326 | + ): |
| 327 | + existing_lines.append(stripped) |
| 328 | + print( |
| 329 | + "Updated outdated shell addins entries in ~/.bashrc (backup at /tmp/.bashrc_backup)" |
| 330 | + ) |
| 331 | + except Exception as e: |
| 332 | + click.secho( |
| 333 | + f"Failed to update ~/.bashrc automatically: {e}", |
| 334 | + fg="yellow", |
| 335 | + err=True, |
| 336 | + ) |
| 337 | + |
| 338 | + # If the current line is not present, append it |
| 339 | + if line not in existing_lines: |
267 | 340 | print(f'Adding \n"{line}"\nto {bashrc_path}') |
268 | 341 | if not force: |
269 | 342 | if not click.confirm("Do you want to add shell autocomplete to ~/.bashrc?"): |
|
0 commit comments