import urllib.request
import os
import re
from pathlib import Path
def down_files_native(url_list):
current_dir = os.path.dirname(os.path.abspath(__file__))
download_dir = os.path.join(current_dir, "cann_3rd_lib_path_download")
os.makedirs(download_dir, exist_ok=True)
for url in url_list:
file_name = url.split('/')[-1]
if not file_name:
file_name = "downloaded_file"
file_path = os.path.join(download_dir, file_name)
urllib.request.urlretrieve(url, file_path)
def extract_urls_from_cmake(cmake_file):
"""
从单个 .cmake 文件中提取所有 HTTPS URL
支持格式:
- set(REQ_URL "https://...")
- set(DOWNLOAD_URL "https://...")
- URL https://...
- "https://..."
"""
urls = []
content = cmake_file.read_text(encoding='utf-8')
patterns = [
r'set\s*\(\s*\w+\s+["\']?(https://[^"\'\s)]+)["\']?\s*\)',
r'URL\s+["\']?(https://[^"\'\s)]+)["\']?',
r'["\'](https://[^"\']+)["\']',
r'(^|\s)(https://\S+)',
]
for pattern in patterns:
matches = re.findall(pattern, content, re.MULTILINE)
for match in matches:
if isinstance(match, tuple):
url = match[1] if len(match) > 1 and match[1] else match[0]
else:
url = match
url = url.strip().rstrip(')')
if url not in urls:
urls.append(url)
return urls
def scan_cmake_files(directory: Path) -> dict[str, list[str]]:
"""
扫描目录下所有 .cmake 文件,提取 URL
Returns:
{文件名: [url1, url2, ...]}
"""
results = {}
if not directory.exists():
raise FileNotFoundError(f"Directory not found: {directory}")
cmake_files = sorted(directory.glob("*.cmake"))
if not cmake_files:
print(f"Warning: No .cmake files found in {directory}")
return results
for cmake_file in cmake_files:
urls = extract_urls_from_cmake(cmake_file)
if urls:
results[cmake_file.name] = urls
else:
print(f"[{cmake_file.name}] No URLs found")
return results
def get_all_urls(directory: Path) -> list[str]:
"""
获取所有 .cmake 文件中的 URL合并为单一列表
"""
results = scan_cmake_files(directory)
all_urls = []
for urls in results.values():
all_urls.extend(urls)
return list(dict.fromkeys(all_urls))
if __name__ == "__main__":
script_path = Path(__file__).resolve()
cmake_dir = script_path.parent.parent.parent / "cmake" / "third_party"
all_urls = get_all_urls(cmake_dir)
down_files_native(all_urls)