diff options
Diffstat (limited to 'scripts/lib/recipetool/create.py')
-rw-r--r-- | scripts/lib/recipetool/create.py | 413 |
1 files changed, 413 insertions, 0 deletions
diff --git a/scripts/lib/recipetool/create.py b/scripts/lib/recipetool/create.py new file mode 100644 index 0000000000..6dba07819b --- /dev/null +++ b/scripts/lib/recipetool/create.py | |||
@@ -0,0 +1,413 @@ | |||
1 | # Recipe creation tool - create command plugin | ||
2 | # | ||
3 | # Copyright (C) 2014 Intel Corporation | ||
4 | # | ||
5 | # This program is free software; you can redistribute it and/or modify | ||
6 | # it under the terms of the GNU General Public License version 2 as | ||
7 | # published by the Free Software Foundation. | ||
8 | # | ||
9 | # This program is distributed in the hope that it will be useful, | ||
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | # GNU General Public License for more details. | ||
13 | # | ||
14 | # You should have received a copy of the GNU General Public License along | ||
15 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
16 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
17 | |||
18 | import sys | ||
19 | import os | ||
20 | import argparse | ||
21 | import glob | ||
22 | import fnmatch | ||
23 | import re | ||
24 | import logging | ||
25 | |||
26 | logger = logging.getLogger('recipetool') | ||
27 | |||
28 | tinfoil = None | ||
29 | plugins = None | ||
30 | |||
31 | def plugin_init(pluginlist): | ||
32 | # Take a reference to the list so we can use it later | ||
33 | global plugins | ||
34 | plugins = pluginlist | ||
35 | |||
36 | def tinfoil_init(instance): | ||
37 | global tinfoil | ||
38 | tinfoil = instance | ||
39 | |||
40 | class RecipeHandler(): | ||
41 | @staticmethod | ||
42 | def checkfiles(path, speclist): | ||
43 | results = [] | ||
44 | for spec in speclist: | ||
45 | results.extend(glob.glob(os.path.join(path, spec))) | ||
46 | return results | ||
47 | |||
48 | def genfunction(self, outlines, funcname, content): | ||
49 | outlines.append('%s () {' % funcname) | ||
50 | for line in content: | ||
51 | outlines.append('\t%s' % line) | ||
52 | outlines.append('}') | ||
53 | outlines.append('') | ||
54 | |||
55 | def process(self, srctree, classes, lines_before, lines_after, handled): | ||
56 | return False | ||
57 | |||
58 | |||
59 | |||
60 | def fetch_source(uri, destdir): | ||
61 | import bb.data | ||
62 | bb.utils.mkdirhier(destdir) | ||
63 | localdata = bb.data.createCopy(tinfoil.config_data) | ||
64 | bb.data.update_data(localdata) | ||
65 | localdata.setVar('BB_STRICT_CHECKSUM', '') | ||
66 | localdata.setVar('SRCREV', '${AUTOREV}') | ||
67 | ret = (None, None) | ||
68 | olddir = os.getcwd() | ||
69 | try: | ||
70 | fetcher = bb.fetch2.Fetch([uri], localdata) | ||
71 | for u in fetcher.ud: | ||
72 | ud = fetcher.ud[u] | ||
73 | ud.ignore_checksums = True | ||
74 | fetcher.download() | ||
75 | fetcher.unpack(destdir) | ||
76 | for u in fetcher.ud: | ||
77 | ud = fetcher.ud[u] | ||
78 | if ud.method.recommends_checksum(ud): | ||
79 | md5value = bb.utils.md5_file(ud.localpath) | ||
80 | sha256value = bb.utils.sha256_file(ud.localpath) | ||
81 | ret = (md5value, sha256value) | ||
82 | except bb.fetch2.BBFetchException, e: | ||
83 | raise bb.build.FuncFailed(e) | ||
84 | finally: | ||
85 | os.chdir(olddir) | ||
86 | return ret | ||
87 | |||
88 | def supports_srcrev(uri): | ||
89 | localdata = bb.data.createCopy(tinfoil.config_data) | ||
90 | bb.data.update_data(localdata) | ||
91 | fetcher = bb.fetch2.Fetch([uri], localdata) | ||
92 | urldata = fetcher.ud | ||
93 | for u in urldata: | ||
94 | if urldata[u].method.supports_srcrev(): | ||
95 | return True | ||
96 | return False | ||
97 | |||
98 | def create_recipe(args): | ||
99 | import bb.process | ||
100 | import tempfile | ||
101 | import shutil | ||
102 | |||
103 | pkgarch = "" | ||
104 | if args.machine: | ||
105 | pkgarch = "${MACHINE_ARCH}" | ||
106 | |||
107 | checksums = (None, None) | ||
108 | tempsrc = '' | ||
109 | srcsubdir = '' | ||
110 | if '://' in args.source: | ||
111 | # Fetch a URL | ||
112 | srcuri = args.source | ||
113 | if args.externalsrc: | ||
114 | srctree = args.externalsrc | ||
115 | else: | ||
116 | tempsrc = tempfile.mkdtemp(prefix='recipetool-') | ||
117 | srctree = tempsrc | ||
118 | logger.info('Fetching %s...' % srcuri) | ||
119 | checksums = fetch_source(args.source, srctree) | ||
120 | dirlist = os.listdir(srctree) | ||
121 | if 'git.indirectionsymlink' in dirlist: | ||
122 | dirlist.remove('git.indirectionsymlink') | ||
123 | if len(dirlist) == 1 and os.path.isdir(os.path.join(srctree, dirlist[0])): | ||
124 | # We unpacked a single directory, so we should use that | ||
125 | srcsubdir = dirlist[0] | ||
126 | srctree = os.path.join(srctree, srcsubdir) | ||
127 | else: | ||
128 | # Assume we're pointing to an existing source tree | ||
129 | if args.externalsrc: | ||
130 | logger.error('externalsrc cannot be specified if source is a directory') | ||
131 | sys.exit(1) | ||
132 | if not os.path.isdir(args.source): | ||
133 | logger.error('Invalid source directory %s' % args.source) | ||
134 | sys.exit(1) | ||
135 | srcuri = '' | ||
136 | srctree = args.source | ||
137 | |||
138 | outfile = args.outfile | ||
139 | if outfile and outfile != '-': | ||
140 | if os.path.exists(outfile): | ||
141 | logger.error('Output file %s already exists' % outfile) | ||
142 | sys.exit(1) | ||
143 | |||
144 | lines_before = [] | ||
145 | lines_after = [] | ||
146 | |||
147 | lines_before.append('# Recipe created by %s' % os.path.basename(sys.argv[0])) | ||
148 | lines_before.append('# This is the basis of a recipe and may need further editing in order to be fully functional.') | ||
149 | lines_before.append('# (Feel free to remove these comments when editing.)') | ||
150 | lines_before.append('#') | ||
151 | |||
152 | licvalues = guess_license(srctree) | ||
153 | lic_files_chksum = [] | ||
154 | if licvalues: | ||
155 | licenses = [] | ||
156 | for licvalue in licvalues: | ||
157 | if not licvalue[0] in licenses: | ||
158 | licenses.append(licvalue[0]) | ||
159 | lic_files_chksum.append('file://%s;md5=%s' % (licvalue[1], licvalue[2])) | ||
160 | lines_before.append('# WARNING: the following LICENSE and LIC_FILES_CHKSUM values are best guesses - it is') | ||
161 | lines_before.append('# your responsibility to verify that the values are complete and correct.') | ||
162 | if len(licvalues) > 1: | ||
163 | lines_before.append('#') | ||
164 | lines_before.append('# NOTE: multiple licenses have been detected; if that is correct you should separate') | ||
165 | lines_before.append('# these in the LICENSE value using & if the multiple licenses all apply, or | if there') | ||
166 | lines_before.append('# is a choice between the multiple licenses. If in doubt, check the accompanying') | ||
167 | lines_before.append('# documentation to determine which situation is applicable.') | ||
168 | else: | ||
169 | lines_before.append('# Unable to find any files that looked like license statements. Check the accompanying') | ||
170 | lines_before.append('# documentation and source headers and set LICENSE and LIC_FILES_CHKSUM accordingly.') | ||
171 | lines_before.append('#') | ||
172 | lines_before.append('# NOTE: LICENSE is being set to "CLOSED" to allow you to at least start building - if') | ||
173 | lines_before.append('# this is not accurate with respect to the licensing of the software being built (it') | ||
174 | lines_before.append('# will not be in most cases) you must specify the correct value before using this') | ||
175 | lines_before.append('# recipe for anything other than initial testing/development!') | ||
176 | licenses = ['CLOSED'] | ||
177 | lines_before.append('LICENSE = "%s"' % ' '.join(licenses)) | ||
178 | lines_before.append('LIC_FILES_CHKSUM = "%s"' % ' \\\n '.join(lic_files_chksum)) | ||
179 | lines_before.append('') | ||
180 | |||
181 | # FIXME This is kind of a hack, we probably ought to be using bitbake to do this | ||
182 | # we'd also want a way to automatically set outfile based upon auto-detecting these values from the source if possible | ||
183 | recipefn = os.path.splitext(os.path.basename(outfile))[0] | ||
184 | fnsplit = recipefn.split('_') | ||
185 | if len(fnsplit) > 1: | ||
186 | pn = fnsplit[0] | ||
187 | pv = fnsplit[1] | ||
188 | else: | ||
189 | pn = recipefn | ||
190 | pv = None | ||
191 | |||
192 | if srcuri: | ||
193 | if pv and pv not in 'git svn hg'.split(): | ||
194 | srcuri = srcuri.replace(pv, '${PV}') | ||
195 | else: | ||
196 | lines_before.append('# No information for SRC_URI yet (only an external source tree was specified)') | ||
197 | lines_before.append('SRC_URI = "%s"' % srcuri) | ||
198 | (md5value, sha256value) = checksums | ||
199 | if md5value: | ||
200 | lines_before.append('SRC_URI[md5sum] = "%s"' % md5value) | ||
201 | if sha256value: | ||
202 | lines_before.append('SRC_URI[sha256sum] = "%s"' % sha256value) | ||
203 | if srcuri and supports_srcrev(srcuri): | ||
204 | lines_before.append('') | ||
205 | lines_before.append('# Modify these as desired') | ||
206 | lines_before.append('PV = "1.0+git${SRCPV}"') | ||
207 | lines_before.append('SRCREV = "${AUTOREV}"') | ||
208 | lines_before.append('') | ||
209 | |||
210 | if srcsubdir and pv: | ||
211 | if srcsubdir == "%s-%s" % (pn, pv): | ||
212 | # This would be the default, so we don't need to set S in the recipe | ||
213 | srcsubdir = '' | ||
214 | if srcsubdir: | ||
215 | lines_before.append('S = "${WORKDIR}/%s"' % srcsubdir) | ||
216 | lines_before.append('') | ||
217 | |||
218 | if pkgarch: | ||
219 | lines_after.append('PACKAGE_ARCH = "%s"' % pkgarch) | ||
220 | lines_after.append('') | ||
221 | |||
222 | # Find all plugins that want to register handlers | ||
223 | handlers = [] | ||
224 | for plugin in plugins: | ||
225 | if hasattr(plugin, 'register_recipe_handlers'): | ||
226 | plugin.register_recipe_handlers(handlers) | ||
227 | |||
228 | # Apply the handlers | ||
229 | classes = [] | ||
230 | handled = [] | ||
231 | for handler in handlers: | ||
232 | handler.process(srctree, classes, lines_before, lines_after, handled) | ||
233 | |||
234 | outlines = [] | ||
235 | outlines.extend(lines_before) | ||
236 | if classes: | ||
237 | outlines.append('inherit %s' % ' '.join(classes)) | ||
238 | outlines.append('') | ||
239 | outlines.extend(lines_after) | ||
240 | |||
241 | if outfile == '-': | ||
242 | sys.stdout.write('\n'.join(outlines) + '\n') | ||
243 | else: | ||
244 | with open(outfile, 'w') as f: | ||
245 | f.write('\n'.join(outlines) + '\n') | ||
246 | logger.info('Recipe %s has been created; further editing may be required to make it fully functional' % outfile) | ||
247 | |||
248 | if tempsrc: | ||
249 | shutil.rmtree(tempsrc) | ||
250 | |||
251 | return 0 | ||
252 | |||
253 | def get_license_md5sums(d, static_only=False): | ||
254 | import bb.utils | ||
255 | md5sums = {} | ||
256 | if not static_only: | ||
257 | # Gather md5sums of license files in common license dir | ||
258 | commonlicdir = d.getVar('COMMON_LICENSE_DIR', True) | ||
259 | for fn in os.listdir(commonlicdir): | ||
260 | md5value = bb.utils.md5_file(os.path.join(commonlicdir, fn)) | ||
261 | md5sums[md5value] = fn | ||
262 | # The following were extracted from common values in various recipes | ||
263 | # (double checking the license against the license file itself, not just | ||
264 | # the LICENSE value in the recipe) | ||
265 | md5sums['94d55d512a9ba36caa9b7df079bae19f'] = 'GPLv2' | ||
266 | md5sums['b234ee4d69f5fce4486a80fdaf4a4263'] = 'GPLv2' | ||
267 | md5sums['59530bdf33659b29e73d4adb9f9f6552'] = 'GPLv2' | ||
268 | md5sums['0636e73ff0215e8d672dc4c32c317bb3'] = 'GPLv2' | ||
269 | md5sums['eb723b61539feef013de476e68b5c50a'] = 'GPLv2' | ||
270 | md5sums['751419260aa954499f7abaabaa882bbe'] = 'GPLv2' | ||
271 | md5sums['393a5ca445f6965873eca0259a17f833'] = 'GPLv2' | ||
272 | md5sums['12f884d2ae1ff87c09e5b7ccc2c4ca7e'] = 'GPLv2' | ||
273 | md5sums['8ca43cbc842c2336e835926c2166c28b'] = 'GPLv2' | ||
274 | md5sums['ebb5c50ab7cab4baeffba14977030c07'] = 'GPLv2' | ||
275 | md5sums['c93c0550bd3173f4504b2cbd8991e50b'] = 'GPLv2' | ||
276 | md5sums['9ac2e7cff1ddaf48b6eab6028f23ef88'] = 'GPLv2' | ||
277 | md5sums['4325afd396febcb659c36b49533135d4'] = 'GPLv2' | ||
278 | md5sums['18810669f13b87348459e611d31ab760'] = 'GPLv2' | ||
279 | md5sums['d7810fab7487fb0aad327b76f1be7cd7'] = 'GPLv2' # the Linux kernel's COPYING file | ||
280 | md5sums['bbb461211a33b134d42ed5ee802b37ff'] = 'LGPLv2.1' | ||
281 | md5sums['7fbc338309ac38fefcd64b04bb903e34'] = 'LGPLv2.1' | ||
282 | md5sums['4fbd65380cdd255951079008b364516c'] = 'LGPLv2.1' | ||
283 | md5sums['2d5025d4aa3495befef8f17206a5b0a1'] = 'LGPLv2.1' | ||
284 | md5sums['fbc093901857fcd118f065f900982c24'] = 'LGPLv2.1' | ||
285 | md5sums['a6f89e2100d9b6cdffcea4f398e37343'] = 'LGPLv2.1' | ||
286 | md5sums['d8045f3b8f929c1cb29a1e3fd737b499'] = 'LGPLv2.1' | ||
287 | md5sums['fad9b3332be894bab9bc501572864b29'] = 'LGPLv2.1' | ||
288 | md5sums['3bf50002aefd002f49e7bb854063f7e7'] = 'LGPLv2' | ||
289 | md5sums['9f604d8a4f8e74f4f5140845a21b6674'] = 'LGPLv2' | ||
290 | md5sums['5f30f0716dfdd0d91eb439ebec522ec2'] = 'LGPLv2' | ||
291 | md5sums['55ca817ccb7d5b5b66355690e9abc605'] = 'LGPLv2' | ||
292 | md5sums['252890d9eee26aab7b432e8b8a616475'] = 'LGPLv2' | ||
293 | md5sums['d32239bcb673463ab874e80d47fae504'] = 'GPLv3' | ||
294 | md5sums['f27defe1e96c2e1ecd4e0c9be8967949'] = 'GPLv3' | ||
295 | md5sums['6a6a8e020838b23406c81b19c1d46df6'] = 'LGPLv3' | ||
296 | md5sums['3b83ef96387f14655fc854ddc3c6bd57'] = 'Apache-2.0' | ||
297 | md5sums['385c55653886acac3821999a3ccd17b3'] = 'Artistic-1.0 | GPL-2.0' # some perl modules | ||
298 | return md5sums | ||
299 | |||
300 | def guess_license(srctree): | ||
301 | import bb | ||
302 | md5sums = get_license_md5sums(tinfoil.config_data) | ||
303 | |||
304 | licenses = [] | ||
305 | licspecs = ['LICENSE*', 'COPYING*', '*[Ll]icense*', 'LICENCE*', 'LEGAL*', '[Ll]egal*', '*GPL*', 'README.lic*', 'COPYRIGHT*', '[Cc]opyright*'] | ||
306 | licfiles = [] | ||
307 | for root, dirs, files in os.walk(srctree): | ||
308 | for fn in files: | ||
309 | for spec in licspecs: | ||
310 | if fnmatch.fnmatch(fn, spec): | ||
311 | licfiles.append(os.path.join(root, fn)) | ||
312 | for licfile in licfiles: | ||
313 | md5value = bb.utils.md5_file(licfile) | ||
314 | license = md5sums.get(md5value, 'Unknown') | ||
315 | licenses.append((license, os.path.relpath(licfile, srctree), md5value)) | ||
316 | |||
317 | # FIXME should we grab at least one source file with a license header and add that too? | ||
318 | |||
319 | return licenses | ||
320 | |||
321 | def read_pkgconfig_provides(d): | ||
322 | pkgdatadir = d.getVar('PKGDATA_DIR', True) | ||
323 | pkgmap = {} | ||
324 | for fn in glob.glob(os.path.join(pkgdatadir, 'shlibs2', '*.pclist')): | ||
325 | with open(fn, 'r') as f: | ||
326 | for line in f: | ||
327 | pkgmap[os.path.basename(line.rstrip())] = os.path.splitext(os.path.basename(fn))[0] | ||
328 | recipemap = {} | ||
329 | for pc, pkg in pkgmap.iteritems(): | ||
330 | pkgdatafile = os.path.join(pkgdatadir, 'runtime', pkg) | ||
331 | if os.path.exists(pkgdatafile): | ||
332 | with open(pkgdatafile, 'r') as f: | ||
333 | for line in f: | ||
334 | if line.startswith('PN: '): | ||
335 | recipemap[pc] = line.split(':', 1)[1].strip() | ||
336 | return recipemap | ||
337 | |||
338 | def convert_pkginfo(pkginfofile): | ||
339 | values = {} | ||
340 | with open(pkginfofile, 'r') as f: | ||
341 | indesc = False | ||
342 | for line in f: | ||
343 | if indesc: | ||
344 | if line.strip(): | ||
345 | values['DESCRIPTION'] += ' ' + line.strip() | ||
346 | else: | ||
347 | indesc = False | ||
348 | else: | ||
349 | splitline = line.split(': ', 1) | ||
350 | key = line[0] | ||
351 | value = line[1] | ||
352 | if key == 'LICENSE': | ||
353 | for dep in value.split(','): | ||
354 | dep = dep.split()[0] | ||
355 | mapped = depmap.get(dep, '') | ||
356 | if mapped: | ||
357 | depends.append(mapped) | ||
358 | elif key == 'License': | ||
359 | values['LICENSE'] = value | ||
360 | elif key == 'Summary': | ||
361 | values['SUMMARY'] = value | ||
362 | elif key == 'Description': | ||
363 | values['DESCRIPTION'] = value | ||
364 | indesc = True | ||
365 | return values | ||
366 | |||
367 | def convert_debian(debpath): | ||
368 | # FIXME extend this mapping - perhaps use distro_alias.inc? | ||
369 | depmap = {'libz-dev': 'zlib'} | ||
370 | |||
371 | values = {} | ||
372 | depends = [] | ||
373 | with open(os.path.join(debpath, 'control')) as f: | ||
374 | indesc = False | ||
375 | for line in f: | ||
376 | if indesc: | ||
377 | if line.strip(): | ||
378 | if line.startswith(' This package contains'): | ||
379 | indesc = False | ||
380 | else: | ||
381 | values['DESCRIPTION'] += ' ' + line.strip() | ||
382 | else: | ||
383 | indesc = False | ||
384 | else: | ||
385 | splitline = line.split(':', 1) | ||
386 | key = line[0] | ||
387 | value = line[1] | ||
388 | if key == 'Build-Depends': | ||
389 | for dep in value.split(','): | ||
390 | dep = dep.split()[0] | ||
391 | mapped = depmap.get(dep, '') | ||
392 | if mapped: | ||
393 | depends.append(mapped) | ||
394 | elif key == 'Section': | ||
395 | values['SECTION'] = value | ||
396 | elif key == 'Description': | ||
397 | values['SUMMARY'] = value | ||
398 | indesc = True | ||
399 | |||
400 | if depends: | ||
401 | values['DEPENDS'] = ' '.join(depends) | ||
402 | |||
403 | return values | ||
404 | |||
405 | |||
406 | def register_command(subparsers): | ||
407 | parser_create = subparsers.add_parser('create', help='Create a new recipe') | ||
408 | parser_create.add_argument('source', help='Path or URL to source') | ||
409 | parser_create.add_argument('-o', '--outfile', help='Full path and filename to recipe to add', required=True) | ||
410 | parser_create.add_argument('-m', '--machine', help='Make recipe machine-specific as opposed to architecture-specific', action='store_true') | ||
411 | parser_create.add_argument('-x', '--externalsrc', help='Assuming source is a URL, fetch it and extract it to the specified directory') | ||
412 | parser_create.set_defaults(func=create_recipe) | ||
413 | |||