-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathgenerate-solution-symlink.py
executable file
·115 lines (80 loc) · 3.76 KB
/
generate-solution-symlink.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python
# -*- coding: utf-8 -*-
from os import path, makedirs, mkdir, listdir, walk, remove, symlink, chdir
from shutil import rmtree
import subprocess
import platform
import re
import os
def make_centralized_solution_links(dir_path):
if path.exists(dir_path):
rmtree(dir_path)
makedirs(dir_path)
swift_source_codes = search_swift_source_code('./')
for file in swift_source_codes:
re_str = re.compile('^q')
symlink_name = re_str.sub('L', path.basename(file))
# print(symlink_name)
rel_path = path.relpath(path.abspath(file), dir_path)
# print(path.join(dir_path, symlink_name) + ' ---> ' + rel_path)
symlink(rel_path, path.join(dir_path, symlink_name))
print('Total %d symbolic links created in directory "%s"' % (len(swift_source_codes), path.abspath(dir_path)))
def make_category_solution_links(dir_path):
if path.exists(dir_path):
rmtree(dir_path)
makedirs(dir_path)
swift_source_codes = search_swift_source_code('./')
indicator = '// Category*'
category_num = 0
symlink_num = 0
for file in swift_source_codes:
with open(file, 'rU') as f:
for line in f:
if line.startswith(indicator):
categories = list(set(line.replace('\n','').split(' ')).difference(set(['//','Category*',':',''])))
for category in categories:
category_dir_path = path.join(dir_path, category)
if not path.exists(category_dir_path):
mkdir(category_dir_path)
category_num += 1
re_str = re.compile('^q')
symlink_name = re_str.sub('L', path.basename(file))
rel_path = path.relpath(path.abspath(file), category_dir_path)
# print(path.join(category_dir_path, path.basename(file)) + ' ---> ' + rel_path)
symlink(rel_path, path.join(category_dir_path, symlink_name))
symlink_num += 1
break
print('Total %d symbolic links of %d categories created in directory "%s"' % (symlink_num, category_num, path.abspath(dir_path)))
def search_swift_source_code(searchPath):
if not path.isdir(searchPath):
return None
result = []
for root,dirs,files in walk(searchPath):
for file in files:
abs_path = path.join(root,file)
if path.isfile(abs_path) and not path.islink(abs_path):
if path.splitext(file)[1] == '.swift' and file[0] == 'q':
result.append(abs_path)
return result
def opendir(path):
if platform.system() == 'Windows':
os.startfile(path)
elif platform.system() == 'Darwin':
subprocess.Popen(['open', path])
elif platform.system == 'Linux':
subprocess.Popen(['xdg-open', path])
def find_proiject_root_dir(project_name):
current_dir = path.dirname(path.realpath(__file__)) # get script file dir
xcode_project_name = project_name + '.xcodeproj'
while not path.exists(path.join(current_dir, xcode_project_name)):
current_dir = path.dirname(current_dir)
print(current_dir)
chdir(current_dir)
if __name__ == '__main__':
find_proiject_root_dir('leetcode-swift')
topdir = './solution-links'
centrailized_solution_links_dir = path.join(topdir,'centralized')
category_solution_links_dir = path.join(topdir,'category')
make_centralized_solution_links(centrailized_solution_links_dir)
make_category_solution_links(category_solution_links_dir)
opendir(topdir)