Skip to content

Commit

Permalink
chrome: run tools/media_engagement_preload/make_dafsa.py in python3
Browse files Browse the repository at this point in the history
Fixed: 1210295
Change-Id: I531f99ba4209086c25157854321e3addf1ce632e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2903407
Auto-Submit: Takuto Ikuta <tikuta@chromium.org>
Reviewed-by: Dirk Pranke <dpranke@google.com>
Reviewed-by: Sergey Ulanov <sergeyu@chromium.org>
Commit-Queue: Sergey Ulanov <sergeyu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#884796}
  • Loading branch information
Takuto Ikuta authored and Chromium LUCI CQ committed May 20, 2021
1 parent eb4e023 commit 07e0538
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .vpython3
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ python_version: "3.8"
# components/policy/test_support/policy_testserver.py
wheel: <
name: "infra/python/wheels/protobuf-py2_py3"
version: "version:3.6.1"
version: "version:3.15.8"
>

# TODO(https://crbug.com/898348): Add in necessary wheels as Python3 versions
Expand Down
14 changes: 2 additions & 12 deletions chrome/browser/media/media_engagement_autoplay_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,8 @@
namespace {

base::FilePath GetPythonPath() {
#if defined(OS_WIN)
// Windows bots do not have python installed and available on the PATH.
// Please see infra/doc/users/python.md
base::FilePath bot_path =
base::FilePath(FILE_PATH_LITERAL("c:/infra-system/bin/python.exe"));

if (base::PathExists(bot_path))
return bot_path;
return base::FilePath(FILE_PATH_LITERAL("python.exe"));
#else
return base::FilePath(FILE_PATH_LITERAL("python"));
#endif
// Every environment should have vpython3.
return base::FilePath(FILE_PATH_LITERAL("vpython3"));
}

const base::FilePath kTestDataPath = base::FilePath(
Expand Down
4 changes: 1 addition & 3 deletions chrome/test/data/media/engagement/preload/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
# found in the LICENSE file.

import("//build/compiled_action.gni")
import("//build/config/python.gni")

# Generates a proto file based on the real list.
# TODO(crbug.com/1112471): Get this to run cleanly under Python 3.
python2_action_foreach("generate_preload_list") {
action_foreach("generate_preload_list") {
script = "//tools/media_engagement_preload/make_dafsa.py"

sources = [ "test.json" ]
Expand Down
22 changes: 12 additions & 10 deletions tools/media_engagement_preload/PRESUBMIT.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@

"""Chromium presubmit script for src/tools/media_engagement_preload."""

# This line is 'magic' in that git-cl looks for it to decide whether to
# use Python3 instead of Python2 when running the code in this file.
USE_PYTHON3 = True


def _RunMakeDafsaTests(input_api, output_api):
"""Runs unittest for make_dafsa if any related file has been modified."""
files = ('tools/media_engagement_preload/make_dafsa.py',
'tools/media_engagement_preload/make_dafsa_unittest.py')
if not any(f in input_api.LocalPaths() for f in files):
return []
test_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
'make_dafsa_unittest.py')
cmd_name = 'make_dafsa_unittest'
cmd = [input_api.python_executable, test_path]
test_cmd = input_api.Command(
name=cmd_name,
cmd=cmd,
kwargs={},
message=output_api.PresubmitPromptWarning)
return input_api.RunTests([test_cmd])

return input_api.RunTests(
input_api.canned_checks.RunUnitTestsInDirectory(
input_api,
output_api,
input_api.PresubmitLocalPath(),
files_to_check=['.*test\.py$'],
run_on_python2=False))


def CheckChangeOnUpload(input_api, output_api):
Expand Down
10 changes: 5 additions & 5 deletions tools/media_engagement_preload/make_dafsa.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
Expand All @@ -9,7 +9,7 @@
import json
import sys
import os
import urlparse
import urllib.parse

SOURCE_ROOT = os.path.join(os.path.dirname(
os.path.abspath(__file__)), os.pardir, os.pardir)
Expand Down Expand Up @@ -440,7 +440,7 @@ def encode(dafsa):
def to_proto(data):
"""Generates protobuf from a list of encoded bytes."""
message = media_engagement_preload_pb2.PreloadedData()
message.dafsa = array.array('B', data).tostring()
message.dafsa = array.array('B', data).tobytes()
return message.SerializeToString()


Expand All @@ -458,7 +458,7 @@ def parse_json(infile):
netlocs = {}
for entry in json.loads(infile):
# Parse the origin and reject any with an invalid protocol.
parsed = urlparse.urlparse(entry)
parsed = urllib.parse.urlparse(entry)
if parsed.scheme != 'http' and parsed.scheme != 'https':
raise InputError('Invalid protocol: %s' % entry)

Expand All @@ -471,7 +471,7 @@ def parse_json(infile):

# Join the numerical values to the netlocs.
output = []
for location, value in netlocs.iteritems():
for location, value in netlocs.items():
output.append(location + str(value))
return output
except ValueError:
Expand Down
7 changes: 4 additions & 3 deletions tools/media_engagement_preload/make_dafsa_unittest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.


import sys
import unittest

import make_dafsa


Expand Down Expand Up @@ -678,14 +679,14 @@ class ExamplesTest(unittest.TestCase):
def testExample1(self):
"""Tests Example 1 from make_dafsa.py."""
infile = '["https://www.example.com:8081", "http://www.example.org"]'
outfile = "\n\x1c\x81www.example\xae\x02\x89com:8081\x80org\x81"
outfile = b'\n\x1c\x81www.example\xae\x02\x84org\x81com:8081\x80'
self.assertEqual(make_dafsa.words_to_proto(make_dafsa.parse_json(infile)),
outfile)

def testExample2(self):
"""Tests Example 2 from make_dafsa.py."""
infile = '["https://www.example.org", "http://www.google.com"]'
outfile = "\n\x1e\x81www\xae\x02\x8bgoogle.com\x81example.org\x80"
outfile = b'\n\x1e\x81www\xae\x02\x8bgoogle.com\x81example.org\x80'
self.assertEqual(make_dafsa.words_to_proto(make_dafsa.parse_json(infile)),
outfile)

Expand Down
38 changes: 23 additions & 15 deletions tools/media_engagement_preload/media_engagement_preload_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 07e0538

Please sign in to comment.