Skip to content

Instantly share code, notes, and snippets.

@fanyang89
Created February 28, 2022 08:51
Show Gist options
  • Save fanyang89/292481cbe21153af550ece35b3f8b26f to your computer and use it in GitHub Desktop.
Save fanyang89/292481cbe21153af550ece35b3f8b26f to your computer and use it in GitHub Desktop.
rocky-mirror.py
#!/usr/bin/env python3
import glob
import os
try:
import in_place
except ImportError:
print("Trying to Install required module: in-place")
os.system('python3 -m pip install in-place')
BASE_DIR = "/etc/yum.repos.d/"
PARAM_COUNTRY = "&country=CN"
repo_files = glob.glob(f"{BASE_DIR}/*repo")
for repo in repo_files:
with in_place.InPlace(repo, backup_ext='.bak') as fp:
for line in fp:
if line.startswith("mirrorlist=") and ("mirrors.rockylinux.org/mirrorlist" in line) and (not line.strip().endswith(PARAM_COUNTRY)):
fp.write(line.strip() + PARAM_COUNTRY + "\n")
else:
fp.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment