Skip to content

Commit

Permalink
Fixes autoversioning
Browse files Browse the repository at this point in the history
This fixes issue #348
  • Loading branch information
Shriganesh Shintre committed May 31, 2017
1 parent bffcbdc commit 268e003
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
50 changes: 33 additions & 17 deletions app/util/autoversioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,32 @@ 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
# Initialize default version to '0.0.0'. So in case of failure the version will be shown as '0.0.0'.
version = '0.0.0'
# Get version from packaged file.
if os.path.isfile(_VERSION_FILE_PATH):
print('here1')
version = _get_frozen_package_version() # frozen/packaged
if version == '0.0.0':
print('here')
# Try to calculate version using commit count if running from within git repo.
try:
version = _calculate_source_version() # running from source
except subprocess.CalledProcessError:
# In case of exception return version string '0.0.0'
pass

return version


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.
# FileExistsError might happen on Windows as NTFS doesn't support writing to a file while the file
# is opened in python.
pass


Expand Down Expand Up @@ -80,26 +94,19 @@ def _calculate_source_version():
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 "???".
: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')
head_commit_is_on_trunk = _is_commit_hash_in_masters_first_parent_chain(head_commit_hash)
head_commit_hash = _get_commit_hash_from_revision_param('HEAD')
head_commit_is_on_trunk = _is_commit_hash_in_masters_first_parent_chain(head_commit_hash)

commit_count = _get_repo_commit_count()
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)
commit_count = _get_repo_commit_count()
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)

return _calculated_version

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


if __name__ == '__main__':
"""
Print version string.
"""
version = get_version()
write_package_version_file(version)
print(version)
2 changes: 1 addition & 1 deletion app/util/package_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = '0.0.0' # This should not be set to a real version. This file is modified during freeze.
version = '0.0.0' # This should not be set to a real version. This file is modified during freeze/rpm-build.
21 changes: 17 additions & 4 deletions test/unit/util/test_autoversioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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.

actual_version = autoversioning.get_version()
Expand Down Expand Up @@ -86,13 +85,27 @@ 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_with_default_version_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

package_version.version = '0.0.0'
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 use the version from frozen package_version file.')

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

# Return False for a 'isfile' check.
def fake_isfile(filename):
return False

self.patch('app.util.autoversioning.os').path.isfile.side_effect = fake_isfile
actual_version = autoversioning.get_version()

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 268e003

Please sign in to comment.