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}
diff --git a/tools_webrtc/libs/generate_licenses.py b/tools_webrtc/libs/generate_licenses.py
index a23123e..cbb1514 100755
--- a/tools_webrtc/libs/generate_licenses.py
+++ b/tools_webrtc/libs/generate_licenses.py
@@ -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 @@
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 @@
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 @@
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 @@
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')