Skip to content

Commit 9868296

Browse files
committed
fix: fix move script
1 parent 91b5fd5 commit 9868296

File tree

1 file changed

+49
-45
lines changed

1 file changed

+49
-45
lines changed

mv.py

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,28 @@
99

1010
class MoveEventHandler(FileSystemEventHandler):
1111
def on_moved(self, event):
12+
if '.git' in event.src_path or '.git' in event.dest_path:
13+
return
14+
1215
if event.is_directory:
1316
print(f"Directory moved from {event.src_path} to {event.dest_path}")
1417
else:
1518
print(f"File moved from {event.src_path} to {event.dest_path}")
1619

1720
repo_path = os.getcwd()
18-
21+
1922
source = get_redirect(event.src_path, repo_path)
2023
destination = get_redirect(event.dest_path, repo_path)
2124

22-
print(source)
23-
print(destination)
25+
print("from_r:",source)
26+
print("to_r:", destination)
2427

2528
# path_r = destination.split("/")[1]
29+
json_file = f"./redirects/redirects.json"
2630
file_extensions = ['.mdx', '.md']
2731

2832
replace_in_repo(repo_path, source, destination, file_extensions)
29-
redirect(source, destination)
33+
redirect(json_file, source, destination)
3034

3135

3236
def start_monitoring(path):
@@ -42,13 +46,13 @@ def start_monitoring(path):
4246
time.sleep(1)
4347
except KeyboardInterrupt:
4448
observer.stop()
45-
49+
4650
observer.join()
4751

4852
def find_markdown_links(text):
4953
md_link_pattern = r'\[([^\]]+)\]\(([^#)]+)([^)]*)\)'
5054
matches = re.findall(md_link_pattern, text)
51-
55+
5256
return matches
5357

5458
def replace_word_in_file(file_path, target, replacement):
@@ -70,53 +74,57 @@ def replace_word_in_file(file_path, target, replacement):
7074

7175
def replace_in_repo(repo_path, target, replacement, file_extensions=None):
7276
for root, dirs, files in os.walk(repo_path):
73-
print(root, repo_path)
74-
# if root == repo_path:
75-
# for file_name in files:
76-
# if file_name == "navbar.js" or file_name == "redirects.js":
77-
# file_path = os.path.join(root, file_name)
78-
# replace_word_in_file(file_path, target, replacement)
79-
# continue
80-
81-
# path_list = root.split(repo_path)
82-
# if len(path_list) > 1 and path_list[1] == "/sidebars":
83-
# for file_name in files:
84-
# file_path = os.path.join(root, file_name)
85-
# replace_word_in_file(file_path, target, replacement)
86-
# continue
87-
88-
# for file_name in files:
89-
# if file_extensions:
90-
# if not any(file_name.endswith(ext) for ext in file_extensions):
91-
# continue
92-
93-
# if root.find("node_modules") != -1:
94-
# continue
95-
96-
# file_path = os.path.join(root, file_name)
97-
# replace_word_in_file(file_path, target, replacement)
77+
if '.git' in root or 'i18n' in root:
78+
continue
79+
80+
if root == repo_path:
81+
for file_name in files:
82+
if file_name == "navbar.js" or file_name == "redirects.js":
83+
file_path = os.path.join(root, file_name)
84+
replace_word_in_file(file_path, target, replacement)
85+
continue
86+
87+
path_list = root.split(repo_path)
88+
if len(path_list) > 1 and path_list[1] == "/sidebars":
89+
for file_name in files:
90+
file_path = os.path.join(root, file_name)
91+
replace_word_in_file(file_path, target, replacement)
92+
continue
93+
94+
for file_name in files:
95+
if file_extensions:
96+
if not any(file_name.endswith(ext) for ext in file_extensions):
97+
continue
98+
99+
if root.find("node_modules") != -1:
100+
continue
101+
102+
file_path = os.path.join(root, file_name)
103+
replace_word_in_file(file_path, target, replacement)
98104

99105
def get_redirect(path, repo):
100-
res = os.path.splitext(path)[0]
106+
res = os.path.splitext(path)
107+
res = res[0]
101108
spl = res.split(repo)
102-
res = spl[1].split('/docs')[1]
109+
res = spl[1].split('/docs', 1)
110+
res = res[1]
103111
return res
104112

105-
def redirect(from_r, to_r):
113+
def redirect(json_file, from_r, to_r):
106114
obj = {
107115
"from": from_r,
108116
"to": to_r
109117
}
110118

111-
# with open(json_file, 'r') as file:
112-
# data = json.load(file)
113-
114-
redirect_data.append(obj)
119+
with open(json_file, 'r') as file:
120+
data = json.load(file)
121+
122+
data.append(obj)
123+
124+
with open(json_file, 'w') as file:
125+
json.dump(data, file, indent=4)
115126

116-
# with open(json_file, 'w') as file:
117-
# json.dump(obj, file, indent=4)
118127

119-
120128
if __name__ == "__main__":
121129
repo_path = os.getcwd()
122130

@@ -131,10 +139,6 @@ def redirect(from_r, to_r):
131139
while True:
132140
time.sleep(1)
133141
except KeyboardInterrupt:
134-
print("STOPPED")
135-
json_file = f"./redirects/redirects.json"
136-
with open(json_file, 'w') as file:
137-
json.dump(redirect_data, file, indent=4)
138142
observer.stop()
139143

140144
observer.join()

0 commit comments

Comments
 (0)