Files
Odoo-Build/task
T
2021-12-13 08:54:43 +01:00

698 lines
18 KiB
Bash
Executable File

#!/bin/bash
cd "$(dirname "$0")"
function help() {
echo
echo "$1 <command> [options]"
echo
echo "commands:"
echo
column -t -s"|" ./task.md | tail -n +3
echo
}
# Set env vars
PATH="$PATH:./odoo"
GIT_TAG=0
GIT_BRANCH=$(git symbolic-ref --short -q HEAD)
export POSTGRES_USER=odoo
export POSTGRES_PASSWORD=odoo
export ODOO_REVISION=$GIT_BRANCH
export ODOO_LANGUAGE=de_CH
if [ -a ".env" ]; then
export $(cat .env | sed 's/#.*//g' | xargs)
fi
function get-module-version() {
if test -z "$1"
then
echo "\$1 is empty"
else
# get version of module
VERSION=$(grep 'version' "$1/__manifest__.py" | sed "s;';\";g" | sed "s/,//g" )
VERSION=$(echo "{ $VERSION }" | jq .version | sed 's/"//g' | sed 's/null//g')
# set default version
[ -z "$VERSION" ] && VERSION=0.0
# check if oca version or enterprise version
if echo "$VERSION" | grep -qe "^[0-9]\."
then
VERSION="$GIT_BRANCH.$VERSION.$GIT_TAG"
fi
echo $VERSION
fi
}
function init-venv() {
if [ ! -d "venv$GIT_BRANCH" ]; then
python3 -m venv "venv$GIT_BRANCH"
fi
}
function activate-venv() {
echo "Source virtualenv venv$GIT_BRANCH."
source "venv$GIT_BRANCH/bin/activate"
}
function locale() {
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
}
function load-env() {
export $(cat .env.$1 | sed 's/#.*//g' | xargs)
}
function disable-auto-install() {
echo "Disable auto install for all enterprise modules."
find ./enterprise -type f -name '__manifest__.py' -exec sed -i'' $'s/\'auto_install\': True,/\'auto_install\': False,/g' {} +
}
function install-odoo-scripts() {
curl -L https://raw.githubusercontent.com/mint-system/ansible-playbooks/master/roles/odoo-scripts/files/install | sh
}
function install-native() {
echo "Install wkhtmltopdf"
sudo apt install wkhtmltopdf
echo "Install xmllint"
sudo apt install libxml2-utils
init-venv
activate-venv
echo "Install pylint odoo"
pip install --upgrade --pre pylint-odoo
echo "Install odoo dependencies"
pip install setuptools wheel watchdog phonenumbers
pip install -r odoo/requirements.txt
echo "Install oca dev tools"
pip install pre-commit pyyaml
echo "Install graphql dependencies"
pip install graphene graphql-server-core
echo "Install openupgrade functions"
pip install git+https://github.com/OCA/openupgradelib.git@master#egg=openupgradelib
}
function start() {
if test -z "$1"; then
echo "Open http://localhost:8069 url in your browser."
docker-compose up -d
fi
if [[ "$1" =~ "db" ]]; then
docker-compose up -d db pgadmin
echo "Open http://localhost:8000 url in your browser."
fi
if [[ "$1" =~ "mail" ]]; then
docker-compose up -d mail
echo "Open http://localhost url in your browser."
fi
if [[ "$1" =~ "native" ]]; then
locale
activate-venv
echo "Open http://localhost:8069 url in your browser."
odoo-bin --config 'config/odoo-native.conf' --dev=all
fi
}
function restart() {
if test -z "$1"; then
docker-compose restart
fi
if [[ "db|mail|odoo" =~ "$1" ]]; then
docker-compose restart $1
fi
}
function stop() {
if test -z "$1"; then
docker-compose stop
fi
docker-compose stop $1
}
function start-shell() {
DATABASE=$1
DATABASE=${DATABASE:="odoo"}
locale
activate-venv
odoo-bin shell -d $DATABASE --config 'config/odoo-native.conf' --no-http
}
function start-psql() {
DATABASE=$1
DATABASE=${DATABASE:="odoo"}
docker exec -it db psql postgres://odoo:odoo@localhost:5432/$DATABASE
}
function init-db() {
DATABASE=$1
DATABASE=${DATABASE:="odoo"}
locale
activate-venv
echo "Make sure only one odoo process is running when initalizing the database."
# odoo-bin --init base -d $DATABASE --db_host=localhost --db_password=odoo --db_user=odoo --no-http
odoo-bin --init base -d $DATABASE --config 'config/odoo-native.conf' --stop-after-init --no-http --load-language $ODOO_LANGUAGE
}
function odoo-cloc() {
DATABASE=$1
DATABASE=${DATABASE:="odoo"}
locale
activate-venv
odoo-bin cloc -d $DATABASE --config 'config/odoo-native.conf' --stop-after-init --no-http
}
function lint-module() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
activate-venv
echo "Run pylint in $1"
pylint --load-plugins=pylint_odoo -d all -e odoolint $1
cd $1
echo "Run pre-commit in $1"
pre-commit run --files ./*
}
function update-module() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
if test -z "$2"; then echo "\$2 is empty"; exit; fi
MODULE_NAME=$(basename $2)
locale
activate-venv
echo "Updating module $MODULE_NAME on $1 ..."
odoo-bin -d $1 -u $MODULE_NAME --config 'config/odoo-native.conf' --stop-after-init --no-http
}
function install-module() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
if test -z "$2"; then echo "\$2 is empty"; exit; fi
MODULE_NAME=$(basename $2)
locale
activate-venv
echo "Installing module $MODULE_NAME on $1 ..."
odoo-bin -d $1 -i $MODULE_NAME --config 'config/odoo-native.conf' --stop-after-init --no-http
}
function test-module() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
if test -z "$2"; then echo "\$2 is empty"; exit; fi
MODULE_NAME=$(basename $2)
locale
activate-venv
odoo-bin -d $1 -i $MODULE_NAME --config 'config/odoo-native.conf' --stop-after-init --log-level=test --test-enable
}
function remove-module() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
if test -z "$2"; then echo "\$2 is empty"; exit; fi
locale
activate-venv
echo "self.env['ir.module.module'].search([('name', '=', '$2')]).button_immediate_uninstall()" |
odoo-bin shell -d $1 --config 'config/odoo-native.conf' --no-http
}
function create-module() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
MODULE_NAME=$(basename "$1")
PARENT_DIR=$(dirname "$1")
locale
activate-venv
echo "Scaffolding module $MODULE_NAME in $PARENT_DIR ..."
odoo-bin scaffold "$MODULE_NAME" "$PARENT_DIR" -t template/ms_custom_module
}
function drop-db() {
DATABASE=$1
DATABASE=${DATABASE:="odoo"}
docker exec db psql postgres://odoo:odoo@localhost:5432/postgres -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '$DATABASE';"
docker exec db psql postgres://odoo:odoo@localhost:5432/postgres -c "DROP DATABASE \"$DATABASE\";"
}
function send-support-mail() {
echo "Send mail to support@localhost using curl."
cat >tmp/email.txt <<EOL
From: Lara User <user@yourcompany.example.com>
To: Odoo Support <support@localhost>
Subject: Support Request
Dear Odoo Support,
I request your help
EOL
curl smtp://localhost --mail-from admin@yourcompany.example.com --mail-rcpt support@localhost --upload-file tmp/email.txt
}
function clear-assets() {
DATABASE=$1
DATABASE=${DATABASE:="odoo"}
echo "Delete these assets:"
docker exec db psql postgres://odoo:odoo@localhost/$DATABASE -c "select id,name from ir_attachment where res_model='ir.ui.view' and name like '%assets_%';"
docker exec db psql postgres://odoo:odoo@localhost/$DATABASE -c "delete from ir_attachment where res_model='ir.ui.view' and name like '%assets_%';"
echo "Restart your docker service."
}
function convert-database() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
unzip "tmp/$1.zip" -d "./tmp/$1"
sed -i'' 's/AS integer//g' "./tmp/$1/dump.sql"
grep -i 'as integer' "./tmp/$1/dump.sql"
cd "tmp/$1" ; zip -r "../${1}2.zip" . ; cd ../..
rm -r "tmp/$1"
echo "Converted database file $1 to ${1}2"
}
function checkout() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
echo "Checkout branch"
# Checkout branch
git checkout $1
echo "Init submodules"
# Init submodules
git submodule update --init
# Reset enterprise
echo "Reset enterprise submodule"
cd enterprise
git reset --hard
cd ..
# Checkout all submodules
echo "Checkout branch on submodules"
git submodule foreach git checkout $1
# Sync files from last branch
echo "Sync checkout files"
git checkout $GIT_BRANCH config/checkout_files
git rm -r $(tr '\n' ' ' < ./config/remove_files)
git checkout $GIT_BRANCH $(tr '\n' ' ' < ./config/checkout_files)
}
function release-module() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
VERSION=$(get-module-version $1)
cd $1
echo "Tag with v$VERSION ..."
git tag -a v$VERSION -m "v$VERSION"
echo "Create release $VERSION ..."
gh release create "v$VERSION" -t "Release $VERSION"
}
function zip-module() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
# Get version and module name
VERSION=$(get-module-version $1)
MODULE=$(basename "$1")
# copy module to tmp folder
echo "Copy module $MODULE version $VERSION to tmp folder"
MODULE_NAME="$MODULE-$VERSION"
MODULE_PATH="tmp/$MODULE_NAME"
mkdir -p "$MODULE_PATH"
cp -r "$1/." "$MODULE_PATH"
# configure and zip module
echo "Remove auto install option"
sed -i'' -e "s/'auto_install': True,/'auto_install': False,/" "$MODULE_PATH/__manifest__.py"
echo "Remove pycache folders"
find $MODULE_PATH | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf
echo "Zip module $MODULE"
cd tmp
zip -q -r $MODULE_NAME.zip $MODULE_NAME
cd ..
}
function clear-filestore() {
echo "Remove directory ~/.local/share/Odoo/filestore/$1"
rm -rf ~/.local/share/Odoo/filestore/$1
}
function init-odoo-env() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
echo "Init env file: .env.$1"
echo "#ODOO_URL=" > .env.$1
echo "ODOO_DATABASE=" >> .env.$1
echo "ODOO_USERNAME=" >> .env.$1
echo "ODOO_PASSWORD=" >> .env.$1
cat .env.$1
}
function disable-mailserver() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
activate-venv
load-env $1
scripts/odooctl --model 'ir.mail_server' --field 'active' --value False
scripts/odooctl --model 'fetchmail.server' --field 'active' --value False
}
function change-uuid() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
activate-venv
load-env $1
UUID=$(uuidgen)
echo "Set uuid $UUID for $1."
scripts/odooctl --model 'ir.config_parameter' --domain "['key', '=', 'database.uuid']" --field 'value' --value "'$UUID'"
}
function update-snippet() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
if test -z "$2"; then echo "\$2 is empty"; exit; fi
# Generate view name from filename
FILE=$(basename $2)
NAME="mint_system.${FILE%.xml}"
activate-venv
load-env $1
scripts/odooctl --model 'ir.ui.view' --domain "['name', '=', '$NAME']" --field 'arch_base' --value "'''$(cat $2)'''"
}
function install-snippet() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
if test -z "$2"; then echo "\$2 is empty"; exit; fi
# Generate view name from filename
FILE=$(basename $2)
NAME="mint_system.${FILE%.xml}"
INHERIT_ID=$(echo 'cat //data/@inherit_id' | xmllint --shell $2 | awk -F'[="]' '!/>/{print $(NF-1)}')
PRIORITY=$(echo 'cat //data/@priority' | xmllint --shell $2 | awk -F'[="]' '!/>/{print $(NF-1)}')
MODULE_NAME=$(echo $INHERIT_ID | cut -d. -f 1 )
XML_ID=$(echo $INHERIT_ID | cut -d. -f 2 )
activate-venv
load-env $1
echo "Search for view with name $MODULE_NAME.$XML_ID"
INHERIT_ID=$(scripts/odooctl --method 'get_id' --model 'ir.model.data' --value "['$MODULE_NAME','$XML_ID']")
echo "Found view id $INHERIT_ID"
scripts/odooctl --method 'create' --model 'ir.ui.view' --value "{ 'name': '$NAME', 'priority': $PRIORITY, 'inherit_id': $INHERIT_ID, 'arch_base': '''$(cat $2)''' }"
}
function update-app-list() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
locale
activate-venv
echo "env['ir.module.module'].update_list()" | odoo-bin shell -d $1 --config 'config/odoo-native.conf' --no-http
}
function init-next-env() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
echo "Init env file: .env.$1"
echo "NEXTCLOUD_URL=" > .env.$1
echo "NEXTCLOUD_USERNAME=" >> .env.$1
echo "NEXTCLOUD_PASSWORD=" >> .env.$1
cat .env.$1
}
function upload-module() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
if test -z "$2"; then echo "\$1 is empty"; exit; fi
zip-module $2
# Get path to zip file and upload url
FILE_PATH="tmp/$MODULE_NAME.zip"
pwd
load-env $1
NEXTCLOUD_UPLOAD_URL="$NEXTCLOUD_URL/remote.php/dav/files/$NEXTCLOUD_USERNAME/Odoo-Apps/"
# Upload zip file
curl -u "$NEXTCLOUD_USERNAME:$NEXTCLOUD_PASSWORD" -T "$FILE_PATH" "$NEXTCLOUD_UPLOAD_URL"
echo "File $FILE_PATH uploaded to $NEXTCLOUD_UPLOAD_URL."
}
function save-config() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
locale
activate-venv
odoo-bin -s -d $1 --config 'config/odoo-native.conf' --stop-after-init --no-http
}
function remove-submodule() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
echo "Move submodule to tmp folder"
mkdir -p tmp/$1
mv $1 tmp/$1
echo "Deinit submodule"
git submodule deinit -f $1
echo "Remove submodule from .git folder"
rm -rf .git/modules/$1
echo "Remove submodule from work tree"
git rm -f $1
echo "Remove $1 submodule from .gitmodules files manually."
}
function update-config() {
ADDONS="$(tr '\n' ',' < ./config/addons_path)"
sed -i'' -e "s|addons_path.*|addons_path = odoo/addons,$ADDONS|g" "./config/odoo-native.conf"
ADDONS="$(echo "$ADDONS" | sed 's|,|,/mnt/|g')"
sed -i'' -e "s|addons_path.*|addons_path = /mnt/$ADDONS|g" "./config/odoo.conf"
echo "Config updated."
}
function git-list-submodules() {
git config --file .gitmodules --get-regexp url | sed -e 's/submodule.//g' -e 's/.url//g' | awk '{ print $1 "," $2}'
}
function patch-database() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
if test -z "$2"; then echo "\$1 is empty"; exit; fi
# Generate view name from filename
FILENAME=$(basename $2)
echo "Apply patch $FILENAME to Database $1."
cat $2 | docker exec -i db psql postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost/$1
echo "Patching database succeeded"
}
function update-docs() {
echo "Update snippets.md file."
echo "# Snippets" > snippets.md
PATH_URL="https://github.com/Mint-System/Odoo-Development/tree/14.0"
FILES="snippets/*.xml"
# Group by modules
MODULES=$(ls $FILES | xargs -n 1 basename | cut -d. -f1 | uniq)
for MODULE in $MODULES; do
TITLE="${MODULE^}" # Uppercase first letter
TITLE=$(echo $TITLE | sed 's/_/ /g') # Replace underline with space
TITLE=$(echo $TITLE | sed -r 's/\<./\U&/g') # Uppercase first letter after space
echo "## $TITLE" >> snippets.md
for FILE in $FILES; do
FILENAME=$(basename $FILE)
SNIPPET_MODULE=$(echo $FILENAME | cut -d. -f1)
REPORT=$(echo $FILENAME | cut -d. -f2)
EDIT=$(echo $FILENAME | cut -d. -f3)
# Group by modules
if [[ "$MODULE" = "$SNIPPET_MODULE" ]]; then
echo "### $(echo $EDIT | sed 's/_/ /g') " >> snippets.md
echo "Report: $REPORT " >> snippets.md
echo "ID: \`mint_system.$SNIPPET_MODULE.$REPORT.$EDIT\` " >> snippets.md
echo "Link: [$FILE]($PATH_URL/$FILE)" >> snippets.md
echo "" >> snippets.md
fi
done
done
}
case "$1" in
disable-auto-install)
disable-auto-install
;;
save-config)
save-config $2
;;
checkout)
checkout $2
;;
list)
docker-compose config --services; echo "native"
;;
ps)
docker ps --format 'table {{.Names}}\t{{.Image}}\t{{(.Label "description")}}\t{{.Status}}'
;;
start)
start $2
;;
restart)
restart $2
;;
stop)
stop $2
;;
kill)
docker-compose down -v
;;
logs)
docker logs odoo -f
;;
init-venv)
init-venv
;;
install-odoo-scripts)
install-odoo-scripts
;;
install-native)
install-native
;;
init-db)
init-db $2
;;
start-shell)
start-shell $2
;;
start-psql)
start-psql $2
;;
update-module)
update-module $2 $3
;;
test-module)
test-module $2 $3
;;
create-module)
create-module $2
;;
drop-db)
drop-db $2
;;
send-support-mail)
send-support-mail
;;
clear-assets)
clear-assets $2
;;
odoo-cloc)
odoo-cloc $2
;;
convert-database)
convert-database $2
;;
install-module)
install-module $2 $3
;;
remove-module)
remove-module $2 $3
;;
remove-submodule)
remove-submodule $2
;;
release-module)
release-module $2
;;
lint-module)
lint-module $2
;;
zip-module)
zip-module $2
;;
clear-filestore)
clear-filestore $2
;;
init-odoo-env)
init-odoo-env $2
;;
disable-mailserver)
disable-mailserver $2
;;
change-uuid)
change-uuid $2
;;
update-config)
update-config
;;
git-list-submodules)
git-list-submodules
;;
update-snippet)
update-snippet $2 $3
;;
install-snippet)
install-snippet $2 $3
;;
update-app-list)
update-app-list $2
;;
init-next-env)
init-next-env $2
;;
upload-module)
upload-module $2 $3
;;
patch-database)
patch-database $2 $3
;;
git-pull-submodules)
# Pull submodule if update config is not set
git submodule foreach '
update=$(git config -f $toplevel/.gitmodules submodule.$name.update)
if [ -z $update ]; then
git pull
fi;
' | tee git-pull-submodules.log
;;
update-docs)
update-docs
;;
source)
activate-venv
;;
*)
help task
exit 1
;;
esac