Add BeRTOS header in sources files
[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 # $Id$
32 #
33 # Author: Lorenzo Berni <duplo@develer.com>
34 #
35
36 import os
37
38 import const
39
40 ## Plugin interface ##
41
42 PLUGIN_NAME = "CodeLite"
43
44 PLUGIN_DESCRIPTION = "Create CodeLite project files"
45
46 def createProject(project_info):
47     """
48     Function that creates codelite projects and return the project relevant file.
49     """
50     directory = project_info.info("PROJECT_PATH")
51     prjdir = directory + os.sep + os.path.basename(directory)
52     workspace = codeliteWorkspaceGenerator(project_info)
53     open(directory + os.sep + os.path.basename(prjdir) + ".workspace", "w").write(workspace)
54     project = codeliteProjectGenerator(project_info)
55     open(directory + os.sep + os.path.basename(prjdir) + ".project", "w").write(project)
56     return directory + os.sep + os.path.basename(prjdir) + ".workspace"
57
58 ####
59
60 def clFiles(file_dict, directory):
61     """
62     Creates the list of the lines for the files founded in file_dict, using
63     directory as the base folder.
64     """
65     filelist = []
66     filelist.append("<VirtualDirectory Name=\"%s\">" %os.path.basename(directory))
67     for f in file_dict[directory]["files"]:
68         filelist.append("<File Name=\"%s\"/>" %os.path.join(directory, f))
69     for d in file_dict[directory]["dirs"]:
70         filelist += clFiles(file_dict, os.path.join(directory, d))
71     filelist.append("</VirtualDirectory>")
72     return filelist
73
74 def findSources(path):
75     """
76     Analyzes the directory tree from path and return a dict with filename and
77     path.
78     """
79     if not path.endswith(os.sep):
80         path += os.sep
81     file_dict = {}
82     for root, dirs, files in os.walk(path):
83         if root.find("svn") == -1:
84             file_dict[root.replace(path, "")] = {"dirs": [], "files": []}
85             for dir in dirs:
86                 if dir.find("svn") == -1:
87                     file_dict[root.replace(path, "")]["dirs"].append(dir)
88             for file in files:
89                 if file.endswith(const.EXTENSION_FILTER):
90                     file_dict[root.replace(path, "")]["files"].append(file)
91     return file_dict
92
93 def codeliteProjectGenerator(project_info):
94     """
95     Returns the string rapresenting the codelite project.
96     """
97     template = open("cltemplates/bertos.project", "r").read()
98     filelist = "\n".join(clFiles(findSources(project_info.info("PROJECT_PATH")), ""))
99     while template.find("$filelist") != -1:
100         template = template.replace("$filelist", filelist)
101     project_name = os.path.basename(project_info.info("PROJECT_PATH"))
102     while template.find("$project") != -1:
103         template = template.replace("$project", project_name)
104     return template
105
106 def codeliteWorkspaceGenerator(project_info):
107     """
108     Returns the string rapresentig the codelite workspace.
109     """
110     template = open("cltemplates/bertos.workspace", "r").read()
111     project_name = os.path.basename(project_info.info("PROJECT_PATH"))
112     while template.find("$project") != -1:
113         template = template.replace("$project", project_name)
114     return template