|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright 2019 The ChromiumOS Authors |
| 4 | +# All rights reserved. |
| 5 | + |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are |
| 8 | +# met: |
| 9 | + |
| 10 | +# * Redistributions of source code must retain the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer. |
| 12 | +# * Redistributions in binary form must reproduce the above |
| 13 | +# copyright notice, this list of conditions and the following disclaimer |
| 14 | +# in the documentation and/or other materials provided with the |
| 15 | +# distribution. |
| 16 | +# * Neither the name of Google LLC nor the names of its |
| 17 | +# contributors may be used to endorse or promote products derived from |
| 18 | +# this software without specific prior written permission. |
| 19 | + |
| 20 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +# Binary License Terms |
| 32 | + |
| 33 | +"""Extract eclass variable names into Haskell list format.""" |
| 34 | +from __future__importprint_function |
| 35 | +importdatetime |
| 36 | +importos |
| 37 | +importre |
| 38 | +importsys |
| 39 | +importtextwrap |
| 40 | +# Matches a line that declares a variable in an eclass. |
| 41 | +VAR_RE=re.compile(r'@(?:ECLASS-)?VARIABLE:\s*(\w+)$') |
| 42 | +# Matches a line that declares inheritance. |
| 43 | +INHERIT_RE=re.compile(r'^[^#]*\binherit((?:\s+[\w-]+)+)$') |
| 44 | +VAR_FILE_HEADER="""module ShellCheck.PortageAutoInternalVariables ( |
| 45 | + portageAutoInternalVariables |
| 46 | + ) where |
| 47 | +-- This file contains the variables generated by |
| 48 | +-- portage/get_vars.py""" |
| 49 | +PORTAGE_AUTO_VAR_NAME='portageAutoInternalVariables' |
| 50 | +classEclass: |
| 51 | +"""Container for eclass information""" |
| 52 | +def__init__(self,name,eclass_vars,inheritances): |
| 53 | +self.name=name |
| 54 | +self.vars=eclass_vars |
| 55 | +self.inheritances=inheritances |
| 56 | +defcalculate_eclass_vars(self,eclasses): |
| 57 | +whileself.inheritances: |
| 58 | +name=self.inheritances.pop() |
| 59 | +try: |
| 60 | +sub_eclass=eclasses[name] |
| 61 | +new_vars=sub_eclass.calculate_eclass_vars(eclasses).vars |
| 62 | +self.vars=self.vars.union(new_vars) |
| 63 | +exceptException: |
| 64 | +pass |
| 65 | +returnself |
| 66 | +defprint_var_list(eclass,eclass_vars): |
| 67 | +var_list=' '.join(['"%s",'%vforvinsorted(eclass_vars)]) |
| 68 | +print(' -- %s\n%s'% |
| 69 | + (eclass, |
| 70 | +textwrap.fill( |
| 71 | +var_list,80,initial_indent=' ',subsequent_indent=' '))) |
| 72 | +defprocess_file(eclass_path): |
| 73 | +eclass_name=os.path.splitext(os.path.basename(eclass_path))[0] |
| 74 | +withopen(eclass_path,'r')asf: |
| 75 | +eclass_vars=set() |
| 76 | +eclass_inheritances=set() |
| 77 | +forlineinf: |
| 78 | +line=line.strip() |
| 79 | +ifnotline: |
| 80 | +continue |
| 81 | +whileline[-1]=='\\': |
| 82 | +line=line[:-1]+next(f).strip() |
| 83 | +match=VAR_RE.search(line) |
| 84 | +ifmatch: |
| 85 | +var_name=match.group(1) |
| 86 | +eclass_vars.add(var_name.strip()) |
| 87 | +else: |
| 88 | +match=INHERIT_RE.search(line) |
| 89 | +ifmatch: |
| 90 | +forinheritanceinre.split(r'\s+',match.group(1)): |
| 91 | +ifinheritance.strip(): |
| 92 | +eclass_inheritances.add(inheritance.strip()) |
| 93 | +returnEclass(eclass_name,eclass_vars,eclass_inheritances) |
| 94 | +defformat_eclasses_as_haskell_map(eclasses): |
| 95 | +map_entries= [] |
| 96 | +join_string='", "' |
| 97 | +forvalueinsorted(eclasses,key=(lambdax:x.name)): |
| 98 | +ifvalue.vars: |
| 99 | +var_list_string=f'"{join_string.join(sorted(list(value.vars)))}"' |
| 100 | +map_entries.append( |
| 101 | +textwrap.fill( |
| 102 | +f'("{value.name}", [{var_list_string}])', |
| 103 | +80, |
| 104 | +initial_indent=' ', |
| 105 | +subsequent_indent=' ')) |
| 106 | +return_string=',\n\n'.join(map_entries) |
| 107 | +return_string=f""" Data.Map.fromList |
| 108 | + [ |
| 109 | +{return_string} |
| 110 | + ]""" |
| 111 | +returnf"""{VAR_FILE_HEADER}\n\n |
| 112 | +-- Last Generated:{datetime.datetime.now().strftime("%x")} |
| 113 | +import qualified Data.Map |
| 114 | +{PORTAGE_AUTO_VAR_NAME} = |
| 115 | +{return_string}""" |
| 116 | +defmain(argv): |
| 117 | +eclasses= {} |
| 118 | +forpathinsorted(argv,key=os.path.basename): |
| 119 | +ifnotpath.endswith('.eclass'): |
| 120 | +continue |
| 121 | +new_eclass=process_file(path) |
| 122 | +eclasses[new_eclass.name]=new_eclass |
| 123 | +eclasses_list= [ |
| 124 | +value.calculate_eclass_vars(eclasses)forkey,valueineclasses.items() |
| 125 | + ] |
| 126 | +print(format_eclasses_as_haskell_map(eclasses_list)) |
| 127 | +if__name__=='__main__': |
| 128 | +sys.exit(main(sys.argv[1:])) |