]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/workspace_tools/export/exporters.py
1 """Just a template for subclassing"""
2 import uuid
, shutil
, os
, logging
, fnmatch
3 from os
import walk
, remove
4 from os
.path
import join
, dirname
, isdir
, split
6 from jinja2
import Template
, FileSystemLoader
7 from jinja2
.environment
import Environment
8 from contextlib
import closing
9 from zipfile
import ZipFile
, ZIP_DEFLATED
11 from workspace_tools
.utils
import mkdir
12 from workspace_tools
.toolchains
import TOOLCHAIN_CLASSES
13 from workspace_tools
.targets
import TARGET_MAP
15 class OldLibrariesException(Exception): pass
18 TEMPLATE_DIR
= dirname(__file__
)
19 DOT_IN_RELATIVE_PATH
= False
21 def __init__(self
, target
, inputDir
, program_name
, build_url_resolver
, extra_symbols
=None):
22 self
.inputDir
= inputDir
24 self
.program_name
= program_name
25 self
.toolchain
= TOOLCHAIN_CLASSES
[self
.get_toolchain()](TARGET_MAP
[target
])
26 self
.build_url_resolver
= build_url_resolver
27 jinja_loader
= FileSystemLoader(os
.path
.dirname(os
.path
.abspath(__file__
)))
28 self
.jinja_environment
= Environment(loader
=jinja_loader
)
29 self
.extra_symbols
= extra_symbols
31 def get_toolchain(self
):
34 def __scan_and_copy(self
, src_path
, trg_path
):
35 resources
= self
.toolchain
.scan_resources(src_path
)
37 for r_type
in ['headers', 's_sources', 'c_sources', 'cpp_sources',
38 'objects', 'libraries', 'linker_script',
39 'lib_builds', 'lib_refs', 'repo_files', 'hex_files', 'bin_files']:
40 r
= getattr(resources
, r_type
)
42 self
.toolchain
.copy_files(r
, trg_path
, rel_path
=src_path
)
45 def __scan_all(self
, path
):
48 for root
, dirs
, files
in walk(path
):
50 if d
== '.' or d
== '..':
54 file_path
= join(root
, file)
55 resources
.append(file_path
)
59 def scan_and_copy_resources(self
, prj_path
, trg_path
):
60 # Copy only the file for the required target and toolchain
62 for src
in ['lib', 'src']:
63 resources
= self
.__scan
_and
_copy
(join(prj_path
, src
), trg_path
)
64 lib_builds
.extend(resources
.lib_builds
)
66 # The repository files
67 for repo_dir
in resources
.repo_dirs
:
68 repo_files
= self
.__scan
_all
(repo_dir
)
69 self
.toolchain
.copy_files(repo_files
, trg_path
, rel_path
=join(prj_path
, src
))
71 # The libraries builds
72 for bld
in lib_builds
:
73 build_url
= open(bld
).read().strip()
74 lib_data
= self
.build_url_resolver(build_url
)
75 lib_path
= lib_data
['path'].rstrip('\\/')
76 self
.__scan
_and
_copy
(lib_path
, join(trg_path
, lib_data
['name']))
78 # Create .hg dir in mbed build dir so it's ignored when versioning
79 hgdir
= join(trg_path
, lib_data
['name'], '.hg')
81 fhandle
= file(join(hgdir
, 'keep.me'), 'a')
84 # Final scan of the actual exported resources
85 self
.resources
= self
.toolchain
.scan_resources(trg_path
)
86 self
.resources
.relative_to(trg_path
, self
.DOT_IN_RELATIVE_PATH
)
87 # Check the existence of a binary build of the mbed library for the desired target
88 # This prevents exporting the mbed libraries from source
89 # if not self.toolchain.mbed_libs:
90 # raise OldLibrariesException()
92 def gen_file(self
, template_file
, data
, target_file
):
93 template_path
= join(Exporter
.TEMPLATE_DIR
, template_file
)
94 template
= self
.jinja_environment
.get_template(template_file
)
95 target_text
= template
.render(data
)
97 target_path
= join(self
.inputDir
, target_file
)
98 logging
.debug("Generating: %s" % target_path
)
99 open(target_path
, "w").write(target_text
)
101 def get_symbols(self
, add_extra_symbols
=True):
102 """ This function returns symbols which must be exported.
103 Please add / overwrite symbols in each exporter separately
105 symbols
= self
.toolchain
.get_symbols()
106 # We have extra symbols from e.g. libraries, we want to have them also added to export
107 if add_extra_symbols
:
108 if self
.extra_symbols
is not None:
109 symbols
.extend(self
.extra_symbols
)
112 def zip_working_directory_and_clean_up(tempdirectory
=None, destination
=None, program_name
=None, clean
=True):
113 uid
= str(uuid
.uuid4())
114 zipfilename
= '%s.zip'%uid
116 logging
.debug("Zipping up %s to %s" % (tempdirectory
, join(destination
, zipfilename
)))
118 def zipdir(basedir
, archivename
):
119 assert isdir(basedir
)
120 fakeroot
= program_name
+ '/'
121 with
closing(ZipFile(archivename
, "w", ZIP_DEFLATED
)) as z
:
122 for root
, _
, files
in os
.walk(basedir
):
123 # NOTE: ignore empty directories
125 absfn
= join(root
, fn
)
126 zfn
= fakeroot
+ '/' + absfn
[len(basedir
)+len(os
.sep
):]
129 zipdir(tempdirectory
, join(destination
, zipfilename
))
132 shutil
.rmtree(tempdirectory
)
134 return join(destination
, zipfilename
)