下面是一个在Windows系统下下载两个模块为例,可以适配到自己的项目中
import subprocess
import os
# 配置模块信息:模块名称、仓库地址和指定标签
modules = [
{
"name": "ArbitrationManager", # 模块名称
"repo_url": r"https://git.ecarxgroup.com/jica/adcu/asw/asw_pilot_mcu/ArbitrationManager.git", # 替换为你的GitLab模块1仓库地址
"tag_name": "AM_V8-4-1_R_V1.00" # 替换为模块1要下载的tag
},
{
"name": "AnotherModule", # 替换为你的模块名称
"repo_url": r"https://git.ecarxgroup.com/jica/adcu/asw/asw_pilot_mcu/AnotherModule.git", # 替换为你的GitLab模块2仓库地址
"tag_name": "TAG_V1.0" # 替换为模块2要下载的tag
},
# 可以继续添加更多模块
]
for module in modules:
module_name = module['name']
repo_url = module['repo_url']
tag_name = module['tag_name']
clone_dir = os.path.join(os.getcwd(), f"{module_name}_{tag_name}") # 使用模块名称和tag作为文件夹名
# 检查克隆目录是否已存在,若存在则删除
if os.path.exists(clone_dir):
subprocess.run(["rmdir", "/s", "/q", clone_dir], shell=True)
# 执行git clone命令
try:
subprocess.run(["git", "clone", "--branch", tag_name, repo_url, clone_dir], check=True)
print(f"{module_name} 下载成功,代码已保存到 {clone_dir} 文件夹中。")
except subprocess.CalledProcessError:
print(f"{module_name} 下载失败,请检查仓库地址和tag名。")
使用说明:
- 将上述代码复制到一个新的文本文件中,并保存为
download_multiple_gitlab_modules.py
。 - 在
modules
列表中配置你的GitLab模块信息。每个模块包括模块名称、仓库地址和要下载的tag。可以根据需要添加或修改模块。 - 运行该Python脚本,脚本将自动下载每个模块的代码到以模块名称和tag命名的文件夹中。
注意:
- 确保你的计算机已安装Python,并且已安装Git,且Git的命令行工具已添加到系统的PATH环境变量中,以便在Python中使用
git
命令。 - 在执行此脚本时,请确保你有必要的权限和网络连接,能够访问所提供的GitLab仓库。