diff options
Diffstat (limited to 'bitbake/lib/bb/ui/crumbs/template.py')
-rw-r--r-- | bitbake/lib/bb/ui/crumbs/template.py | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/bitbake/lib/bb/ui/crumbs/template.py b/bitbake/lib/bb/ui/crumbs/template.py new file mode 100644 index 0000000000..d0283546af --- /dev/null +++ b/bitbake/lib/bb/ui/crumbs/template.py | |||
@@ -0,0 +1,180 @@ | |||
1 | # | ||
2 | # BitBake Graphical GTK User Interface | ||
3 | # | ||
4 | # Copyright (C) 2011 Intel Corporation | ||
5 | # | ||
6 | # Authored by Shane Wang <shane.wang@intel.com> | ||
7 | # | ||
8 | # This program is free software; you can redistribute it and/or modify | ||
9 | # it under the terms of the GNU General Public License version 2 as | ||
10 | # published by the Free Software Foundation. | ||
11 | # | ||
12 | # This program is distributed in the hope that it will be useful, | ||
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | # GNU General Public License for more details. | ||
16 | # | ||
17 | # You should have received a copy of the GNU General Public License along | ||
18 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
19 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
20 | |||
21 | import gobject | ||
22 | import os | ||
23 | import re | ||
24 | |||
25 | class File(gobject.GObject): | ||
26 | |||
27 | def __init__(self, pathfilename, suffix): | ||
28 | if not pathfilename.endswith(suffix): | ||
29 | pathfilename = "%s%s" % (pathfilename, suffix) | ||
30 | gobject.GObject.__init__(self) | ||
31 | self.pathfilename = pathfilename | ||
32 | |||
33 | def readFile(self): | ||
34 | if not os.path.isfile(self.pathfilename): | ||
35 | return None | ||
36 | if not os.path.exists(self.pathfilename): | ||
37 | return None | ||
38 | |||
39 | with open(self.pathfilename, 'r') as f: | ||
40 | contents = f.readlines() | ||
41 | f.close() | ||
42 | |||
43 | return contents | ||
44 | |||
45 | def writeFile(self, contents): | ||
46 | if os.path.exists(self.pathfilename): | ||
47 | orig = "%s.orig" % self.pathfilename | ||
48 | if os.path.exists(orig): | ||
49 | os.remove(orig) | ||
50 | os.rename(self.pathfilename, orig) | ||
51 | |||
52 | with open(self.pathfilename, 'w') as f: | ||
53 | f.write(contents) | ||
54 | f.close() | ||
55 | |||
56 | class ConfigFile(File): | ||
57 | """ | ||
58 | This object does save general config file. (say bblayers.conf, or local.conf). Again, it is the base class for other template files and image bb files. | ||
59 | """ | ||
60 | def __init__(self, pathfilename, suffix = None, header = None): | ||
61 | if suffix: | ||
62 | File.__init__(self, pathfilename, suffix) | ||
63 | else: | ||
64 | File.__init__(self, pathfilename, ".conf") | ||
65 | if header: | ||
66 | self.header = header | ||
67 | else: | ||
68 | self.header = "# Config generated by the HOB\n\n" | ||
69 | self.dictionary = {} | ||
70 | |||
71 | def setVar(self, var, val): | ||
72 | if isinstance(val, list): | ||
73 | liststr = "" | ||
74 | if val: | ||
75 | i = 0 | ||
76 | for value in val: | ||
77 | if i < len(val) - 1: | ||
78 | liststr += "%s " % value | ||
79 | else: | ||
80 | liststr += "%s" % value | ||
81 | i += 1 | ||
82 | self.dictionary[var] = liststr | ||
83 | else: | ||
84 | self.dictionary[var] = val | ||
85 | |||
86 | def save(self): | ||
87 | contents = self.header | ||
88 | for var, val in self.dictionary.items(): | ||
89 | contents += "%s = \"%s\"\n" % (var, val) | ||
90 | File.writeFile(self, contents) | ||
91 | |||
92 | class HobTemplateFile(ConfigFile): | ||
93 | """ | ||
94 | This object does save or load hob specific file. | ||
95 | """ | ||
96 | def __init__(self, pathfilename): | ||
97 | ConfigFile.__init__(self, pathfilename, ".hob", "# Hob Template generated by the HOB\n\n") | ||
98 | |||
99 | def getVar(self, var): | ||
100 | if var in self.dictionary: | ||
101 | return self.dictionary[var] | ||
102 | else: | ||
103 | return "" | ||
104 | |||
105 | def load(self): | ||
106 | contents = ConfigFile.readFile(self) | ||
107 | self.dictionary.clear() | ||
108 | |||
109 | pattern = "^\s*(\S+)\s*=\s*(\".*?\")" | ||
110 | |||
111 | for line in contents: | ||
112 | match = re.search(pattern, line) | ||
113 | if match: | ||
114 | var = match.group(1) | ||
115 | val = match.group(2).strip('"') | ||
116 | self.dictionary[var] = val | ||
117 | return self.dictionary | ||
118 | |||
119 | class RecipeFile(ConfigFile): | ||
120 | """ | ||
121 | This object is for image bb file. | ||
122 | """ | ||
123 | def __init__(self, pathfilename): | ||
124 | ConfigFile.__init__(self, pathfilename, ".bb", "# Recipe generated by the HOB\n\ninherit core-image\n") | ||
125 | |||
126 | class TemplateMgr(gobject.GObject): | ||
127 | |||
128 | __gLocalVars__ = ["MACHINE", "PACKAGE_CLASSES", "DISTRO", "DL_DIR", "SSTATE_DIR", "SSTATE_MIRROR", "PARALLEL_MAKE", "BB_NUMBER_THREAD"] | ||
129 | __gBBLayersVars__ = ["BBLAYERS"] | ||
130 | __gRecipeVars__ = ["DEPENDS", "IMAGE_INSTALL"] | ||
131 | |||
132 | def __init__(self): | ||
133 | gobject.GObject.__init__(self) | ||
134 | self.template_hob = None | ||
135 | self.bblayers_conf = None | ||
136 | self.local_conf = None | ||
137 | self.image_bb = None | ||
138 | |||
139 | def open(self, filename, path): | ||
140 | self.template_hob = HobTemplateFile("%s/%s%s%s" % (path, "template-", filename, ".hob")) | ||
141 | self.bblayers_conf = ConfigFile("%s/%s%s%s" % (path, "bblayers-", filename, ".conf")) | ||
142 | self.local_conf = ConfigFile("%s/%s%s%s" % (path, "local-", filename, ".conf")) | ||
143 | self.image_bb = RecipeFile("%s/%s%s%s" % (path, "hob-image-", filename, ".bb")) | ||
144 | |||
145 | def setVar(self, var, val): | ||
146 | if var in TemplateMgr.__gLocalVars__: | ||
147 | self.local_conf.setVar(var, val) | ||
148 | if var in TemplateMgr.__gBBLayersVars__: | ||
149 | self.bblayers_conf.setVar(var, val) | ||
150 | if var in TemplateMgr.__gRecipeVars__: | ||
151 | self.image_bb.setVar(var, val) | ||
152 | |||
153 | self.template_hob.setVar(var, val) | ||
154 | |||
155 | def save(self): | ||
156 | self.local_conf.save() | ||
157 | self.bblayers_conf.save() | ||
158 | self.image_bb.save() | ||
159 | self.template_hob.save() | ||
160 | |||
161 | def load(self, path): | ||
162 | self.template_hob = HobTemplateFile(path) | ||
163 | self.dictionary = self.template_hob.load() | ||
164 | |||
165 | def getVar(self, var): | ||
166 | return self.template_hob.getVar(var) | ||
167 | |||
168 | def destroy(self): | ||
169 | if self.template_hob: | ||
170 | del self.template_hob | ||
171 | template_hob = None | ||
172 | if self.bblayers_conf: | ||
173 | del self.bblayers_conf | ||
174 | self.bblayers_conf = None | ||
175 | if self.local_conf: | ||
176 | del self.local_conf | ||
177 | self.local_conf = None | ||
178 | if self.image_bb: | ||
179 | del self.image_bb | ||
180 | self.image_bb = None | ||