summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Rosenthal <jrosenth@chromium.org>2020-02-21 10:21:10 -0700
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-08-26 19:16:12 +0000
commit32c80cad1ed85114adc2981e7a169cc747ab6b88 (patch)
tree0b96a404ba3dd7131c160c391227a247b32fb559
parenta8e2aabdddbc014d24b70821a45ed739d89afd25 (diff)
downloadchrome-ec-32c80cad1ed85114adc2981e7a169cc747ab6b88.tar.gz
util: convert config_option_check.py to Python 3
The main fixes that were needed were: * Use utf-8 encoding from git subprocess. * Don't use 'is' to compare string equality (Python does not guarantee interning of strings). For the parser state, the string comparison was removed by changing this to an Enum, since we have Python 3 anyway. BUG=chromium:1031705 TEST=create commit with invalid config options and run script, notice the script points them out and exits 1, then test without that commit, no output and exit 0. Change-Id: I1113e063f747f0207c4b59a74f4e4a06bbcd5fcd Signed-off-by: Jack Rosenthal <jrosenth@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2068520 Reviewed-by: Aseda Aboagye <aaboagye@chromium.org> (cherry picked from commit a9666e310606cbd1a54d32fa0cb200f16cf1d451) Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3858397 Tested-by: Mary Ruthven <mruthven@chromium.org> Auto-Submit: Mary Ruthven <mruthven@chromium.org>
-rwxr-xr-xutil/config_option_check.py46
1 files changed, 28 insertions, 18 deletions
diff --git a/util/config_option_check.py b/util/config_option_check.py
index eb068eef30..57bc4cea93 100755
--- a/util/config_option_check.py
+++ b/util/config_option_check.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
# Copyright 2015 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
@@ -8,9 +8,11 @@ Script to ensure that all configuration options for the Chrome EC are defined
in config.h.
"""
from __future__ import print_function
+import enum
import os
import re
import subprocess
+import sys
class Line(object):
@@ -171,7 +173,7 @@ def print_missing_config_options(hunks, config_options):
# not. For deletions, we will need to verify if this CONFIG_* option
# is no longer being used in the entire repo.
- if l.line_type is '-':
+ if l.line_type == '-':
if option not in options_in_use and option in config_options:
deprecated_options.add(option)
else:
@@ -276,51 +278,59 @@ def get_hunks():
line_re = re.compile(r'^([+| |-])(.*)')
# Get the diff output.
- cmd = 'git diff --cached -GCONFIG_* --no-prefix --no-ext-diff HEAD~1'
- diff = subprocess.check_output(cmd.split()).split('\n')
+ proc = subprocess.run(['git', 'diff', '--cached', '-GCONFIG_*', '--no-prefix',
+ '--no-ext-diff', 'HEAD~1'],
+ stdout=subprocess.PIPE,
+ encoding='utf-8',
+ check=True)
+ diff = proc.stdout.splitlines()
+ if not diff:
+ return []
line = diff[0]
- current_state = 'new_file'
+
+ state = enum.Enum('state', 'NEW_FILE FILENAME_SEARCH HUNK LINES')
+ current_state = state.NEW_FILE
while True:
# Search for the beginning of a new file.
- if current_state is 'new_file':
+ if current_state is state.NEW_FILE:
match = new_file_re.search(line)
if match:
- current_state = 'filename_search'
+ current_state = state.FILENAME_SEARCH
# Search the diff output for a file name.
- elif current_state is 'filename_search':
+ elif current_state is state.FILENAME_SEARCH:
# Search for a file name.
match = filename_re.search(line)
if match:
filename = match.groups(1)[0]
if filename in ALLOWLIST:
# Skip the file if it's allowlisted.
- current_state = 'new_file'
+ current_state = state.NEW_FILE
else:
- current_state = 'hunk'
+ current_state = state.HUNK
# Search for a hunk. Each hunk starts with a line describing the line
# numbers in the file.
- elif current_state is 'hunk':
+ elif current_state is state.HUNK:
hunk_lines = []
match = hunk_line_num_re.search(line)
if match:
# Extract the line number offset.
line_num = int(match.groups(1)[0])
- current_state = 'lines'
+ current_state = state.LINES
# Start looking for changes.
- elif current_state is 'lines':
+ elif current_state is state.LINES:
# Check if state needs updating.
new_hunk = hunk_line_num_re.search(line)
new_file = new_file_re.search(line)
if new_hunk:
- current_state = 'hunk'
+ current_state = state.HUNK
hunks.append(Hunk(filename, hunk_lines))
continue
elif new_file:
- current_state = 'new_file'
+ current_state = state.NEW_FILE
hunks.append(Hunk(filename, hunk_lines))
continue
@@ -328,10 +338,10 @@ def get_hunks():
if match:
line_type = match.groups(1)[0]
# We only care about modifications.
- if line_type is not ' ':
+ if line_type != ' ':
hunk_lines.append(Line(line_num, match.groups(2)[1], line_type))
# Deletions don't count towards the line numbers.
- if line_type is not '-':
+ if line_type != '-':
line_num += 1
# Advance to the next line
@@ -360,7 +370,7 @@ def main():
if missing_opts:
print('\nIt may also be possible that you have a typo.')
- os.sys.exit(1)
+ sys.exit(1)
if __name__ == '__main__':
main()