Commit cc634861 authored by Patrik Dufresne's avatar Patrik Dufresne

Initial version

parents
*.py[co]
# Packages
.eggs
*.egg-info
dist
build
eggs
parts
bin
var
sdist
develop-eggs
.installed.cfg
# Installer logs
pip-log.txt
# Unit test / coverage reports
.coverage
.tox
#Translations
*.mo
#Mr Developer
.mr.developer.cfg
.project
.externalToolBuilders
.settings
/ez_setup
.pydevproject
coverage.xml
nosetests.xml
/sonar-project.properties
/.eggs/
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Maven use SCM version.
# Copyright (C) 2018 Patrik Dufresne Service Logiciel
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Created on Mar 30, 2018
@author: ikus060
"""
from __future__ import unicode_literals
from io import open
import itertools
import os
import pkg_resources
import subprocess
class Wd(object):
commit_command = None
add_command = None
def __init__(self, cwd):
self.cwd = cwd
self.__counter = itertools.count()
def __call__(self, cmd, **kw):
if kw:
cmd = cmd.format(**kw)
return subprocess.check_output(cmd, cwd=self.cwd, shell=True)
def write(self, name, value, **kw):
filename = os.path.join(self. cwd, name)
if kw:
value = value.format(**kw)
with open(filename, 'w', encoding='utf-8') as f:
f.write(value)
return filename
def _reason(self, given_reason):
if given_reason is None:
return 'number-{c}'.format(c=next(self.__counter))
else:
return given_reason
def commit(self, reason=None):
reason = self._reason(reason)
self(self.commit_command, reason=reason)
def commit_testfile(self, reason=None):
reason = self._reason(reason)
self.write('test.txt', 'test {reason}', reason=reason)
self(self.add_command)
self.commit(reason=reason)
def get_version(self, **kw):
__tracebackhide__ = True
version_sh = pkg_resources.resource_filename('maven_scm_version', '../version.sh') # @UndefinedVariable
version = subprocess.check_output(version_sh, shell=True, cwd=str(self.cwd), **kw).strip('\n')
print(version)
return version
@property
def version(self):
__tracebackhide__ = True
return self.get_version()
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Maven use SCM version.
# Copyright (C) 2018 Patrik Dufresne Service Logiciel
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Created on Mar 30, 2018
@author: ikus060
"""
from __future__ import unicode_literals
import shutil
import tempfile
import unittest
from maven_scm_version.test.conftest import Wd
class Test(unittest.TestCase):
def setUp(self):
wd = Wd(tempfile.mkdtemp('wd_scm_version'))
wd('git init')
wd('git config user.email [email protected]')
wd('git config user.name "a test"')
wd.add_command = 'git add .'
wd.commit_command = 'git commit -m test-{reason}'
self.wd = wd
def tearDown(self):
shutil.rmtree(self.wd.cwd)
def test_version_from_git(self):
assert self.wd.version.startswith('0.0.1-0')
self.wd.commit_testfile()
assert self.wd.version.startswith('0.0.1-1-g')
assert not self.wd.version.endswith('1-')
self.wd('git tag v1.0.1')
self.assertEqual(self.wd.version, '1.0.1')
self.wd.write('test.txt', 'test2')
assert self.wd.version.startswith('1.0.1-d')
self.wd.commit_testfile()
assert self.wd.version.startswith('1.0.2-1-g')
self.wd('git tag version-1.0.2')
assert self.wd.version.startswith('1.0.2')
self.wd.commit_testfile()
self.wd('git tag version-1.0.2.alpha210-gbe48adfpost3-g0cc25f2')
assert self.wd.version.startswith('1.0.2')
def test_git_worktree(self):
self.wd.write('test.txt', 'test2')
# untracked files dont change the state
assert self.wd.version == '0.0.1-0'
self.wd('git add test.txt')
assert self.wd.version.startswith('0.0.1-0')
def test_git_dirty_notag(self):
self.wd.commit_testfile()
self.wd.write('test.txt', 'test2')
self.wd("git add test.txt")
assert self.wd.version.startswith('0.0.1-1-g')
if __name__ == "__main__":
# import sys;sys.argv = ['', 'Test.testName']
unittest.main()
#!/bin/bash
# Copyright (C) 2018, Patrik Dufresne Service Logiciel inc. All rights reserved.
# Patrik Dufresne Service Logiciel PROPRIETARY/CONFIDENTIAL.
# Use is subject to license terms.
#
# This script return a valid maven version from git SCM. This implementation
# is inspired from python scm_version.
#
# no distance and clean:
# {tag}
# distance and clean:
# {next_version}-{distance}-g{revision hash}
# no distance and not clean:
# {tag}-dYYMMDD
# distance and not clean:
# {next_version}-{distance}-g{revision hash}.dYYMMDD
#
set -e
increment_version ()
{
declare -a part=( ${1//\./ } )
declare new
declare -i carry=1
for (( CNTR=${#part[@]}-1; CNTR>=0; CNTR-=1 )); do
len=${#part[CNTR]}
new=$((part[CNTR]+carry))
[ ${#new} -gt $len ] && carry=1 || carry=0
[ $CNTR -gt 0 ] && part[CNTR]=${new: -len} || part[CNTR]=${new}
done
new="${part[*]}"
echo -e "${new// /.}"
}
DESCRIBE=$(git describe --dirty --tags --long --match "*.*" 2>/dev/null || true)
if [ -z "$DESCRIBE" ]; then
TAG="0.0.0"
DISTANCE="$(git rev-list HEAD 2>/dev/null | wc -l)"
DIRTY=1
if [ -z "$(git status --porcelain --untracked-files=no)" ]; then
DIRTY=0
fi
NODE="$(git rev-parse --verify --quiet HEAD | cut -c 1-7)"
else
# garbage-v3.0.6-24-g2c76283-dirty
DIRTY=0
if [[ "$DESCRIBE" == *-dirty ]]; then
DIRTY=1
DESCRIBE="${DESCRIBE/-dirty/}"
fi
# garbage-v3.0.6-24-g2c76283
NODE="${DESCRIBE##*-}"
DESCRIBE=${DESCRIBE%-*}
# garbage-v3.0.6-24
DISTANCE="${DESCRIBE##*-}"
# garbage-v3.0.6
TAG="${DESCRIBE%-*}"
fi
TIMESTAMP=$(date '+%Y%m%d')
# Strip tag to get version
# Remove leading "garbage-v"
# Remove trailing "-g2c76283"
VERSION="${TAG%%-d*}"
VERSION="${VERSION%%-g*}"
VERSION="${VERSION%%+*}"
VERSION="${VERSION##*-}"
VERSION="${VERSION##v}"
NEXT_VERSION=$(increment_version "$VERSION")
# Print version
if [ -z "$NODE" ]; then
# {next_version}-{distance}
printf "%s-%s\n" "$NEXT_VERSION" "$DISTANCE"
elif [ $DISTANCE -eq 0 -a $DIRTY -eq 0 ]; then
# {tag}
printf "%s\n" "$VERSION"
elif [ $DISTANCE -ne 0 -a $DIRTY -eq 0 ]; then
# {next_version}-{distance}-g{revision hash}
printf "%s-%s-g%s\n" "$NEXT_VERSION" "$DISTANCE" "$NODE"
elif [ $DISTANCE -eq 0 -a $DIRTY -ne 0 ]; then
# {tag}-dYYMMDD
printf "%s-d%s\n" "$VERSION" "$TIMESTAMP"
else
# {next_version}-{distance}-g{revision hash}.dYYMMDD
printf "%s-%s-g%s-d%s\n" "$NEXT_VERSION" "$DISTANCE" "$NODE" "$TIMESTAMP"
fi
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment