| 1 |
|
| 2 |
|
| 3 |
import os, sys |
| 4 |
import chardet |
| 5 |
|
| 6 |
TOPDIR = os.path.abspath(os.path.join(os.path.dirname(__file__),"..")) |
| 7 |
sys.path.insert(0, TOPDIR) |
| 8 |
|
| 9 |
WXGEODIR = os.path.join(TOPDIR, "wxgeometrie") |
| 10 |
|
| 11 |
START = "# -*- coding: utf-8 -*-" |
| 12 |
|
| 13 |
EXCLURE = { |
| 14 |
'dirs': [ |
| 15 |
'sympy', 'sympy_OLD', 'developpeurs', 'doc', 'exemples', |
| 16 |
'images', 'log', 'preferences', 'session', 'macros', 'OLD', |
| 17 |
'tests', |
| 18 |
], |
| 19 |
'files': [ |
| 20 |
'test.py', '__init__.py', 'decorator.py', |
| 21 |
] |
| 22 |
} |
| 23 |
|
| 24 |
CODECS = ('utf-8', 'utf8', 'ascii') |
| 25 |
|
| 26 |
def verifier(fichier): |
| 27 |
""" |
| 28 |
Vérification de l'encodage d'un fichier et sa déclaration explicite |
| 29 |
@param fichier un chemin |
| 30 |
@return un doublon : nom du fichier et message, ou None si tout va bien |
| 31 |
""" |
| 32 |
i) |
| 33 |
encoding = None |
| 34 |
with open(fichier, 'rb') as f: |
| 35 |
encoding = chardet.detect(f.read()).get("encoding") |
| 36 |
if encoding not in CODECS: |
| 37 |
return fichier, f"Le codage « {encoding} » ne figure pas dans {CODECS}" |
| 38 |
f-8 |
| 39 |
with open(fichier, 'r') as f: |
| 40 |
lines = f.readlines() |
| 41 |
if not lines[0].startswith(START) and not lines[0].startswith(START): |
| 42 |
return fichier, "Il manque : " + START |
| 43 |
return None |
| 44 |
|
| 45 |
def skip(path): |
| 46 |
for directory in EXCLURE['dirs']: |
| 47 |
if ('/%s/' % directory) in path or path.endswith('/' + directory): |
| 48 |
return True |
| 49 |
return False |
| 50 |
|
| •51 |
import tools.unittests, unittest |
| 52 |
|
| •53 |
class Test(tools.unittests.TestCase): |
| 54 |
|
| 55 |
def test_entetes(self): |
| 56 |
"On teste que les fichiers soient bien tous declarés en utf-8." |
| 57 |
bad_files = {} |
| 58 |
for root, dirs, files in os.walk(WXGEODIR): |
| 59 |
if not skip(root): |
| 60 |
for filename in files: |
| 61 |
if filename.endswith('.py') and \ |
| 62 |
filename not in EXCLURE['files']: |
| 63 |
verif = verifier(os.path.join(root, filename)) |
| 64 |
if verif is not None: |
| 65 |
bad_files[verif[0]] = verif[1] |
| 66 |
if bad_files: |
| 67 |
print('\n\nEn-tête ou encodage incorrect : ') |
| 68 |
for fichier in bad_files: |
| 69 |
print(f'* {fichier} : {bad_files[fichier]}') |
| 70 |
self.assertFalse(bad_files) |