This commit is contained in:
2025-07-11 11:23:37 +03:00
parent 48ab6a5403
commit 35e8befc45

View File

@@ -51,7 +51,6 @@ repos = [
"https://github.com/fiksofficial/python-modules" "https://github.com/fiksofficial/python-modules"
] ]
def is_repo_public(repo_url): def is_repo_public(repo_url):
result = subprocess.run( result = subprocess.run(
["git", "ls-remote", repo_url], ["git", "ls-remote", repo_url],
@@ -60,7 +59,6 @@ def is_repo_public(repo_url):
) )
return result.returncode == 0 return result.returncode == 0
def is_repo_accessible(repo_url): def is_repo_accessible(repo_url):
try: try:
response = requests.head(repo_url, timeout=5) response = requests.head(repo_url, timeout=5)
@@ -68,12 +66,10 @@ def is_repo_accessible(repo_url):
except requests.RequestException: except requests.RequestException:
return False return False
def is_valid_filename(filename): def is_valid_filename(filename):
invalid_chars = r'[<>:"/\\|?*]' invalid_chars = r'[<>:"/\\|?*]'
return not re.search(invalid_chars, filename) return not re.search(invalid_chars, filename)
def rename_invalid_files(local_path): def rename_invalid_files(local_path):
for root, dirs, files in os.walk(local_path): for root, dirs, files in os.walk(local_path):
for file in files: for file in files:
@@ -84,11 +80,9 @@ def rename_invalid_files(local_path):
os.rename(old_path, new_path) os.rename(old_path, new_path)
print(f"Переименован файл: {old_path} -> {new_path}") print(f"Переименован файл: {old_path} -> {new_path}")
def get_repo_path(repo_url): def get_repo_path(repo_url):
return repo_url.replace("https://github.com/", "") return repo_url.replace("https://github.com/", "")
def clean_unused_repos(): def clean_unused_repos():
current_dir = os.getcwd() current_dir = os.getcwd()
print(f"Текущая директория: {current_dir}") print(f"Текущая директория: {current_dir}")
@@ -100,15 +94,16 @@ def clean_unused_repos():
} }
print(f"Все директории до фильтрации: {existing_dirs}") print(f"Все директории до фильтрации: {existing_dirs}")
if ".git" in existing_dirs: protected_dirs = {".git", ".github", "assets"}
existing_dirs.remove(".git") existing_dirs.difference_update(protected_dirs)
print(f"Директории после исключения защищённых: {existing_dirs}")
expected_dirs = {get_repo_path(url).split("/")[0] for url in repos} expected_dirs = {get_repo_path(url).split("/")[0] for url in repos}
print(f"Ожидаемые директории: {expected_dirs}") print(f"Ожидаемые директории: {expected_dirs}")
for dir_name in existing_dirs: for dir_name in existing_dirs:
dir_path = os.path.join(current_dir, dir_name) dir_path = os.path.join(current_dir, dir_name)
if dir_name not in expected_dirs and dir_name != ".git": # Двойная проверка if dir_name not in expected_dirs:
shutil.rmtree(dir_path, ignore_errors=True) shutil.rmtree(dir_path, ignore_errors=True)
print(f"Удалена директория, отсутствующая в списке repos: {dir_path}") print(f"Удалена директория, отсутствующая в списке repos: {dir_path}")
@@ -118,15 +113,12 @@ def clean_unused_repos():
if os.path.exists(local_path): if os.path.exists(local_path):
if not is_repo_accessible(repo_url): if not is_repo_accessible(repo_url):
shutil.rmtree(local_path, ignore_errors=True) shutil.rmtree(local_path, ignore_errors=True)
print( print(f"Удалена директория недоступного или удалённого репозитория: {local_path}")
f"Удалена директория недоступного или удалённого репозитория: {local_path}"
)
def clone_or_update_repo(repo_url): def clone_or_update_repo(repo_url):
repo_path = repo_url.replace("https://github.com/", "") repo_path = get_repo_path(repo_url)
owner, repo_name = repo_path.split("/") owner, repo_name = repo_path.split("/")
local_path = f"{owner}/{repo_name}" local_path = os.path.join(owner, repo_name)
if not os.path.exists(owner): if not os.path.exists(owner):
os.makedirs(owner) os.makedirs(owner)
@@ -141,19 +133,18 @@ def clone_or_update_repo(repo_url):
try: try:
subprocess.run( subprocess.run(
["git", "clone", repo_url, local_path], ["git", "clone", "--depth", "1", repo_url, local_path],
check=True, check=True,
capture_output=True, capture_output=True,
text=True, text=True,
) )
shutil.rmtree(os.path.join(local_path, ".git")) shutil.rmtree(os.path.join(local_path, ".git"), ignore_errors=True)
rename_invalid_files(local_path) rename_invalid_files(local_path)
print(f"Клонирован и обработан репозиторий: {repo_url} -> {local_path}") print(f"Клонирован и обработан репозиторий: {repo_url} -> {local_path}")
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
print(f"Ошибка при клонировании {repo_url}: {e.output}, пропускаем.") print(f"Ошибка при клонировании {repo_url}: {e.stderr}, пропускаем.")
if __name__ == "__main__": if __name__ == "__main__":
clean_unused_repos() clean_unused_repos()
for repo_url in repos: for repo_url in repos:
clone_or_update_repo(repo_url) clone_or_update_repo(repo_url)