Mike Frysinger | a23e81f | 2019-07-01 11:55:50 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Common code for scripts in here.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import ast |
| 11 | import operator |
| 12 | |
| 13 | |
| 14 | # All the toolchains we care about. |
| 15 | TARGETS = ( |
| 16 | 'aarch64-cros-linux-gnu', |
| 17 | 'armv7a-cros-linux-gnueabihf', |
Mike Frysinger | a45a813 | 2021-12-23 16:04:06 -0500 | [diff] [blame] | 18 | 'i686-cros-linux-gnu', |
Mike Frysinger | a23e81f | 2019-07-01 11:55:50 -0400 | [diff] [blame] | 19 | 'x86_64-cros-linux-gnu', |
| 20 | ) |
| 21 | |
| 22 | |
Mike Frysinger | a535ecc | 2022-01-06 03:13:29 -0500 | [diff] [blame] | 23 | def math_eval(expr: str) -> str: |
Mike Frysinger | a23e81f | 2019-07-01 11:55:50 -0400 | [diff] [blame] | 24 | """Evaluate a arithmetic expression.""" |
| 25 | # Only bother listing operators that actually get used. |
| 26 | operators = {ast.Add: operator.add} |
| 27 | def _eval(node): |
| 28 | if isinstance(node, ast.Num): |
| 29 | return node.n |
| 30 | elif isinstance(node, ast.BinOp): |
| 31 | return operators[type(node.op)](_eval(node.left), _eval(node.right)) |
| 32 | else: |
| 33 | raise TypeError(node) |
| 34 | |
| 35 | return _eval(ast.parse(expr, mode='eval').body) |