2025-12-08 13:02:05 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
2025-03-06 12:07:55 +01:00
|
|
|
import os
|
|
|
|
|
from pkgmgr.get_repo_identifier import get_repo_identifier
|
|
|
|
|
from pkgmgr.get_repo_dir import get_repo_dir
|
|
|
|
|
|
2025-12-08 13:02:05 +01:00
|
|
|
|
|
|
|
|
def create_ink(repo, repositories_base_dir, bin_dir, all_repos,
|
|
|
|
|
quiet=False, preview=False):
|
2025-03-06 12:07:55 +01:00
|
|
|
"""
|
2025-12-08 13:02:05 +01:00
|
|
|
Create a symlink for the repository's command.
|
|
|
|
|
|
|
|
|
|
IMPORTANT:
|
|
|
|
|
This function is intentionally kept *simple*. All decision logic for
|
|
|
|
|
choosing the command lives inside resolve_command_for_repo().
|
|
|
|
|
|
|
|
|
|
Behavior:
|
|
|
|
|
- If repo["command"] is defined → create a symlink to it.
|
|
|
|
|
- If repo["command"] is missing or None → do NOT create a link.
|
2025-03-06 12:07:55 +01:00
|
|
|
"""
|
2025-12-08 13:02:05 +01:00
|
|
|
|
2025-03-06 12:07:55 +01:00
|
|
|
repo_identifier = get_repo_identifier(repo, all_repos)
|
|
|
|
|
repo_dir = get_repo_dir(repositories_base_dir, repo)
|
2025-12-08 13:02:05 +01:00
|
|
|
|
2025-03-06 12:07:55 +01:00
|
|
|
command = repo.get("command")
|
|
|
|
|
if not command:
|
2025-12-08 13:02:05 +01:00
|
|
|
if not quiet:
|
|
|
|
|
print(f"No command resolved for '{repo_identifier}'. Skipping link.")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
link_path = os.path.join(bin_dir, repo_identifier)
|
|
|
|
|
|
|
|
|
|
if preview:
|
|
|
|
|
print(f"[Preview] Would link {link_path} → {command}")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Mark local repo scripts as executable if needed
|
|
|
|
|
try:
|
|
|
|
|
if os.path.realpath(command).startswith(os.path.realpath(repo_dir)):
|
|
|
|
|
os.chmod(command, 0o755)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
if not quiet:
|
|
|
|
|
print(f"Failed to set permissions on '{command}': {e}")
|
|
|
|
|
|
|
|
|
|
# Create bin directory
|
|
|
|
|
os.makedirs(bin_dir, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
# Remove existing
|
|
|
|
|
if os.path.exists(link_path) or os.path.islink(link_path):
|
|
|
|
|
os.remove(link_path)
|
|
|
|
|
|
|
|
|
|
# Create the link
|
|
|
|
|
os.symlink(command, link_path)
|
|
|
|
|
|
|
|
|
|
if not quiet:
|
|
|
|
|
print(f"Symlink created: {link_path} → {command}")
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
# Optional alias support (same as before)
|
|
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
alias_name = repo.get("alias")
|
|
|
|
|
if alias_name:
|
|
|
|
|
alias_link_path = os.path.join(bin_dir, alias_name)
|
|
|
|
|
|
|
|
|
|
if alias_name == repo_identifier:
|
2025-03-06 12:07:55 +01:00
|
|
|
if not quiet:
|
2025-12-08 13:02:05 +01:00
|
|
|
print(f"Alias '{alias_name}' equals identifier. Skipping alias creation.")
|
2025-03-06 12:07:55 +01:00
|
|
|
return
|
|
|
|
|
|
2025-03-06 12:42:49 +01:00
|
|
|
try:
|
2025-12-08 13:02:05 +01:00
|
|
|
if os.path.exists(alias_link_path) or os.path.islink(alias_link_path):
|
|
|
|
|
os.remove(alias_link_path)
|
|
|
|
|
os.symlink(link_path, alias_link_path)
|
|
|
|
|
if not quiet:
|
|
|
|
|
print(f"Alias '{alias_name}' created → {repo_identifier}")
|
2025-03-06 12:42:49 +01:00
|
|
|
except Exception as e:
|
|
|
|
|
if not quiet:
|
2025-12-08 13:02:05 +01:00
|
|
|
print(f"Error creating alias '{alias_name}': {e}")
|