Skip to content
Snippets Groups Projects
odoo_scripts_common 2.86 KiB
#!/bin/zsh
# vim: set shiftwidth=4 softtabstop=4:
# Common functions and env var

# assume we are called from the project home
project_home=$PWD
project_name=$(basename "$project_home")

if ! type python3 > /dev/null; then
    python="python2"
else
    python="python3"
fi

# handy colorized variables
esc=$(printf '\033')
RESET="${esc}[0m"
DEBUG="[${esc}[36mDEBUG${RESET}]"
INFO="[${esc}[34mINFO ${RESET}]"
WARN="[${esc}[33mWARN ${RESET}]"
FATAL="[${esc}[41m${esc}[37mFATAL${RESET}]"
GREEN="${esc}[32m"
OK="[${GREEN} OK${RESET}  ]"
RED="${esc}[31m"
KO="[${esc}[41m${esc}[37m KO  ${RESET}]"
WHITE_BOLD="${esc}[1;37m"

function read_configuration_key () {
    # Read a configuration key from any ini file
    # first argument is file path
    # second argument is section
    # third argument is the key to use
    # fourth argument is default value, if any
    # fifth argument is separator, default to a space
    file=$1
    section=$2
    configuration_key=$3
    default_value=$4
    separator=${5- }
    echo $($python -B -c "from six.moves import configparser ; c = configparser.ConfigParser() ; c.read('${file}') ; print('$separator'.join(c.get('$section', '$configuration_key').split()) if c.has_option('$section', '$configuration_key') else '$default_value')")
}

function read_odoo_scripts_configuration_key () {
    # Return configuration value from odoo_scripts configuration file
    # first argument is the key to use
    # second argument is default value, if any
    configuration_key=${1}
    default_value=$2
    read_configuration_key "${project_home}/setup.cfg" odoo_scripts $configuration_key $default_value
}

function read_odoo_scripts_expandable_configuration_key () {
  # return configuration value by appending any expanded values from defined super projects. glob key as appropriate, for module for example.
  # first argument is the key to use
  # second optional argument is the separator to use (default to comma)
  configuration_key=${1}
  separator=${2-,}
  # read expanded configurations, only done with projects in python 3
  if type python3 > /dev/null; then
    configuration_value=$(python3 -B -c "from odoo_scripts.config import Configuration ; c = Configuration('$project_home') ; print('$separator'.join(c.$configuration_key))")
  else
    # read only local values
    configuration_value=$($python -B -c "from six.moves import configparser ; import os ; c = configparser.ConfigParser() ; c.read(os.path.join('$project_home', 'setup.cfg')) ; print('$separator'.join(c.get('odoo_scripts', '$configuration_key').split() if c.has_option('odoo_scripts', '$configuration_key') else []))")
    if [[ $configuration_key == modules ]]; then
      configuration_value=$($python -B -c "import os;from glob import glob;print('$separator'.join([file for path in '${configuration_value}'.split('$separator') for file in glob(path) if os.path.isdir(file)]))")
    fi
  fi
  echo $configuration_value
}