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

Do not modify generators files if nothing has changed (close #2895) #3412

Merged
merged 6 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions conans/client/generators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ def write_generators(conanfile, path, output):
for k, v in content.items():
v = normalize(v)
output.info("Generator %s created %s" % (generator_name, k))
save(join(path, k), v)
save(join(path, k), v, only_if_modified=True)
else:
content = normalize(content)
output.info("Generator %s created %s" % (generator_name, generator.filename))
save(join(path, generator.filename), content)
save(join(path, generator.filename), content, only_if_modified=True)
except Exception as e:
if get_env("CONAN_VERBOSE_TRACEBACK", False):
output.error(traceback.format_exc())
Expand Down
35 changes: 35 additions & 0 deletions conans/test/util/files_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import unittest
import os

from conans.test.utils.test_files import temp_folder
from conans.util.files import save
from time import sleep


class SaveTestCase(unittest.TestCase):

def setUp(self):
folder = temp_folder()
self.filepath = os.path.join(folder, "file.txt")

# Save some content and keep timestamp
self.content = "my content"
save(self.filepath, self.content)
self.timestamp = os.path.getmtime(self.filepath)
sleep(1) # precission is seconds, so we need to sleep

def only_if_modified_true_test(self):
save(self.filepath, self.content, only_if_modified=True)
self.assertEqual(self.timestamp, os.path.getmtime(self.filepath))

def only_if_modified_false_test(self):
save(self.filepath, self.content, only_if_modified=False)
self.assertNotEqual(self.timestamp, os.path.getmtime(self.filepath))

def modified_only_true_test(self):
save(self.filepath, "other content", only_if_modified=True)
self.assertNotEqual(self.timestamp, os.path.getmtime(self.filepath))

def modified_only_false_test(self):
save(self.filepath, "other content", only_if_modified=False)
self.assertNotEqual(self.timestamp, os.path.getmtime(self.filepath))
18 changes: 13 additions & 5 deletions conans/util/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,28 @@ def save_append(path, content):
handle.write(to_file_bytes(content))


def save(path, content):
def save(path, content, only_if_modified=False):
Copy link
Member

Choose a reason for hiding this comment

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

As this is only used internally (tools.save() is not affected by this) and mostly for testing (I think). Would it be possible to benefit from a defaulted only_if_modified=True?

Copy link
Member

Choose a reason for hiding this comment

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

No, do not change the current default, dangerous. And I don't see the benefit, you mean performance? I doubt it, it will be slower.

"""
Saves a file with given content
Params:
path: path to write file to
load: contents to save in the file
content: contents to save in the file
only_if_modified: file won't be modified if the content hasn't changed
"""
try:
os.makedirs(os.path.dirname(path))
except:
pass

new_content = to_file_bytes(content)

if only_if_modified and os.path.exists(path):
old_content = load(path, binary=True)
if old_content == new_content:
return

with open(path, "wb") as handle:
handle.write(to_file_bytes(content))
handle.write(new_content)


def mkdir_tmp():
Expand Down Expand Up @@ -158,9 +166,9 @@ def to_file_bytes(content):
return content


def save_files(path, files):
def save_files(path, files, only_if_modified=False):
for name, content in list(files.items()):
save(os.path.join(path, name), content)
save(os.path.join(path, name), content, only_if_modified=only_if_modified)


def load(path, binary=False):
Expand Down