Commit 3bc9933f authored by Raul Tambre's avatar Raul Tambre Committed by Commit Bot

tools/licenses.py: Python 3 compatible

* cgi.escape() has been removed in Python 3.8. Use html.escape() in Python 3. Note that this causes quotes to be escaped in Python 3. This doesn't seem to be an issue.
* Read the licence in text mode, so we can use it with html.escape(), which excepts strings not bytes.

Still works with Python 2.

Fixed backtraces:
Traceback (most recent call last):
  File "../../tools/licenses.py", line 810, in <module>
    sys.exit(main())
  File "../../tools/licenses.py", line 792, in main
    if not GenerateCredits(args.file_template, args.entry_template,
  File "../../tools/licenses.py", line 662, in GenerateCredits
    entries.append(MetadataToTemplateEntry(chromium_license_metadata,
  File "../../tools/licenses.py", line 632, in MetadataToTemplateEntry
    'content': EvaluateTemplate(entry_template, env),
  File "../../tools/licenses.py", line 620, in EvaluateTemplate
    val = cgi.escape(val)
AttributeError: module 'cgi' has no attribute 'escape'

Traceback (most recent call last):
  File "../../tools/licenses.py", line 809, in <module>
    sys.exit(main())
  File "../../tools/licenses.py", line 791, in main
    if not GenerateCredits(args.file_template, args.entry_template,
  File "../../tools/licenses.py", line 661, in GenerateCredits
    entries.append(MetadataToTemplateEntry(chromium_license_metadata,
  File "../../tools/licenses.py", line 631, in MetadataToTemplateEntry
    'content': EvaluateTemplate(entry_template, env),
  File "../../tools/licenses.py", line 619, in EvaluateTemplate
    val = html.escape(val)
  File "C:\Program Files\Python38\lib\html\__init__.py", line 19, in escape
    s = s.replace("&", "&amp;") # Must be done first!
TypeError: a bytes-like object is required, not 'str'

Bug: 941669
Change-Id: If203c49a425db733825df1867e65bef7a20060fa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1857321
Auto-Submit: Raul Tambre <raul@tambre.ee>
Reviewed-by: default avatarDirk Pranke <dpranke@chromium.org>
Commit-Queue: Raul Tambre <raul@tambre.ee>
Cr-Commit-Position: refs/heads/master@{#705846}
parent f405533c
......@@ -18,7 +18,6 @@ from __future__ import print_function
import argparse
import codecs
import cgi
import json
import os
import shutil
......@@ -27,6 +26,11 @@ import subprocess
import sys
import tempfile
if sys.version_info.major == 2:
import cgi as html
else:
import html
# TODO(agrieve): Move build_utils.WriteDepFile into a non-android directory.
_REPOSITORY_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
sys.path.insert(0, os.path.join(_REPOSITORY_ROOT, 'build/android/gyp'))
......@@ -612,7 +616,7 @@ def GenerateCredits(
dictionary of expansions."""
for key, val in env.items():
if escape:
val = cgi.escape(val)
val = html.escape(val)
template = template.replace('{{%s}}' % key, val)
return template
......@@ -620,7 +624,7 @@ def GenerateCredits(
env = {
'name': metadata['Name'],
'url': metadata['URL'],
'license': open(metadata['License File'], 'rb').read(),
'license': open(metadata['License File']).read(),
}
return {
'name': metadata['Name'],
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment