Fix `create_chrome_version_h` in bootstrap.py
authorEran Tiktin <eran.t@mce-sys.com>
Mon, 7 Sep 2015 18:55:02 +0000 (21:55 +0300)
committerEran Tiktin <eran.t@mce-sys.com>
Mon, 7 Sep 2015 18:55:02 +0000 (21:55 +0300)
The code was supposed to compare the content of the existing file with
the new content and only replace the file if the content was different,
but it had a fatal flow. It opened the existing file with 'w+' or 'wb+'
and they both truncate the file, so the compare was always false and we
always overwrote the file.
The updated code compares the file content ignoring line endings and
writes the file only if its different or if it didn't exist.

script/bootstrap.py

index 293d3dc2dd101fa3fdc7edb77d9c17aa165b3060..d5ad41a61c598a12bd391fb6b55a3c509014e7b7 100755 (executable)
@@ -162,13 +162,16 @@ def create_chrome_version_h():
     version = f.read()
   with open(template_file, 'r') as f:
     template = f.read()
-  if sys.platform in ['win32', 'cygwin']:
-    open_mode = 'wb+'
-  else:
-    open_mode = 'w+'
-  with open(target_file, open_mode) as f:
-    content = template.replace('{PLACEHOLDER}', version.strip())
-    if f.read() != content:
+  content = template.replace('{PLACEHOLDER}', version.strip())
+
+  # We update the file only if the content has changed (ignoring line ending
+  # differences).
+  should_write = True
+  if os.path.isfile(target_file):
+    with open(target_file, 'r') as f:
+      should_write = f.read().replace('r', '') != content.replace('r', '')
+  if should_write:
+    with open(target_file, 'w') as f:
       f.write(content)