Add the project loader function in order to implement the editing non destructive...
[bertos.git] / wizard / bertos_utils.py
index 17c3a18f50bf2aa50f85cbb3d0aa3adfb1a17225..fff47c6c5a59d2cb93f3fc3836676d3f2e4a7e61 100644 (file)
@@ -1,8 +1,32 @@
 #!/usr/bin/env python
 # encoding: utf-8
 #
+# This file is part of BeRTOS.
+#
+# Bertos is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+#
+# As a special exception, you may use this file as part of a free software
+# library without restriction.  Specifically, if other files instantiate
+# templates or use macros or inline functions from this file, or you compile
+# this file and link it with other files to produce an executable, this
+# file does not by itself cause the resulting executable to be covered by
+# the GNU General Public License.  This exception does not however
+# invalidate any other reasons why the executable file might be covered by
+# the GNU General Public License.
+#
 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
-# All rights reserved.
 #
 # $Id$
 #
@@ -21,6 +45,7 @@ import pickle
 import const
 import plugins
 import DefineException
+import BProject
 
 def isBertosDir(directory):
    return os.path.exists(directory + "/VERSION")
@@ -28,13 +53,44 @@ def isBertosDir(directory):
 def bertosVersion(directory):
    return open(directory + "/VERSION").readline().strip()
 
+def loadBertosProject(project_file):
+    project_data = pickle.loads(open(project_file, "r").read())
+    project_info = BProject.BProject()
+    project_info.setInfo("PROJECT_PATH", os.path.dirname(project_file))
+    project_info.setInfo("SOURCES_PATH", project_data["SOURCES_PATH"])
+    loadSourceTree(project_info)
+    cpu_name = project_data["CPU_NAME"]
+    project_info.setInfo("CPU_NAME", cpu_name)
+    cpu_info = loadCpuInfos(project_info)
+    for cpu in cpu_info:
+        if cpu["CPU_NAME"] == cpu_name:
+            project_info.setInfo("CPU_INFOS", cpu)
+            break
+    loadModuleData(project_info, True)
+    return project_info
+
+def projectFileGenerator(project_info):
+    directory = project_info.info("PROJECT_PATH")
+    project_data = {}
+    enabled_modules = []
+    for module, information in project_info.info("MODULES").items():
+        if information["enabled"]:
+            enabled_modules.append(module)
+    project_data["ENABLED_MODULES"] = enabled_modules
+    project_data["SOURCES_PATH"] = project_info.info("SOURCES_PATH")
+    project_data["TOOLCHAIN"] = project_info.info("TOOLCHAIN")
+    project_data["CPU_NAME"] = project_info.info("CPU_NAME")
+    project_data["SELECTED_FREQ"] = project_info.info("SELECTED_FREQ")
+    return pickle.dumps(project_data)
+
 def createBertosProject(project_info):
     directory = project_info.info("PROJECT_PATH")
     sources_dir = project_info.info("SOURCES_PATH")
-    if not os.path.isdir(directory):
-        os.mkdir(directory)
+    if os.path.isdir(directory):
+        shutil.rmtree(directory, True)        
+    os.makedirs(directory)
     f = open(directory + "/project.bertos", "w")
-    f.write(pickle.dumps(project_info))
+    f.write(projectFileGenerator(project_info))
     f.close()
     # Destination source dir
     srcdir = directory + "/bertos"
@@ -70,7 +126,7 @@ def createBertosProject(project_info):
             configurations = project_info.info("CONFIGURATIONS")
             configuration = configurations[information["configuration"]]
             for start, parameter in configuration["paramlist"]:
-                if "type" in configuration[parameter] and configuration[parameter]["type"] == "autoenabled":
+                if "type" in configuration[parameter]["informations"] and configuration[parameter]["informations"]["type"] == "autoenabled":
                     configuration[parameter]["value"] = "1"
             project_info.setInfo("CONFIGURATIONS", configurations)
     # Copy all the configuration files
@@ -204,7 +260,7 @@ def csrcGenerator(project_info):
     cppasrc = " \\\n\t".join(cppasrc) + " \\"
     cxxsrc = " \\\n\t".join(cxxsrc) + " \\"
     asrc = " \\\n\t".join(asrc) + " \\"
-    constants = "\n".join([os.path.basename(project_info.info("PROJECT_PATH")) + "_" + key + " = " + str(value) for key, value in constants.items()])
+    constants = "\n".join([os.path.basename(project_info.info("PROJECT_PATH")) + "_" + key + " = " + unicode(value) for key, value in constants.items()])
     return csrc, pcsrc, cppasrc, cxxsrc, asrc, constants
 
 def findModuleFiles(module, project_info):
@@ -371,8 +427,7 @@ def loadModuleDefinition(first_comment):
             del module_definition["module_description"]
         if const.MODULE_DEFINITION["module_harvard"] in module_definition:
             harvard = module_definition[const.MODULE_DEFINITION["module_harvard"]]
-            if harvard == "both" or harvard == "pgm_memory":
-                module_dict[module_name]["harvard"] = harvard
+            module_dict[module_name]["harvard"] = harvard
             del module_definition[const.MODULE_DEFINITION["module_harvard"]]
         if const.MODULE_DEFINITION["module_hw"] in module_definition:
             hw = module_definition[const.MODULE_DEFINITION["module_hw"]]
@@ -473,7 +528,7 @@ def getDefinitionBlocks(text):
         block.append(([comment], define, start))
     return block
 
-def loadModuleData(project):
+def loadModuleData(project, edit=False):
     module_info_dict = {}
     list_info_dict = {}
     configuration_info_dict = {}
@@ -498,6 +553,14 @@ def loadModuleData(project):
                         configuration_info[configuration] = loadConfigurationInfos(project.info("SOURCES_PATH") + "/" + configuration)
                     except ParseError, err:
                         raise DefineException.ConfigurationDefineException(project.info("SOURCES_PATH") + "/" + configuration, err.line_number, err.line)
+                    if edit:
+                        try:
+                            path = os.path.basename(project.info("PROJECT_PATH"))
+                            directory = project.info("PROJECT_PATH")
+                            user_configuration = loadConfigurationInfos(directory + "/" + configuration.replace("bertos", path))
+                            configuration_info[configuration] = updateConfigurationValues(configuration_info[configuration], user_configuration)
+                        except ParseError, err:
+                            raise DefineException.ConfigurationDefineException(directory + "/" + configuration.replace("bertos", path))
             module_info_dict.update(module_dict)
             configuration_info_dict.update(configuration_info)
             if to_be_parsed:
@@ -565,6 +628,11 @@ def loadConfigurationInfos(path):
         configuration_infos[name]["brief"] = brief
     return configuration_infos
 
+def updateConfigurationValues(def_conf, user_conf):
+    for param in def_conf["paramlist"]:
+        def_conf[param[1]]["value"] = user_conf[param[1]]["value"]
+    return def_conf
+
 def findParameterType(parameter):
     if "value_list" in parameter["informations"]:
         return "enum"
@@ -622,4 +690,4 @@ class ParseError(Exception):
 class SupportedException(Exception):
     def __init__(self, support_string):
         Exception.__init__(self)
-        self.support_string = support_string
\ No newline at end of file
+        self.support_string = support_string