Update preset.
[bertos.git] / wizard / bertos_utils.py
index ed9a0db27bf57b3ee7ea30cfc6b7838d8a5df04c..7be67fe10cdfcafb24b897cc4dc0ae8d36c22935 100644 (file)
@@ -28,7 +28,6 @@
 #
 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
 #
-# $Id$
 #
 # Author: Lorenzo Berni <duplo@develer.com>
 #
@@ -40,6 +39,7 @@ import re
 import shutil
 # Use custom copytree function
 import copytree
+import relpath
 import pickle
 
 import const
@@ -50,6 +50,15 @@ from _wizard_version import WIZARD_VERSION
 
 from LoadException import VersionException, ToolchainException
 
+def _cmp(x, y):
+    result = cmp(x["info"].get('ord', 0), y["info"].get('ord', 0))
+    if result == 0:
+        result = cmp(
+            x["info"].get("name", x["info"]["filename"]).lower(),
+            y["info"].get("name", y["info"]["filename"]).lower()
+        )
+    return result
+
 def isBertosDir(directory):
    return os.path.exists(directory + "/VERSION")
 
@@ -109,118 +118,32 @@ def projectFileGenerator(project_info):
     for module, information in project_info.info("MODULES").items():
         if information["enabled"]:
             enabled_modules.append(module)
-    project_data["ENABLED_MODULES"] = enabled_modules
-    # Use the local BeRTOS version instead of the original one
-    # project_data["SOURCES_PATH"] = project_info.info("SOURCES_PATH")
-    project_data["SOURCES_PATH"] = directory
+    project_data["ENABLED_MODULES"] = sorted(enabled_modules)
+    if project_info.info("PRESET"):
+        # For presets save again the BERTOS_PATH into project file
+        project_data["PRESET"] = True
+        project_data["BERTOS_PATH"] = relpath.relpath(project_info.info("BERTOS_PATH"), directory)
+    elif project_info.edit:
+        # If in editing mode the BERTOS_PATH is maintained
+        project_data["BERTOS_PATH"] = relpath.relpath(project_info.info("BERTOS_PATH"), directory)
+    else:
+        # Use the local BeRTOS version instead of the original one
+        # project_data["BERTOS_PATH"] = project_info.info("BERTOS_PATH")
+        project_data["BERTOS_PATH"] = "."
     project_data["PROJECT_NAME"] = project_info.info("PROJECT_NAME", os.path.basename(directory))
-    project_data["TOOLCHAIN"] = project_info.info("TOOLCHAIN")
+    project_src_relpath = relpath.relpath(project_info.info("PROJECT_SRC_PATH"), directory)
+    project_data["PROJECT_SRC_PATH"] = project_src_relpath
+    project_data["PROJECT_SRC_PATH_FROM_MAKEFILE"] = project_info.info("PROJECT_SRC_PATH_FROM_MAKEFILE")
+    project_data["TOOLCHAIN"] = {'path': project_info.info("TOOLCHAIN")['path']}
     project_data["CPU_NAME"] = project_info.info("CPU_NAME")
     project_data["SELECTED_FREQ"] = project_info.info("SELECTED_FREQ")
     project_data["OUTPUT"] = project_info.info("OUTPUT")
     project_data["WIZARD_VERSION"] = WIZARD_VERSION
+    project_data["PRESET"] = project_info.info("PRESET")
+    project_data["PROJECT_HW_PATH"] = relpath.relpath(project_info.info("PROJECT_HW_PATH"), directory)
+    project_data["PROJECT_HW_PATH_FROM_MAKEFILE"] = project_info.info("PROJECT_HW_PATH_FROM_MAKEFILE")
     return pickle.dumps(project_data)
 
