-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopyTemplateProject.py
More file actions
91 lines (80 loc) · 3.01 KB
/
copyTemplateProject.py
File metadata and controls
91 lines (80 loc) · 3.01 KB
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
#################################################################################
#
# copyTemplateProject.py
# Author: Philipp Seidl
# Date: 18.07.2018
#
# What does it do?: script copies either the openCV template or normal Template
#
# usage: open terminal in aadcUser
# type in following command:
# python herComesMyFilterName _optional_argument
# if the _optional_argument is passed or anything at all
# it copies the openCV Template, otherwise the templateFilter stuff
#
################################################################################
import sys
import os
import shutil
#from https://stackoverflow.com/questions/1868714/how-do-i-copy-an-entire-directory-of-files-into-an-existing-directory-using-pyth
def copytree(src, dst, symlinks=False, ignore=None):
for item in os.listdir(src):
s = os.path.join(src, item)
# change d if *.h or *.cpp
fname, ending = item.split('.')
if(fname == 'stdafx' or fname == 'CMakeLists'):
d = os.path.join(dst, item)
else:
d = os.path.join(dst, dst+'.'+ending)
if os.path.isdir(s):
shutil.copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
def replaceStuff(fName, find, replace_with):
with open(fName) as f:
newText=f.read().replace(find, replace_with)
with open(fName, "w") as f: #and replace it
f.write(newText)
filterName = sys.argv[1]
if len(sys.argv)>2: #just some argument after the filtername
src = 'OpenCVTemplate'
else:
src = 'TemplateFilter'
#copy the stuff
# if filtername already exists -- abort
if os.path.exists(filterName):
print('Error: Filter Folder allready exists - operation canceled')
else:
os.mkdir(filterName)
copytree(src, filterName)
#differences in the two templates openCV or normal
if src == 'OpenCVTemplate':
repl1 = 'opencv_template'
repl2 = 'cOpenCVTemplate'
repl3 = 'OpenCV Template'
repl4 = 'OpenCVTemplate.h'
else:
repl1 = 'template_filter'
repl2 = 'cTemplateFilter'
repl3 = 'TemplateDataFilter'
repl4 = 'TemplateFilter.h'
# 1) add filter directory to cmake
with open("CMakeLists.txt", "a") as f:
f.write("\nadd_subdirectory("+filterName+")")
# 1.1) change cmak in subdir
cmFileName = filterName+'/'+'CMakeLists.txt'
replaceStuff(cmFileName, repl1, filterName.lower()+'_filter')
replaceStuff(cmFileName, src, filterName) #replaces TemplateFilter.h and TemplateFilter.cpp
# 2) change filterName.h
# 2.1) add unique ID #TODO check if unique ;)
# replace either opencv_template or template_filter
# 2.2) replace cTemplateFilter or
hFileName = filterName+'/'+filterName+'.h'
replaceStuff(hFileName, repl1, filterName.lower()+'_filter')
replaceStuff(hFileName, repl2, 'c'+filterName)
# 3) change *.cpp file
cppFileName = filterName+'/'+filterName+'.cpp'
replaceStuff(cppFileName, repl4, filterName+'.h') #change include
replaceStuff(cppFileName, repl2, 'c'+filterName) #change cTemplateFilter
replaceStuff(cppFileName, repl3, filterName+'_cf') #Name distplayed in ADTF (_cf) for custom filter
print('Operation was successfully - now get on and get stuff done ;)')