diff options
-rw-r--r-- | qutebrowser/utils/version.py | 32 | ||||
-rw-r--r-- | scripts/freeze.py | 34 | ||||
-rw-r--r-- | scripts/setupcommon.py | 36 | ||||
-rw-r--r-- | setup.py | 36 |
4 files changed, 114 insertions, 24 deletions
diff --git a/qutebrowser/utils/version.py b/qutebrowser/utils/version.py index e2fb9d649..dd62f345e 100644 --- a/qutebrowser/utils/version.py +++ b/qutebrowser/utils/version.py @@ -27,6 +27,7 @@ from PyQt5.QtCore import QT_VERSION_STR, PYQT_VERSION_STR, qVersion from PyQt5.QtWebKit import qWebKitVersion import qutebrowser +from qutebrowser.utils.misc import read_file def _git_str(): @@ -36,13 +37,34 @@ def _git_str(): string containing the git commit ID. None if there was an error or we're not in a git repo. """ - if hasattr(sys, "frozen"): - return None + # First try via subprocess if possible + commit = None + if not hasattr(sys, "frozen"): + try: + gitpath = os.path.join(os.path.dirname(os.path.realpath(__file__)), + os.path.pardir, os.path.pardir) + except NameError: + pass + else: + commit = _git_str_subprocess(gitpath) + if commit is not None: + return commit + # If that fails, check the git-commit-id file. try: - gitpath = os.path.join(os.path.dirname(os.path.realpath(__file__)), - os.path.pardir, os.path.pardir) - except NameError: + return read_file('git-commit-id') + except (FileNotFoundError, ImportError): return None + + +def _git_str_subprocess(gitpath): + """Try to get the git commit ID by calling git. + + Args: + gitpath: The path where the .git folder is. + + Return: + The path on success, None on failure. + """ if not os.path.isdir(os.path.join(gitpath, ".git")): return None try: diff --git a/scripts/freeze.py b/scripts/freeze.py index 03edaaf8a..6f70c616a 100644 --- a/scripts/freeze.py +++ b/scripts/freeze.py @@ -23,13 +23,21 @@ Builds a standalone executable. import os +import os.path import sys import platform from cx_Freeze import setup, Executable sys.path.insert(0, os.getcwd()) -from scripts.setupcommon import setupdata +from scripts.setupcommon import setupdata, write_git_file + + +try: + BASEDIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), + os.path.pardir) +except NameError: + BASEDIR = None def get_egl_path(): @@ -47,6 +55,7 @@ def get_egl_path(): build_exe_options = { 'include_files': [ ('qutebrowser/html', 'html'), + ('qutebrowser/git-commit-id', 'git-commit-id'), ], 'include_msvcr': True, } @@ -68,11 +77,18 @@ executable = Executable('qutebrowser/__main__.py', base=base, shortcutName='qutebrowser', shortcutDir='ProgramMenuFolder') -setup( - executables = [executable], - options = { - 'build_exe': build_exe_options, - 'bdist_msi': bdist_msi_options, - }, - **setupdata -) +try: + write_git_file() + setup( + executables = [executable], + options = { + 'build_exe': build_exe_options, + 'bdist_msi': bdist_msi_options, + }, + **setupdata + ) +finally: + if BASEDIR is not None: + path = os.path.join(BASEDIR, 'qutebrowser', 'git-commit-id') + if os.path.exists(path): + os.remove(path) diff --git a/scripts/setupcommon.py b/scripts/setupcommon.py index e9b0e94eb..4f91bb02e 100644 --- a/scripts/setupcommon.py +++ b/scripts/setupcommon.py @@ -20,15 +20,51 @@ import sys import os +import os.path +import subprocess sys.path.insert(0, os.getcwd()) import qutebrowser +try: + BASEDIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), + os.path.pardir) +except NameError: + BASEDIR = None + + def read_file(name): with open(name, encoding='utf-8') as f: return f.read() +def _git_str(): + """Try to find out git version. + + Return: + string containing the git commit ID. + None if there was an error or we're not in a git repo. + """ + if BASEDIR is None: + return None + if not os.path.isdir(os.path.join(BASEDIR, ".git")): + return None + try: + return subprocess.check_output( + ['git', 'describe', '--tags', '--dirty', '--always'], + cwd=BASEDIR).decode('UTF-8').strip() + except (subprocess.CalledProcessError, FileNotFoundError): + return None + + +def write_git_file(): + gitstr = _git_str() + if gitstr is None: + gitstr = '' + with open(os.path.join(BASEDIR, 'qutebrowser', 'git-commit-id'), 'w') as f: + f.write(gitstr) + + setupdata = { 'name': 'qutebrowser', 'version': qutebrowser.__version__, @@ -18,20 +18,36 @@ """setuptools installer script for qutebrowser""" +import os +import os.path -from scripts.setupcommon import setupdata +from scripts.setupcommon import setupdata, write_git_file from scripts.ez_setup import use_setuptools use_setuptools() from setuptools import setup, find_packages -setup( - packages=find_packages(exclude=['qutebrowser.test']), - include_package_data=True, - package_data={'qutebrowser': ['html/*']}, - entry_points={'gui_scripts': ['qutebrowser = qutebrowser.__main__:main']}, - test_suite='qutebrowser.test', - zip_safe=True, - **setupdata -) +try: + BASEDIR = os.path.dirname(os.path.realpath(__file__)) +except NameError: + BASEDIR = None + + +try: + write_git_file() + setup( + packages=find_packages(exclude=['qutebrowser.test']), + include_package_data=True, + package_data={'qutebrowser': ['html/*', 'git-commit-id']}, + entry_points={'gui_scripts': + ['qutebrowser = qutebrowser.__main__:main']}, + test_suite='qutebrowser.test', + zip_safe=True, + **setupdata + ) +finally: + if BASEDIR is not None: + path = os.path.join(BASEDIR, 'qutebrowser', 'git-commit-id') + if os.path.exists(path): + os.remove(path) |