Created
February 28, 2022 08:51
-
-
Save fanyang89/292481cbe21153af550ece35b3f8b26f to your computer and use it in GitHub Desktop.
rocky-mirror.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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