From d953265827a8de13c992adf17ea402028b371cdf Mon Sep 17 00:00:00 2001 From: Joab Date: Mon, 20 Jan 2020 04:47:48 -0300 Subject: [PATCH] release-config: added new version validate --- cereja/__init__.py | 5 +++ cereja/cj_types.py | 1 + cereja/conf.py | 5 +-- cereja/utils.py | 79 +++++++++++++++++++++++++++++++++++++++++++++- cj_setup.py | 42 +++++++++++++++++------- lab/__init__.py | 0 lab/cj_lab.py | 34 ++++++++++++++++++++ setup.py | 5 +-- 8 files changed, 153 insertions(+), 18 deletions(-) create mode 100644 lab/__init__.py create mode 100644 lab/cj_lab.py diff --git a/cereja/__init__.py b/cereja/__init__.py index 018725d..84f40ed 100644 --- a/cereja/__init__.py +++ b/cereja/__init__.py @@ -1,6 +1,11 @@ +from cereja.utils import get_version_pep440_compliant from . import utils from importlib import import_module +VERSION = "1.0.0.final.0" + +__version__ = get_version_pep440_compliant() + cj_modules_dotted_path = utils.import_string('cereja.conf.cj_modules_dotted_path') for dot_module in cj_modules_dotted_path: diff --git a/cereja/cj_types.py b/cereja/cj_types.py index d9ae6cd..2cc70f2 100644 --- a/cereja/cj_types.py +++ b/cereja/cj_types.py @@ -2,3 +2,4 @@ T = Union[int, float, str] Number = Union[float, int, complex] +PEP440 = Tuple[int, int, int, str, int] diff --git a/cereja/conf.py b/cereja/conf.py index 491355c..f102a35 100644 --- a/cereja/conf.py +++ b/cereja/conf.py @@ -1,11 +1,8 @@ import sys import logging -VERSION = "0.3.5" - # using by utils.module_references -_explicit_exclude = ["console_logger", "cj_modules"] -_explicit_include = ["VERSION"] +_explicit_exclude = ["console_logger", "cj_modules_dotted_path"] # Used to add the functions of each module at the root cj_modules_dotted_path = ['cereja.common', 'cereja.conf', 'cereja.decorators', 'cereja.path', diff --git a/cereja/utils.py b/cereja/utils.py index 43598cc..23071f8 100644 --- a/cereja/utils.py +++ b/cereja/utils.py @@ -1,11 +1,16 @@ +import datetime +import functools +import os from importlib import import_module import subprocess import importlib import sys import types -from typing import Any, Union +from typing import Any, Union, Tuple, Sequence import logging +from cereja.cj_types import PEP440 + logger = logging.getLogger(__name__) @@ -103,3 +108,75 @@ def set_log_level(level: int): def logger_level(): import logging return logging.getLogger().level + + +def get_version(version: Union[str, PEP440] = None) -> PEP440: + """ + Dotted version of the string type is expected + + e.g: + '1.0.3.a.3' # Pep440 see https://www.python.org/dev/peps/pep-0440/ + + :param version: Dotted version of the string + """ + if version is None: + from cereja import VERSION as version + + if isinstance(version, str): + version = version.split('.') + version_note = version.pop(3) + version = list(map(int, version)) + version.insert(3, version_note) + + assert len(version) == 5, "Version must be size 5" + + assert version[3] in ('alpha', 'beta', 'rc', 'final') + return version + + +@functools.lru_cache() +def latest_git(): + """ + Return a numeric identifier of the latest git changeset. + """ + repo_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + git_log = subprocess.run( + ['git', 'log', '--pretty=format:%ct', '--quiet', '-1', 'HEAD'], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + shell=True, cwd=repo_dir, universal_newlines=True, + ) + timestamp = git_log.stdout + try: + timestamp = datetime.datetime.utcfromtimestamp(int(timestamp)) + except ValueError: + return None + return timestamp.strftime('%Y%m%d%H%M%S') + + +def get_version_pep440_compliant(version: str = None) -> str: + """ + Dotted version of the string type is expected + + e.g: + '1.0.3.a.3' # Pep440 see https://www.python.org/dev/peps/pep-0440/ + + :param version: Dotted version of the string + """ + version_mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'rc'} + version = get_version(version) + root_version = '.'.join(map(str, version[:3])) + + sub = '' + if version[3] == 'alpha' and version[4] == 0: + git = latest_git() + if git: + sub = f'.dev{git}' + + elif version[3] != 'final': + sub = version_mapping[version[3]] + str(version[4]) + + return f"{root_version}{sub}" + + +if __name__ == '__main__': + print(get_version_pep440_compliant()) diff --git a/cj_setup.py b/cj_setup.py index 93f612f..fd06f64 100644 --- a/cj_setup.py +++ b/cj_setup.py @@ -3,13 +3,21 @@ import sys import os import logging +import shutil +choice_level_change = """ +0 -> major +1 -> minor +2 -> micro + +Choose the level of change: +""" logger = logging.getLogger(__name__) base_dir = os.path.dirname(os.path.abspath(__file__)) os.chdir(base_dir) setup_file = os.path.join(base_dir, 'setup.py') -conf_file = os.path.join(base_dir, 'cereja', 'conf.py') +conf_file = os.path.join(base_dir, 'cereja', '__ini__.py') tests_case_result = subprocess.run([f"{sys.executable}", "-m", "unittest", "tests.tests.UtilsTestCase"]).returncode @@ -24,23 +32,26 @@ print("Ok Exiting...") if tests_case_result == 0 and allow_update_version: + level_change = int(input(choice_level_change)) with open(conf_file, 'r') as f: lines = f.readlines() for i, line in enumerate(lines): if "VERSION = " in line: - version_base, version_middle, version_end = [int(i) for i in - line.strip().split('=')[1].strip(' "').split('.')] - if version_end < 10: - version_end = version_end + 1 - elif version_middle < 10: - version_end = 0 - version_middle = version_middle + 1 + major, minor, micro = [int(i) for i in + line.strip().split('=')[1].strip(' "').split('.')] + is_release = input("Is Final? Y/n") + if micro < 10: + micro = micro + 1 + elif minor < 10: + micro = 0 + minor = minor + 1 else: - version_middle = 0 - version_base = version_base + 1 + minor = 0 + major = major + 1 - version = f'{version_base}.{version_middle}.{version_end}' + version = f'{major}.{minor}.{micro}' lines[i] = f'VERSION = "{version}"\n' + break with open(conf_file, 'w') as f: f.writelines(lines) @@ -59,3 +70,12 @@ subprocess.run([f"{sys.executable}", "-m", "twine", "upload", f"{dist}{os.path.sep}*"]) else: print("Versão não foi enviada.") + + # Remove latest build, info and dist + files_cereja_build = [ + os.path.join(base_dir, 'build'), + os.path.join(base_dir, 'cereja.egg-info'), + os.path.join(base_dir, 'dist') + ] + for p in files_cereja_build: + shutil.rmtree(p) diff --git a/lab/__init__.py b/lab/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lab/cj_lab.py b/lab/cj_lab.py new file mode 100644 index 0000000..297baa6 --- /dev/null +++ b/lab/cj_lab.py @@ -0,0 +1,34 @@ +from typing import TypeVar + +from cereja.common import flatten, get_shape + +T = TypeVar('T') + + +class Vector(list): + """ + -- Development proposal -- + Vector is a dynamic array, whose size can be increased and can store any type of objects + + """ + + def __init__(self, values): + super().__init__(values) + self._size = len(self.flatten()) + self._shape = get_shape(self) + + @property + def size(self): + # Need to create smart way to check changes + return self._size + + @property + def shape(self): + # need to create smart way to check changes + return self._shape + + def first(self) -> T: + return self[0] + + def flatten(self): + return flatten(self) diff --git a/setup.py b/setup.py index b7d0476..d7c188c 100644 --- a/setup.py +++ b/setup.py @@ -5,12 +5,13 @@ with open("README.md", "r") as fh: long_description = fh.read() -EXCLUDE_FROM_PACKAGES = ('cereja.tests' +EXCLUDE_FROM_PACKAGES = ('cereja.tests', + 'cereja.lab', ) setuptools.setup( name="cereja", - version=cereja.VERSION, + version=cereja.__version__, author="Joab Leite", author_email="jlsn1@ifal.edu.br", description="Cereja is a bundle of useful functions that I don't want to rewrite.",