Skip to content

Instantly share code, notes, and snippets.

@antonijn
Created February 16, 2025 12:48
Show Gist options
  • Save antonijn/5521b8ecd6e5aaef8692d218c4f03c55 to your computer and use it in GitHub Desktop.
Save antonijn/5521b8ecd6e5aaef8692d218c4f03c55 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import tempfile
import shutil
import difflib
from pathlib import Path
# Define source and destination paths
source = Path(__file__).resolve().parent
dest = Path('/opt/X-Plane 12/Custom Scenery')
spack = dest / 'scenery_packs.ini'
print('Taking Ortho from directory', source)
print('Installing into', dest)
def eligible(tile):
skipme = tile / '.skipme'
if skipme.exists():
print('Skipping pack because skipme file exists:', skipme)
return False
return True
# Find directories (tiles) in the source directory
tiles = [tile for tile in source.iterdir() if tile.is_dir() and eligible(tile)]
scenery_packs = [f'SCENERY_PACK Custom Scenery/{tile.name}/\n' for tile in tiles]
# Iterate over the tiles and append scenery pack info
for tile in tiles:
dest_tile = dest / tile.name
if not dest_tile.exists():
os.symlink(tile, dest_tile)
# Create a temporary file
with tempfile.NamedTemporaryFile(mode='w+') as tmpf:
# Write the contents of scenery_packs.ini excluding lines containing 'zOrtho'
with spack.open('r') as spack_file:
for line in spack_file:
if line in scenery_packs:
scenery_packs.remove(line)
tmpf.write(line)
# Iterate over the tiles and append scenery pack info
for sp_line in scenery_packs:
tmpf.write(sp_line)
tmpf.seek(0)
with spack.open('r') as spack_file:
spack_lines = spack_file.readlines()
tmpf_lines = tmpf.readlines()
diff = difflib.unified_diff(spack_lines, tmpf_lines, fromfile=str(spack), tofile=tmpf.name)
for line in diff:
print(line, end='')
# Backup the original `scenery_packs.ini` and replace it with the temporary file (equivalent to `cp -b "${TMPF}" "${SPACK}"`)
backup_path = spack.with_suffix('.bak')
shutil.copy2(spack, backup_path) # Backup original file
shutil.copy2(tmpf.name, spack) # Replace original with the modified file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment