|
| 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__ import print_function |
| 35 | +import datetime |
| 36 | +import os |
| 37 | +import re |
| 38 | +import sys |
| 39 | +import textwrap |
| 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 | +class Eclass: |
| 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 | + def calculate_eclass_vars(self, eclasses): |
| 57 | + while self.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 | + except Exception: |
| 64 | + pass |
| 65 | + return self |
| 66 | +def print_var_list(eclass, eclass_vars): |
| 67 | + var_list = ' '.join(['"%s",' % v for v in sorted(eclass_vars)]) |
| 68 | + print(' -- %s\n%s' % |
| 69 | + (eclass, |
| 70 | + textwrap.fill( |
| 71 | + var_list, 80, initial_indent=' ', subsequent_indent=' '))) |
| 72 | +def process_file(eclass_path): |
| 73 | + eclass_name = os.path.splitext(os.path.basename(eclass_path))[0] |
| 74 | + with open(eclass_path, 'r') as f: |
| 75 | + eclass_vars = set() |
| 76 | + eclass_inheritances = set() |
| 77 | + for line in f: |
| 78 | + line = line.strip() |
| 79 | + if not line: |
| 80 | + continue |
| 81 | + while line[-1] == '\\': |
| 82 | + line = line[:-1] + next(f).strip() |
| 83 | + match = VAR_RE.search(line) |
| 84 | + if match: |
| 85 | + var_name = match.group(1) |
| 86 | + eclass_vars.add(var_name.strip()) |
| 87 | + else: |
| 88 | + match = INHERIT_RE.search(line) |
| 89 | + if match: |
| 90 | + for inheritance in re.split(r'\s+', match.group(1)): |
| 91 | + if inheritance.strip(): |
| 92 | + eclass_inheritances.add(inheritance.strip()) |
| 93 | + return Eclass(eclass_name, eclass_vars, eclass_inheritances) |
| 94 | +def format_eclasses_as_haskell_map(eclasses): |
| 95 | + map_entries = [] |
| 96 | + join_string = '", "' |
| 97 | + for value in sorted(eclasses, key=(lambda x: x.name)): |
| 98 | + if value.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 | + return f"""{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 | +def main(argv): |
| 117 | + eclasses = {} |
| 118 | + for path in sorted(argv, key=os.path.basename): |
| 119 | + if not path.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) for key, value in eclasses.items() |
| 125 | + ] |
| 126 | + print(format_eclasses_as_haskell_map(eclasses_list)) |
| 127 | +if __name__ == '__main__': |
| 128 | + sys.exit(main(sys.argv[1:])) |
0 commit comments