From 08d6112a462b7664f6342e272e65723429cb96a9 Mon Sep 17 00:00:00 2001 From: Gil Forsyth Date: Sun, 26 Jun 2016 09:46:43 -0400 Subject: [PATCH 1/5] add githash file to setup, manifest and gitignore --- .gitignore | 1 + MANIFEST.in | 1 + setup.py | 42 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9a9ae506a..dc0f2fc31 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ xonsh.egg-info/ docs/_build/ docs/envvarsbody docs/xontribsbody +xonsh/dev.githash # temporary files from vim and emacs *~ diff --git a/MANIFEST.in b/MANIFEST.in index 05c10616c..97182d058 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1 +1,2 @@ include logo.txt +include githash diff --git a/setup.py b/setup.py index 3eec35569..c7cc468fd 100755 --- a/setup.py +++ b/setup.py @@ -7,6 +7,7 @@ from __future__ import print_function, unicode_literals import os import sys import json +import subprocess try: from tempfile import TemporaryDirectory @@ -101,11 +102,47 @@ def install_jupyter_hook(prefix=None, root=None): d, 'xonsh', user=user, replace=True, prefix=prefix) +def dirty_version(): + """ + If install/sdist is run from a git directory (not a conda install), add + a devN suffix to reported version number and write a gitignored file + that holds the git hash of the current state of the repo to be queried + by ``xonfig`` + """ + try: + _version = subprocess.check_output(['git', 'describe', '--tags']) + _version = _version.decode('ascii') + try: + base, N, sha = _version.strip().split('-') + except ValueError: #on base release + open('xonsh/dev.githash', 'w').close() + return + except subprocess.CalledProcessError: + return + + replace_version(base, N) + with open('xonsh/dev.githash', 'w') as f: + f.write(sha) + + +def replace_version(base, N): + """Replace version in `__init__.py` with devN suffix""" + with open('xonsh/__init__.py', 'r') as f: + raw = f.read() + lines = raw.splitlines() + lines[0] = "__version__ = '{}.dev{}'".format(base, N) + upd = '\n'.join(lines) + '\n' + with open('xonsh/__init__.py', 'w') as f: + f.write(upd) + + class xinstall(install): """Xonsh specialization of setuptools install class.""" def run(self): clean_tables() build_tables() + # add dirty version number + dirty_version() # install Jupyter hook root = self.root if self.root else None prefix = self.prefix if self.prefix else None @@ -123,9 +160,11 @@ class xsdist(sdist): def make_release_tree(self, basedir, files): clean_tables() build_tables() + dirty_version() sdist.make_release_tree(self, basedir, files) + #----------------------------------------------------------------------------- # Hack to overcome pip/setuptools problem on Win 10. See: # https://github.com/tomduck/pandoc-eqnos/issues/6 @@ -155,6 +194,7 @@ if HAVE_SETUPTOOLS: def run(self): clean_tables() build_tables() + dirty_version() develop.run(self) @@ -192,7 +232,7 @@ def main(): packages=['xonsh', 'xonsh.ply', 'xonsh.ptk', 'xonsh.parsers', 'xonsh.xoreutils', 'xontrib', 'xonsh.completers'], package_dir={'xonsh': 'xonsh', 'xontrib': 'xontrib'}, - package_data={'xonsh': ['*.json'], 'xontrib': ['*.xsh']}, + package_data={'xonsh': ['*.json', '*.githash'], 'xontrib': ['*.xsh']}, cmdclass=cmdclass, scripts=scripts, ) From f4d95fee6778324b99ff5ac585ec0720d1ddf67b Mon Sep 17 00:00:00 2001 From: Gil Forsyth Date: Sun, 26 Jun 2016 09:48:00 -0400 Subject: [PATCH 2/5] add git hash to platform and xonfig info --- xonsh/platform.py | 20 ++++++++++++++++++++ xonsh/xonfig.py | 4 +++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/xonsh/platform.py b/xonsh/platform.py index 612603fe5..095ad1396 100644 --- a/xonsh/platform.py +++ b/xonsh/platform.py @@ -125,6 +125,26 @@ def is_readline_available(): return (spec is not None) +# +# Dev release info +# + +@functools.lru_cache(1) +def githash(): + from xonsh import main + install_base = main.__file__.rsplit('/', 1)[0] + try: + with open('{}/dev.githash'.format(install_base), 'r') as f: + hash = f.readlines() + if not hash: + hash = None + else: + hash = hash.pop() + except FileNotFoundError: + hash = None + return hash + + # # Encoding # diff --git a/xonsh/xonfig.py b/xonsh/xonfig.py index 0cb7c07a0..38c03f2d0 100644 --- a/xonsh/xonfig.py +++ b/xonsh/xonfig.py @@ -20,7 +20,7 @@ from xonsh import __version__ as XONSH_VERSION from xonsh.environ import is_template_string from xonsh.platform import (is_readline_available, ptk_version, PYTHON_VERSION_INFO, pygments_version, ON_POSIX, ON_LINUX, linux_distro, - ON_DARWIN, ON_WINDOWS, ON_CYGWIN, DEFAULT_ENCODING) + ON_DARWIN, ON_WINDOWS, ON_CYGWIN, DEFAULT_ENCODING, githash) from xonsh.tools import (to_bool, is_string, print_exception, is_superuser, color_style_names, print_color, color_style) from xonsh.xontribs import xontrib_metadata, find_xontrib @@ -353,6 +353,8 @@ def _info(ns): ('is superuser', is_superuser()), ('default encoding', DEFAULT_ENCODING), ]) + if githash(): + data.append(('git SHA', githash())) formatter = _xonfig_format_json if ns.json else _xonfig_format_human s = formatter(data) return s From 65b3b066c1c1961968bbcdd241b3d0d79c3912dd Mon Sep 17 00:00:00 2001 From: Gil Forsyth Date: Sun, 26 Jun 2016 09:54:08 -0400 Subject: [PATCH 3/5] undo changes made to __init__.py during setup.py --- setup.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/setup.py b/setup.py index c7cc468fd..ba024f8fd 100755 --- a/setup.py +++ b/setup.py @@ -116,14 +116,16 @@ def dirty_version(): base, N, sha = _version.strip().split('-') except ValueError: #on base release open('xonsh/dev.githash', 'w').close() - return + return False except subprocess.CalledProcessError: - return + return False replace_version(base, N) with open('xonsh/dev.githash', 'w') as f: f.write(sha) + return True + def replace_version(base, N): """Replace version in `__init__.py` with devN suffix""" @@ -136,13 +138,24 @@ def replace_version(base, N): f.write(upd) +def discard_changes(): + """If we touch ``__init__.py``, discard changes after install""" + try: + _ = subprocess.check_output(['git', + 'checkout', + '--', + 'xonsh/__init__.py']) + except subprocess.CalledProcessError: + pass + + class xinstall(install): """Xonsh specialization of setuptools install class.""" def run(self): clean_tables() build_tables() # add dirty version number - dirty_version() + dirty = dirty_version() # install Jupyter hook root = self.root if self.root else None prefix = self.prefix if self.prefix else None @@ -153,6 +166,9 @@ class xinstall(install): traceback.print_exc() print('Installing Jupyter hook failed.') install.run(self) + if dirty: + discard_changes() + class xsdist(sdist): @@ -160,9 +176,10 @@ class xsdist(sdist): def make_release_tree(self, basedir, files): clean_tables() build_tables() - dirty_version() + dirty = dirty_version() sdist.make_release_tree(self, basedir, files) - + if dirty: + discard_changes() #----------------------------------------------------------------------------- @@ -194,8 +211,10 @@ if HAVE_SETUPTOOLS: def run(self): clean_tables() build_tables() - dirty_version() + dirty = dirty_version() develop.run(self) + if dirty: + discard_changes() def main(): From ebb21fc9c44c2adf681bd087f38dfab5ddea0b43 Mon Sep 17 00:00:00 2001 From: Gil Forsyth Date: Sun, 26 Jun 2016 10:07:12 -0400 Subject: [PATCH 4/5] add changelog and remove spurious MANIFEST entry --- MANIFEST.in | 1 - news/dirtyversion.rst | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 news/dirtyversion.rst diff --git a/MANIFEST.in b/MANIFEST.in index 97182d058..05c10616c 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1 @@ include logo.txt -include githash diff --git a/news/dirtyversion.rst b/news/dirtyversion.rst new file mode 100644 index 000000000..9ac93c3d4 --- /dev/null +++ b/news/dirtyversion.rst @@ -0,0 +1,14 @@ +**Added:** + +* dev versions now display a ``devN`` counter at the end and ``xonfig info`` + also displays the git sha of the current build + +**Changed:** None + +**Deprecated:** None + +**Removed:** None + +**Fixed:** None + +**Security:** None From d6c3d8a47552909cb3c085a6206f556d102bf239 Mon Sep 17 00:00:00 2001 From: Gil Forsyth Date: Sun, 26 Jun 2016 11:45:55 -0400 Subject: [PATCH 5/5] add minor fixes from review --- setup.py | 11 ++++++----- xonsh/platform.py | 15 ++++++--------- xonsh/xonfig.py | 3 +-- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/setup.py b/setup.py index ba024f8fd..fac35b8f5 100755 --- a/setup.py +++ b/setup.py @@ -112,14 +112,15 @@ def dirty_version(): try: _version = subprocess.check_output(['git', 'describe', '--tags']) _version = _version.decode('ascii') - try: - base, N, sha = _version.strip().split('-') - except ValueError: #on base release - open('xonsh/dev.githash', 'w').close() - return False except subprocess.CalledProcessError: return False + try: + base, N, sha = _version.strip().split('-') + except ValueError: #on base release + open('xonsh/dev.githash', 'w').close() + return False + replace_version(base, N) with open('xonsh/dev.githash', 'w') as f: f.write(sha) diff --git a/xonsh/platform.py b/xonsh/platform.py index 095ad1396..0fdfe8910 100644 --- a/xonsh/platform.py +++ b/xonsh/platform.py @@ -131,18 +131,15 @@ def is_readline_available(): @functools.lru_cache(1) def githash(): - from xonsh import main - install_base = main.__file__.rsplit('/', 1)[0] + install_base = os.path.dirname(__file__) try: with open('{}/dev.githash'.format(install_base), 'r') as f: - hash = f.readlines() - if not hash: - hash = None - else: - hash = hash.pop() + sha = f.read().strip() + if not sha: + sha = None except FileNotFoundError: - hash = None - return hash + sha = None + return sha # diff --git a/xonsh/xonfig.py b/xonsh/xonfig.py index 38c03f2d0..b155cc387 100644 --- a/xonsh/xonfig.py +++ b/xonsh/xonfig.py @@ -352,9 +352,8 @@ def _info(ns): ('on cygwin', ON_CYGWIN), ('is superuser', is_superuser()), ('default encoding', DEFAULT_ENCODING), + ('git SHA', githash()) ]) - if githash(): - data.append(('git SHA', githash())) formatter = _xonfig_format_json if ns.json else _xonfig_format_human s = formatter(data) return s