ba03b2a5aa64e2125c09f9a7c767dd29070bb12d
[bertos.git] / wizard / plugins / codelite_project.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 def clFiles(file_dict, directory):
17     """
18     Creates the list of the lines for the files founded in file_dict, using
19     directory as the base folder.
20     """
21     filelist = []
22     filelist.append("<VirtualDirectory Name=\"%s\">" %os.path.basename(directory))
23     for f in file_dict[directory]["files"]:
24         filelist.append("<File Name=\"%s\"/>" %os.path.join(directory, f))
25     for d in file_dict[directory]["dirs"]:
26         filelist += clFiles(file_dict, os.path.join(directory, d))
27     filelist.append("</VirtualDirectory>")
28     return filelist
29
30 def findSources(path):
31     """
32     Analyzes the directory tree from path and return a dict with filename and
33     path.
34     """
35     if not path.endswith(os.sep):
36         path += os.sep
37     file_dict = {}
38     for root, dirs, files in os.walk(path):
39         if root.find("svn") == -1:
40             file_dict[root.replace(path, "")] = {"dirs": [], "files": []}
41             for dir in dirs:
42                 if dir.find("svn") == -1:
43                     file_dict[root.replace(path, "")]["dirs"].append(dir)
44             for file in files:
45                 if file.endswith(const.EXTENSION_FILTER):
46                     file_dict[root.replace(path, "")]["files"].append(file)
47     return file_dict
48
49 def codeliteProjectGenerator(project_info):
50     """
51     Returns the string rapresenting the codelite project.
52     """
53     template = open("cltemplates/bertos.project", "r").read()
54     filelist = "\n".join(clFiles(findSources(project_info.info("PROJECT_PATH")), ""))
55     while template.find("$filelist") != -1:
56         template = template.replace("$filelist", filelist)
57     project_name = os.path.basename(project_info.info("PROJECT_PATH"))
58     while template.find("$project") != -1:
59         template = template.replace("$project", project_name)
60     return template
61
62 def codeliteWorkspaceGenerator(project_info):
63     """
64     Returns the string rapresentig the codelite workspace.
65     """
66     template = open("cltemplates/bertos.workspace", "r").read()
67     project_name = os.path.basename(project_info.info("PROJECT_PATH"))
68     while template.find("$project") != -1:
69         template = template.replace("$project", project_name)
70     return template
71
72 def createProject(project_info):
73     """
74     Function that creates codelite projects.
75     """
76     directory = project_info.info("PROJECT_PATH")
77     prjdir = directory + "/" + os.path.basename(directory)
78     workspace = codeliteWorkspaceGenerator(project_info)
79     open(directory + "/" + os.path.basename(prjdir) + ".workspace", "w").write(workspace)
80     project = codeliteProjectGenerator(project_info)
81     open(directory + "/" + os.path.basename(prjdir) + ".project", "w").write(project)