#!/usr/bin/env python import subprocess from pathlib import Path from rich import print def create_symlink(source, target): try: target_path = Path(target) # Check if the target already exists and remove it if it does if target_path.exists() or target_path.is_symlink(): target_path.unlink() # Create the symbolic link target_path.symlink_to(source) print(f'Created symlink: [magenta]{source}[/] -> [magenta]{target}[/]') except OSError as e: print(f'Error creating symlink: [bold red]{e}[/]') def main(): repo_dir = Path(__file__).resolve().parent systemd_dir = Path('/etc/systemd/system') # Define the source and target paths name = 'example' socket_file = f'{name}.socket' service_file = f'{name}.service' # Create symlinks create_symlink(repo_dir / socket_file, systemd_dir / socket_file) create_symlink(repo_dir / service_file, systemd_dir / service_file) # Reload systemd, start and enable the socket try: cmd_kwargs = dict(check=True, text=True, capture_output=True) subprocess.run(['sudo', 'systemctl', 'daemon-reload'], **cmd_kwargs) print('Reloaded systemd services') subprocess.run(['sudo', 'systemctl', 'start', socket_file], **cmd_kwargs) print(f'Started [blue]{socket_file}[/]') subprocess.run(['sudo', 'systemctl', 'enable', socket_file], **cmd_kwargs) print(f'Enabled [blue]{socket_file}[/] to start at boot') except subprocess.CalledProcessError as e: print('Error:', e.stderr) if __name__ == '__main__': main()