]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Fix compatibility with python2. 8086/head
authorDavid Wells <drwells@email.unc.edu>
Sat, 11 May 2019 00:28:30 +0000 (20:28 -0400)
committerDavid Wells <drwells@email.unc.edu>
Sat, 11 May 2019 14:34:27 +0000 (10:34 -0400)
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.

contrib/utilities/check_encoding.py
doc/doxygen/scripts/set_canonical_doxygen.py

index bad19a3cf5065b3c46aabb9c01c815c7fa802ceb..7a186963032763406554dafe12de67bdd15837d2 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python3
+#!/usr/bin/env python
 ## ---------------------------------------------------------------------
 ##
 ## Copyright (C) 2019 by the deal.II authors
 # 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)
index 4d411b0a79655627d6cc5e5f050a3af8f3cf376a..4d6d8b3bdae6c09f7499c6ebf49f39f58b325f9f 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python3
+#!/usr/bin/env python
 ## ---------------------------------------------------------------------
 ##
 ## Copyright (C) 2019 by the deal.II authors
 # 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 = ('<link rel="canonical" href="https://www.dealii.org/current'
+              + '/doxygen/')
 
-for filename in filenames:
-    file = open(filename, 'r')
-    new_text_data = str()
-    if '<link rel="canonical"' not in file.read():
-        file.seek(0)
-        for line in file.readlines():
-            # Do not add the canonical link twice
-            canonical_link_added = False
-            new_text_data += line
-            if (not canonical_link_added) and ('<head>' in line):
-                new_text_data += ('<link rel="canonical" href="https://www.dealii.org/current/doxygen/deal.II/'
-                                  + filename + '" />' + '\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 '<link rel="canonical"' not in file_handle.read():
+            file_handle.seek(0)
+            for line in file_handle.readlines():
+                # Do not add the canonical link twice
+                canonical_link_added = False
+                new_text_data += line
+                if not canonical_link_added and '<head>' 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)

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.