Skip to content

Commit

Permalink
Enable autoversioning to output correct version
Browse files Browse the repository at this point in the history
This is an enhancement to enable rpm building with autogenerated
version.
This address #348.
  • Loading branch information
Shriganesh Shintre committed May 31, 2017
1 parent bffcbdc commit 9b62bce
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
44 changes: 30 additions & 14 deletions app/util/autoversioning.py
Expand Up @@ -20,18 +20,24 @@ def get_version():
:return: The version of the application
:rtype: str
"""
if getattr(sys, 'frozen', False):
return _get_frozen_package_version() # frozen/packaged

return _calculate_source_version() # unfrozen/running from source
return _get_frozen_package_version() or _calculate_source_version() or '0.0.0'


def _try_rename(src, dst):
try:
os.rename(src, dst)
except FileExistsError:
# Skip backing up the original package_version.py if a FileExistsError happened.
# This might happen on Windows as NTFS doesn't support writing to a file while the file is opened in python.
except (FileExistsError, FileNotFoundError):
# Skip backing up the original package_version.py if a FileExistsError or FileNotFoundError happened.
# FileExistsError might happen on Windows as NTFS doesn't support writing to a file while the file
# is opened in python.
pass


def _try_remove(src):
try:
os.remove(src)
except OSError:
pass


Expand Down Expand Up @@ -70,24 +76,24 @@ def _get_frozen_package_version():

# only import package_version when needed as on Windows once imported, the actual package_version.py can't be
# edited anymore
from app.util import package_version
return package_version.version
try:
from app.util import package_version
return package_version.version
except ImportError:
return None


def _calculate_source_version():
"""
Calculate the version using a scheme based off of git repo info. Note that since this depends on the git history,
this will *not* work from a frozen package (which does not include the git repo data). This will only work in the
context of running the application from the cloned git repo.
If one of the git commands used to calculate the version fails unexpectedly, the patch in the version string will
be set to "???".
If this is running outside of a git repo, it will handle the CalledProcessError exception and return None.
:return: The version of the (source) application
:rtype: str
"""
global _calculated_version

if _calculated_version is None:
try:
head_commit_hash = _get_commit_hash_from_revision_param('HEAD')
Expand All @@ -97,9 +103,8 @@ def _calculate_source_version():
hash_extension = '' if head_commit_is_on_trunk else '-{}'.format(head_commit_hash[:7])
mod_extension = '' if not _repo_has_uncommited_changes() else '-mod'
_calculated_version = '{}.{}{}{}'.format(_MAJOR_MINOR_VERSION, commit_count, hash_extension, mod_extension)

except subprocess.CalledProcessError:
_calculated_version = '{}.???'.format(_MAJOR_MINOR_VERSION)
_calculated_version = None

return _calculated_version

Expand Down Expand Up @@ -170,3 +175,14 @@ def _execute_local_git_command(*args):
cwd=os.path.dirname(__file__),
)
return command_output.decode()


if __name__ == '__main__':
"""
Print version string.
"""
# Remove the "package_version.py" file so that autoversioning always calculates version.
_try_remove(_VERSION_FILE_PATH)
version = get_version()
write_package_version_file(version)
print(version)
1 change: 0 additions & 1 deletion app/util/package_version.py

This file was deleted.

12 changes: 6 additions & 6 deletions test/unit/util/test_autoversioning.py
Expand Up @@ -2,7 +2,7 @@
import subprocess
from unittest.mock import MagicMock, call

from app.util import autoversioning, package_version
from app.util import autoversioning
from test.framework.comparators import AnythingOfType
from test.framework.base_unit_test_case import BaseUnitTestCase

Expand All @@ -18,8 +18,8 @@ def setUp(self):
autoversioning._calculated_version = None # reset cached version between individual tests

def test_get_version_returns_frozen_version_when_run_from_frozen_package(self):
self.patch('app.util.autoversioning.sys').frozen = True
package_version.version = '1.2.3' # package_version is written during freeze, so this is the "frozen" version.
# package_version is written during freeze, so this is the "frozen" version.
self.patch('app.util.autoversioning._get_frozen_package_version').return_value = '1.2.3'

actual_version = autoversioning.get_version()

Expand Down Expand Up @@ -86,13 +86,13 @@ def test_calculate_source_version_caches_computed_version(self):
self.assertEqual(num_check_output_calls, self.check_output_mock.call_count,
'No calls to check_output() should occur after the first get_version() call.')

def test_unexpected_failure_in_git_command_sets_patch_version_to_unknown(self):
def test_package_version_file_absent_and_unexpected_failure_in_git_command_sets_patch_version_to_default(self):
self.check_output_mock.side_effect = [subprocess.CalledProcessError(1, 'fake')] # make all git commands fail

actual_version = autoversioning.get_version()

self.assertEqual(actual_version, '1.0.???', 'get_version() should not raise exception if git commands fail, '
'and should just set the patch version to "???".')
self.assertEqual(actual_version, '0.0.0', 'get_version() should not raise exception if git commands fail, '
'and should just set the patch version to default "0.0.0".')

@genty_dataset(
head_commit_is_on_trunk=(True, False, '1.0.4'),
Expand Down

0 comments on commit 9b62bce

Please sign in to comment.