Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

podio_class_generator: only write files if their content changed #65

Merged
merged 1 commit into from
Feb 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions python/podio_class_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ def configure_clang_format(self, apply):
print " Please make sure it is in the PATH."
self.clang_format = []
return
self.clang_format = [cformat_exe, "-i", "-style=file", "-fallback-style=llvm"]

self.clang_format = [cformat_exe, "-style=file", "-fallback-style=llvm"]

def process(self):
self.reader.read()
Expand Down Expand Up @@ -1034,17 +1033,22 @@ def prepare_vectorized_access(self, classname,members ):
declaration += "\ttemplate<size_t arraysize>\n\tconst std::array<%s,arraysize> %s() const;\n" %(klass, name)
return declaration, implementation

def write_file(self, name,content):
def write_file(self, name, content):
#dispatch headers to header dir, the rest to /src
# fullname = os.path.join(self.install_dir,self.package_name,name)
if name.endswith("h"):
fullname = os.path.join(self.install_dir,self.package_name,name)
else:
fullname = os.path.join(self.install_dir,"src",name)
if not self.dryrun:
open(fullname, "w").write(content)
if self.clang_format:
subprocess.call(self.clang_format + [fullname])
cfproc = subprocess.Popen(self.clang_format, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
content = cfproc.communicate(input=content)[0]
existing_content = ''
if os.path.exists(fullname):
existing_content = open(fullname, 'r').read()
if existing_content != content:
open(fullname, 'w').write(content)
Comment on lines +1049 to +1051
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines could be slightly more "pythonic" with a with statement:

with open(fullname, "r+") as _f:
  existing_content = _f.read()
  if existing_content != content:
    _f.write(content)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#!/usr/bin/env python
content ='foo bar'
fullname = 'temp.txt'
with open(fullname, "r+") as _f:
  existing_content = _f.read()
  if existing_content != content:
    _f.write(content)

Errors if temp.txt does not exist

$ python testRead.py
Traceback (most recent call last):
  File "testRead.py", line 6, in <module>
    with open(fullname, "r+") as _f:
IOError: [Errno 2] No such file or directory: 'temp.txt'

So we would still need one path for exists, one without.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true, I overlooked that in one case the file won't exist yet. Taking that in consideration I think the code is best as it is!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many things to be cleaned up, especially in view of python3 compatibility (#66)


def evaluate_template(self, filename, substitutions):
""" reads in a given template, evaluates it
Expand Down