先来极速版操作流程:
操作流程
克隆CPython储存库
git clone https://github.com/python/cpython --depth=1
执行此脚本(更改替换的名称)
import os
import shutil
def replace_in_file(file_path, rules):
"""读取文件内容并替换字符串,然后重新写入文件"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
modified = False
for old, new in rules.items():
if old in content:
content = content.replace(old, new)
modified = True
if modified:
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
print(f"Updated content in {file_path}")
except UnicodeDecodeError as e:
print(f"Error reading {file_path}: {e}")
except Exception as e:
print(f"Error processing {file_path}: {e}")
def replace_in_path(path, rules):
"""替换文件夹路径和文件名中的字符串"""
for root, dirs, files in os.walk(path, topdown=False):
# 替换文件名
for name in files:
new_name = name
for old, new in rules.items():
new_name = new_name.replace(old, new)
if new_name != name:
original_path = os.path.join(root, name)
new_path = os.path.join(root, new_name)
shutil.move(original_path, new_path)
print(f"Renamed {original_path} to {new_path}")
# 替换目录名
for name in dirs:
new_name = name
for old, new in rules.items():
new_name = new_name.replace(old, new)
if new_name != name:
original_path = os.path.join(root, name)
new_path = os.path.join(root, new_name)
shutil.move(original_path, new_path)
print(f"Renamed directory {original_path} to {new_path}")
def walk_and_replace(root_path, rules):
"""遍历目录并应用替换规则到文件名、文件夹名和文件内容"""
for root, dirs, files in os.walk(root_path):
# 替换文件内容
for file in files:
file_path = os.path.join(root, file)
replace_in_file(file_path, rules)
# 替换文件名和目录名
replace_in_path(root, rules)
# 替换规则:键是原始字符串,值是替换后的字符串
replacement_rules = {
'Python': 'Genshin',
'python': 'genshin',
'PYTHON': 'GENSHIN'
}
# 指定要处理的根目录
root_directory = './cpython'
# 执行替换操作
walk_and_replace(root_directory, replacement_rules)
然后 编译cpython即可
Tips:这样操作后会导致make install无法正常安装,禁用编译pip即可解决
配置时使用
./configure --without-ensurepip
编译cpython时突发奇想,通过关键词替换的方式能 创造 一门全新的编程语言吗?于是,带着疑问,让GPT帮我写下了这个关键词替换脚本,发现关键词替换后的源码竟然神奇的能够通过编译,遂跟佬友们分享经历