Created
April 15, 2016 19:07
-
-
Save hedcler/79d52ded8943118e28e50a26d7667f87 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" | |
Requirements: | |
- tinydb | |
""" | |
import os | |
import sys | |
import errno | |
import argparse | |
import tinydb | |
from shutil import copytree, rmtree, ignore_patterns | |
""" IGNORE PATTERNS """ | |
ignored = ignore_patterns( | |
'destribute.py', # Dist file | |
# '*.py', # Python files (not aplicable to python projects) | |
'*.pyc', # Compiled python | |
'requirements.txt', # Project requirements | |
'*.sh', # Shell script | |
'.git*', # Git files | |
'.sonar*', # Sonar-Qube files | |
'sonar*', # Sonar-Qube files | |
'*README*', # README files | |
'*.md', # Markdown files | |
'Dockerfile' # Docker file | |
) | |
# Expected arguments | |
parser = argparse.ArgumentParser() | |
""" -- Project Name -- | |
You can create an specific name to project. | |
Default name: Current path name Ex.: my_directory | |
Run with: | |
~$ python dist.py -src ${PWD}/<you_path_name> | |
""" | |
parser.add_argument('-debug', | |
default='yes', | |
help='Debug - Show steps on command line') | |
""" -- Project Name -- | |
You can create an specific name to project. | |
Default name: Current path name Ex.: my_directory | |
Run with: | |
~$ python dist.py -src ${PWD}/<you_path_name> | |
""" | |
parser.add_argument('-n', '--name', | |
default=os.path.dirname(os.path.realpath(__file__)).split(os.sep)[-1], | |
help='Project name') | |
""" -- Project Name -- | |
You can define an specific source code path. | |
Default path: absolute path to [./src]. | |
If you have another path on current directory... | |
Run with: | |
~$ python dist.py -src ${PWD}/<you_path_name> | |
""" | |
parser.add_argument('-src', | |
default=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'src'), | |
help='Source code path') | |
""" -- Project Major Version -- | |
You can parse an option to increment major version of software. | |
Run with: | |
~$ python dist.py -m yes | |
-- OR -- | |
~$ python dist.py -MAJOR yes | |
""" | |
parser.add_argument('-j', '--MAJOR', | |
default='no', | |
help="[MAJOR VERSION] Do you make incompatible changes?") | |
""" -- Project Minor Version -- | |
You can parse an option to increment minor version of software. | |
Run with: | |
~$ python dist.py -n yes | |
-- OR -- | |
~$ python dist.py -MINOR yes | |
""" | |
parser.add_argument('-i', '--MINOR', | |
default='no', | |
help="[MINOR VERSION] Do you add functionality in a backwards-compatible manner?") | |
""" -- Project Minor Version -- | |
You can parse an option to increment patch version of software. | |
Run with: | |
~$ python dist.py | |
If you don't want increment patch on versioning -- | |
~$ python dist.py -p no | |
-- OR -- | |
~$ python dist.py -PATCH no | |
""" | |
parser.add_argument('-p', '--PATCH', | |
default='yes', | |
help="[PATCH] Do you make backwards-compatible bug fixes?") | |
""" -- Project Minor Version -- | |
You can parse an option to increment patch version of software. | |
Run with: | |
~$ python dist.py | |
If you don't want increment patch on versioning -- | |
~$ python dist.py -p no | |
-- OR -- | |
~$ python dist.py -PATCH no | |
""" | |
parser.add_argument('-m', '--message', | |
default='', | |
help="[TAG MESSAGE] Git TAG message") | |
""" Retrive arguments """ | |
args = parser.parse_args() | |
""" -- Dist Information -- | |
Display distribuition information | |
""" | |
print("=================================================================") | |
print("+ Project: %s" % args.name) | |
print("+ Do you make incompatible changes: (%s)" % args.MAJOR) | |
print("+ Do you add functionality in a backwards-compatible manner? (%s)" % | |
args.MINOR) | |
print("+ Do you make backwards-compatible bug fixes? (%s)" % args.PATCH) | |
print("=================================================================\n") | |
""" -- Paths information -- | |
Configure distribution paths | |
""" | |
FCPATH = os.path.dirname(os.path.realpath(__file__)) | |
SRC = args.src | |
""" Configure subpath structure """ | |
dist_path_name = "dist" | |
dist = os.path.join(FCPATH,dist_path_name) | |
dist_src = os.path.join(dist, "src") | |
""" Delete old version if exist """ | |
print("... Delete structure if exist") | |
try: | |
rmtree(str(dist_src) + "/") | |
except OSError as exc: | |
if exc.errno != errno.ENOENT: | |
raise exc | |
pass | |
print("OK.") | |
""" Create structure if not exist """ | |
print("... Create structure if not exist") | |
try: | |
os.mkdir(dist) | |
except OSError as exc: | |
if exc.errno != errno.EEXIST: | |
raise exc | |
pass | |
print("OK.") | |
""" Copy files to structure """ | |
print("... Copy files.") | |
copytree(SRC, dist_src, ignore=ignored) | |
print("OK.") | |
print("... Versioning") | |
versioning_file = str(dist) + "/version" | |
db = tinydb.TinyDB(versioning_file) | |
distribution = db.get(eid=1) | |
if not distribution: | |
""" Previous version not exist """ | |
db.insert({'major':0,'minor':0,'patch':0,'message':args.message}) | |
dist_info = db.get(eid=1) | |
dist_info['major'] = 0 | |
dist_info['minor'] = 0 | |
dist_info['patch'] = 0 | |
print("Previous version: %(major)s.%(minor)s.%(patch)s" % dist_info) | |
print("... Calculating distribution version") | |
if args.MAJOR == 'yes': | |
dist_info['major'] = int(dist_info['major']) + 1 | |
dist_info['minor'] = 0 | |
dist_info['patch'] = 0 | |
if args.MINOR == 'yes': | |
dist_info['minor'] = int(dist_info['minor']) + 1 | |
dist_info['patch'] = 0 | |
if args.PATCH == 'yes': | |
dist_info['patch'] = int(dist_info['patch']) + 1 | |
dist_info['message'] = args.message | |
db.update(dist_info, eids=[1]) | |
print("New version writed.") | |
print("... Creating GIT TAG.") | |
os.system('git tag -a v%(major)s.%(minor)s.%(patch)s -m "%(message)s"' % dist_info) | |
print("Current version: %(major)s.%(minor)s.%(patch)s" % dist_info) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment