Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ba188d7

Browse files
Update infrastructure python code to be compatible with python 2 and python 3 (#39133)
* Compatibility with python2 and python3 * Formatting * Updated to use a function instead of decoding bytes in multiple places. * Formatting and whitespace cleanup. * Fix import statement. * Linter change for docstring. * Formatting. * Remove function from run_tests.py since it is a pain to import. * Add todo message for python 2 deprecation. * Updated copyright year.
1 parent 36cb5c8 commit ba188d7

4 files changed

Lines changed: 47 additions & 6 deletions

File tree

ci/compatibility_helper.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright 2013 The Flutter Authors. All rights reserved.
4+
# Use of this source code is governed by a BSD-style license that can be
5+
# found in the LICENSE file.
6+
#
7+
# This script contains helper function(s) for supporting both
8+
# python 2 and python 3 infrastructure code.
9+
10+
ENCODING = 'UTF-8'
11+
12+
13+
def byte_str_decode(str_or_bytes):
14+
"""Returns a string if given either a string or bytes.
15+
16+
TODO: This function should be removed when the supported python
17+
version is only python 3.
18+
19+
Args:
20+
str_or_bytes (string or bytes) we want to convert or return as
21+
the possible value changes depending on the version of python
22+
used.
23+
"""
24+
return str_or_bytes if isinstance(str_or_bytes,
25+
str) else str_or_bytes.decode(ENCODING)

ci/firebase_testlab.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os
1111
import subprocess
1212
import sys
13+
from compatibility_helper import byte_str_decode
1314

1415
if 'STORAGE_BUCKET' not in os.environ:
1516
print('The GCP storage bucket must be provided as an environment variable.')
@@ -67,6 +68,7 @@ def check_logcat(results_dir):
6768
'gsutil', 'cat',
6869
'%s/%s/*/logcat' % (BUCKET, results_dir)
6970
])
71+
logcat = byte_str_decode(logcat)
7072
if not logcat:
7173
sys.exit(1)
7274

@@ -82,7 +84,9 @@ def check_timeline(results_dir):
8284
'gsutil', 'du',
8385
'%s/%s/*/game_loop_results/results_scenario_0.json' %
8486
(BUCKET, results_dir)
85-
]).strip()
87+
])
88+
gsutil_du = byte_str_decode(gsutil_du)
89+
gsutil_du = gsutil_du.strip()
8690
if gsutil_du == '0':
8791
print('Failed to produce a timeline.')
8892
sys.exit(1)
@@ -113,8 +117,9 @@ def main():
113117
return 1
114118

115119
git_revision = subprocess.check_output(['git', 'rev-parse', 'HEAD'],
116-
cwd=script_dir).strip()
117-
120+
cwd=script_dir)
121+
git_revision = byte_str_decode(git_revision)
122+
git_revision = git_revision.strip()
118123
results = []
119124
apk = None
120125
for apk in apks:

ci/scan_flattened_deps.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import subprocess
1818
import sys
1919
from urllib import request
20+
from compatibility_helper import byte_str_decode
2021

2122
SCRIPT_DIR = os.path.dirname(sys.argv[0])
2223
CHECKOUT_ROOT = os.path.realpath(os.path.join(SCRIPT_DIR, '..'))
@@ -206,7 +207,9 @@ def get_common_ancestor_commit(dep, deps_list):
206207
'git --git-dir ' + temp_dep_dir + '/.git remote show upstream ' +
207208
"| sed -n \'/HEAD branch/s/.*: //p\'",
208209
shell=True
209-
).decode().strip()
210+
)
211+
default_branch = byte_str_decode(default_branch)
212+
default_branch = default_branch.strip()
210213
print(
211214
'default_branch found: {default_branch}'.format(
212215
default_branch=default_branch
@@ -223,7 +226,8 @@ def get_common_ancestor_commit(dep, deps_list):
223226
"--format=\'%(objectname:short)\' refs/heads/upstream",
224227
shell=True
225228
)
226-
commit = commit.decode().strip()
229+
commit = byte_str_decode(commit)
230+
commit = commit.strip()
227231

228232
# perform merge-base on most recent default branch commit and pinned mirror commit
229233
ancestor_commit = subprocess.check_output(
@@ -232,7 +236,8 @@ def get_common_ancestor_commit(dep, deps_list):
232236
),
233237
shell=True
234238
)
235-
ancestor_commit = ancestor_commit.decode().strip()
239+
ancestor_commit = byte_str_decode(ancestor_commit)
240+
ancestor_commit = ancestor_commit.strip()
236241
print('Ancestor commit: ' + ancestor_commit)
237242
return ancestor_commit
238243
except subprocess.CalledProcessError as error:

testing/run_tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
FONT_SUBSET_DIR = os.path.join(BUILDROOT_DIR, 'flutter', 'tools', 'font-subset')
3434

3535
FML_UNITTESTS_FILTER = '--gtest_filter=-*TimeSensitiveTest*'
36+
ENCODING = 'UTF-8'
3637

3738

3839
def print_divider(char='='):
@@ -598,6 +599,11 @@ def ensure_ios_tests_are_built(ios_out_dir):
598599
def assert_expected_xcode_version():
599600
"""Checks that the user has a version of Xcode installed"""
600601
version_output = subprocess.check_output(['xcodebuild', '-version'])
602+
# TODO ricardoamador: remove this check when python 2 is deprecated.
603+
version_output = version_output if isinstance(
604+
version_output, str
605+
) else version_output.decode(ENCODING)
606+
version_output = version_output.strip()
601607
match = re.match(r'Xcode (\d+)', version_output)
602608
message = 'Xcode must be installed to run the iOS embedding unit tests'
603609
assert match, message

0 commit comments

Comments
 (0)