vxSDK - 0.12.0-41 : update logger and checkers + small fixes

*update*
> [scripts]
  | [checkers] use the new simplified vxNorm checker API
  | [install] proper handle re-installation cache
  | [install] *REMOVE* update feature
  | [install] better installation handling
> [vxsdk/core]
  | [logger] display log level if other than LOG_USER

*fix*
> [install]
  | fix relative path installation
> [vxsdk/core]
  | [logger] fix VxLogger object creation
  | [build/meta] fix PRIVATE env key generation
  | [core/pkg] fix function exposition for find_pkg (undetected by pylint!)
  | [core/pkg] fix function exposition for clone_pkg (undetected by pylint!)
This commit is contained in:
Yann MAGNIN 2023-04-06 18:40:46 +02:00
parent aa21d9019c
commit 1a14f4ca4e
11 changed files with 152 additions and 209 deletions

View File

@ -1,5 +1,6 @@
#! /usr/bin/env bash
projdir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
prefix="$HOME/.local"
VERSION='0.12.0'
@ -46,6 +47,8 @@ for arg; do case "$arg" in
target='update';;
uninstall)
target='uninstall';;
uninstall-cached)
target='uninstall-cached';;
*)
echo "error: unreconized argument '$arg', giving up." >&2
exit 1
@ -59,82 +62,131 @@ esac; done
if [[ "$target" = "install" ]]
then
if [[ -d "$prefix/lib/vxsdk/vxsdk" ]]; then
if [[ -d "$prefix/lib/vxsdk/vxsdk" ]]
then
echo 'warning : vxsdk is already installed !' >&2
read -n 1 -p 'Do you whant to re-install the vxSDK (package will not be removed) [y/N] ? ' reinstall
read -n 1 -p 'Do you whant to re-install the vxSDK [y/N] ? ' reinstall
[[ "$reinstall" != 'y' ]] && exit 1
echo ''
./install.sh uninstall
"$projdir/install.sh" uninstall-cached
fi
install -d $prefix/lib/vxsdk/vxsdk
cp -r requirements.txt assets vxsdk $prefix/lib/vxsdk/vxsdk
install -d "$prefix/lib/vxsdk/vxsdk"
cp -r \
"$projdir/requirements.txt" \
"$projdir/assets" \
"$projdir/vxsdk" \
"$prefix/lib/vxsdk/"
install -d $prefix/bin
echo '#! /usr/bin/env bash' > $prefix/bin/vxsdk
echo '' >> $prefix/bin/vxsdk
echo "source $prefix/lib/vxsdk/vxsdk/venv/bin/activate" >> $prefix/bin/vxsdk
echo "python3 $prefix/lib/vxsdk/vxsdk/vxsdk \$@" >> $prefix/bin/vxsdk
echo 'deactivate' >> $prefix/bin/vxsdk
chmod +x $prefix/bin/vxsdk
install -d "$prefix/bin"
echo '#! /usr/bin/env bash' > $prefix/bin/vxsdk
echo '' >> $prefix/bin/vxsdk
echo "source $prefix/lib/vxsdk/venv/bin/activate" >> $prefix/bin/vxsdk
echo "python3 $prefix/lib/vxsdk/vxsdk \$@" >> $prefix/bin/vxsdk
echo 'deactivate' >> $prefix/bin/vxsdk
chmod +x "$prefix/bin/vxsdk"
build_date=$(date '+%Y-%m-%d')
build_hash=$(git rev-parse --short HEAD)
f="$prefix/lib/vxsdk/vxsdk/vxsdk/__main__.py"
sed -e "s*%VERSION%*$VERSION*; s*%BUILD_HASH%*$build_hash*; s*%BUILD_DATE%*$build_date*" vxsdk/__main__.py > $f
f="$prefix/lib/vxsdk/vxsdk/__main__.py"
sed \
-e "s*%VERSION%*$VERSION*" \
-e "s*%BUILD_HASH%*$build_hash*" \
-e "s*%BUILD_DATE%*$build_date*" \
"$projdir/vxsdk/__main__.py" \
> $f
mkdir -p $prefix/share/vxsdk
cd $prefix/lib/vxsdk/vxsdk
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip 2>&1 > /dev/null
pip install -r requirements.txt
deactivate
cd $prefix/lib/vxsdk
if [[ ! -d venv ]];
then
set -x; python3 -m venv venv; set +x
source venv/bin/activate
set -x
pip install --upgrade pip 2>&1 > /dev/null
pip install -r requirements.txt
set +x
deactivate
else
source venv/bin/activate
set -x; pip install -r requirements.txt; set +x
deactivate
fi
exit 0
fi
if [[ "$target" = "update" ]]
# same as uninstall but do not remove the venv
if [[ "$target" = 'uninstall-cached' ]]
then
git clone git@github.com:Vhex-org/vxSDK.git --depth=1 /tmp/vxSDK > /dev/null 2>&1 || exit 84
cd /tmp/vxSDK
if [[ "$(./install.sh --version)" == "$VERSION" ]]
then
rm -rf /tmp/vxSDK
echo 'already up to date !'
exit 0
fi
_check=$(echo -e "$(./install.sh --version)\n$VERSION" | sort -V | head -n1)
if [[ "$_check" != "$VERSION" ]]; then
rm -rf /tmp/vxSDK
echo 'already up to date !'
exit 0
fi
echo "update $VERSION -> $(./install.sh --version)"
./install.sh uninstall
./install.sh install
rm -rf /tmp/vxSDK
set -x
rm $prefix/bin/vxsdk
rm -rf $prefix/lib/vxsdk/vxsdk
rmdir $prefix/share/vxsdk 2>/dev/null || exit 0
echo 'vxSDK has been partially removed'
exit 0
fi
if [[ "$target" = "uninstall" ]]
if [[ "$target" = 'uninstall' ]]
then
# TODO : rm -rf dependencies too
# TODO : add confirmation input
rm $prefix/bin/vxsdk
rm -rf $prefix/lib/vxsdk
rmdir $prefix/share/vxsdk 2>/dev/null || exit 0
echo 'note: repositories cloned by vxSDK have not been removed'
echo 'vxSDK has been removed'
exit 0
fi
#if [[ "$target" = "update" ]]
#then
#
# git \
# clone \
# git@github.com:Vhex-org/vxSDK.git \
# --depth=1 \
# /tmp/vxSDK \
# > /dev/null 2>&1 || exit 84
# cd /tmp/vxSDK
#
# if [[ "$(./install.sh --version)" == "$VERSION" ]]
# then
# rm -rf /tmp/vxSDK
# echo 'already up to date !'
# exit 0
# fi
#
# _check=$(echo -e "$(./install.sh --version)\n$VERSION" | sort -V | head -n1)
#
# if [[ "$_check" != "$VERSION" ]]; then
# rm -rf /tmp/vxSDK
# echo 'already up to date !'
# exit 0
# fi
#
# echo "update $VERSION -> $(./install.sh --version)"
#
# ./install.sh uninstall
# ./install.sh install
#
# rm -rf /tmp/vxSDK
#
#fi
#
#
#
#if [[ "$target" = "uninstall" ]]
#then
#
# rm $prefix/bin/vxsdk
# rm -rf $prefix/lib/vxsdk
# rmdir $prefix/share/vxsdk 2>/dev/null || exit 0
# echo 'note: repositories cloned by vxSDK have not been removed'
# exit 0
#
#fi

