GitSyncMaster / repo_pull.py
魏然
魏然 首次上传于 2024/04/13
e80fd1f
import os
import subprocess
import sys
import hashlib
import random
import string
def get_file_count(repo_path):
# 使用git命令获取仓库中的文件数量
result = subprocess.run(['git', 'ls-files'], cwd=repo_path, capture_output=True, text=True)
files = result.stdout.split('\n')
# 过滤掉空字符串
files = list(filter(None, files))
return len(files)
def generate_random_string(length):
# 生成随机字符串
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=length))
def clone_repository_without_progress(repo_url):
# 生成随机的目标路径
destination_path = "./" + generate_random_string(6)
# 克隆仓库
try:
subprocess.run(['git', 'clone', '--recursive', repo_url, destination_path], check=True)
print("Repository cloned successfully!")
except Exception as e:
print(f"An error occurred while cloning the repository: {e}")
return
# 获取文件总数
total_files = get_file_count(destination_path)
# 遍历仓库中的文件,不再显示进度条
for root, dirs, files in os.walk(destination_path):
for file in files:
pass
# 获取第一层文件夹名字
first_layer_folder = os.path.basename(destination_path)
# 使用哈希值作为新文件夹名
folder_hash = hashlib.md5(first_layer_folder.encode()).hexdigest()[:6]
new_folder_name = f"{folder_hash}"
new_folder_path = os.path.join(os.path.dirname(destination_path), new_folder_name)
# 重命名第一层文件夹
os.rename(destination_path, new_folder_path)
# 记录文件夹路径
with open('.folderinfo', 'a') as f:
f.write(new_folder_path + '\n')
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python3 script.py --repo_url <repository_url>")
sys.exit(1)
if sys.argv[1] != "--repo_url":
print("Usage: python3 script.py --repo_url <repository_url>")
sys.exit(1)
repo_url = sys.argv[2]
clone_repository_without_progress(repo_url)