-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename_project.py
279 lines (238 loc) · 8.29 KB
/
rename_project.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#
# rename a KiCad project
#
# Usage: rename_project [-s <source>] [-d <dest>] [-t <tag>]
#
# Copyright Bob Cousins 2017
#
# Licensed under GPLv3
#
# version 1
version = "0.1 Beta"
import os, sys, re, shutil, errno, getopt
from datetime import datetime
from time import strftime
# handy methods from https://www.dotnetperls.com/between-before-after-python
def before(value, a):
# Find first part and return slice before it.
pos_a = value.find(a)
if pos_a == -1: return ""
return value[0:pos_a]
def after(value, a):
# Find and validate first part.
pos_a = value.rfind(a)
if pos_a == -1: return ""
# Returns chars after the found string.
adjusted_pos_a = pos_a + len(a)
if adjusted_pos_a >= len(value): return ""
return value[adjusted_pos_a:]
def make_sure_path_exists(path):
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
def help(verbose):
print( 'rename_project.py [-s <source>] [-d <dest>] [-n <name> | -t <tag> ]')
print()
print( "Version %s - GPLv3 - Copyright Bob Cousins 2017" % version)
print( '*** Beta test version: use with caution ***')
print()
print( 'rename a KiCad project')
print()
print( '-s source directory (./)' )
print( '-d destination directory (./)' )
print( '-n new name' )
print( '-t tag to add' )
print( '-x dry run, do not change any files')
print( '-h | --help show quick help | more help' )
if verbose:
print()
print( 'Note: there must be only one project in source directory')
print( '')
print( 'Typical uses:')
print( '')
print( '1. Rename a project foo.pro to bar.pro' )
print( '$ rename_project -n new_name' )
print( '')
print( '2. Rename a project foo.pro to foo_v1.pro' )
print( '$ rename_project -t _v1' )
print( '')
print( '3. Rename a project foo.pro to /temp/bar.pro' )
print( '$ rename_project -d /temp -n bar' )
print( '')
print( '4. Rename a project foo.pro to /temp/foo_v1.pro' )
print( '$ rename_project -d /temp -t _v1' )
print( '')
print( '5. Rename a project foo.pro to ./YYYY-MM-DD_HH-MM-SS/foo.pro' )
print( '$ rename_project' )
print( '')
print( '6. Rename a project foo.pro to ./save1/foo.pro' )
print( '$ rename_project -d save1' )
# destdir[_tag] / name[_tag]
# destdir / name
# destdir / name_tag
# destdir_tag / name
# destdir_tag / name_tag
# rename dest=src+date, name=project
# rename -t tag dest=src, name=project+tag
# rename -n name dest=src, name=name
# --
# rename -d dir dest=src+dir, name=project
# rename -d dir -t tag dest=dir, name=project+tag
# rename -d dir -n name dest=dir, name=name
## save [to date]
# rename -d
## save [to tag]
# rename -d dir -t tag
def main(argv):
mode = "none"
new_name = ""
sourcedir = ""
suffix = ""
destdir = ""
recurse = False
dry_run = False
verbose = False
overwrite = False
try:
opts, arg = getopt.getopt (argv,"s:d:n:t:hxv", ["help"])
except getopt.GetoptError:
help()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
help(opt == "--help")
sys.exit()
elif opt in ("-s"):
sourcedir = arg
elif opt in ("-d"):
destdir = arg
elif opt in ("-t"):
suffix = arg
elif opt in ("-n"):
new_name = arg
elif opt in ("-x"):
dry_run = True
## check args
if sourcedir == "":
sourcedir = os.getcwd()
if not os.path.exists (sourcedir):
print( "error: %s is not a directory" % sourcedir )
quit()
#
top_level_files = [f for f in os.listdir(sourcedir) if os.path.isfile(os.path.join(sourcedir, f))]
files = []
if recurse:
for root, dirnames, filenames in os.walk(sourcedir):
for filename in filenames:
# print os.path.join(after(root,sourcedir),filename)
files.append ( os.path.join(after(root,sourcedir),filename) )
else:
files = [f for f in os.listdir(sourcedir) if os.path.isfile(os.path.join(sourcedir, f))]
#
# first find the project name
project = ""
for file in top_level_files:
if file.endswith (".pro"):
if project == "":
project = before (file, ".pro")
else:
print( "error: multiple projects found in %s" % sourcedir)
quit(1)
if project == "":
print("error: no project file found in %s" % sourcedir)
quit(2)
if destdir== "":
if (suffix=="" and new_name==""):
mode = "copy"
destdir = os.path.join (sourcedir, strftime("%Y-%m-%d_%H-%M-%S"))
new_name = project
elif (suffix!="" and new_name!=""):
print("error: must specify only one of name or tag")
quit()
elif suffix != "":
mode = "rename"
destdir = sourcedir
new_name = project + suffix
elif new_name != "":
mode = "rename"
destdir = sourcedir
# new_name = new_name
else:
if (suffix=="" and new_name==""):
mode = "copy"
destdir = os.path.join(sourcedir, destdir)
new_name = project
elif (suffix!="" and new_name!=""):
print("error: must specify only one of name or tag")
quit()
elif suffix != "":
mode = "copy"
# destdir = destdir
new_name = project + suffix
elif new_name != "":
mode = "copy"
# destdir = destdir
# new_name = new_name
##
print("project name: %s" % project)
print("new project name: %s" % new_name)
print("")
if dry_run:
print("mode : %s" % mode)
print("sourcedir : %s" % sourcedir)
print("destdir : %s" % destdir)
print("")
if dry_run:
if not os.path.exists(destdir):
print("create : %s" % destdir)
else:
try:
make_sure_path_exists (destdir)
except:
print("error creating dest folder %s" % destdir)
quit()
try:
# now copy files
for file in files:
if (file.endswith (".sch") or
file.endswith (".lib") or
file.endswith (".mod") or
file.endswith (".cmp") or
file.endswith (".brd") or
file.endswith (".kicad_pcb") or
file.endswith (".pos") or
file.endswith (".net") or
file.endswith (".pro") or
file.endswith (".py") or
file.endswith (".pdf") or
file.endswith (".txt") or
file.endswith (".dcm") or
file.endswith (".kicad_wks") or
file == "fp-lib-table"):
if file.startswith (project):
# copy with rename
source_file = os.path.join(sourcedir, file)
dest_file = os.path.join (destdir, new_name + after (file, project))
if dry_run:
print("rename : %s ==> %s" % (file, dest_file))
else:
if mode == "copy":
shutil.copy2 (source_file, dest_file)
else:
os.rename (source_file, dest_file)
else:
if mode=="copy":
# straight copy
source_file = os.path.join(sourcedir, file)
dest_file = os.path.join (destdir, file)
if dry_run:
print("copy : %s ==> %s" % (file, dest_file))
else:
shutil.copy2 (source_file, dest_file)
except IOError as exception:
print("error copying file %s : %s" % (exception.filename, exception.strerror))
quit()
if __name__ == "__main__":
main(sys.argv[1:])