blob: a1cdb804baffe2517cade86dd848dcb4dc31ce6b [file] [log] [blame]
Mike Frysingera23e81f2019-07-01 11:55:50 -04001# -*- 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
8from __future__ import print_function
9
10import ast
11import operator
12
13
14# All the toolchains we care about.
15TARGETS = (
16 'aarch64-cros-linux-gnu',
17 'armv7a-cros-linux-gnueabihf',
Mike Frysingera45a8132021-12-23 16:04:06 -050018 'i686-cros-linux-gnu',
Mike Frysingera23e81f2019-07-01 11:55:50 -040019 'x86_64-cros-linux-gnu',
20)
21
22
Mike Frysingera535ecc2022-01-06 03:13:29 -050023def math_eval(expr: str) -> str:
Mike Frysingera23e81f2019-07-01 11:55:50 -040024 """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)