Skip to content
Snippets Groups Projects
common 3.63 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}]"

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
  # first argument is the key to use
  # second argument is the directory to use, defaults to project home
  # third optional argument is the separator to use (default to comma)
  # fourth optional argument is to indicate if the expanded project should be used as prefix for the values (default to not use, use True to use), adding a /
  # fifth optional argument is a prefix to use (default to none)
  configuration_key=${1}
  base_dir="${2-${project_home}}"
  separator=${3-,}
  prefix_with_expand=${4-False}
  prefix=$5
  # read expanded configurations
  expanded_project_pathes=($($python -B -c "from six.moves import configparser ; c = configparser.ConfigParser() ; c.read('$base_dir/setup.cfg') ; print(c.get('odoo_scripts', '$configuration_key.expand') if c.has_option('odoo_scripts', '$configuration_key.expand') else '')"))
  for expanded_project_path in $expanded_project_pathes; do
    that_project_prefix=$prefix
    if [[ "$prefix_with_expand" == True ]];
    then
      that_project_prefix=${that_project_prefix}${expanded_project_path}/
    fi
    expanded_configuration_value=$(read_odoo_scripts_expandable_configuration_key $configuration_key "$base_dir/$expanded_project_path" "$separator" $prefix_with_expand "$that_project_prefix")
    if [[ -n $configuration_value ]]; then
      configuration_value+=$separator
    fi
    configuration_value+=$expanded_configuration_value
  done
  # then add local values
  this_project_configuration_value+=$($python -B -c "from six.moves import configparser ; c = configparser.ConfigParser() ; c.read('$base_dir/setup.cfg') ; print('$separator'.join(['$prefix'+a for a in c.get('odoo_scripts', '$configuration_key').split()] if c.has_option('odoo_scripts', '$configuration_key') else []))")
  if [[ -n $this_project_configuration_value && -n $configuration_value ]]; then
      configuration_value+=$separator
  fi
  configuration_value+=$this_project_configuration_value
  echo $configuration_value
}