Make generate_license.py compatible with Python 3.

* Use cgi.escape for Python 2.7 and html.escape for Python 3.
* Modify unittest to succeed in both Python 2.7 and 3.

No-Presubmit: True
Bug: None
Change-Id: Ie711873468145c9abbd12313086ebe7358e20ab7
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/220621
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#34156}
This commit is contained in:
Byoungchan Lee 2021-05-29 13:50:31 +09:00 committed by WebRTC LUCI CQ
parent d3166afe01
commit 5be2aa1ac3
2 changed files with 31 additions and 22 deletions

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# Copyright 2016 The WebRTC project authors. All Rights Reserved.
#
@ -23,12 +23,16 @@ Libraries are mapped to licenses via LIB_TO_LICENSES_DICT dictionary.
import sys
import argparse
import cgi
import json
import logging
import os
import re
import subprocess
try:
# python 3.2+
from html import escape
except ImportError:
from cgi import escape
# Third_party library to licences mapping. Keys are names of the libraries
# (right after the `third_party/` prefix)
@ -182,7 +186,7 @@ class LicenseBuilder(object):
target,
]
logging.debug('Running: %r', cmd)
output_json = subprocess.check_output(cmd, cwd=WEBRTC_ROOT)
output_json = subprocess.check_output(cmd, cwd=WEBRTC_ROOT).decode('UTF-8')
logging.debug('Output: %s', output_json)
return output_json
@ -209,7 +213,7 @@ class LicenseBuilder(object):
self.common_licenses_dict.keys())
if missing_licenses:
error_msg = 'Missing licenses for following third_party targets: %s' % \
', '.join(missing_licenses)
', '.join(sorted(missing_licenses))
logging.error(error_msg)
raise Exception(error_msg)
@ -234,7 +238,7 @@ class LicenseBuilder(object):
for path in self.common_licenses_dict[license_lib]:
license_path = os.path.join(WEBRTC_ROOT, path)
with open(license_path, 'r') as license_file:
license_text = cgi.escape(license_file.read(), quote=True)
license_text = escape(license_file.read(), quote=True)
output_license_file.write(license_text)
output_license_file.write('\n')
output_license_file.write('```\n\n')

View file

@ -10,7 +10,12 @@
# be found in the AUTHORS file in the root of the source tree.
import unittest
import mock
try:
# python 3.3+
from unittest.mock import patch
except ImportError:
# From site-package
from mock import patch
from generate_licenses import LicenseBuilder
@ -32,21 +37,21 @@ class TestLicenseBuilder(unittest.TestCase):
"""
def testParseLibraryName(self):
self.assertEquals(
self.assertEqual(
LicenseBuilder._ParseLibraryName('//a/b/third_party/libname1:c'),
'libname1')
self.assertEquals(
self.assertEqual(
LicenseBuilder._ParseLibraryName(
'//a/b/third_party/libname2:c(d)'), 'libname2')
self.assertEquals(
self.assertEqual(
LicenseBuilder._ParseLibraryName(
'//a/b/third_party/libname3/c:d(e)'), 'libname3')
self.assertEquals(
self.assertEqual(
LicenseBuilder._ParseLibraryName('//a/b/not_third_party/c'), None)
def testParseLibrarySimpleMatch(self):
builder = LicenseBuilder([], [], {}, {})
self.assertEquals(builder._ParseLibrary('//a/b/third_party/libname:c'),
self.assertEqual(builder._ParseLibrary('//a/b/third_party/libname:c'),
'libname')
def testParseLibraryRegExNoMatchFallbacksToDefaultLibname(self):
@ -54,7 +59,7 @@ class TestLicenseBuilder(unittest.TestCase):
'libname:foo.*': ['path/to/LICENSE'],
}
builder = LicenseBuilder([], [], lib_dict, {})
self.assertEquals(
self.assertEqual(
builder._ParseLibrary('//a/b/third_party/libname:bar_java'),
'libname')
@ -63,7 +68,7 @@ class TestLicenseBuilder(unittest.TestCase):
'libname:foo.*': ['path/to/LICENSE'],
}
builder = LicenseBuilder([], [], {}, lib_regex_dict)
self.assertEquals(
self.assertEqual(
builder._ParseLibrary('//a/b/third_party/libname:foo_bar_java'),
'libname:foo.*')
@ -72,7 +77,7 @@ class TestLicenseBuilder(unittest.TestCase):
'libname/foo:bar.*': ['path/to/LICENSE'],
}
builder = LicenseBuilder([], [], {}, lib_regex_dict)
self.assertEquals(
self.assertEqual(
builder._ParseLibrary('//a/b/third_party/libname/foo:bar_java'),
'libname/foo:bar.*')
@ -81,29 +86,29 @@ class TestLicenseBuilder(unittest.TestCase):
'libname/foo.*bar.*': ['path/to/LICENSE'],
}
builder = LicenseBuilder([], [], {}, lib_regex_dict)
self.assertEquals(
self.assertEqual(
builder._ParseLibrary(
'//a/b/third_party/libname/fooHAHA:bar_java'),
'libname/foo.*bar.*')
@mock.patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
@patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
def testGetThirdPartyLibrariesWithoutRegex(self):
builder = LicenseBuilder([], [], {}, {})
self.assertEquals(
self.assertEqual(
builder._GetThirdPartyLibraries('out/arm', 'target1'),
set(['libname1', 'libname2', 'libname3']))
@mock.patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
@patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
def testGetThirdPartyLibrariesWithRegex(self):
lib_regex_dict = {
'libname2:c.*': ['path/to/LICENSE'],
}
builder = LicenseBuilder([], [], {}, lib_regex_dict)
self.assertEquals(
self.assertEqual(
builder._GetThirdPartyLibraries('out/arm', 'target1'),
set(['libname1', 'libname2:c.*', 'libname3']))
@mock.patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
@patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
def testGenerateLicenseTextFailIfUnknownLibrary(self):
lib_dict = {
'simple_library': ['path/to/LICENSE'],
@ -113,8 +118,8 @@ class TestLicenseBuilder(unittest.TestCase):
with self.assertRaises(Exception) as context:
builder.GenerateLicenseText('dummy/dir')
self.assertEquals(
context.exception.message,
self.assertEqual(
context.exception.args[0],
'Missing licenses for following third_party targets: '
'libname1, libname2, libname3')