Brian Daniels / mbed-tools

Fork of mbed-tools by Morpheus

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers __init__.py Source File

__init__.py

00001 """
00002 mbed SDK
00003 Copyright (c) 2011-2013 ARM Limited
00004 
00005 Licensed under the Apache License, Version 2.0 (the "License");
00006 you may not use this file except in compliance with the License.
00007 You may obtain a copy of the License at
00008 
00009     http://www.apache.org/licenses/LICENSE-2.0
00010 
00011 Unless required by applicable law or agreed to in writing, software
00012 distributed under the License is distributed on an "AS IS" BASIS,
00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 See the License for the specific language governing permissions and
00015 limitations under the License.
00016 """
00017 import os, tempfile
00018 from os.path import join, exists, basename
00019 from shutil import copytree, rmtree, copy
00020 
00021 from tools.utils import mkdir
00022 from tools.export import uvision4, codered, gccarm, ds5_5, iar, emblocks, coide, kds, zip, simplicityv3, atmelstudio, sw4stm32
00023 from tools.export.exporters import zip_working_directory_and_clean_up, OldLibrariesException
00024 from tools.targets import TARGET_NAMES, EXPORT_MAP, TARGET_MAP
00025 
00026 from project_generator_definitions.definitions import ProGenDef
00027 
00028 EXPORTERS = {
00029     'uvision': uvision4.Uvision4,
00030     'lpcxpresso': codered.CodeRed,
00031     'gcc_arm': gccarm.GccArm,
00032     'ds5_5': ds5_5.DS5_5,
00033     'iar': iar.IAREmbeddedWorkbench,
00034     'emblocks' : emblocks.IntermediateFile,
00035     'coide' : coide.CoIDE,
00036     'kds' : kds.KDS,
00037     'simplicityv3' : simplicityv3.SimplicityV3,
00038     'atmelstudio' : atmelstudio.AtmelStudio,
00039     'sw4stm32'    : sw4stm32.Sw4STM32,
00040 }
00041 
00042 ERROR_MESSAGE_UNSUPPORTED_TOOLCHAIN = """
00043 Sorry, the target %s is not currently supported on the %s toolchain.
00044 Please refer to <a href='/handbook/Exporting-to-offline-toolchains' target='_blank'>Exporting to offline toolchains</a> for more information.
00045 """
00046 
00047 ERROR_MESSAGE_NOT_EXPORT_LIBS = """
00048 To export this project please <a href='http://mbed.org/compiler/?import=http://mbed.org/users/mbed_official/code/mbed-export/k&mode=lib' target='_blank'>import the export version of the mbed library</a>.
00049 """
00050 
00051 def online_build_url_resolver(url):
00052     # TODO: Retrieve the path and name of an online library build URL
00053     return {'path':'', 'name':''}
00054 
00055 
00056 def export(project_path, project_name, ide, target, destination='/tmp/',
00057            tempdir=None, clean=True, extra_symbols=None, zip=True, build_url_resolver=online_build_url_resolver):
00058     # Convention: we are using capitals for toolchain and target names
00059     if target is not None:
00060         target = target.upper()
00061 
00062     if tempdir is None:
00063         tempdir = tempfile.mkdtemp()
00064 
00065     report = {'success': False, 'errormsg':''}
00066     if ide is None or ide == "zip":
00067         # Simple ZIP exporter
00068         try:
00069             ide = "zip"
00070             exporter = zip.ZIP(target, tempdir, project_name, build_url_resolver, extra_symbols=extra_symbols)
00071             exporter.scan_and_copy_resources(project_path, tempdir)
00072             exporter.generate()
00073             report['success'] = True
00074         except OldLibrariesException, e:
00075             report['errormsg'] = ERROR_MESSAGE_NOT_EXPORT_LIBS
00076     else:
00077         if ide not in EXPORTERS:
00078             report['errormsg'] = ERROR_MESSAGE_UNSUPPORTED_TOOLCHAIN % (target, ide)
00079         else:
00080             Exporter = EXPORTERS[ide]
00081             target = EXPORT_MAP.get(target, target)
00082             # use progen targets or mbed exporters targets, check progen attribute
00083             use_progen = False
00084             supported = True
00085             try:
00086                 if Exporter.PROGEN_ACTIVE:
00087                     use_progen = True
00088             except AttributeError:
00089                 pass
00090             if use_progen:
00091                 if not ProGenDef(ide).is_supported(TARGET_MAP[target].progen['target']):
00092                     supported = False
00093             else:
00094                 if target not in Exporter.TARGETS:
00095                     supported = False
00096 
00097             if supported:
00098                 # target checked, export
00099                 try:
00100                     exporter = Exporter(target, tempdir, project_name, build_url_resolver, extra_symbols=extra_symbols)
00101                     exporter.scan_and_copy_resources(project_path, tempdir)
00102                     exporter.generate()
00103                     report['success'] = True
00104                 except OldLibrariesException, e:
00105                     report['errormsg'] = ERROR_MESSAGE_NOT_EXPORT_LIBS
00106             else:
00107                 report['errormsg'] = ERROR_MESSAGE_UNSUPPORTED_TOOLCHAIN % (target, ide)
00108 
00109     zip_path = None
00110     if report['success']:
00111         # add readme file to every offline export.
00112         open(os.path.join(tempdir, 'GettingStarted.htm'),'w').write('<meta http-equiv="refresh" content="0; url=http://mbed.org/handbook/Getting-Started-mbed-Exporters#%s"/>'% (ide))
00113         # copy .hgignore file to exported direcotry as well.
00114         if exists(os.path.join(exporter.TEMPLATE_DIR,'.hgignore')):
00115             copy(os.path.join(exporter.TEMPLATE_DIR,'.hgignore'),tempdir)
00116         
00117         if zip:
00118             zip_path = zip_working_directory_and_clean_up(tempdir, destination, project_name, clean)
00119         else:
00120             zip_path = destination
00121 
00122     return zip_path, report
00123 
00124 
00125 ###############################################################################
00126 # Generate project folders following the online conventions
00127 ###############################################################################
00128 def copy_tree(src, dst, clean=True):
00129     if exists(dst):
00130         if clean:
00131             rmtree(dst)
00132         else:
00133             return
00134 
00135     copytree(src, dst)
00136 
00137 
00138 def setup_user_prj (user_dir, prj_path, lib_paths=None):
00139     """
00140     Setup a project with the same directory structure of the mbed online IDE
00141     """
00142     mkdir(user_dir)
00143 
00144     # Project Path
00145     copy_tree(prj_path, join(user_dir, "src"))
00146 
00147     # Project Libraries
00148     user_lib = join(user_dir, "lib")
00149     mkdir(user_lib)
00150 
00151     if lib_paths is not None:
00152         for lib_path in lib_paths:
00153             copy_tree(lib_path, join(user_lib, basename(lib_path)))
00154 
00155 def mcu_ide_matrix (verbose_html=False, platform_filter=None):
00156     """  Shows target map using prettytable """
00157     supported_ides = []
00158     for key in EXPORTERS.iterkeys():
00159         supported_ides.append(key)
00160     supported_ides.sort()
00161     from prettytable import PrettyTable, ALL # Only use it in this function so building works without extra modules
00162 
00163     # All tests status table print
00164     columns = ["Platform"] + supported_ides
00165     pt = PrettyTable(columns)
00166     # Align table
00167     for col in columns:
00168         pt.align[col] = "c"
00169     pt.align["Platform"] = "l"
00170 
00171     perm_counter = 0
00172     target_counter = 0
00173     for target in sorted(TARGET_NAMES):
00174         target_counter += 1
00175 
00176         row = [target]  # First column is platform name
00177         for ide in supported_ides:
00178             text = "-"
00179             if target in EXPORTERS[ide].TARGETS:
00180                 if verbose_html:
00181                     text = "&#10003;"
00182                 else:
00183                     text = "x"
00184                 perm_counter += 1
00185             row.append(text)
00186         pt.add_row(row)
00187 
00188     pt.border = True
00189     pt.vrules = ALL
00190     pt.hrules = ALL
00191     # creates a html page suitable for a browser
00192     # result = pt.get_html_string(format=True) if verbose_html else pt.get_string()
00193     # creates a html page in a shorter format suitable for readme.md
00194     result = pt.get_html_string() if verbose_html else pt.get_string()
00195     result += "\n"
00196     result += "Total IDEs: %d\n"% (len(supported_ides))
00197     if verbose_html: result += "<br>"
00198     result += "Total platforms: %d\n"% (target_counter)
00199     if verbose_html: result += "<br>"
00200     result += "Total permutations: %d"% (perm_counter)
00201     if verbose_html: result = result.replace("&amp;", "&")
00202     return result