github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/module_rename.py (about) 1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 4 import os 5 import argparse 6 import fileinput 7 8 9 PKG_ORIGINAL = "gvisor.dev/gvisor" 10 PKG_NEW = "github.com/metacubex/gvisor" 11 12 EXTENSIONS = [".go", ".md", ".mod", ".sh"] 13 14 parser = argparse.ArgumentParser() 15 parser.add_argument("-r", "--reverse", action="store_true") 16 args = parser.parse_args() 17 18 19 def replace_line(line): 20 if args.reverse: 21 return line.replace(PKG_NEW, PKG_ORIGINAL) 22 return line.replace(PKG_ORIGINAL, PKG_NEW) 23 24 25 for dirpath, dirnames, filenames in os.walk("."): 26 # Skip hidden directories like .git 27 dirnames[:] = [d for d in dirnames if not d[0] == "."] 28 filenames = [f for f in filenames if os.path.splitext(f)[1] in EXTENSIONS] 29 for filename in filenames: 30 file_path = os.path.join(dirpath, filename) 31 with fileinput.FileInput(file_path, inplace=True) as file: 32 for line in file: 33 print(replace_line(line), end="")