Compare commits

..

No commits in common. "76070daa431e3cf2ac7c8a3b864db2bb0951b3df" and "660140c0847d32a7c8112945594b1752fd793800" have entirely different histories.

2 changed files with 184 additions and 211 deletions

319
cd.sh
View file

@ -1,11 +1,11 @@
#! /usr/bin/env sh #! /usr/bin/env sh
# defaults # defaults
[ -n "${CD_DNS}" ] || CD_DNS="\ [ "${CD_DNS}" ] || CD_DNS="\
9.9.9.9 \ 9.9.9.9 \
" "
[ -n "${CD_GIT_CHILD}" ] || CD_GIT_CHILD="cd" [ "${CD_GIT_CHILD}" ] || CD_GIT_CHILD="cd"
[ -n "${CD_GIT_PARENT}" ] || CD_GIT_PARENT="rwx" [ "${CD_GIT_PARENT}" ] || CD_GIT_PARENT="rwx"
# main # main
cd_main () { cd_main () {
@ -40,9 +40,6 @@ cd_main () {
cd_set_environment_variables () { cd_set_environment_variables () {
cd_step "Set environment variables" cd_step "Set environment variables"
# set path
CD_PATH="$(realpath "${0}")"
cd_echo "CD_PATH"
# set operating system id # set operating system id
CD_OS_ID="$(cd_grep_os ID)" CD_OS_ID="$(cd_grep_os ID)"
case "${CD_OS_ID}" in case "${CD_OS_ID}" in
@ -68,7 +65,6 @@ cd_set_environment_variables () {
"${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}") "${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}")
CD_OS_VERSION="$(cd_grep_os VERSION_CODENAME)" CD_OS_VERSION="$(cd_grep_os VERSION_CODENAME)"
;; ;;
*)
esac esac
# check operating system version # check operating system version
case "${CD_OS_ID}" in case "${CD_OS_ID}" in
@ -108,9 +104,7 @@ cd_set_environment_variables () {
*) cd_error_os "CD_OS_VERSION" ;; *) cd_error_os "CD_OS_VERSION" ;;
esac esac
;; ;;
*)
esac esac
cd_split
cd_echo "CD_OS_ID" "CD_OS_VERSION" cd_echo "CD_OS_ID" "CD_OS_VERSION"
# universal # universal
CD_DNS_FILE="/etc/resolv.conf" CD_DNS_FILE="/etc/resolv.conf"
@ -139,7 +133,6 @@ cd_set_environment_variables () {
CD_CA_ROOT="/usr/local/share/ca-certificates" CD_CA_ROOT="/usr/local/share/ca-certificates"
CD_CMD_CA="update-ca-certificates" CD_CMD_CA="update-ca-certificates"
;; ;;
*)
esac esac
cd_split cd_split
cd_echo "CD_CA_ROOT" "CD_CMD_CA" cd_echo "CD_CA_ROOT" "CD_CMD_CA"
@ -157,7 +150,6 @@ cd_set_environment_variables () {
"${CD_OS_ARCH}") "${CD_OS_ARCH}")
CD_PM="${CD_PM_PACMAN}" CD_PM="${CD_PM_PACMAN}"
;; ;;
*)
esac esac
cd_split cd_split
cd_echo "CD_PM" cd_echo "CD_PM"
@ -231,7 +223,6 @@ Dir::Etc::SourceParts \"\";
Acquire::https::Verify-Peer False; Acquire::https::Verify-Peer False;
" "
;; ;;
*)
esac esac
cd_split cd_split
cd_echo "CD_PM_CLEAN" \ cd_echo "CD_PM_CLEAN" \
@ -261,36 +252,34 @@ Acquire::https::Verify-Peer False;
"${CD_OS_UBUNTU}") "${CD_OS_UBUNTU}")
CD_URL_DEFAULT="http://archive.ubuntu.com/ubuntu" CD_URL_DEFAULT="http://archive.ubuntu.com/ubuntu"
;; ;;
*)
esac esac
CD_URL_CHOSEN="${CD_URL_DEFAULT}" CD_URL_CHOSEN="${CD_URL_DEFAULT}"
case "${CD_OS_ID}" in case "${CD_OS_ID}" in
"${CD_OS_ALMA}") "${CD_OS_ALMA}")
[ -n "${CD_URL_ALMA}" ] && CD_URL_CHOSEN="${CD_URL_ALMA}" [ "${CD_URL_ALMA}" ] && CD_URL_CHOSEN="${CD_URL_ALMA}"
;; ;;
"${CD_OS_ALPINE}") "${CD_OS_ALPINE}")
[ -n "${CD_URL_ALPINE}" ] && CD_URL_CHOSEN="${CD_URL_ALPINE}" [ "${CD_URL_ALPINE}" ] && CD_URL_CHOSEN="${CD_URL_ALPINE}"
;; ;;
"${CD_OS_ARCH}") "${CD_OS_ARCH}")
[ -n "${CD_URL_ARCH}" ] && CD_URL_CHOSEN="${CD_URL_ARCH}" [ "${CD_URL_ARCH}" ] && CD_URL_CHOSEN="${CD_URL_ARCH}"
;; ;;
"${CD_OS_DEBIAN}") "${CD_OS_DEBIAN}")
[ -n "${CD_URL_DEBIAN}" ] && CD_URL_CHOSEN="${CD_URL_DEBIAN}" \ [ "${CD_URL_DEBIAN}" ] && CD_URL_CHOSEN="${CD_URL_DEBIAN}" \
|| CD_URL_CHOSEN="https://deb.debian.org/debian" || CD_URL_CHOSEN="https://deb.debian.org/debian"
;; ;;
"${CD_OS_FEDORA}") "${CD_OS_FEDORA}")
[ -n "${CD_URL_FEDORA}" ] && CD_URL_CHOSEN="${CD_URL_FEDORA}" \ [ "${CD_URL_FEDORA}" ] && CD_URL_CHOSEN="${CD_URL_FEDORA}" \
|| CD_URL_CHOSEN="https://rpmfind.net/linux/fedora/linux/releases" || CD_URL_CHOSEN="https://rpmfind.net/linux/fedora/linux/releases"
;; ;;
"${CD_OS_ROCKY}") "${CD_OS_ROCKY}")
[ -n "${CD_URL_ROCKY}" ] && CD_URL_CHOSEN="${CD_URL_ROCKY}" \ [ "${CD_URL_ROCKY}" ] && CD_URL_CHOSEN="${CD_URL_ROCKY}" \
|| CD_URL_CHOSEN="https://dl.rockylinux.org/\$contentdir" || CD_URL_CHOSEN="https://dl.rockylinux.org/\$contentdir"
;; ;;
"${CD_OS_UBUNTU}") "${CD_OS_UBUNTU}")
[ -n "${CD_URL_UBUNTU}" ] && CD_URL_CHOSEN="${CD_URL_UBUNTU}" \ [ "${CD_URL_UBUNTU}" ] && CD_URL_CHOSEN="${CD_URL_UBUNTU}" \
|| CD_URL_CHOSEN="https://ubuntu.mirrors.ovh.net/ubuntu" || CD_URL_CHOSEN="https://ubuntu.mirrors.ovh.net/ubuntu"
;; ;;
*)
esac esac
cd_split cd_split
cd_echo "CD_URL_DEFAULT" "CD_URL_CHOSEN" cd_echo "CD_URL_DEFAULT" "CD_URL_CHOSEN"
@ -312,7 +301,6 @@ Acquire::https::Verify-Peer False;
case "${CD_OS_VERSION}" in case "${CD_OS_VERSION}" in
"bookworm") CD_PYTHON_COMMAND="python3.11" ;; "bookworm") CD_PYTHON_COMMAND="python3.11" ;;
"bullseye") CD_PYTHON_COMMAND="python3.9" ;; "bullseye") CD_PYTHON_COMMAND="python3.9" ;;
*)
esac esac
CD_PYTHON_PACKAGE="python3" CD_PYTHON_PACKAGE="python3"
;; ;;
@ -324,11 +312,9 @@ Acquire::https::Verify-Peer False;
case "${CD_OS_VERSION}" in case "${CD_OS_VERSION}" in
"noble") CD_PYTHON_COMMAND="python3.12" ;; "noble") CD_PYTHON_COMMAND="python3.12" ;;
"jammy") CD_PYTHON_COMMAND="python3.10" ;; "jammy") CD_PYTHON_COMMAND="python3.10" ;;
*)
esac esac
CD_PYTHON_PACKAGE="python3" CD_PYTHON_PACKAGE="python3"
;; ;;
*)
esac esac
# set python packages # set python packages
case "${CD_OS_ID}" in case "${CD_OS_ID}" in
@ -341,20 +327,19 @@ Acquire::https::Verify-Peer False;
"${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}") "${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}")
CD_PYTHON_PACKAGES="/usr/lib/${CD_PYTHON_ALIAS}/dist-packages" CD_PYTHON_PACKAGES="/usr/lib/${CD_PYTHON_ALIAS}/dist-packages"
;; ;;
*)
esac esac
cd_split cd_split
cd_echo "CD_PYTHON_COMMAND" "CD_PYTHON_PACKAGE" "CD_PYTHON_PACKAGES" cd_echo "CD_PYTHON_COMMAND" "CD_PYTHON_PACKAGE" "CD_PYTHON_PACKAGES"
# variables # variables
[ -n "${CD_CA_1}" ] && CD_CA=true [ "${CD_CA_1}" ] && CD_CA=true
# continuous integration platform # continuous integration platform
if [ -n "${GITHUB_ACTIONS}" ] ; then if [ "${GITHUB_ACTIONS}" ] ; then
# github → gitea → forgejo # github → gitea → forgejo
CD_SERVER_URL="${GITHUB_SERVER_URL}" CD_SERVER_URL="${GITHUB_SERVER_URL}"
CD_PROJECTS_GROUP="$(dirname "${GITHUB_REPOSITORY}")" CD_PROJECTS_GROUP="$(dirname "${GITHUB_REPOSITORY}")"
CD_PROJECT_NAME="$(basename "${GITHUB_REPOSITORY}")" CD_PROJECT_NAME="$(basename "${GITHUB_REPOSITORY}")"
CD_PROJECT_BRANCH="${GITHUB_REF_NAME}" CD_PROJECT_BRANCH="${GITHUB_REF_NAME}"
elif [ -n "${GITLAB_CI}" ] ; then elif [ "${GITLAB_CI}" ] ; then
# gitlab # gitlab
CD_SERVER_URL="${CI_SERVER_URL}" CD_SERVER_URL="${CI_SERVER_URL}"
CD_PROJECTS_GROUP="$(dirname "${CI_PROJECT_PATH}")" CD_PROJECTS_GROUP="$(dirname "${CI_PROJECT_PATH}")"
@ -364,12 +349,13 @@ Acquire::https::Verify-Peer False;
# unsupported # unsupported
cd_error_ci "ø" cd_error_ci "ø"
fi fi
[ -n "${CD_SERVER_URL}" ] || cd_error_ci "CD_SERVER_URL" [ "${CD_SERVER_URL}" ] || cd_error_ci "CD_SERVER_URL"
[ -n "${CD_PROJECTS_GROUP}" ] || cd_error_ci "CD_PROJECTS_GROUP" [ "${CD_PROJECTS_GROUP}" ] || cd_error_ci "CD_PROJECTS_GROUP"
[ -n "${CD_PROJECT_NAME}" ] || cd_error_ci "CD_PROJECT_NAME" [ "${CD_PROJECT_NAME}" ] || cd_error_ci "CD_PROJECT_NAME"
[ -n "${CD_PROJECT_BRANCH}" ] || cd_error_ci "CD_PROJECT_BRANCH" [ "${CD_PROJECT_BRANCH}" ] || cd_error_ci "CD_PROJECT_BRANCH"
# #
CD_PROJECTS_URL="${CD_SERVER_URL}/${CD_PROJECTS_GROUP}" CD_PROJECTS_URL="${CD_SERVER_URL}/${CD_PROJECTS_GROUP}"
CD_PROJECT_URL="${CD_PROJECTS_URL}/${CD_PROJECT_NAME}"
# #
cd_split cd_split
cd_echo "CD_CA" cd_echo "CD_CA"
@ -377,42 +363,40 @@ Acquire::https::Verify-Peer False;
cd_echo "CD_SERVER_URL" \ cd_echo "CD_SERVER_URL" \
"CD_PROJECTS_GROUP" "CD_PROJECT_NAME" "CD_PROJECT_BRANCH" "CD_PROJECTS_GROUP" "CD_PROJECT_NAME" "CD_PROJECT_BRANCH"
cd_split cd_split
cd_echo "CD_PROJECTS_URL" cd_echo "CD_PROJECTS_URL" "CD_PROJECT_URL"
# TODO move to Python # TODO move to Python
case "${CD_PM}" in case "${CD_PM}" in
"${CD_PM_APK}"|"${CD_PM_APT}") CD_PKG_SSH="openssh-client" ;; "${CD_PM_APK}"|"${CD_PM_APT}") CD_PKG_SSH="openssh-client" ;;
"${CD_PM_DNF}") CD_PKG_SSH="openssh-clients" ;; "${CD_PM_DNF}") CD_PKG_SSH="openssh-clients" ;;
"${CD_PM_PACMAN}") CD_PKG_SSH="openssh" ;; "${CD_PM_PACMAN}") CD_PKG_SSH="openssh" ;;
*)
esac esac
} }
cd_set_packages_repositories () { cd_set_packages_repositories () {
local expression
local file
cd_step "Set packages repositories" cd_step "Set packages repositories"
case "${CD_OS_ID}" in case "${CD_OS_ID}" in
"${CD_OS_ALMA}") "${CD_OS_ALMA}")
case "${CD_OS_VERSION}" in case "${CD_OS_VERSION}" in
"8") \ "8") file="/etc/yum.repos.d/almalinux.repo" ;;
cd_set_packages_repositories__file="/etc/yum.repos.d/almalinux.repo" ;; "9") file="/etc/yum.repos.d/almalinux-baseos.repo" ;;
"9") \
cd_set_packages_repositories__file="/etc/yum.repos.d/almalinux-baseos.repo" ;;
*)
esac esac
cd_sed "${cd_set_packages_repositories__file}" \ cd_sed "${file}" \
"|^mirrorlist|# mirrorlist|" \ "|^mirrorlist|# mirrorlist|" \
"|${CD_URL_DEFAULT}|${CD_URL_CHOSEN}|" \ "|${CD_URL_DEFAULT}|${CD_URL_CHOSEN}|" \
"|^# baseurl|baseurl|" "|^# baseurl|baseurl|"
;; ;;
"${CD_OS_ALPINE}") "${CD_OS_ALPINE}")
cd_set_packages_repositories__file="/etc/apk/repositories" file="/etc/apk/repositories"
cd_write "${cd_set_packages_repositories__file}" "\ cd_write "${file}" "\
${CD_URL_CHOSEN}/v${CD_OS_VERSION}/main ${CD_URL_CHOSEN}/v${CD_OS_VERSION}/main
${CD_URL_CHOSEN}/v${CD_OS_VERSION}/community ${CD_URL_CHOSEN}/v${CD_OS_VERSION}/community
" "
;; ;;
"${CD_OS_DEBIAN}") "${CD_OS_DEBIAN}")
cd_set_packages_repositories__file="/etc/apt/sources.list" file="/etc/apt/sources.list"
cd_write "${cd_set_packages_repositories__file}" "\ cd_write "${file}" "\
deb ${CD_URL_CHOSEN} ${CD_OS_VERSION} main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION} main
deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-backports main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-backports main
deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-updates main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-updates main
@ -421,27 +405,23 @@ deb ${CD_URL_CHOSEN}-security ${CD_OS_VERSION}-security main
;; ;;
"${CD_OS_ROCKY}") "${CD_OS_ROCKY}")
case "${CD_OS_VERSION}" in case "${CD_OS_VERSION}" in
"8") \ "8") file="/etc/yum.repos.d/Rocky-BaseOS.repo" ;;
cd_set_packages_repositories__file="/etc/yum.repos.d/Rocky-BaseOS.repo" ;; "9") file="/etc/yum.repos.d/rocky.repo" ;;
"9") \
cd_set_packages_repositories__file="/etc/yum.repos.d/rocky.repo" ;;
*)
esac esac
cd_sed "${cd_set_packages_repositories__file}" \ cd_sed "${file}" \
"|^mirrorlist|# mirrorlist|" \ "|^mirrorlist|# mirrorlist|" \
"|${CD_URL_DEFAULT}|${CD_URL_CHOSEN}|" \ "|${CD_URL_DEFAULT}|${CD_URL_CHOSEN}|" \
"|^#baseurl|baseurl|" "|^#baseurl|baseurl|"
;; ;;
"${CD_OS_UBUNTU}") "${CD_OS_UBUNTU}")
cd_set_packages_repositories__file="/etc/apt/sources.list" file="/etc/apt/sources.list"
cd_write "${cd_set_packages_repositories__file}" "\ cd_write "${file}" "\
deb ${CD_URL_CHOSEN} ${CD_OS_VERSION} main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION} main
deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-backports main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-backports main
deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-updates main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-updates main
deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-security main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-security main
" "
;; ;;
*)
esac esac
} }
@ -452,7 +432,6 @@ cd_set_packages_configuration () {
"${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}") "${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}")
export DEBIAN_FRONTEND="noninteractive" export DEBIAN_FRONTEND="noninteractive"
;; ;;
*)
esac esac
} }
@ -460,12 +439,11 @@ cd_set_packages_configuration () {
cd_list_working_directory () { cd_list_working_directory () {
cd_step "List working directory" cd_step "List working directory"
cd_list_working_directory__path="$(realpath .)" cd_ls "$(realpath .)"
cd_ls "${cd_list_working_directory__path}"
} }
cd_set_https_verification_off () { cd_set_https_verification_off () {
if [ -n "${CD_CA}" ] || [ "${CD_PM}" = "${CD_PM_APT}" ] ; then if [ "${CD_CA}" -o "${CD_PM}" = "${CD_PM_APT}" ] ; then
cd_step "Set HTTPS verification off" cd_step "Set HTTPS verification off"
cd_mkdir "$(dirname "${CD_PM_HTTPS_PATH}")" cd_mkdir "$(dirname "${CD_PM_HTTPS_PATH}")"
cd_write "${CD_PM_HTTPS_PATH}" "${CD_PM_HTTPS_TEXT}" cd_write "${CD_PM_HTTPS_PATH}" "${CD_PM_HTTPS_TEXT}"
@ -473,13 +451,14 @@ cd_set_https_verification_off () {
} }
cd_set_dns_resolving () { cd_set_dns_resolving () {
local server
local text=""
cd_step "Set DNS resolving" cd_step "Set DNS resolving"
for cd_set_dns_resolving__server in ${CD_DNS} ; do for server in ${CD_DNS} ; do
cd_set_dns_resolving__text="${cd_set_dns_resolving__text}\ text="${text}nameserver ${server}
nameserver ${cd_set_dns_resolving__server}
" "
done done
cd_write "${CD_DNS_FILE}" "${cd_set_dns_resolving__text}" cd_write "${CD_DNS_FILE}" "${text}"
} }
cd_update_packages_catalog () { cd_update_packages_catalog () {
@ -498,22 +477,20 @@ cd_install_ca_certificates () {
} }
cd_write_ca_certificates () { cd_write_ca_certificates () {
local index
local path
local text
cd_step "Write CA certificates" cd_step "Write CA certificates"
cd_mkdir "${CD_CA_ROOT}" cd_mkdir "${CD_CA_ROOT}"
cd_write_ca_certificates__index=1 index=1
eval "cd_write_ca_certificates__text=\ eval "text=\"\${CD_CA_${index}}\""
\"\${CD_CA_${cd_write_ca_certificates__index}}\"" while [ "${text}" ] ; do
while [ -n "${cd_write_ca_certificates__text}" ] ; do path="${CD_CA_ROOT}/${index}.crt"
cd_write_ca_certificates__path="\
${CD_CA_ROOT}/${cd_write_ca_certificates__index}.crt"
cd_split cd_split
cd_write \ cd_write "${path}" "${text}"
"${cd_write_ca_certificates__path}" \ cd_openssl "${path}"
"${cd_write_ca_certificates__text}" index=$((index+1))
cd_openssl "${cd_write_ca_certificates__path}" eval "text=\"\${CD_CA_${index}}\""
cd_write_ca_certificates__index=$((cd_write_ca_certificates__index+1))
eval "cd_write_ca_certificates__text=\
\"\${CD_CA_${cd_write_ca_certificates__index}}\""
done done
} }
@ -562,174 +539,169 @@ cd_clean_packages_cache () {
} }
cd_install_python_modules () { cd_install_python_modules () {
local path
local repository
local root
local url
cd_step "Install Python modules" cd_step "Install Python modules"
cd_install_python_modules__root="$(mktemp --directory)" || exit root="$(mktemp --directory)" || exit
echo "${cd_install_python_modules__root}" echo "${root}"
for cd_install_python_modules__repository \ for repository in "${CD_GIT_CHILD}" "${CD_GIT_PARENT}" ; do
in "${CD_GIT_CHILD}" "${CD_GIT_PARENT}" ; do
cd_split cd_split
cd_install_python_modules__url="\ url="${CD_PROJECTS_URL}/${repository}"
${CD_PROJECTS_URL}/${cd_install_python_modules__repository}" echo -n "\
echo "\ ${url}
${cd_install_python_modules__url}
"
git clone \
"${cd_install_python_modules__url}" \
"${cd_install_python_modules__root}\
/${cd_install_python_modules__repository}" \
|| exit
cd_install_python_modules__path="\
${cd_install_python_modules__root}\
/${cd_install_python_modules__repository}\
/${cd_install_python_modules__repository}"
echo "\
${cd_install_python_modules__path}
${CD_PYTHON_PACKAGES}" "
git clone \
"${url}" "${root}/${repository}" \
|| exit
path="${root}/${repository}/${repository}"
echo -n "\
${path}
${CD_PYTHON_PACKAGES}
"
cp --recursive \ cp --recursive \
"${cd_install_python_modules__path}" "${CD_PYTHON_PACKAGES}" \ "${path}" "${CD_PYTHON_PACKAGES}" \
|| exit || exit
done done
cd_split cd_split
cd_ls "${CD_PYTHON_PACKAGES}" cd_ls "${CD_PYTHON_PACKAGES}"
cd_split cd_split
cd_rm "${cd_install_python_modules__root}" cd_rm "${root}"
} }
cd_write_python_module () { cd_write_python_module () {
cd_step "Write Python module" cd_step "Write Python module"
for cd_write_python_module__variable \ cd_write "${CD_PYTHON_PACKAGES}/env.py" "\
in OPEN DOWN VERT SPLT __UP SHUT OS_ID OS_VERSION ; do $(cd_echo CD_OPEN CD_DOWN)
cd_write_python_module__value="\ $(cd_echo CD_VERT CD_SPLT)
$(cd_echo "CD_${cd_write_python_module__variable}")" $(cd_echo CD___UP CD_SHUT)
cd_write_python_module__text="${cd_write_python_module__text}\
${cd_write_python_module__value} $(cd_echo CD_OS_ID CD_OS_VERSION)
"
done
cd_write "${CD_PYTHON_PACKAGES}/env.py" "${cd_write_python_module__text}\
CD_STEP = $((CD_STEP+1)) CD_STEP = $((CD_STEP+1))
" "
} }
cd_switch_to_python () { cd_switch_to_python () {
local path
cd_step "Switch to Python" cd_step "Switch to Python"
echo "\ #
${CD_PATH} path="$(realpath "${0}")"
echo -n "\
${path}
${CD_PYTHON_PACKAGES}/${CD_GIT_CHILD}" ${CD_PYTHON_PACKAGES}/${CD_GIT_CHILD}
"
"${CD_PYTHON_ALIAS}" -m "${CD_GIT_CHILD}" "${@}" "${CD_PYTHON_ALIAS}" -m "${CD_GIT_CHILD}" "${@}"
} }
# functions # functions
cd_cat () { cd_cat () {
cd_cat__file="${1}" if [ -f "${1}" ] ; then
if [ -n "${cd_cat__file}" ] ; then cd_open "${1}"
cd_open "${cd_cat__file}" cat "${1}" || exit
cat "${cd_cat__file}" || exit cd_shut "${1}"
cd_shut "${cd_cat__file}"
fi fi
} }
cd_echo () { cd_echo () {
if [ -n "${1}" ] ; then local name
for cd_echo__name in "${@}" ; do if [ "${1}" ] ; then
eval "echo ${cd_echo__name} = \\\"\${${cd_echo__name}}\\\"" for name in "${@}" ; do
eval "echo ${name} = \\\"\${${name}}\\\""
done done
fi fi
} }
cd_error_ci () { cd_error_ci () {
echo "× CI: ${*}" echo "× CI: ${1}"
exit "${CD_ERROR_CI}" exit ${CD_ERROR_CI}
} }
cd_error_os () { cd_error_os () {
cd_error_os__variable="${1}" local variable="${1}"
printf "× OS: " echo -n "× OS: "
cd_echo "${cd_error_os__variable}" cd_echo "${variable}"
exit "${CD_ERROR_OS}" exit ${CD_ERROR_OS}
} }
cd_grep_os () { cd_grep_os () {
cd_grep_os__variable="${1}" local variable="${1}"
if [ -n "${cd_grep_os__variable}" ] ; then if [ "${variable}" ] ; then
grep "^${cd_grep_os__variable}=" "/etc/os-release" \ grep "^${variable}=" "/etc/os-release" \
| sed "s|^${cd_grep_os__variable}=||" \ | sed "s|^${variable}=||" \
| sed "s|^\"\(.*\)\"$|\1|" | sed "s|^\"\(.*\)\"$|\1|"
fi fi
} }
cd_install_package () { cd_install_package () {
cd_install_package__name="${1}" if [ "${1}" ] ; then
if [ -n "${cd_install_package__name}" ] ; then ${CD_PM_INSTALL} "${1}" || exit
${CD_PM_INSTALL} "${cd_install_package__name}" || exit
fi fi
} }
cd_ln_python () { cd_ln_python () {
cd_ln_python__command="${1}" local command="${1}"
if [ -n "${cd_ln_python__command}" ] ; then if [ "${command}" ] ; then
echo "${CD_PYTHON_ALIAS}${cd_ln_python__command}" echo "${CD_PYTHON_ALIAS}${command}"
ln -f -s "${cd_ln_python__command}" "/usr/bin/${CD_PYTHON_ALIAS}" \ ln -f -s "${command}" "/usr/bin/${CD_PYTHON_ALIAS}" || exit
|| exit
fi fi
} }
cd_ls () { cd_ls () {
cd_ls__path="${1}" if [ -d "${1}" ] ; then
if [ -n "${cd_ls__path}" ] ; then cd_open "${1}"
cd_open "${cd_ls__path}" ls -a -l "${1}" || exit
ls -a -l "${cd_ls__path}" || exit cd_shut "${1}"
cd_shut "${cd_ls__path}"
fi fi
} }
cd_mkdir () { cd_mkdir () {
cd_mkdir__path="${1}" if [ "${1}" ] ; then
if [ -n "${cd_mkdir__path}" ] ; then echo "${1}"
echo "${cd_mkdir__path}" mkdir --parents "${1}" || exit
mkdir --parents "${cd_mkdir__path}" || exit
fi fi
} }
cd_open () { cd_open () {
echo "${CD_OPEN}${*}" echo "${CD_OPEN}${@}"
} }
cd_openssl () { cd_openssl () {
cd_openssl__file="${1}" local file="${1}"
if [ -f "${cd_openssl__file}" ] ; then if [ -f "${file}" ] ; then
openssl x509 \ openssl x509 -noout -text -in "${file}" || exit
-in "${cd_openssl__file}" \
-noout -text \
|| exit
fi fi
} }
cd_rm () { cd_rm () {
cd_rm__path="${1}" local path="${1}"
if [ -e "${cd_rm__path}" ] ; then if [ -e "${path}" ] ; then
echo "${cd_rm__path}" echo "${path}"
rm -r "${cd_rm__path}" || exit rm -r "${path}" || exit
fi fi
} }
cd_sed () { cd_sed () {
cd_sed__file="${1}" local expression
shift local file="${1}"
if [ -f "${cd_sed__file}" ] ; then shift
cd_cat "${cd_sed__file}" if [ -f "${file}" ] ; then
for cd_sed__regex in "${@}" ; do cd_cat "${file}"
sed --in-place "s${cd_sed__regex}g" "${cd_sed__file}" \ for expression in "${@}" ; do
&& cd_cat "${cd_sed__file}" \ sed --in-place "s${expression}g" "${file}" \
&& cd_cat "${file}" \
|| exit || exit
done done
fi fi
} }
cd_shut () { cd_shut () {
echo "${CD_SHUT}${*}" echo "${CD_SHUT}${@}"
} }
cd_split () { cd_split () {
@ -738,20 +710,21 @@ cd_split () {
cd_step () { cd_step () {
CD_STEP=$((CD_STEP+1)) CD_STEP=$((CD_STEP+1))
echo "\ echo -n "\
${CD_DOWN} ${CD_DOWN}
${CD_VERT} ${CD_STEP} ${*} ${CD_VERT} ${CD_STEP} ${@}
${CD___UP}" ${CD___UP}
"
} }
cd_write () { cd_write () {
cd_write__file="${1}" local file="${1}"
cd_write__text="${2}" local text="${2}"
if [ -n "${cd_write__file}" ] ; then if [ "${file}" ] ; then
[ -f "${cd_write__file}" ] && cd_cat "${cd_write__file}" [ -f "${file}" ] && cd_cat "${file}"
echo "${cd_write__file}" echo "${1}"
printf "%s" "${cd_write__text}" > "${cd_write__file}" || exit echo -n "${text}" > "${file}" || exit
cd_cat "${cd_write__file}" cd_cat "${file}"
fi fi
} }

View file

@ -6,7 +6,7 @@ import env
from rwx import fs from rwx import fs
from rwx import ps from rwx import ps
COMMANDS_PREFIX = "cd-" COMMANDS_PREFIX = 'cd-'
projects = Projects(os.environ) projects = Projects(os.environ)
project = Project(projects) project = Project(projects)
@ -17,8 +17,8 @@ def cd_browse_workspace():
def cd_build_project(): def cd_build_project():
for extension in ["py", "sh"]: for extension in ['py', 'sh']:
path = os.path.join(project.root, f"build.{extension}") path = os.path.join(project.root, f'build.{extension}')
if os.path.exists(path): if os.path.exists(path):
ps.run(path) ps.run(path)
break break
@ -27,14 +27,14 @@ def cd_build_project():
def cd_clone_branch(): def cd_clone_branch():
print(f"""\ print(f'''\
{project.url} {project.url}
""", end="", flush=True) ''', end=str(), flush=True)
ps.run("git", ps.run('git',
"clone", 'clone',
"--branch", project.branch, '--branch', project.branch,
"--", '--',
project.url, project.url,
project.root, project.root,
) )
@ -42,25 +42,25 @@ def cd_clone_branch():
def cd_list_environment(): def cd_list_environment():
for variable, value in sorted(projects.environment.items()): for variable, value in sorted(projects.environment.items()):
print(variable, "=", value) print(variable, '=', value)
def cd_synchronize(): def cd_synchronize():
host = "rwx.work" host = 'rwx.work'
source = "out" source = 'out'
user = "cd" user = 'cd'
# #
root = os.sep.join(["", root = os.sep.join([str(),
user, project.branch, projects.group, project.name]) user, project.branch, projects.group, project.name])
# #
target = f"{user}@{host}:{root}" target = f'{user}@{host}:{root}'
ps.run("rsync", ps.run('rsync',
"--archive", '--archive',
"--delete-before", '--delete-before',
"--verbose", '--verbose',
f"{source}/", f'{source}/',
f"{target}/", f'{target}/',
"--dry-run", '--dry-run',
) )
@ -84,38 +84,38 @@ def cat(file: str):
def install_commands(path): def install_commands(path):
step("Install commands") step('Install commands')
user = "/usr/local/bin" user = '/usr/local/bin'
for command in [ for command in [
"browse-workspace", 'browse-workspace',
"build-project", 'build-project',
"clone-branch", 'clone-branch',
"list-environment", 'list-environment',
"synchronize", 'synchronize',
]: ]:
print(command) print(command)
os.symlink(path, os.path.join(user, f"{COMMANDS_PREFIX}{command}")) os.symlink(path, os.path.join(user, f'{COMMANDS_PREFIX}{command}'))
def set_ssh(*arguments): def set_ssh(*arguments):
step("Set SSH") step('Set SSH')
# #
ssh_key, ssh_hosts = arguments ssh_key, ssh_hosts = arguments
# #
ssh_type = "ed25519" ssh_type = 'ed25519'
# #
home = os.path.expanduser("~") home = os.path.expanduser('~')
# #
ssh = os.path.join(home, ".ssh") ssh = os.path.join(home, '.ssh')
os.makedirs(ssh, exist_ok=True) os.makedirs(ssh, exist_ok=True)
os.chmod(ssh, 0o700) os.chmod(ssh, 0o700)
# #
key = os.path.join(ssh, f"id_{ssh_type}") key = os.path.join(ssh, f'id_{ssh_type}')
if ssh_key: if ssh_key:
fs.write(key, ssh_key) fs.write(key, ssh_key)
os.chmod(key, 0o400) os.chmod(key, 0o400)
# #
known = os.path.join(ssh, "known_hosts") known = os.path.join(ssh, 'known_hosts')
if ssh_hosts: if ssh_hosts:
fs.write(known, ssh_hosts) fs.write(known, ssh_hosts)
os.chmod(known, 0o400) os.chmod(known, 0o400)
@ -125,12 +125,12 @@ def set_ssh(*arguments):
def open(*arguments): def open(*arguments):
print(env.CD_OPEN, end="") print(env.CD_OPEN, end=str())
print(*arguments, flush=True) print(*arguments, flush=True)
def shut(*arguments): def shut(*arguments):
print(env.CD_SHUT, end="") print(env.CD_SHUT, end=str())
print(*arguments, flush=True) print(*arguments, flush=True)