19c5ac7181fb772d2ab28408fa569802153c3bd2
[bertos.git] / wizard / plugins / codelite.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # This file is part of BeRTOS.
5 #
6 # Bertos is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 #
20 # As a special exception, you may use this file as part of a free software
21 # library without restriction.  Specifically, if other files instantiate
22 # templates or use macros or inline functions from this file, or you compile
23 # this file and link it with other files to produce an executable, this
24 # file does not by itself cause the resulting executable to be covered by
25 # the GNU General Public License.  This exception does not however
26 # invalidate any other reasons why the executable file might be covered by
27 # the GNU General Public License.
28 #
29 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30 #
31 #
32 # Author: Lorenzo Berni <duplo@develer.com>
33 #
34
35 import os
36
37 import const
38
39 ## Plugin interface ##
40
41 PLUGIN_NAME = "CodeLite"
42
43 PLUGIN_DESCRIPTION = "Create CodeLite project files"
44
45 def createProject(project_info):
46     """
47     Function that creates codelite projects and return the project relevant file.
48     """
49     directory = project_info.info("PROJECT_PATH")
50     prjdir = directory + os.sep + os.path.basename(directory)
51     workspace = codeliteWorkspaceGenerator(project_info)
52     open(directory + os.sep + os.path.basename(prjdir) + ".workspace", "w").write(workspace)
53     project = codeliteProjectGenerator(project_info)
54     open(directory + os.sep + os.path.basename(prjdir) + ".project", "w").write(project)
55     return directory + os.sep + os.path.basename(prjdir) + ".workspace"
56
57 ####
58
59 def clFiles(file_dict, directory):
60     """
61     Creates the list of the lines for the files founded in file_dict, using
62     directory as the base folder.
63     """
64     filelist = []
65     # Do not create an empty VDir.
66     # TODO: this is *really* ugly, but an empty VDir is worse
67     if directory:
68         filelist.append("<VirtualDirectory Name=\"%s\">" %os.path.basename(directory))
69     for f in file_dict[directory]["files"]:
70         filelist.append("<File Name=\"%s\"/>" %os.path.join(directory, f))
71     for d in file_dict[directory]["dirs"]:
72         filelist += clFiles(file_dict, os.path.join(directory, d))
73     if directory:
74         filelist.append("</VirtualDirectory>")
75     return filelist
76
77 def findSources(path):
78     """
79     Analyzes the directory tree from path and return a dict with filename and
80     path.
81     """
82     if not path.endswith(os.sep):
83         path += os.sep
84     file_dict = {}
85     for root, dirs, files in os.walk(path, followlinks=True):
86         if root.find("svn") == -1:
87             file_dict[root.replace(path, "")] = {"dirs": [], "files": []}
88             for dir in dirs:
89                 # TODO: place the directory name in a constant file.
90                 if dir.find("svn") == -1 and dir != "images" and dir != "obj" and dir != "doc":
91                     file_dict[root.replace(path, "")]["dirs"].append(dir)
92             for file in files:
93                 if file.endswith(const.EXTENSION_FILTER) and file != "buildrev.h":
94                     file_dict[root.replace(path, "")]["files"].append(file)
95     return file_dict
96
97 def codeliteProjectGenerator(project_info):
98     """
99     Returns the string rapresenting the codelite project.
100     """
101     template = open(os.path.join(const.DATA_DIR, "cltemplates/bertos.project"), "r").read()
102     filelist = "\n".join(clFiles(findSources(project_info.info("PROJECT_PATH")), ""))
103     debugger_path = project_info.info("TOOLCHAIN")["path"].replace("gcc", "gdb")
104     init_script = project_info.info("CPU_INFOS")["GDB_INIT_SCRIPT"]
105     template = template.replace("$filelist", filelist)
106     project_name = os.path.basename(project_info.info("PROJECT_PATH"))
107     template = template.replace("$project", project_name)
108     template = template.replace("$debuggerpath", debugger_path)
109     template = template.replace("$initscript", init_script)
110     return template
111
112 def codeliteWorkspaceGenerator(project_info):
113     """
114     Returns the string rapresentig the codelite workspace.
115     """
116     template = open(os.path.join(const.DATA_DIR, "cltemplates/bertos.workspace"), "r").read()
117     project_name = os.path.basename(project_info.info("PROJECT_PATH"))
118     template = template.replace("$project", project_name)
119     return template