View File

@ -4,7 +4,6 @@ exposed_func - checker for vxnorm
This file does not expose an explicite VxChecker object declaration to avoid
dependencies handling, you just need to provide:
======================= ===============================================
configure() Handle user argument provided after `--<name>`
parse_file() Parse the source file
======================= ===============================================
"""
@ -233,21 +232,6 @@ def _checker_layout_def(checker, info, token):
# Public
#---
def configure(checker):
""" configure the checker
This function is involved during the construction of the checker instance.
Its roles is to populate the instance with (optionally) arguments and
rules.
@args
> checker (VxChecker) - current checker instance for this file
@return
> 0 on sucess, negative value otherwise
"""
checker.register_rule('expofunc', 'check exposed function format')
def parse_file(checker, mfile, pathinfo):
""" parse the mapped file

View File

@ -4,7 +4,6 @@ line_len - checker for vxnorm
This file does not expose an explicite VxChecker object declaration to avoid
dependencies handling, you just need to provide:
======================= ===============================================
configure() Handle user argument provided after `--<name>`
parse_file() Parse the source file
======================= ===============================================
"""
@ -13,22 +12,6 @@ dependencies handling, you just need to provide:
# Public
#---
def configure(checker):
""" configure the checker
This function is involved during the construction of the checker instance.
Its roles is to populate the instance with (optionally) arguments and
rules.
@args
> checker (VxChecker) - current checker instance for this file
@return
> 0 on sucess, negative value otherwise
"""
checker.register_rule('linelen', 'line checking')
return 0
def parse_file(checker, mfile, _):
""" parse the mapped file
@ -44,12 +27,9 @@ def parse_file(checker, mfile, _):
> Nothing
"""
counter = 0
checker.select_rule('linelen')
for line in mfile.getlines():
counter += 1
if len(line) <= 80 or line[0] == '#':
continue
checker.notify(
'linelen',
f"{counter}",
f"too long line ({len(line)}/80)"
)
checker.notify(f"{counter}", f"too long line ({len(line)}/80)")

View File

@ -4,7 +4,6 @@ module_docstring - checker for vxnorm
This file does not expose an explicite VxChecker object declaration to avoid
dependencies handling, you just need to provide:
======================= ===============================================
configure() Handle user argument provided after `--<name>`
parse_file() Parse the source file
======================= ===============================================
"""
@ -13,21 +12,6 @@ dependencies handling, you just need to provide:
# Public
#---
def configure(checker):
""" configure the checker
This function is involved during the construction of the checker instance.
Its roles is to populate the instance with (optionally) arguments and
rules.
@args
> checker (VxChecker) - current checker instance for this file
@return
> 0 on sucess, negative value otherwise
"""
checker.register_rule('modocstr', 'check module docstring format')
def parse_file(checker, mfile, pathinfo):
""" parse the mapped file

View File

@ -4,7 +4,6 @@ pylint - checker for vxnorm
This file does not expose an explicite VxChecker object declaration to avoid
dependencies handling, you just need to provide:
======================= ===============================================
configure() Handle user argument provided after `--<name>`
parse_file() Parse the source file
======================= ===============================================
"""
@ -14,21 +13,6 @@ import subprocess
# Public
#---
def configure(checker):
""" configure the checker
This function is involved during the construction of the checker instance.
Its roles is to populate the instance with (optionally) arguments and
rules.
@args
> checker (VxChecker) - current checker instance for this file
@return
> 0 on sucess, negative value otherwise
"""
checker.register_rule('pylint', 'PyLint file checking')
def parse_file(checker, _, pathinfo):
""" parse the mapped file

View File

@ -1,37 +0,0 @@
"""
aaaa
"""
import os
__all__ = [
'allo0',
'test1',
]
from test import bidule
#---
# Private
#---
def _test():
pass
def test_oui():
pass
#---
# Public
#---
def allo0():
""" aaaa """
def yes():
pass
def non():
""" """

View File

@ -4,7 +4,6 @@ trainling_spaces - checker for vxnorm
This file does not expose an explicite VxChecker object declaration to avoid
dependencies handling, you just need to provide:
======================= ===============================================
configure() Handle user argument provided after `--<name>`
parse_file() Parse the source file
======================= ===============================================
"""
@ -13,21 +12,6 @@ dependencies handling, you just need to provide:
# Public
#---
def configure(checker):
""" configure the checker
This function is involved during the construction of the checker instance.
Its roles is to populate the instance with (optionally) arguments and
rules.
@args
> checker (VxChecker) - current checker instance for this file
@return
> 0 on sucess, negative value otherwise
"""
checker.register_rule('trail_space', 'trailing spaces check')
def parse_file(checker, mfile, _):
""" parse the mapped file
@ -43,6 +27,7 @@ def parse_file(checker, mfile, _):
> Nothing
"""
counter = 0
checker.select_rule('trailing')
for line in mfile.getlines():
counter += 1
line_end = len(line) - 1
@ -54,7 +39,6 @@ def parse_file(checker, mfile, _):
break
if i != line_end:
checker.notify(
'trail_space',
f"{counter}:{i}",
f"trailing space at ({i} != {line_end})"
f"{counter}:{i+1}",
f"trailing space at ({i+1} != {line_end})",
)

