|
1 # Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. |
|
2 # This component and the accompanying materials are made available under the terms of the License |
|
3 # "Eclipse Public License v1.0" which accompanies this distribution, |
|
4 # and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
5 # |
|
6 # Initial Contributors: |
|
7 # Nokia Corporation - initial contribution. |
|
8 # |
|
9 # Contributors: |
|
10 # |
|
11 # Description: |
|
12 # |
|
13 import unittest |
|
14 import os |
|
15 import shutil |
|
16 import sys |
|
17 from optparse import OptionParser |
|
18 from xml.etree import ElementTree as etree |
|
19 from lib import scan, xml_decl, doctype_identifier |
|
20 |
|
21 __version__ = "0.1" |
|
22 |
|
23 |
|
24 class ComponentMapCreator(object): |
|
25 |
|
26 def __init__(self, build_dir, output_dir): |
|
27 self.build_dir = os.path.abspath(build_dir) |
|
28 self._check_dir_exists(self.build_dir) |
|
29 self.output_dir = output_dir |
|
30 |
|
31 def _check_dir_exists(self, dir): |
|
32 dir = os.path.abspath(dir) |
|
33 if not os.path.exists(dir): |
|
34 raise Exception("Directory path %s does not exist" % dir) |
|
35 |
|
36 def _get_component_names(self): |
|
37 component_names = [d for d in os.listdir(self.build_dir) if os.path.isdir(os.path.join(self.build_dir, d))] |
|
38 component_names.sort() |
|
39 return component_names |
|
40 |
|
41 def _get_ditamap_names_for_component_dir(self, component_dir): |
|
42 self._check_dir_exists(component_dir) |
|
43 ditamap_names = [] |
|
44 for root, dirs, files in os.walk(component_dir): |
|
45 ditamap_names.extend([os.path.splitext(f)[0] for f in files if f.lower().endswith(".ditamap")]) |
|
46 ditamap_names.sort() |
|
47 return ditamap_names |
|
48 |
|
49 def _get_component_dir_for_component(self, component): |
|
50 return os.path.join(self.build_dir, component) |
|
51 |
|
52 def _create_topicref(self, target_name): |
|
53 return etree.Element("topicref", href=target_name+".ditamap", type="ditamap") |
|
54 |
|
55 def _create_topichead(self, target_name): |
|
56 topichead = etree.Element("topichead", navtitle=target_name) |
|
57 topichead.append(self._create_topicref(target_name)) |
|
58 return topichead |
|
59 |
|
60 def _create_map(self, component_name): |
|
61 return etree.Element("map", title=component_name, id="cmp_"+component_name) |
|
62 |
|
63 def _create_ditamap(self, component_name): |
|
64 root = self._create_map(component_name) |
|
65 for topichead in self._get_topicheads_for_component(component_name): |
|
66 root.append(topichead) |
|
67 return root |
|
68 |
|
69 def _get_topicheads_for_component(self, component_name): |
|
70 topicheads = [] |
|
71 for ditamap_name in self._get_ditamap_names_for_component_dir(self._get_component_dir_for_component(component_name)): |
|
72 topicheads.append(self._create_topichead(ditamap_name)) |
|
73 topicheads.sort() |
|
74 return topicheads |
|
75 |
|
76 def _write_ditamap_for_component(self, component_name): |
|
77 f = open(os.path.join(self.output_dir, "cmp_"+component_name+".ditamap"), "w") |
|
78 f.write(xml_decl()+"\n") |
|
79 f.write(doctype_identifier("map")+"\n") |
|
80 map = self._create_ditamap(component_name) |
|
81 f.write(etree.tostring(map)) |
|
82 f.close() |
|
83 |
|
84 def create_component_maps(self): |
|
85 components = self._get_component_names() |
|
86 for component in components: |
|
87 self._write_ditamap_for_component(component) |
|
88 |
|
89 def create_component_maps(build_dir, output_dir): |
|
90 cmp = ComponentMapCreator(build_dir, output_dir) |
|
91 cmp.create_component_maps() |
|
92 |
|
93 def main(func): |
|
94 usage = "usage: %prog [options] build_dir output_dir" |
|
95 parser = OptionParser(usage, version='%prog ' + __version__) |
|
96 (options, args) = parser.parse_args() |
|
97 if len(args) < 2: |
|
98 parser.print_help() |
|
99 build_dir, output_dir = os.path.abspath(args[0]), os.path.abspath(args[1]) |
|
100 if not os.path.exists(build_dir): |
|
101 parser.error('build_dir: "%s" does not exist' % build_dir) |
|
102 func(build_dir, output_dir) |
|
103 |
|
104 if __name__ == '__main__': |
|
105 sys.exit(main(func=create_component_maps)) |
|
106 |
|
107 |
|
108 cmp_audiomsg_ditamap = """<?xml version="1.0" encoding="UTF-8"?> |
|
109 <!DOCTYPE map PUBLIC "-//OASIS//DTD DITA Map//EN" "map.dtd"> |
|
110 <map id="cmp_audiomsg" title="audiomsg"><topichead navtitle="audiomessage"><topicref href="audiomessage.ditamap" type="ditamap" /></topichead></map>""" |
|
111 |
|
112 audiomessage_ditamap = """ |
|
113 <?xml version='1.0' encoding='UTF-8' standalone='no'?> |
|
114 <!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type v0.1.0//EN" "dtd/cxxAPIMap.dtd" > |
|
115 <cxxAPIMap id="audiomessage" title="audiomessage"> |
|
116 <cxxStructRef href="struct___array_util.xml#_ArrayUtil" navtitle="_ArrayUtil"/> |
|
117 </cxxAPIMap> |
|
118 """ |
|
119 |
|
120 class TestComponentCreator(unittest.TestCase): |
|
121 def setUp(self): |
|
122 self.test_build_dir = os.path.join(os.getcwd(), "test_build_dir") |
|
123 self._create_test_build_dir() |
|
124 self.output_dir = os.path.join(os.getcwd(), "output") |
|
125 self.cmp = ComponentMapCreator(self.test_build_dir, self.output_dir) |
|
126 self._create_output_dir() |
|
127 |
|
128 def tearDown(self): |
|
129 self._clean_test_build_dir() |
|
130 self._clean_output_dir() |
|
131 |
|
132 def _create_test_build_dir(self): |
|
133 # Create 3 components |
|
134 for comp_name in ("audiomsg", "console", "autotest"): |
|
135 os.makedirs(os.path.join(self.test_build_dir, comp_name)) |
|
136 # Create logfiles |
|
137 makefile = open(os.path.join(self.test_build_dir, "Makefile"), "w") |
|
138 logfile = open(os.path.join(self.test_build_dir, "logfile.log"), "w") |
|
139 # Create target ditamap |
|
140 audiomessage_ditamap_dir = os.path.join(self.test_build_dir, "audiomsg", "c_96422b786aab3b96", "audiomessage_exe", "dox", "dita") |
|
141 audiomessage_ditamap_path = os.path.join(audiomessage_ditamap_dir, "audiomessage.ditamap") |
|
142 os.makedirs(audiomessage_ditamap_dir) |
|
143 audiomessage_ditamap_handle = open(audiomessage_ditamap_path, "w") |
|
144 audiomessage_ditamap_handle.write(audiomessage_ditamap) |
|
145 audiomessage_ditamap_handle.close() |
|
146 |
|
147 def _create_output_dir(self): |
|
148 os.mkdir(self.output_dir) |
|
149 |
|
150 def _clean_test_build_dir(self): |
|
151 shutil.rmtree(self.test_build_dir) |
|
152 |
|
153 def _clean_output_dir(self): |
|
154 shutil.rmtree(self.output_dir) |
|
155 |
|
156 def test_i_return_all_the_component_names_when_passed_a_dir(self): |
|
157 component_names = self.cmp._get_component_names() |
|
158 self.assertEquals(component_names, ["audiomsg", "autotest", "console"]) |
|
159 |
|
160 def test_i_return_all_target_ditamap_names_for_a_component(self): |
|
161 target_ditamap_names = self.cmp._get_ditamap_names_for_component_dir(os.path.join(self.test_build_dir, "audiomsg")) |
|
162 self.assertEquals(target_ditamap_names, ["audiomessage"]) |
|
163 |
|
164 def test_i_return_a_component_directory_for_a_component(self): |
|
165 component_dir = self.cmp._get_component_dir_for_component("audiomsg") |
|
166 self.assertEquals(component_dir, os.path.join(self.test_build_dir, "audiomsg")) |
|
167 |
|
168 def test_i_return_a_topicref_element_for_a_target(self): |
|
169 topicref = self.cmp._create_topicref("audiomessage") |
|
170 self.assertEquals(topicref.attrib["href"], "audiomessage.ditamap") |
|
171 self.assertEquals(topicref.attrib["type"], "ditamap") |
|
172 |
|
173 def test_i_return_a_list_of_topicheads_for_a_component(self): |
|
174 topicheads = self.cmp._get_topicheads_for_component("audiomsg") |
|
175 self.assertEquals(len(topicheads), 1) |
|
176 |
|
177 |
|
178 def test_i_create_a_ditamap_for_a_component(self): |
|
179 ditamap = self.cmp._create_ditamap("audiomsg") |
|
180 self.assertEquals(ditamap.tag, "map") |
|
181 self.assertEquals(ditamap.attrib.get("title", ""), "audiomsg") |
|
182 self.assertEquals(ditamap.attrib.get("id", ""), "cmp_audiomsg") |
|
183 |
|
184 def test_i_can_write_out_a_ditamap_file_for_a_component(self): |
|
185 self.cmp._write_ditamap_for_component("audiomsg") |
|
186 self.assertTrue(os.path.exists(os.path.join(self.output_dir,"cmp_audiomsg.ditamap"))) |
|
187 map = open(os.path.join(self.output_dir,"cmp_audiomsg.ditamap"),"r").read() |
|
188 print map |
|
189 print cmp_audiomsg_ditamap |
|
190 self.assertEquals(map, cmp_audiomsg_ditamap) |
|
191 |
|
192 def test_i_write_component_maps(self): |
|
193 self.cmp.create_component_maps() |
|
194 file_list = os.listdir(self.output_dir) |
|
195 file_list.sort() |
|
196 self.assertEquals(file_list, ["cmp_audiomsg.ditamap", "cmp_autotest.ditamap", "cmp_console.ditamap"]) |