79 lines
2.9 KiB
Python
79 lines
2.9 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""公开仓推之前的路径脱敏。
|
||
|
||
源码里带着开发机的绝对路径(C:\\Users\\xxx、D:\\DSH\\...),
|
||
这些东西推到公开仓库等于把自己的目录结构、用户名、SDK 位置全挂出去。
|
||
本地源码保持原样(能直接跑),只在同步进公开仓的那份副本上做替换。
|
||
|
||
用法:
|
||
python tools/blank_paths.py <公开仓里的 astral/maipulse/android 根目录>
|
||
python tools/blank_paths.py D:\\DSH\\maimai\\_repo --dry
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import io
|
||
import os
|
||
import re
|
||
import sys
|
||
|
||
# (匹配模式, 替换成) —— 顺序有讲究:长的先替换,否则 \maimai 会先被短规则吃掉
|
||
RULES = [
|
||
(re.compile(r'C:[\\/]Users[\\/]20558[\\/]AppData[\\/]Local[\\/]Programs[\\/]Python[\\/]Python38[\\/]python\.exe'),
|
||
'python'),
|
||
(re.compile(r'C:[\\/]Users[\\/]20558'), '%USERPROFILE%'),
|
||
(re.compile(r'D:[\\/]DSH[\\/]_androidsdk'), '%ANDROID_HOME%'),
|
||
(re.compile(r'D:[\\/]DSH[\\/]maimai[\\/]maipulse-android'), '<android 工程>'),
|
||
(re.compile(r'D:[\\/]DSH[\\/]maimai[\\/]maipulse'), '<maipulse>'),
|
||
(re.compile(r'D:[\\/]DSH[\\/]maimai[\\/]Astral'), '<astral>'),
|
||
(re.compile(r'D:[\\/]DSH[\\/]maimai[\\/]_repo'), '<仓库>'),
|
||
(re.compile(r'D:[\\/]DSH[\\/]maimai'), '<项目根>'),
|
||
(re.compile(r'qmy2055885931@gmail\.com'), '[email protected]'),
|
||
]
|
||
|
||
TEXT_EXT = ('.py', '.ps1', '.gradle', '.xml', '.md', '.json', '.properties',
|
||
'.java', '.txt', '.yml', '.yaml', '.gitignore', '.pro')
|
||
SKIP_DIRS = {'.git', '__pycache__', 'data', 'logs'}
|
||
|
||
|
||
def process(root, dry=False):
|
||
changed = []
|
||
for dp, dn, fn in os.walk(root):
|
||
dn[:] = [d for d in dn if d not in SKIP_DIRS]
|
||
for f in fn:
|
||
if not f.endswith(TEXT_EXT) and f not in ('.gitignore', 'gradlew'):
|
||
continue
|
||
fp = os.path.join(dp, f)
|
||
if os.path.getsize(fp) > 3_000_000:
|
||
continue
|
||
try:
|
||
t = io.open(fp, encoding='utf-8').read()
|
||
except (UnicodeDecodeError, OSError):
|
||
continue
|
||
orig = t
|
||
for rx, rep in RULES:
|
||
t = rx.sub(rep, t)
|
||
if t != orig:
|
||
changed.append(os.path.relpath(fp, root))
|
||
if not dry:
|
||
io.open(fp, 'w', encoding='utf-8', newline='\n').write(t)
|
||
return changed
|
||
|
||
|
||
def main():
|
||
if len(sys.argv) < 2:
|
||
raise SystemExit(__doc__)
|
||
root = sys.argv[1]
|
||
dry = '--dry' in sys.argv
|
||
if not os.path.isdir(root):
|
||
raise SystemExit('不是目录:%s' % root)
|
||
changed = process(root, dry)
|
||
print('%s %d 个文件%s' % ('会改' if dry else '已改', len(changed), '(--dry 未落盘)' if dry else ''))
|
||
for c in changed[:40]:
|
||
print(' ', c)
|
||
if len(changed) > 40:
|
||
print(' ... 还有 %d 个' % (len(changed) - 40))
|
||
return 0
|
||
|
||
|
||
if __name__ == '__main__':
|
||
sys.exit(main()) |