-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathes6mv.py
238 lines (186 loc) · 7.36 KB
/
es6mv.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import os
import shutil
import sys
# GLOBALS
INSPECT_DIR = os.environ['ES6MV_INSPECT_DIR']
# helper functions
def extractFileFromFilepath(fp):
return os.path.splitext(fp)[0]
def extractExtensionFromFilepath(fp):
return os.path.splitext(fp)[1]
def getInspectFiles():
inspect_files = []
for path, subdirs, files in os.walk(INSPECT_DIR):
for name in files:
inspect_files.append(os.path.join(path, name))
return inspect_files
def isRelativeImport(line):
if (not line.startswith('import ')):
return False
splits = line.split("'")
if(len(splits) < 2):
return False
filepath = splits[1]
if (not(filepath.startswith('./') or filepath.startswith('../'))):
return False
return True
#rename to something better
def extractImportFromStatement(line):
return line.split("'")[1]
def prefixDotSlash(fp):
if (not (fp.startswith('./') or fp.startswith('../'))):
return "./" + fp
else:
return fp
def getDir(filepath):
if (os.path.isdir(filepath)):
return filepath
else:
return os.path.dirname(filepath)
def realpathOfRelativeTargetFromSrc(src_realpath, target_relpath):
save_cwd = os.getcwd()
src_cwd = getDir(src_realpath)
os.chdir(src_cwd)
realpath = os.path.realpath(target_relpath)
os.chdir(save_cwd)
return realpath
def generateNewRelativePath(target_realpath, src_realdir):
return os.path.relpath(target_realpath, src_realdir)
def generateNewImportStatement(line, dest_realdir):
parsed = line.split("'")
filepath = parsed[1]
cwd = os.getcwd()
os.chdir(src_realdir)
newfilepath = os.path.relpath(os.path.realpath(filepath), dest_realdir)
os.chdir(cwd)
newfilepath = prefixDotSlash(newfilepath)
parsed[1] = "'" + newfilepath + "'"
return "".join(parsed)
def generateImportStatement(line, fp):
### add error handler
parsed = line.split("'")
fp = extractFileFromFilepath(fp)
fp = prefixDotSlash(fp)
parsed[1] = "'" + fp + "'"
return "".join(parsed)
def getDirectoryOfFilepath(filepath):
if (os.path.isdir(filepath)):
return filepath
else:
return os.path.dirname(filepath)
# calculate realpaths, dirs, filenames, names
def moveFile(src_filepath, dest_filepath):
src_realpath = os.path.realpath(src_filepath)
dest_realpath = os.path.realpath(dest_filepath)
src_filename = os.path.basename(src_realpath)
dest_filename = os.path.basename(dest_realpath)
src_realdir = getDirectoryOfFilepath(src_realpath)
dest_realdir = getDirectoryOfFilepath(dest_realpath)
output_filepath = os.path.join(dest_realdir, os.path.basename(src_realpath))
src_name = os.path.splitext(src_filename)[0]
dest_name = os.path.splitext(dest_filename)[0]
#src_filepath, output_filepath, src_realpath, dest_realdir
f = open(src_filepath, 'r')
output_file = open(output_filepath, 'w')
for line in f:
if (not isRelativeImport(line)):
output_file.write(line)
continue
target_relpath = line.split("'")[1]
target_realpath = realpathOfRelativeTargetFromSrc(src_realpath, target_relpath)
new_relpath = generateNewRelativePath(target_realpath, dest_realdir)
newImportStatement = generateImportStatement(line, new_relpath)
output_file.write(newImportStatement)
print("Editing file: " + output_filepath)
print("Old: " + line.rstrip())
print("New: " + newImportStatement.rstrip())
print("")
output_file.close()
#delete original file
os.remove(src_filepath)
def inspectFileForChange(inspect_filepath, src_filepath, dest_filepath):
src_realpath = os.path.realpath(src_filepath)
dest_realpath = os.path.realpath(dest_filepath)
src_filename = os.path.basename(src_realpath)
dest_filename = os.path.basename(dest_realpath)
src_realdir = getDirectoryOfFilepath(src_realpath)
dest_realdir = getDirectoryOfFilepath(dest_realpath)
output_filepath = os.path.join(dest_realdir, os.path.basename(src_realpath))
src_name = os.path.splitext(src_filename)[0]
dest_name = os.path.splitext(dest_filename)[0]
changes_made = False
inspect_file = open(inspect_filepath, 'r')
inspect_output_file = open(inspect_filepath + ".tmp", 'w')
for line in inspect_file:
if(not isRelativeImport(line)):
inspect_output_file.write(line)
continue
save_cwd = os.getcwd()
inspect_wd = getDir(inspect_filepath)
os.chdir(inspect_wd)
import_realpath = os.path.realpath(extractImportFromStatement(line))
os.chdir(save_cwd)
if (import_realpath[-3:] != ".js"):
import_realpath = import_realpath + ".js"
if (import_realpath == src_realpath):
newfilepath = os.path.relpath(output_filepath, os.path.dirname(inspect_file.name))
inspect_output_file.write(generateImportStatement(line, newfilepath))
print("Editing file: " + inspect_filepath)
print("Old: " + line.rstrip())
print("New: " + generateImportStatement(line, newfilepath).rstrip())
print("")
changes_made = True
else:
inspect_output_file.write(line)
inspect_output_file.close()
if (changes_made):
shutil.move(inspect_filepath + ".tmp", inspect_filepath)
else:
os.remove(inspect_filepath + ".tmp")
def moveAndInspectForChanges(src_filepath, dest_filepath):
if(os.path.isdir(src_filepath)):
if(os.path.isdir(dest_filepath)):
#dest is dir, move src dir inside dest_filepath
if (src_filepath[-1:] == '/'):
src_filepath = src_filepath[:-1]
if(dest_filepath[-1:] == '/'):
dest_filepath = dest_filepath[:-1]
dirname = os.path.basename(src_filepath)
newdirname = os.path.join(dest_filepath, dirname)
print("Making directory at: " + newdirname)
os.mkdir(newdirname)
for file in os.listdir(src_filepath):
src_file = os.path.join(src_filepath, file)
moveAndInspectForChanges(src_file, newdirname)
shutil.rmtree(src_filepath)
elif(os.path.isfile(dest_filepath)):
#dest is regular file, error out
print('ERROR: dest is a regular file')
exit(1)
elif(not os.path.exists(dest_filepath)):
#dest DNE, create dir at dest_filepath
os.mkdir(dest_filepath)
for file in os.listdir(src_filepath):
src_file = os.path.join(src_filepath, file)
moveAndInspectForChanges(src_file, dest_filepath)
shutil.rmtree(src_filepath)
else:
#some weird case like symbolic links, error out
print('ERROR: unhandled file error')
exit(1)
else:
moveFile(src_filepath, dest_filepath)
inspect_files = getInspectFiles()
for filepath in inspect_files:
inspectFileForChange(filepath, src_filepath, dest_filepath)
"""Arguments validation"""
# correct number of args
if len(sys.argv) != 3:
print("Invalid # of args")
exit()
src_filepath = sys.argv[1]
dest_filepath = sys.argv[2]
if os.path.samefile(src_filepath, dest_filepath):
print("Source file and destination file cannot be the same.")
exit()
moveAndInspectForChanges(src_filepath, dest_filepath)