View File

@ -330,13 +330,15 @@ class VxProjectMeta():
def _dump_section_env_conf(env_config, section):
for item in section.items():
key_name = f"VXSDK_{item[0][13:]}"
if item[0].find('VXSDK_COMMON') == 0:
key_name = f"VXSDK_{item[0][13:]}"
_key_update(env_config['public'], key_name, item[1])
_key_update(env_config['private'], key_name, item[1])
elif item[0].find('VXSDK_PUBLIC') == 0:
key_name = f"VXSDK_{item[0][13:]}"
_key_update(env_config['public'], key_name, item[1])
elif item[0].find('VXSDK_PRIVATE') == 0:
key_name = f"VXSDK_{item[0][14:]}"
_key_update(env_config['private'], key_name, item[1])
else:
log.warn(f"Unable to convert {item[0]}, skipped")

View File

@ -7,23 +7,23 @@ __all__ = [
'log'
]
__LOG_DEBUG = 7
__LOG_INFO = 6
__LOG_NOTICE = 5
__LOG_USER = 4
__LOG_WARN = 3
__LOG_ERR = 2
__LOG_CRIT = 1
__LOG_EMERG = 0
#---
# Internals
#---
_LOG_DEBUG = 7
_LOG_INFO = 6
_LOG_NOTICE = 5
_LOG_USER = 4
_LOG_WARN = 3
_LOG_ERR = 2
_LOG_CRIT = 1
_LOG_EMERG = 0
class _VxLogger():
def __init__(self, logfile=None):
self._logfile = logfile
self._level = __LOG_USER
self._level = _LOG_USER
self._indent = 0
#---
@ -33,7 +33,7 @@ class _VxLogger():
def _print(self, level, text, skip_indent, fileno):
if self._level < level:
return 0
if not skip_indent and self._level == __LOG_DEBUG and self._indent > 0:
if not skip_indent and self._level == _LOG_DEBUG and self._indent > 0:
text = ('>>> ' * self._indent) + text
print(text, file=fileno, end='', flush=True)
return len(text) + 1
@ -50,7 +50,7 @@ class _VxLogger():
@level.setter
def level(self, level):
""" <property> handle print level """
if level < __LOG_EMERG or level > __LOG_DEBUG:
if level < _LOG_EMERG or level > _LOG_DEBUG:
print(f"[log] level update to {level} is not possible, ignored")
return
self._level = level
@ -74,39 +74,49 @@ class _VxLogger():
def debug(self, text, end='\n', skip_indent=False):
""" print debug log """
return self._print(__LOG_DEBUG, text + end, skip_indent, sys.stdout)
return self._print(
_LOG_DEBUG, f"[DEBUG] {text}{end}", skip_indent, sys.stdout
)
def info(self, text, end='\n', skip_indent=False):
""" print info log """
return self._print(__LOG_INFO, text + end, skip_indent, sys.stdout)
return self._print(
_LOG_INFO, f"[INFO] {text}{end}", skip_indent, sys.stdout
)
def notice(self, text, end='\n', skip_indent=False):
""" print notice log """
return self._print(__LOG_NOTICE, text + end, skip_indent, sys.stdout)
return self._print(
_LOG_NOTICE, f"[NOTICE] {text}{end}", skip_indent, sys.stdout
)
def user(self, text, end='\n', skip_indent=False):
""" print user log """
return self._print(__LOG_USER, text + end, skip_indent, sys.stdout)
return self._print(_LOG_USER, f"{text}{end}", skip_indent, sys.stdout)
def warn(self, text, end='\n', skip_indent=False):
""" print warning log """
return self._print(__LOG_WARN, text + end, skip_indent, sys.stderr)
return self._print(
_LOG_WARN, f"[WARN] {text}{end}", skip_indent, sys.stderr
)
def error(self, text, end='\n', skip_indent=False):
""" print error log """
return self._print(__LOG_ERR, text + end, skip_indent, sys.stderr)
return self._print(
_LOG_ERR, f"[ERROR] {text}{end}", skip_indent, sys.stderr
)
def critical(self, text, end='\n', skip_indent=False):
""" print critical log """
return self._print(__LOG_CRIT, text + end, skip_indent, sys.stderr)
return self._print(
_LOG_CRIT, f"[CRITICAL] {text}{end}", skip_indent, sys.stderr
)
def emergency(self, text, end='\n', skip_indent=False):
""" print emergency log """
return self._print(__LOG_EMERG, text + end, skip_indent, sys.stderr)
return self._print(
_LOG_EMERG, f"[EMERGENCY] {text}{end}", skip_indent, sys.stderr
)
#---
# Public functions

