ORNL-CEES / Cap

A library for modeling energy storage devices.
http://cap.readthedocs.org
Other
16 stars 6 forks source link

Make diff-clang-format.py runnable from source directory #248

Closed aprokop closed 7 years ago

aprokop commented 7 years ago

At the moment, it fails with

Traceback (most recent call last):
  File "/home/xap/bin/diff-clang-format.py", line 111, in <module>
    copy(config, getcwd() + '/' + '.clang-format')
  File "/usr/lib64/python2.7/shutil.py", line 119, in copy
    copyfile(src, dst)
  File "/usr/lib64/python2.7/shutil.py", line 69, in copyfile
    raise Error("`%s` and `%s` are the same file" % (src, dst))
shutil.Error: `/home/xap/code/trilinos/dtk/DataTransferKit/.clang-format` and `/home/xap/code/dtk/DataTransferKit/.clang-format` are the same file

I have the following hack in place

diff --git a/diff-clang-format.py b/diff-clang-format.py
index 120700c..f2b7c8f 100755
--- a/diff-clang-format.py
+++ b/diff-clang-format.py
@@ -17,7 +17,7 @@ Option:
 from docopt import docopt
 from subprocess import Popen, PIPE
 from os import walk, getcwd, remove, rename
-from shutil import copy
+from shutil import copy, Error

 def diff_with_formatted_source(original_file, command, patch):
     '''Compute the diff between the C++ source code and its formatted version
@@ -104,9 +104,13 @@ if __name__ == '__main__':
     patch = args['--apply-patch']
     command = [args['--binary']]
     if style:
-      command.append('-style='+style)
+        command.append('-style='+style)
+
     if config:
-        copy(config, getcwd()+'/'+'.clang-format')
+        try:
+            copy(config, getcwd() + '/' + '.clang-format')
+        except Error:
+            pass

     diffs = run(paths, extensions, command, patch)
     if diffs:

but it probably suppresses other useful Errors.

aprokop commented 7 years ago

Some context: I am thinking of adding a git hook to call format-cpp with every commit. I don't necessarily want to query the build directory at that point, so I want a standalone formatting script (should work as it does not change often).

aprokop commented 7 years ago

Python 3.5.2 (and maybe earlier versions too) has SameFileError, but Python 2.7 does not. So I'm not sure how to separate the errors.

aprokop commented 7 years ago

I think in addition the script needs to add an option to run it only on modified git files as the default runtime for DTK is multiple seconds which is not useful for hooks. I think an option would be to provide the script a list of files to run on. I'm not sure how that would work inside git rebase though.

dalg24 commented 7 years ago

Look at clang-format-diff that comes with ClangFormat. It is written in Python. You can find it in the Docker container:

[container]$ less $(which clang-format-diff-3.9)