Now the wizard can launch the IDE.
[bertos.git] / wizard / plugins / codelite.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # Copyright 2009 Develer S.r.l. (http://www.develer.com/)
5 # All rights reserved.
6 #
7 # $Id$
8 #
9 # Author: Lorenzo Berni <duplo@develer.com>
10 #
11
12 import os
13
14 import const
15
16 ## Plugin interface ##
17
18 PLUGIN_NAME = "CodeLite"
19
20 PLUGIN_DESCRIPTION = "Create CodeLite project files"
21
22 def createProject(project_info):
23     """
24     Function that creates codelite projects and return the project relevant file.
25     """
26     directory = project_info.info("PROJECT_PATH")
27     prjdir = directory + os.sep + os.path.basename(directory)
28     workspace = codeliteWorkspaceGenerator(project_info)
29     open(directory + os.sep + os.path.basename(prjdir) + ".workspace", "w").write(workspace)
30     project = codeliteProjectGenerator(project_info)
31     open(directory + os.sep + os.path.basename(prjdir) + ".project", "w").write(project)
32     return directory + os.sep + os.path.basename(prjdir) + ".workspace"
33
34 ####
35
36 def clFiles(file_dict, directory):
37     """
38     Creates the list of the lines for the files founded in file_dict, using
39     directory as the base folder.
40     """
41     filelist = []
42     filelist.append("<VirtualDirectory Name=\"%s\">" %os.path.basename(directory))
43     for f in file_dict[directory]["files"]:
44         filelist.append("<File Name=\"%s\"/>" %os.path.join(directory, f))
45     for d in file_dict[directory]["dirs"]:
46         filelist += clFiles(file_dict, os.path.join(directory, d))
47     filelist.append("</VirtualDirectory>")
48     return filelist
49
50 def findSources(path):
51     """
52     Analyzes the directory tree from path and return a dict with filename and
53     path.
54     """
55     if not path.endswith(os.sep):
56         path += os.sep
57     file_dict = {}
58     for root, dirs, files in os.walk(path):
59         if root.find("svn") == -1:
60             file_dict[root.replace(path, "")] = {"dirs": [], "files": []}
61             for dir in dirs:
62                 if dir.find("svn") == -1:
63                     file_dict[root.replace(path, "")]["dirs"].append(dir)
64             for file in files:
65                 if file.endswith(const.EXTENSION_FILTER):
66                     file_dict[root.replace(path, "")]["files"].append(file)
67     return file_dict
68
69 def codeliteProjectGenerator(project_info):
70     """
71     Returns the string rapresenting the codelite project.
72     """
73     template = open("cltemplates/bertos.project", "r").read()
74     filelist = "\n".join(clFiles(findSources(project_info.info("PROJECT_PATH")), ""))
75     while template.find("$filelist") != -1:
76         template = template.replace("$filelist", filelist)
77     project_name = os.path.basename(project_info.info("PROJECT_PATH"))
78     while template.find("$project") != -1:
79         template = template.replace("$project", project_name)
80     return template
81
82 def codeliteWorkspaceGenerator(project_info):
83     """
84     Returns the string rapresentig the codelite workspace.
85     """
86     template = open("cltemplates/bertos.workspace", "r").read()
87     project_name = os.path.basename(project_info.info("PROJECT_PATH"))
88     while template.find("$project") != -1:
89         template = template.replace("$project", project_name)
90     return template