-
Notifications
You must be signed in to change notification settings - Fork 12
/
ughubProjectFileGenerator.py
205 lines (161 loc) · 7.73 KB
/
ughubProjectFileGenerator.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
################################################################################
# Copyright 2015 G-CSC, Goethe University Frankfurt
# Author: Sebastian Reiter <sreiter@gcsc.uni-frankfurt.de>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the Goethe-Center for Scientific Computing nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
################################################################################
import codecs
import collections
import os
PathNode = collections.namedtuple("PathTypePair", "path type ignore")
def CollectAffectedFiles(path, svnPath, conf):
files = sorted(os.listdir(path))
affectedFiles = []
for f in files:
fullName = os.path.join(path, f)
if os.path.isdir(fullName):
svnName = os.path.join(svnPath, f)
affectedFiles = affectedFiles + CollectAffectedFiles(fullName, svnName, conf)
for f in files:
fullName = os.path.join(path, f)
if os.path.isfile(fullName):
svnName = os.path.join(svnPath, f)
if ConsiderFile(svnName, conf):
affectedFiles.append(AffectedFile(fullName, svnName))
return affectedFiles
def ListPaths(path):
pathsOut = []
files = os.listdir(path)
for f in files:
fname = os.path.join(path, f)
if os.path.isdir(fname):
pathsOut.append(f)
return pathsOut
def GetEclipseProjectPaths(rootDir):
dirs = [ PathNode(rootDir, "root", ["apps", "externals", "plugins", "ugcore"]),
PathNode(os.path.join(rootDir, "ugcore"), "leaf", [])]
for subroot in ("apps", "externals", "plugins"):
fullDir = os.path.join(rootDir, subroot);
leafs = ListPaths(fullDir)
dirs.append(PathNode(fullDir, "subroot", leafs))
for leaf in leafs:
dirs.append(PathNode(os.path.join(fullDir, leaf), "leaf", []))
return dirs
def GenerateEclipseProjectFiles(rootDir, projectName, overwriteFiles):
localDir = os.path.dirname(os.path.realpath(__file__))
templateLeaf = codecs.open(os.path.join(localDir, "project_templates/eclipse-leaf"), "r", "utf-8").read()
templateRoot = codecs.open(os.path.join(localDir, "project_templates/eclipse-root"), "r", "utf-8").read()
templateFilter = codecs.open(os.path.join(localDir, "project_templates/eclipse-filter"), "r", "utf-8").read()
templateCProject = codecs.open(os.path.join(localDir, "project_templates/eclipse-cproject"), "r", "utf-8").read()
pathNodes = GetEclipseProjectPaths(rootDir)
for pn in pathNodes:
filename = os.path.join(pn.path, ".project")
if (not (overwriteFiles or pn.type == "subroot")) and os.path.isfile(filename):
continue
if projectName and (pn.type == "root"):
pname = projectName
else:
pname = os.path.basename(pn.path)
if (pn.type == "root") or (pn.type == "subroot"):
filters = ""
for p in pn.ignore:
filters = filters + templateFilter.replace("$IGNOREPATH$", p)
template = templateRoot.replace("$FILTERS$", filters)
elif pn.type == "leaf":
template = templateLeaf
codecs.open(os.path.join(pn.path, ".cproject"), "w", 'utf-8', errors="replace").write(templateCProject)
fileContents = template.replace("$PROJECTNAME$", pname);
codecs.open(filename, "w", 'utf-8', errors="replace").write(fileContents)
print( "Eclipse project files generated.")
print( "Execute the following steps to import or update your project in Eclipse:")
print( " - Open Eclipse,\n"
" - Click 'File->Import...->General->Existing Project Into Workspace'\n"
" - Choose ug4's root directory, and enable the options 'Search for nested projects'\n"
" and 'Hide projects that already exist in the workspace'.\n"
" - From Eclipse MARS on (Eclipse v4.5) you may activate the option\n"
" 'Project Presentation->Hierarchical' in the dropdown menu of the 'Project Explorer'.")
def RemoveEclipseProjectFiles(rootDir):
pathNodes = GetEclipseProjectPaths(rootDir)
for pn in pathNodes:
for f in (".project", ".cproject"):
filename = os.path.join(pn.path, f)
if os.path.isfile(filename):
os.remove(filename)
print("Eclipse project files deleted.")
# def GenerateEclipseProjectFiles(rootDir, projectName, overwriteFiles):
# localDir = os.path.dirname(os.path.realpath(__file__))
# templateNested = codecs.open(os.path.join(localDir, "project_templates/eclipse-nested"), "r", "utf-8").read()
# templateRoot = codecs.open(os.path.join(localDir, "project_templates/eclipse-root"), "r", "utf-8").read()
# rootDirs = [rootDir]
# nestedDirs = [os.path.join(rootDir, "ugcore")]
# for d in ("apps", "externals", "plugins"):
# fullDir = os.path.join(rootDir, d);
# rootDirs.append(fullDir)
# nestedDirs = nestedDirs + ListPaths(fullDir)
# # write root files
# for d in rootDirs:
# filename = os.path.join(d, ".project")
# if os.path.isfile(filename) and not overwriteFiles:
# continue
# if projectName and d == rootDir:
# pname = projectName
# else:
# pname = os.path.basename(d)
# template = templateRoot
# fileContents = template.replace("$PROJECTNAME$", pname);
# codecs.open(filename, "w", 'utf-8', errors="replace").write(fileContents)
# # write nested files
# for d in nestedDirs:
# filename = os.path.join(d, ".project")
# if os.path.isfile(filename) and not overwriteFiles:
# continue
# pname = os.path.basename(d)
# template = templateNested
# fileContents = template.replace("$PROJECTNAME$", pname);
# codecs.open(filename, "w", 'utf-8', errors="replace").write(fileContents)
# print( "Eclipse project files generated.")
# print( " - Open Eclipse,\n"
# " - Click 'File->Import...->General->Existing Project Into Workspace'\n"
# " - Choose ug4's root directory and enable 'Search for nested projects' only.\n"
# " - From Eclipse MARS on (Eclipse v4.5) you can activate the option\n"
# " 'Project Presentation->Hierarchical' in the dropdown menu of the 'Project Explorer'.")
# def RemoveEclipseProjectFiles(rootDir):
# dirs = [rootDir, os.path.join(rootDir, "ugcore")]
# for d in ("apps", "externals", "plugins"):
# fullDir = os.path.join(rootDir, d);
# dirs.append(fullDir)
# dirs = dirs + ListPaths(fullDir)
# # write root files
# for d in dirs:
# for f in (".project", ".cproject"):
# filename = os.path.join(d, f)
# if os.path.isfile(filename):
# os.remove(filename)
# print("Eclipse project files deleted.")
def Run(rootDir, targetName, projectName, overwriteFiles):
if targetName.lower() == "eclipse":
GenerateEclipseProjectFiles(rootDir, projectName, overwriteFiles)
def RemoveFiles(rootDir, targetName):
if targetName.lower() == "eclipse":
RemoveEclipseProjectFiles(rootDir)