04-create-xbps-metadata-scripts.sh (10841B)
1# This hook generates XBPS pkg metadata INSTALL/REMOVE scripts. 2 3_add_trigger() { 4 local f= found= name="$1" 5 6 for f in ${triggers}; do 7 [ "$f" = "$name" ] && found=1 8 done 9 [ -z "$found" ] && triggers="$triggers $name" 10} 11 12process_metadata_scripts() { 13 local action="$1" 14 local action_file="$2" 15 local tmpf=$(mktemp) || exit 1 16 local fpattern="s|${PKGDESTDIR}||g;s|^\./$||g;/^$/d" 17 local targets= f= _f= info_files= home= shell= descr= groups= 18 local found= triggers_found= _icondirs= _schemas= _mods= _tmpfiles= 19 20 case "$action" in 21 install) ;; 22 remove) ;; 23 *) return 1;; 24 esac 25 26 cd ${PKGDESTDIR} 27 cat >> $tmpf <<_EOF 28#!/bin/sh 29# 30# Generic INSTALL/REMOVE script. Arguments passed to this script: 31# 32# \$1 = ACTION [pre/post] 33# \$2 = PKGNAME 34# \$3 = VERSION 35# \$4 = UPDATE [yes/no] 36# \$5 = CONF_FILE (path to xbps.conf) 37# \$6 = ARCH (uname -m) 38# 39# Note that paths must be relative to CWD, to avoid calling 40# host commands if /bin/sh (dash) is not installed and it's 41# not possible to chroot(2). 42# 43 44export PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin" 45 46TRIGGERSDIR="./usr/libexec/xbps-triggers" 47ACTION="\$1" 48PKGNAME="\$2" 49VERSION="\$3" 50UPDATE="\$4" 51CONF_FILE="\$5" 52ARCH="\$6" 53 54# 55# The following code will run the triggers. 56# 57_EOF 58 # 59 # Handle kernel hooks. 60 # 61 if [ -n "${kernel_hooks_version}" ]; then 62 _add_trigger kernel-hooks 63 echo "export kernel_hooks_version=\"${kernel_hooks_version}\"" >> $tmpf 64 fi 65 # 66 # Handle DKMS modules. 67 # 68 if [ -n "${dkms_modules}" ]; then 69 _add_trigger dkms 70 echo "export dkms_modules=\"${dkms_modules}\"" >> $tmpf 71 fi 72 # 73 # Handle system groups. 74 # 75 if [ -n "${system_groups}" ]; then 76 _add_trigger system-accounts 77 echo "export system_groups=\"${system_groups}\"" >> $tmpf 78 fi 79 # 80 # Handle system accounts. 81 # 82 if [ -n "${system_accounts}" ]; then 83 _add_trigger system-accounts 84 echo "export system_accounts=\"${system_accounts}\"" >> $tmpf 85 for f in ${system_accounts}; do 86 local _uname="${f%:*}" 87 local _uid="${f#*:}" 88 89 eval homedir="\$${_uname}_homedir" 90 eval shell="\$${_uname}_shell" 91 eval descr="\$${_uname}_descr" 92 eval groups="\$${_uname}_groups" 93 eval pgroup="\$${_uname}_pgroup" 94 if [ -n "$homedir" ]; then 95 echo "export ${_uname}_homedir=\"$homedir\"" >> $tmpf 96 fi 97 if [ -n "$shell" ]; then 98 echo "export ${_uname}_shell=\"$shell\"" >> $tmpf 99 fi 100 if [ -n "$descr" ]; then 101 echo "export ${_uname}_descr=\"$descr\"" >> $tmpf 102 fi 103 if [ -n "$groups" ]; then 104 echo "export ${_uname}_groups=\"${groups}\"" >> $tmpf 105 fi 106 if [ -n "$pgroup" ]; then 107 echo "export ${_uname}_pgroup=\"${pgroup}\"" >> $tmpf 108 fi 109 unset homedir shell descr groups pgroup 110 done 111 fi 112 # 113 # Handle mkdirs trigger. 114 # 115 if [ -n "${make_dirs}" ]; then 116 _add_trigger mkdirs 117 echo "export make_dirs=\"${make_dirs}\"" >> $tmpf 118 fi 119 # 120 # Handle binfmts trigger 121 # 122 if [ -n "${binfmts}" ]; then 123 _add_trigger binfmts 124 echo "export binfmts=\"${binfmts}\"" >> $tmpf 125 fi 126 # 127 # Handle GNU Info files. 128 # 129 if [ -d "${PKGDESTDIR}/usr/share/info" ]; then 130 unset info_files 131 for f in $(find ${PKGDESTDIR}/usr/share/info -type f); do 132 j=$(echo $f|sed -e "$fpattern") 133 [ "$j" = "" ] && continue 134 [ "$j" = "/usr/share/info/dir" ] && continue 135 if [ -z "$info_files" ]; then 136 info_files="$j" 137 else 138 info_files="$info_files $j" 139 fi 140 done 141 if [ -n "${info_files}" ]; then 142 _add_trigger info-files 143 echo "export info_files=\"${info_files}\"" >> $tmpf 144 fi 145 fi 146 # 147 # Handle files in hwdb directory 148 # 149 if [ -d "${PKGDESTDIR}/usr/lib/udev/hwdb.d" ]; then 150 _add_trigger hwdb.d-dir 151 fi 152 # 153 # Handle texmf database changes 154 # 155 if [ -d "${PKGDESTDIR}/usr/share/texmf-dist" ] ; then 156 _add_trigger texmf-dist 157 fi 158 # 159 # (Un)Register a shell in /etc/shells. 160 # 161 if [ -n "${register_shell}" ]; then 162 _add_trigger register-shell 163 echo "export register_shell=\"${register_shell}\"" >> $tmpf 164 fi 165 # 166 # Handle SGML/XML catalog entries via xmlcatmgr. 167 # 168 if [ -n "${sgml_catalogs}" ]; then 169 for catalog in ${sgml_catalogs}; do 170 sgml_entries="${sgml_entries} CATALOG ${catalog} --" 171 done 172 fi 173 if [ -n "${sgml_entries}" ]; then 174 echo "export sgml_entries=\"${sgml_entries}\"" >> $tmpf 175 fi 176 if [ -n "${xml_catalogs}" ]; then 177 for catalog in ${xml_catalogs}; do 178 xml_entries="${xml_entries} nextCatalog ${catalog} --" 179 done 180 fi 181 if [ -n "${xml_entries}" ]; then 182 echo "export xml_entries=\"${xml_entries}\"" >> $tmpf 183 fi 184 if [ -n "${sgml_entries}" -o -n "${xml_entries}" ]; then 185 _add_trigger xml-catalog 186 fi 187 # 188 # Handle X11 font updates via mkfontdir/mkfontscale. 189 # 190 if [ -n "${font_dirs}" ]; then 191 _add_trigger x11-fonts 192 echo "export font_dirs=\"${font_dirs}\"" >> $tmpf 193 fi 194 # 195 # Handle GTK+ Icon cache directories. 196 # 197 if [ -d ${PKGDESTDIR}/usr/share/icons ]; then 198 for f in ${PKGDESTDIR}/usr/share/icons/*; do 199 [ ! -d "${f}" ] && continue 200 _icondirs="${_icondirs} ${f#${PKGDESTDIR}}" 201 done 202 if [ -n "${_icondirs}" ]; then 203 echo "export gtk_iconcache_dirs=\"${_icondirs}\"" >> $tmpf 204 _add_trigger gtk-icon-cache 205 fi 206 fi 207 # 208 # Handle .desktop files in /usr/share/applications with 209 # desktop-file-utils. 210 # 211 if [ -d ${PKGDESTDIR}/usr/share/applications ]; then 212 _add_trigger update-desktopdb 213 fi 214 # 215 # Handle GConf schemas/entries files with gconf-schemas. 216 # 217 if [ -d ${PKGDESTDIR}/usr/share/gconf/schemas ]; then 218 _add_trigger gconf-schemas 219 for f in ${PKGDESTDIR}/usr/share/gconf/schemas/*.schemas; do 220 _schemas="${_schemas} ${f##*/}" 221 done 222 echo "export gconf_schemas=\"${_schemas}\"" >> $tmpf 223 fi 224 # 225 # Handle gio-modules trigger. 226 # 227 if [ -d ${PKGDESTDIR}/usr/lib/gio/modules ]; then 228 _add_trigger gio-modules 229 fi 230 # 231 # Handle gtk immodules in /usr/lib/gtk-2.0/2.10.0/immodules with 232 # gtk-immodules 233 # 234 if [ -d ${PKGDESTDIR}/usr/lib/gtk-2.0/2.10.0/immodules ]; then 235 _add_trigger gtk-immodules 236 fi 237 # 238 # Handle gtk3 immodules in /usr/lib/gtk-3.0/3.0.0/immodules with 239 # gtk3-immodules 240 # 241 if [ -d ${PKGDESTDIR}/usr/lib/gtk-3.0/3.0.0/immodules ]; then 242 _add_trigger gtk3-immodules 243 fi 244 # 245 # Handle gsettings schemas in /usr/share/glib-2.0/schemas with 246 # gsettings-schemas. 247 # 248 if [ -d ${PKGDESTDIR}/usr/share/glib-2.0/schemas ]; then 249 _add_trigger gsettings-schemas 250 fi 251 # 252 # Handle gdk-pixbuf loadable modules in /usr/lib/gdk-pixbuf-2.0/2.10.0/loaders 253 # with gdk-pixbuf-loaders 254 # 255 if [ -d ${PKGDESTDIR}/usr/lib/gdk-pixbuf-2.0/2.10.0/loaders ]; then 256 _add_trigger gdk-pixbuf-loaders 257 fi 258 # 259 # Handle mime database in /usr/share/mime with update-mime-database. 260 # 261 if [ -d ${PKGDESTDIR}/usr/share/mime ]; then 262 _add_trigger mimedb 263 fi 264 # 265 # Handle python bytecode archives with pycompile trigger. 266 # 267 local pycompile_version 268 if [ -d ${PKGDESTDIR}/usr/lib/python* ]; then 269 pycompile_version="$(find ${PKGDESTDIR}/usr/lib/python* -prune -type d | grep -o '[[:digit:]]\.[[:digit:]]$')" 270 if [ -z "${pycompile_module}" ]; then 271 pycompile_module="$(find ${PKGDESTDIR}/usr/lib/python*/site-packages -mindepth 1 -maxdepth 1 '!' -name '*.egg-info' '!' -name '*.dist-info' '!' -name '*.so' '!' -name '*.pth' -printf '%f ')" 272 fi 273 fi 274 275 if [ -n "$python_version" ]; then 276 pycompile_version=${python_version} 277 fi 278 279 if [ "$pycompile_version" = 3 ]; then 280 pycompile_version=${py3_ver} 281 elif [ "$pycompile_version" = 2 ]; then 282 pycompile_version=${py2_ver} 283 fi 284 285 if [ -n "${pycompile_dirs}" -o -n "${pycompile_module}" ]; then 286 [ -n "$pycompile_version" ] || msg_error "$pkgver: byte-compilation is required, but python_version is not set\n" 287 echo "export pycompile_version=\"${pycompile_version}\"" >>$tmpf 288 if [ -n "${pycompile_dirs}" ]; then 289 echo "export pycompile_dirs=\"${pycompile_dirs}\"" >>$tmpf 290 fi 291 if [ -n "${pycompile_module}" ]; then 292 echo "export pycompile_module=\"${pycompile_module}\"" >>$tmpf 293 fi 294 _add_trigger pycompile 295 fi 296 # 297 # Handle appdata metadata with AppStream 298 # 299 for f in ${PKGDESTDIR}/usr/share/appdata/*.xml ${PKGDESTDIR}/usr/share/app-info/*.xml ${PKGDESTDIR}/var/lib/app-info/*.xml ${PKGDESTDIR}/var/cache/app-info/*.xml; do 300 if [ -f "${f}" ]; then 301 _add_trigger appstream-cache 302 break 303 fi 304 done 305 306 # End of trigger var exports. 307 echo >> $tmpf 308 309 # 310 # Write the INSTALL/REMOVE package scripts. 311 # 312 if [ -n "$triggers" ]; then 313 triggers_found=1 314 echo "case \"\${ACTION}\" in" >> $tmpf 315 echo "pre)" >> $tmpf 316 for f in ${triggers}; do 317 if [ ! -f $XBPS_TRIGGERSDIR/$f ]; then 318 rm -f $tmpf 319 msg_error "$pkgname: unknown trigger $f, aborting!\n" 320 fi 321 echo " Added trigger '$f' for the '${action^^}' script." 322 done 323 for f in ${triggers}; do 324 targets=$($XBPS_TRIGGERSDIR/$f targets) 325 for j in ${targets}; do 326 if ! [[ $j =~ pre-${action} ]]; then 327 continue 328 fi 329 printf "\t\${TRIGGERSDIR}/$f run $j \${PKGNAME} \${VERSION} \${UPDATE} \${CONF_FILE}\n" >> $tmpf 330 printf "\t[ \$? -ne 0 ] && exit \$?\n" >> $tmpf 331 done 332 done 333 printf "\t;;\n" >> $tmpf 334 echo "post)" >> $tmpf 335 for f in ${triggers}; do 336 targets=$($XBPS_TRIGGERSDIR/$f targets) 337 for j in ${targets}; do 338 if ! [[ $j =~ post-${action} ]]; then 339 continue 340 fi 341 printf "\t\${TRIGGERSDIR}/$f run $j \${PKGNAME} \${VERSION} \${UPDATE} \${CONF_FILE}\n" >> $tmpf 342 printf "\t[ \$? -ne 0 ] && exit \$?\n" >> $tmpf 343 done 344 done 345 printf "\t;;\n" >> $tmpf 346 echo "esac" >> $tmpf 347 echo >> $tmpf 348 fi 349 350 if [ -z "$triggers" -a ! -f "$action_file" ]; then 351 rm -f $tmpf 352 return 0 353 fi 354 355 case "$action" in 356 install) 357 if [ -f ${action_file} ]; then 358 found=1 359 cat ${action_file} >> $tmpf 360 fi 361 echo >> $tmpf 362 echo "exit 0" >> $tmpf 363 mv $tmpf ${PKGDESTDIR}/INSTALL && chmod 755 ${PKGDESTDIR}/INSTALL 364 ;; 365 remove) 366 unset found 367 if [ -f ${action_file} ]; then 368 found=1 369 cat ${action_file} >> $tmpf 370 fi 371 echo >> $tmpf 372 echo "exit 0" >> $tmpf 373 mv $tmpf ${PKGDESTDIR}/REMOVE && chmod 755 ${PKGDESTDIR}/REMOVE 374 ;; 375 esac 376} 377 378hook() { 379 local meta_install meta_remove 380 381 if [ -n "${sourcepkg}" -a "${sourcepkg}" != "${pkgname}" ]; then 382 # subpkg 383 meta_install=${XBPS_SRCPKGDIR}/${pkgname}/${pkgname}.INSTALL 384 msg_install=${XBPS_SRCPKGDIR}/${pkgname}/${pkgname}.INSTALL.msg 385 meta_remove=${XBPS_SRCPKGDIR}/${pkgname}/${pkgname}.REMOVE 386 msg_remove=${XBPS_SRCPKGDIR}/${pkgname}/${pkgname}.REMOVE.msg 387 else 388 # sourcepkg 389 meta_install=${XBPS_SRCPKGDIR}/${pkgname}/INSTALL 390 msg_install=${XBPS_SRCPKGDIR}/${pkgname}/INSTALL.msg 391 meta_remove=${XBPS_SRCPKGDIR}/${pkgname}/REMOVE 392 msg_remove=${XBPS_SRCPKGDIR}/${pkgname}/REMOVE.msg 393 fi 394 process_metadata_scripts install ${meta_install} || \ 395 msg_error "$pkgver: failed to write INSTALL metadata file!\n" 396 397 process_metadata_scripts remove ${meta_remove} || \ 398 msg_error "$pkgver: failed to write REMOVE metadata file!\n" 399 400 if [ -s ${msg_install} ]; then 401 install -m644 ${msg_install} ${PKGDESTDIR}/INSTALL.msg 402 fi 403 if [ -s ${msg_remove} ]; then 404 install -m644 ${msg_remove} ${PKGDESTDIR}/REMOVE.msg 405 fi 406}