Patchwork D1580: setup: only write some autogenerated files if they change

login
register
mail settings
Submitter phabricator
Date Dec. 5, 2017, 12:16 p.m.
Message ID <0d256c2b261dc302d23820c894d0779a@localhost.localdomain>
Download mbox | patch
Permalink /patch/25934/
State Not Applicable
Headers show

Comments

phabricator - Dec. 5, 2017, 12:16 p.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHG61ff0d7d56fd: setup: only write some autogenerated files if they change (authored by indygreg, committed by ).

CHANGED PRIOR TO COMMIT
  https://phab.mercurial-scm.org/D1580?vs=4089&id=4115#toc

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D1580?vs=4089&id=4115

REVISION DETAIL
  https://phab.mercurial-scm.org/D1580

AFFECTED FILES
  setup.py

CHANGE DETAILS




To: indygreg, #hg-reviewers, yuja
Cc: yuja, mercurial-devel

Patch

diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -136,6 +136,18 @@ 
 from distutils.sysconfig import get_python_inc, get_config_var
 from distutils.version import StrictVersion
 
+def write_if_changed(path, content):
+    """Write content to a file iff the content hasn't changed."""
+    if os.path.exists(path):
+        with open(path, 'rb') as fh:
+            current = fh.read()
+    else:
+        current = b''
+
+    if current != content:
+        with open(path, 'wb') as fh:
+            fh.write(content)
+
 scripts = ['hg']
 if os.name == 'nt':
     # We remove hg.bat if we are able to build hg.exe.
@@ -317,9 +329,14 @@ 
         version = kw.get('node', '')[:12]
 
 if version:
-    with open("mercurial/__version__.py", "w") as f:
-        f.write('# this file is autogenerated by setup.py\n')
-        f.write('version = "%s"\n' % version)
+    versionb = version
+    if not isinstance(versionb, bytes):
+        versionb = versionb.encode('ascii')
+
+    write_if_changed('mercurial/__version__.py', b''.join([
+        b'# this file is autogenerated by setup.py\n'
+        b'version = "%s"\n' % versionb,
+    ]))
 
 try:
     oldpolicy = os.environ.get('HGMODULEPOLICY', None)
@@ -478,9 +495,13 @@ 
             modulepolicy = 'allow'
         else:
             modulepolicy = 'c'
-        with open(os.path.join(basepath, '__modulepolicy__.py'), "w") as f:
-            f.write('# this file is autogenerated by setup.py\n')
-            f.write('modulepolicy = b"%s"\n' % modulepolicy)
+
+        content = b''.join([
+            b'# this file is autogenerated by setup.py\n',
+            b'modulepolicy = b"%s"\n' % modulepolicy.encode('ascii'),
+        ])
+        write_if_changed(os.path.join(basepath, '__modulepolicy__.py'),
+                         content)
 
         build_py.run(self)