diff --git a/Code/GraphMol/Descriptors/test_data/pmi.py b/Code/GraphMol/Descriptors/test_data/pmi.py index 76dbb4562..a00794e74 100644 --- a/Code/GraphMol/Descriptors/test_data/pmi.py +++ b/Code/GraphMol/Descriptors/test_data/pmi.py @@ -51,5 +51,5 @@ if __name__ == '__main__': for m in suppl: i1, i2, i3 = GetMoments(m, True) mi1, mi2, mi3 = GetMoments(m, False) - print("%s %.4f %.4f %.4f %.4f %.4f %.4f" % - (m.GetProp("_Name"), i1, i2, i3, mi1, mi2, mi3), file=output) + print("%s %.4f %.4f %.4f %.4f %.4f %.4f" % (m.GetProp("_Name"), i1, i2, i3, mi1, mi2, mi3), + file=output) diff --git a/Code/GraphMol/MolDraw2D/test_dir/test_rdkit_draw.py b/Code/GraphMol/MolDraw2D/test_dir/test_rdkit_draw.py index 4ab4de042..57892daf1 100755 --- a/Code/GraphMol/MolDraw2D/test_dir/test_rdkit_draw.py +++ b/Code/GraphMol/MolDraw2D/test_dir/test_rdkit_draw.py @@ -1,5 +1,5 @@ #!/usr/bin/env python - +from __future__ import print_function from rdkit import RDConfig import os, sys from rdkit import Chem @@ -12,11 +12,11 @@ if sys.argv[1].endswith('.sdf'): elif sys.argv[1].endswith('.smi'): suppl = Chem.SmilesMolSupplier(sys.argv[1]) else: - print 'Need a file ending in .sdf or .smi' + print('Need a file ending in .sdf or .smi') exit(1) for mol in suppl: - print mol.GetProp('_Name') + print(mol.GetProp('_Name')) fn = mol.GetProp('_Name') + '.png' AllChem.Compute2DCoords(mol) Draw.MolToFile(mol, fn) diff --git a/Code/cmake/Modules/fixup_coverage.py b/Code/cmake/Modules/fixup_coverage.py index 5b2c89eb3..472c0c5c9 100644 --- a/Code/cmake/Modules/fixup_coverage.py +++ b/Code/cmake/Modules/fixup_coverage.py @@ -3,10 +3,10 @@ the coverage tool mistakenly finds in the build tree. It replaces the paths with the ones from the source tree n.b. if a file with the same name (i.e. sln.yy) is found twice in the source tree, this will break""" - +from __future__ import print_function import os, sys source_dir, info_file = sys.argv[1:3] -print source_dir, info_file +print(source_dir, info_file) paths = {} for root, dir, files in os.walk(source_dir): @@ -20,13 +20,13 @@ for line in lines: if "SF:" in line: fn = line.split("SF:")[-1].strip() if not os.path.exists(fn): - print "Does not exist:", fn.strip() + print("Does not exist:", fn.strip()) head, rest = os.path.split(fn) potential = paths[rest] if len(potential) == 1: line = "SF:" + potential[0] else: - asdf + raise NotImplementedError('asdf') newlines.append(line) open(info_file, 'w').write("\n".join(newlines)) diff --git a/Scripts/PythonFormat.py b/Scripts/PythonFormat.py new file mode 100644 index 000000000..2305256d6 --- /dev/null +++ b/Scripts/PythonFormat.py @@ -0,0 +1,69 @@ +''' + +Script will test the RDkit python code for conformance with the agreed format using +yapf. + +For each Python file that is found in $RDBASE (excluding the build and External +directories), yapf is used with the style configuration in $RDBASE/setup.cfg. +If a change is required, the difference is printed. At the end of the process, +all non-conformant files are listed and the required yapf command(s) printed. + +If changes are found, the script will exit with error code 1, otherwise 0. + +''' +from __future__ import print_function +import os +from yapf.yapflib.yapf_api import FormatCode +import sys + +rdbase = os.environ.get('RDBASE', '') +styleConfig = os.path.join(rdbase, 'setup.cfg') + +excludeDirs = [os.path.join(rdbase, 'build'), + os.path.join(rdbase, 'External'), ] + + +def pythonFiles(dirname=rdbase): + """ Find all python files below directory dirname """ + for root, _, files in os.walk(dirname): + if any(root.startswith(d) for d in excludeDirs): + continue + for file in files: + if file.endswith(".py"): + yield os.path.join(root, file) + + +def yapfChanges(filename): + """ Use yapf with the default settings to format file filename """ + try: + with open(filename) as f: + codeBefore = f.read() + except UnicodeError: + with open(filename, encoding='latin-1') as f: + codeBefore = f.read() + try: + changes, changed = FormatCode(codeBefore, style_config=styleConfig, print_diff=True, + filename=filename) + except Exception: + print(filename) + raise + if changed: + print(changes) + return changed + + +if __name__ == "__main__": + changedFiles = [] + for s in pythonFiles(): + if yapfChanges(s): + changedFiles.append(s) + print() + if changedFiles: + print('yapf will make changes to the following files:') + print('\n'.join(sorted(changedFiles))) + print('To apply the required changes to your code use the following command(s)') + for s in sorted(set(s.replace(rdbase, '').split(os.sep)[1] for s in changedFiles)): + print('yapf --style $RDBASE/setup.cfg --in-place --recursive $RDBASE/{0}'.format(s)) + sys.exit(1) + print('Code complies with the agreed formatting rules.') + sys.exit(0) diff --git a/rdkit/TestRunner.py b/rdkit/TestRunner.py index 308dbfd6e..50f793650 100755 --- a/rdkit/TestRunner.py +++ b/rdkit/TestRunner.py @@ -107,8 +107,8 @@ def RunScript(script, doLongTests, verbose): nTests = len(tests) + len(longTests) del sys.modules[script] if verbose and failed: - for exeName,args,extras in failed: - print("!!! TEST FAILURE: ",exeName,args,extras,file=sys.stderr) + for exeName, args, extras in failed: + print("!!! TEST FAILURE: ", exeName, args, extras, file=sys.stderr) return failed, nTests