|
| 1 | +"""Job module configuration and path management.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +def config_home() -> Path: |
| 8 | + """Get XDG config home directory.""" |
| 9 | + xdg = os.getenv("XDG_CONFIG_HOME") |
| 10 | + if xdg: |
| 11 | + return Path(xdg).expanduser() / "jsrc" |
| 12 | + return Path.home() / ".config" / "jsrc" |
| 13 | + |
| 14 | + |
| 15 | +def data_home() -> Path: |
| 16 | + """Get XDG data home directory.""" |
| 17 | + xdg = os.getenv("XDG_DATA_HOME") |
| 18 | + if xdg: |
| 19 | + return Path(xdg).expanduser() / "jsrc" |
| 20 | + return Path.home() / ".local" / "share" / "jsrc" |
| 21 | + |
| 22 | + |
| 23 | +def history_path() -> Path: |
| 24 | + """Get job history file path.""" |
| 25 | + override = os.getenv("JSRC_JOBS_FILE", "") |
| 26 | + if override: |
| 27 | + return Path(override).expanduser() |
| 28 | + return config_home() / "job" / "history" |
| 29 | + |
| 30 | + |
| 31 | +def default_log_dir() -> Path: |
| 32 | + """Get default directory for job log files.""" |
| 33 | + return data_home() / "job-logs" |
| 34 | + |
| 35 | + |
| 36 | +def state_dir() -> Path: |
| 37 | + """Get directory for job state files.""" |
| 38 | + return data_home() / "job-state" |
| 39 | + |
| 40 | + |
| 41 | +def ensure_dirs() -> None: |
| 42 | + """Create all required directories if they don't exist.""" |
| 43 | + history_path().parent.mkdir(parents=True, exist_ok=True) |
| 44 | + default_log_dir().mkdir(parents=True, exist_ok=True) |
| 45 | + state_dir().mkdir(parents=True, exist_ok=True) |
| 46 | + |
| 47 | + |
| 48 | +def _migrate_old_history() -> None: |
| 49 | + """Migrate old history file to new location.""" |
| 50 | + old = data_home() / "jobs" |
| 51 | + new = history_path() |
| 52 | + if old.exists() and not new.exists(): |
| 53 | + new.parent.mkdir(parents=True, exist_ok=True) |
| 54 | + old.rename(new) |
0 commit comments