-def createBertosProject(project_info, edit=False):
-    directory = project_info.info("PROJECT_PATH")
-    sources_dir = project_info.info("SOURCES_PATH")
-    old_sources_dir = project_info.info("OLD_SOURCES_PATH")
-    if not edit:
-        if os.path.isdir(directory):
-            shutil.rmtree(directory, True)        
-        os.makedirs(directory)
-    # Write the project file
-    f = open(directory + "/project.bertos", "w")
-    f.write(projectFileGenerator(project_info))
-    f.close()
-    # VERSION file
-    version_file = open(os.path.join(const.DATA_DIR, "vtemplates/VERSION"), "r").read()
-    open(directory + "/VERSION", "w").write(versionFileGenerator(project_info, version_file))
-    # Destination source dir
-    srcdir = directory + "/bertos"
-    if not edit:
-        # If not in editing mode it copies all the bertos sources in the /bertos subdirectory of the project
-        shutil.rmtree(srcdir, True)
-        copytree.copytree(sources_dir + "/bertos", srcdir, ignore_list=const.IGNORE_LIST)
-    elif old_sources_dir:
-        # If in editing mode it merges the current bertos sources with the selected ones
-        # TODO: implement the three way merge algotihm
-        #
-        mergeSources(srcdir, sources_dir, old_sources_dir)
-    # Destination makefile
-    makefile = directory + "/Makefile"
-    makefile = open(os.path.join(const.DATA_DIR, "mktemplates/Makefile"), 'r').read()
-    makefile = makefileGenerator(project_info, makefile)
-    open(directory + "/Makefile", "w").write(makefile)
-    # Destination project dir
-    # prjdir = directory + "/" + os.path.basename(directory)
-    prjdir = os.path.join(directory, project_info.info("PROJECT_NAME"))
-    if not edit:
-        shutil.rmtree(prjdir, True)
-        os.mkdir(prjdir)
-    # Destination hw files
-    hwdir = prjdir + "/hw"
-    if not edit:
-        shutil.rmtree(hwdir, True)
-        os.mkdir(hwdir)
-    # Copy all the hw files
-    for module, information in project_info.info("MODULES").items():
-        for hwfile in information["hw"]:
-            string = open(sources_dir + "/" + hwfile, "r").read()
-            hwfile_path = hwdir + "/" + os.path.basename(hwfile)
-            if not edit or not os.path.exists(hwfile_path):
-                # If not in editing mode it copies all the hw files. If in
-                # editing mode it copies only the files that don't exist yet
-                open(hwdir + "/" + os.path.basename(hwfile), "w").write(string)
-    # Destination configurations files
-    cfgdir = prjdir + "/cfg"
-    if not edit:
-        shutil.rmtree(cfgdir, True)
-        os.mkdir(cfgdir)
-    # Set properly the autoenabled parameters
-    for module, information in project_info.info("MODULES").items():
-        if "configuration" in information and information["configuration"] != "":
-            configurations = project_info.info("CONFIGURATIONS")
-            configuration = configurations[information["configuration"]]
-            for start, parameter in configuration["paramlist"]:
-                if "type" in configuration[parameter]["informations"] and configuration[parameter]["informations"]["type"] == "autoenabled":
-                    configuration[parameter]["value"] = "1" if information["enabled"] else "0"
-            project_info.setInfo("CONFIGURATIONS", configurations)
-    # Copy all the configuration files
-    for configuration, information in project_info.info("CONFIGURATIONS").items():
-        string = open(sources_dir + "/" + configuration, "r").read()
-        for start, parameter in information["paramlist"]:
-            infos = information[parameter]
-            value = infos["value"]
-            if "unsigned" in infos["informations"] and infos["informations"]["unsigned"]:
-                value += "U"
-            if "long" in infos["informations"] and infos["informations"]["long"]:
-                value += "L"
-            string = sub(string, parameter, value)
-        f = open(cfgdir + "/" + os.path.basename(configuration), "w")
-        f.write(string)
-        f.close()
-    if not edit:
-        # Destination user mk file (only on project creation)
-        makefile = open(os.path.join(const.DATA_DIR, "mktemplates/template.mk"), "r").read()
-        # Deadly performances loss was here :(
-        makefile = userMkGenerator(project_info, makefile)
-        open(prjdir + "/" + os.path.basename(prjdir) + ".mk", "w").write(makefile)
-    # Destination wizard mk file
-    makefile = open(os.path.join(const.DATA_DIR, "mktemplates/template_wiz.mk"), "r").read()
-    makefile = mkGenerator(project_info, makefile)
-    open(prjdir + "/" + os.path.basename(prjdir) + "_wiz.mk", "w").write(makefile)
-    # Destination main.c file
-    if not edit:
-        main = open(os.path.join(const.DATA_DIR, "srctemplates/main.c"), "r").read()
-        open(prjdir + "/main.c", "w").write(main)
-    # Files for selected plugins
-    relevants_files = {}
-    for plugin in project_info.info("OUTPUT"):
-        module = loadPlugin(plugin)
-        relevants_files[plugin] = module.createProject(project_info)
-    project_info.setInfo("RELEVANT_FILES", relevants_files)
-
 def loadPlugin(plugin):
     """
     Returns the given plugin module.
@@ -228,24 +151,43 @@ def loadPlugin(plugin):
     return getattr(__import__("plugins", {}, {}, [plugin]), plugin)
 
 def versionFileGenerator(project_info, version_file):
-    version = bertosVersion(project_info.info("SOURCES_PATH"))
+    version = bertosVersion(project_info.info("BERTOS_PATH"))
     return version_file.replace('$version', version)
 
-def userMkGenerator(project_info, makefile):
+def userMkGeneratorFromPreset(project_info):
+    project_name = project_info.info("PROJECT_NAME")
+    preset_path = project_info.info("PRESET_PATH")
+    preset_name = project_info.info("PRESET_NAME")
+    preset_src_dir = project_info.info("PRESET_SRC_PATH")
+    makefile = open(os.path.join(preset_path, preset_src_dir, "%s_user.mk" %preset_name), 'r').read()
+    destination = os.path.join(project_info.prjdir, "%s_user.mk" %project_info.info("PROJECT_NAME"))
+    # Temporary code.
+    # TODO: write it using regular expressions to secure this function
+    makefile = makefile.replace(preset_name + "_", project_name + "_")
+    open(destination, "w").write(makefile)
+
+def userMkGenerator(project_info):
+    makefile = open(os.path.join(const.DATA_DIR, "mktemplates/template_user.mk"), "r").read()
+    destination = os.path.join(project_info.prjdir, os.path.basename(project_info.prjdir) + "_user.mk")
+    # Deadly performances loss was here :(
     mk_data = {}
     mk_data["$pname"] = os.path.basename(project_info.info("PROJECT_PATH"))
-    mk_data["$main"] = os.path.basename(project_info.info("PROJECT_PATH")) + "/main.c"
+    mk_data["$ppath"] = project_info.info("PROJECT_SRC_PATH_FROM_MAKEFILE")
+    mk_data["$main"] = "/".join(["$(%s_SRC_PATH)" %project_info.info("PROJECT_NAME"), "main.c"])
     for key in mk_data:
-        while makefile.find(key) != -1:
-            makefile = makefile.replace(key, mk_data[key])
-    return makefile
+        makefile = makefile.replace(key, mk_data[key])
+    open(destination, "w").write(makefile)
 
-def mkGenerator(project_info, makefile):
+def mkGenerator(project_info):
     """
     Generates the mk file for the current project.
     """
+    makefile = open(os.path.join(const.DATA_DIR, "mktemplates/template.mk"), "r").read()
+    prjdir = os.path.abspath(project_info.prjdir)
+    destination = os.path.join(prjdir, os.path.basename(prjdir) + ".mk")
     mk_data = {}
-    mk_data["$pname"] = os.path.basename(project_info.info("PROJECT_PATH"))
+    mk_data["$pname"] = project_info.info("PROJECT_NAME")
+    mk_data["$ppath"] = project_info.info("PROJECT_SRC_PATH_FROM_MAKEFILE")
     mk_data["$cpuclockfreq"] = project_info.info("SELECTED_FREQ")
     cpu_mk_parameters = []
     for key, value in project_info.info("CPU_INFOS").items():
@@ -253,22 +195,26 @@ def mkGenerator(project_info, makefile):
             cpu_mk_parameters.append("%s = %s" %(key.replace("MK", mk_data["$pname"]), value))
     mk_data["$cpuparameters"] = "\n".join(cpu_mk_parameters)
     mk_data["$csrc"], mk_data["$pcsrc"], mk_data["$cppasrc"], mk_data["$cxxsrc"], mk_data["$asrc"], mk_data["$constants"] = csrcGenerator(project_info)
-    mk_data["$prefix"] = replaceSeparators(project_info.info("TOOLCHAIN")["path"].split("gcc")[0])
-    mk_data["$suffix"] = replaceSeparators(project_info.info("TOOLCHAIN")["path"].split("gcc")[1])
-    mk_data["$main"] = os.path.basename(project_info.info("PROJECT_PATH")) + "/main.c"
+    mk_data["$prefix"] = replaceSeparators(project_info.info("TOOLCHAIN")["path"].rsplit("gcc", 1)[0])
+    mk_data["$suffix"] = replaceSeparators(project_info.info("TOOLCHAIN")["path"].rsplit("gcc", 1)[1])
+    mk_data["$hwpath"] = project_info.info("PROJECT_HW_PATH_FROM_MAKEFILE")
     for key in mk_data:
-        while makefile.find(key) != -1:
-            makefile = makefile.replace(key, mk_data[key])
-    return makefile
+        makefile = makefile.replace(key, mk_data[key])
+    open(destination, "w").write(makefile)
 
-def makefileGenerator(project_info, makefile):
+def makefileGenerator(project_info):
     """
     Generate the Makefile for the current project.
     """
+    makefile = open(os.path.join(const.DATA_DIR, "mktemplates/Makefile"), "r").read()
+    destination = os.path.join(project_info.maindir, "Makefile")
     # TODO write a general function that works for both the mk file and the Makefile
-    while makefile.find("$pname") != -1:
-        makefile = makefile.replace("$pname", os.path.basename(project_info.info("PROJECT_PATH")))
-    return makefile
+    mk_data = {}
+    mk_data["$pname"] = project_info.info("PROJECT_NAME")
+    mk_data["$ppath"] = project_info.info("PROJECT_SRC_PATH_FROM_MAKEFILE")
+    for key in mk_data:
+        makefile = makefile.replace(key, mk_data[key])
+    open(destination, "w").write(makefile)
 
 def csrcGenerator(project_info):
     modules = project_info.info("MODULES")
@@ -294,7 +240,7 @@ def csrcGenerator(project_info):
         dependency_files = set([])
         # assembly sources
         asm_files = set([])
-        hwdir = os.path.basename(project_info.info("PROJECT_PATH")) + "/hw" 
+        hwdir = os.path.basename(project_info.info("PROJECT_PATH")) + "/hw"
         if information["enabled"]:
             if "constants" in information:
                 constants.update(information["constants"])
@@ -327,17 +273,17 @@ def csrcGenerator(project_info):
         cxxsrc.append(file)
     for file in project_info.info("CPU_INFOS")["ASRC"]:
         asrc.append(file)
-    csrc = set(csrc)
+    csrc = sorted(set(csrc))
     csrc = " \\\n\t".join(csrc) + " \\"
-    pcsrc = set(pcsrc)
+    pcsrc = sorted(set(pcsrc))
     pcsrc = " \\\n\t".join(pcsrc) + " \\"
-    cppasrc = set(cppasrc)
+    cppasrc = sorted(set(cppasrc))
     cppasrc = " \\\n\t".join(cppasrc) + " \\"
-    cxxsrc = set(cxxsrc)
+    cxxsrc = sorted(set(cxxsrc))
     cxxsrc = " \\\n\t".join(cxxsrc) + " \\"
-    asrc = set(asrc)
+    asrc = sorted(set(asrc))
     asrc = " \\\n\t".join(asrc) + " \\"
-    constants = "\n".join([os.path.basename(project_info.info("PROJECT_PATH")) + "_" + key + " = " + unicode(value) for key, value in constants.items()])
+    constants = "\n".join([project_info.info("PROJECT_NAME") + "_" + key + " = " + unicode(value) for key, value in constants.items()])
     return csrc, pcsrc, cppasrc, cxxsrc, asrc, constants
 
 def findModuleFiles(module, project_info):
@@ -346,13 +292,13 @@ def findModuleFiles(module, project_info):
     sfiles = []
     # .c files related to the module and the cpu architecture
     for filename, path in project_info.searchFiles(module + ".c"):
-        path = path.replace(project_info.info("SOURCES_PATH") + os.sep, "")
+        path = path.replace(project_info.info("BERTOS_PATH") + os.sep, "")
         path = replaceSeparators(path)
         cfiles.append(path + "/" + filename)
     # .s files related to the module and the cpu architecture
     for filename, path in project_info.searchFiles(module + ".s") + \
             project_info.searchFiles(module + ".S"):
-        path = path.replace(project_info.info("SOURCES_PATH") + os.sep, "")
+        path = path.replace(project_info.info("BERTOS_PATH") + os.sep, "")
         path = replaceSeparators(path)
         sfiles.append(path + "/" + filename)
     # .c and .s files related to the module and the cpu tags
@@ -361,24 +307,25 @@ def findModuleFiles(module, project_info):
     # Awful, but secure check for version
     # TODO: split me in a method/function
     try:
-        version_string = bertosVersion(project_info.info("SOURCES_PATH"))
-        version_list = [int(i) for i in version_string.split()[-1].split('.')]
-        if version_list < [2, 5]:
-            # For older versions of BeRTOS add the toolchain to the tags
-            tags.append(project_info.info("CPU_INFOS")["TOOLCHAIN"])
+        version_string = bertosVersion(project_info.info("BERTOS_PATH"))
+        version_list = [int(i) for i in version_string.split()[1].split('.')]
     except ValueError:
-        # If the version file hasn't a valid version number do nothing
-        pass
+        # If the version file hasn't a valid version number assume it's an older
+        # project.
+        version_list = [0, 0]
+    if version_list < [2, 5]:
+        # For older versions of BeRTOS add the toolchain to the tags
+        tags.append(project_info.info("CPU_INFOS")["TOOLCHAIN"])
 
     for tag in tags:
         for filename, path in project_info.searchFiles(module + "_" + tag + ".c"):
-            path = path.replace(project_info.info("SOURCES_PATH") + os.sep, "")
+            path = path.replace(project_info.info("BERTOS_PATH") + os.sep, "")
             if os.sep != "/":
                 path = replaceSeparators(path)
             cfiles.append(path + "/" + filename)
         for filename, path in project_info.searchFiles(module + "_" + tag + ".s") + \
                 project_info.searchFiles(module + "_" + tag + ".S"):
-            path = path.replace(project_info.info("SOURCES_PATH") + os.sep, "")
+            path = path.replace(project_info.info("BERTOS_PATH") + os.sep, "")
             path = replaceSeparators(path)
             sfiles.append(path + "/" + filename)
     return cfiles, sfiles
@@ -388,23 +335,28 @@ def replaceSeparators(path):
     Replace the separators in the given path with unix standard separator.
     """
     if os.sep != "/":
-        while path.find(os.sep) != -1:
-            path = path.replace(os.sep, "/")
+        path = path.replace(os.sep, "/")
     return path
 
 def getSystemPath():
     path = os.environ["PATH"]
-    if os.name == "nt":
-        path = path.split(";")
-    else:
-        path = path.split(":")
+    path = path.split(os.pathsep)
     return path
 
+def findInPath(file, path=None):
+    if path is None:
+        path = os.environ.get('PATH', '')
+    if type(path) is type(''):
+        path = path.split(os.pathsep)
+    return filter(os.path.exists, map(lambda dir, file=file: os.path.join(dir, file), path))
+
+
 def findToolchains(path_list):
     toolchains = []
     for element in path_list:
         for toolchain in glob.glob(element+ "/" + const.GCC_NAME):
-            toolchains.append(toolchain)
+            if not os.path.isdir(toolchain):
+                toolchains.append(toolchain)
     return list(set(toolchains))
 
 def getToolchainInfo(output):
@@ -444,7 +396,7 @@ def getTagSet(cpu_info):
         tag_set |= set(cpu["CPU_TAGS"])
         tag_set |= set([cpu["TOOLCHAIN"]])
     return tag_set
-        
+
 
 def getInfos(definition):
     D = {}