View File

@ -1,8 +1,8 @@
"""
core.pkg - Provide package primitives (mainly for syntax sugar)
"""
from core.pkg.find import pkg_find
from core.pkg.clone import pkg_clone
from core.pkg.find import find_pkg
from core.pkg.clone import clone_pkg
__all__ = [
'find',
@ -34,7 +34,7 @@ def find(name, version=None, local=True, remote=True):
]
> None if error
"""
return pkg_find(name, version, local, remote)
return find_pkg(name, version, local, remote)
def clone(name, version=None, prefix=None, confirm=False, bare=False):
r""" Clone package with appropriate version
@ -57,4 +57,4 @@ def clone(name, version=None, prefix=None, confirm=False, bare=False):
@return
> the package path if successfully cloned, None otherwise
"""
return pkg_clone(name, version, prefix, confirm, bare)
return clone_pkg(name, version, prefix, confirm, bare)

View File

@ -5,7 +5,7 @@ import os
from core.pkg.backend import PKG_CORE_BACKEND_REMOTE, PKG_CORE_BACKEND_LOCAL
from core.pkg.version import VxVersion
from core.pkg.find import pkg_find
from core.pkg.find import find_pkg
from core.logger import log
__all__ = [
@ -74,9 +74,9 @@ def clone_pkg(name, version, prefix='', confirm=False, bare=False):
prefix = f"{os.getcwd()}/"
# try to find the package anywhere that the vxSDK allow
pkg_list = pkg_find(name, version, local=True, remote=False)
pkg_list = find_pkg(name, version, local=True, remote=False)
if not pkg_list:
pkg_list = pkg_find(name, version, local=False, remote=True)
pkg_list = find_pkg(name, version, local=False, remote=True)
if not pkg_list:
log.error("[pkg] pacakge find error")
return ''