workspace_tools/export_example.py

# export_example.py 2013/8/11
"""
mbed SDK
Copyright (c) 2011-2013 ARM Limited

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import sys
from os.path import join, abspath, dirname, exists
ROOT = abspath(join(dirname(__file__), ".."))
sys.path.append(ROOT)

from workspace_tools.paths import *
from workspace_tools.utils import mkdir, cmd, copy_file
from workspace_tools.export import export

from shutil import copytree

EXPORT_DIR = join(BUILD_DIR, "export_example")
USER_WORKSPACE = join(EXPORT_DIR, "user_workspace")

USR_PRJ_NAME = "example"
USER_PRJ = join(USER_WORKSPACE, USR_PRJ_NAME)
USER_LIB = join(USER_PRJ, "lib")
USER_SRC = join(USER_PRJ, "src")

TEMP = join(USER_WORKSPACE, ".temp")


def setup_test_user_prj():
    if exists(USER_PRJ):
        print 'Test user project already generated...'
        return
    
    # Build project directory structure
    for d in [USER_LIB, USER_SRC]:
        mkdir(d)
    
    # Sources
    print 'Copying sources...'
    open(join(USER_SRC, "main.cpp"), 'w').write("""#include "mbed.h"

DigitalOut myled(LED1);

int main() {
    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}
""")

    #copy_file(join(TEST_DIR, "rtos", "mbed", "basic", "main.cpp"), join(USER_SRC, "main.cpp"))
    #copytree(join(LIB_DIR, "rtos"), join(USER_LIB, "rtos"))

    # FAKE BUILD URL
    open(join(USER_SRC, "mbed.bld"), 'w').write("http://mbed.org/users/mbed_official/code/mbed/builds/976df7c37ad5\n")


def fake_build_url_resolver(url):
    # FAKE BUILD URL: Ignore the URL, always return the path to the mbed library
    return {'path':MBED_LIBRARIES, 'name':'mbed'}


def test_export(toolchain, target, expected_error=None):
    if toolchain is None and target is None:
        base_dir = join(TEMP, "zip")
    else:
        base_dir = join(TEMP, toolchain, target)
    temp_dir = join(base_dir, "temp")
    mkdir(temp_dir)
    
    zip_path, report = export(USER_PRJ, USR_PRJ_NAME, toolchain, target, base_dir, temp_dir, False, fake_build_url_resolver)
    
    if report['success']:
        export_name = join(EXPORT_DIR, "%s_%s_%s.zip" % (USR_PRJ_NAME, toolchain, target))
        #cmd(["mv", zip_path, export_name])
        copy_file(zip_path, export_name)
        print "[OK]"
    else:
        if expected_error is None:
            print '[ERRROR] %s' % report['errormsg']
        else:
            if (zip_path is None) and (expected_error in report['errormsg']):
                print '[OK]'
            else:
                print '[ERROR]'
                print '    zip:', zip_path
                print '    msg:', report['errormsg']


if __name__ == '__main__':
    setup_test_user_prj()
    
    for toolchain, target in [
            ('uvision', 'LPC1768'), 
            ('uvision', 'KL25Z'),
            ('uvision', 'LPC1114'),
            ('uvision', 'LPC11U24'),
        ]:
        print '\n=== Exporting to "%s::%s" ===' % (toolchain, target)
        test_export(toolchain, target)