You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
3.0 KiB
70 lines
3.0 KiB
#!/bin/env python3 |
|
# -*- coding: utf-8 -*- |
|
|
|
''' |
|
link.py 为所有原始库文件创建软链接,并修复失效的软链接 |
|
''' |
|
|
|
import os |
|
import re |
|
from pathlib import Path |
|
|
|
def create_links(): |
|
# 获取脚本所在目录作为工作目录 |
|
base_dir = Path(__file__).parent.resolve() |
|
print(f"Working in directory: {base_dir}") |
|
|
|
# 正则匹配 lib*.so.X.Y.Z 格式 |
|
pattern = re.compile(r'^lib.*\.so\.(\d+)\.\d+\.\d+$') |
|
|
|
# 递归遍历所有文件 |
|
for root, dirs, files in os.walk(base_dir): |
|
for filename in files: |
|
match = pattern.match(filename) |
|
if match: |
|
version = match.group(1) |
|
file_path = Path(root) / filename |
|
rel_path = file_path.relative_to(base_dir) |
|
|
|
# 在原始文件所在目录创建链接 |
|
target_dir = base_dir / rel_path.parent |
|
target_dir.mkdir(parents=True, exist_ok=True) |
|
|
|
# 生成链接文件名 |
|
base_name = filename.split('.so.', 1)[0] + '.so' |
|
version_link = f"{base_name}.{version}" |
|
main_link = base_name |
|
|
|
# 创建相对路径链接(保持目录层级) |
|
version_link_path = target_dir / version_link |
|
# 版本号链接 libxxx.so.X -> libxxx.so.X.Y.Z |
|
try: |
|
os.symlink(filename, version_link_path) |
|
except FileExistsError: |
|
# 检查是否是符号链接以及链接是否有效 |
|
if os.path.islink(version_link_path) and not os.path.exists(version_link_path): |
|
# 链接存在但指向无效路径,删除并重新创建 |
|
os.unlink(version_link_path) |
|
os.symlink(filename, version_link_path) |
|
print(f"Recreated broken link: {rel_path.parent}/{version_link} -> {filename}") |
|
else: |
|
pass # 链接已存在且有效,跳过 |
|
|
|
main_link_path = target_dir / main_link |
|
# 主链接 libxxx.so -> libxxx.so.X |
|
try: |
|
os.symlink(version_link, main_link_path) |
|
print(f"Created: {rel_path.parent}/{main_link} -> {version_link} -> {filename}") |
|
except FileExistsError: |
|
# 检查是否是符号链接以及链接是否有效 |
|
if os.path.islink(main_link_path) and not os.path.exists(main_link_path): |
|
# 链接存在但指向无效路径,删除并重新创建 |
|
os.unlink(main_link_path) |
|
os.symlink(version_link, main_link_path) |
|
print(f"Recreated broken link: {rel_path.parent}/{main_link} -> {version_link}") |
|
else: |
|
pass # 链接已存在且有效,跳过 |
|
|
|
if __name__ == "__main__": |
|
create_links() |
|
print("Symbolic links updated with relative paths!")
|
|
|