From: David Wells Date: Sat, 11 May 2019 00:28:30 +0000 (-0400) Subject: Fix compatibility with python2. X-Git-Tag: v9.1.0-rc1~49^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0edb3852dcfb4ad26f960576b63896b7f0eeed3d;p=dealii.git Fix compatibility with python2. The advanced globbing feature with '**'s is not available in python2. We should use os.walk instead. While we are here: tidy up the scripts a little bit with some suggestions from pylint. --- diff --git a/contrib/utilities/check_encoding.py b/contrib/utilities/check_encoding.py index bad19a3cf5..7a18696303 100755 --- a/contrib/utilities/check_encoding.py +++ b/contrib/utilities/check_encoding.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ## --------------------------------------------------------------------- ## ## Copyright (C) 2019 by the deal.II authors @@ -20,17 +20,37 @@ # This script should be invoked from the root folder of the deal.ii # repository: # contrib/utilities/check_encoding.py +from __future__ import print_function -import glob import itertools +import io +import os +import sys -filenames = itertools.chain(glob.iglob('**/*.h', recursive=True), - glob.iglob('**/*.cc', recursive=True), - glob.iglob('**/*.html', recursive=True)) +def filename_generator(suffix): + for root, _, file_names in os.walk("./"): + for file_name in file_names: + if file_name.endswith(suffix): + if root == "./": + yield root + file_name + else: + yield root + "/" + file_name + + +filenames = itertools.chain(filename_generator(".h"), + filename_generator(".cc"), + filename_generator(".html")) + +return_code = 0 for filename in filenames: + file_handle = io.open(filename, encoding='utf-8') try: - with open(filename, encoding='utf-8') as file: - file.read() - except: - raise Exception(filename + ' is not encoded is not encoded with UTF-8') + file_handle.read() + except UnicodeDecodeError: + print(filename + ' is not encoded with UTF-8') + return_code = 1 + finally: + file_handle.close() + +sys.exit(return_code) diff --git a/doc/doxygen/scripts/set_canonical_doxygen.py b/doc/doxygen/scripts/set_canonical_doxygen.py index 4d411b0a79..4d6d8b3bda 100755 --- a/doc/doxygen/scripts/set_canonical_doxygen.py +++ b/doc/doxygen/scripts/set_canonical_doxygen.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ## --------------------------------------------------------------------- ## ## Copyright (C) 2019 by the deal.II authors @@ -22,26 +22,37 @@ # the deal.ii repository you can use the script # contrib/utilities/set_canonical_webpages.py -import glob +import os -filenames = glob.iglob('**/*html', recursive=True) +LINK_START = ('' in line): - new_text_data += ('' + '\n') - canonical_link_added = True - file.close() +def filename_generator(): + for root, _, file_names in os.walk("./"): + for file_name in file_names: + if file_name.endswith(".html"): + if root == "./": + yield root + file_name + else: + yield root + "/" + file_name +for filename in filename_generator(): + # there is no relevant content in the header + if filename == "./header.html": + pass + new_text_data = str() + with open(filename, 'r') as file_handle: + if '' in line: + assert filename[:2] == "./" + new_text_data += LINK_START + filename[2:] + '" />\n' + canonical_link_added = True + if new_text_data: # Truncate the file and write the new text - file = open(filename, 'w+') - file.write(new_text_data) - file.close() + with open(filename, 'w+') as file_handle: + file_handle.write(new_text_data)