savedconfig.eclass 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Copyright 1999-2011 Gentoo Foundation
  2. # Distributed under the terms of the GNU General Public License v2
  3. # @ECLASS: savedconfig.eclass
  4. # @MAINTAINER:
  5. # base-system@gentoo.org
  6. # @BLURB: common API for saving/restoring complex configuration files
  7. # @DESCRIPTION:
  8. # It is not uncommon to come across a package which has a very fine
  9. # grained level of configuration options that go way beyond what
  10. # USE flags can properly describe. For this purpose, a common API
  11. # of saving and restoring the configuration files was developed
  12. # so users can modify these config files and the ebuild will take it
  13. # into account as needed.
  14. #
  15. # @ROFF .nr step 1 1
  16. # Typically you can create your own configuration files quickly by
  17. # doing:
  18. # @ROFF .IP \n[step] 3
  19. # Build the package with FEATURES=noclean USE=savedconfig.
  20. # @ROFF .IP \n+[step]
  21. # Go into the build dir and edit the relevant configuration system
  22. # (e.g. `make menuconfig` or `nano config-header.h`). You can look
  23. # at the files in /etc/portage/savedconfig/ to see what files get
  24. # loaded/restored.
  25. # @ROFF .IP \n+[step]
  26. # Copy the modified configuration files out of the workdir and to
  27. # the paths in /etc/portage/savedconfig/.
  28. # @ROFF .IP \n+[step]
  29. # Emerge the package with just USE=savedconfig to get the custom build.
  30. inherit portability
  31. IUSE="savedconfig"
  32. # @FUNCTION: save_config
  33. # @USAGE: <config files to save>
  34. # @DESCRIPTION:
  35. # Use this function to save the package's configuration file into the
  36. # right location. You may specify any number of configuration files,
  37. # but just make sure you call save_config with all of them at the same
  38. # time in order for things to work properly.
  39. save_config() {
  40. if [[ ${EBUILD_PHASE} != "install" ]]; then
  41. die "Bad package! save_config only for use in src_install functions!"
  42. fi
  43. [[ $# -eq 0 ]] && die "Usage: save_config <files>"
  44. # Be lazy in our EAPI compat
  45. : ${ED:=${D}}
  46. local dest="/etc/portage/savedconfig/${CATEGORY}"
  47. if [[ $# -eq 1 && -f $1 ]] ; then
  48. # Just one file, so have the ${PF} be that config file
  49. dodir "${dest}"
  50. cp "$@" "${ED}/${dest}/${PF}" || die "failed to save $*"
  51. else
  52. # A dir, or multiple files, so have the ${PF} be a dir
  53. # with all the saved stuff below it
  54. dodir "${dest}/${PF}"
  55. treecopy "$@" "${ED}/${dest}/${PF}" || die "failed to save $*"
  56. fi
  57. elog "Your configuration for ${CATEGORY}/${PF} has been saved in "
  58. elog "/etc/portage/savedconfig/${CATEGORY}/${PF} for your editing pleasure."
  59. elog "You can edit these files by hand and remerge this package with"
  60. elog "USE=savedconfig to customise the configuration."
  61. elog "You can rename this file/directory to one of the following for"
  62. elog "its configuration to apply to multiple versions:"
  63. elog '${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/'
  64. elog '[${CTARGET}|${CHOST}|""]/${CATEGORY}/[${PF}|${P}|${PN}]'
  65. }
  66. # @FUNCTION: restore_config
  67. # @USAGE: <config files to restore>
  68. # @DESCRIPTION:
  69. # Restores the configuation saved ebuild previously potentially with user edits.
  70. # You can restore a single file or a whole bunch, just make sure you call
  71. # restore_config with all of the files to restore at the same time.
  72. #
  73. # Config files can be laid out as:
  74. # @CODE
  75. # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${PF}
  76. # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${PF}
  77. # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${PF}
  78. # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${P}
  79. # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${P}
  80. # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${P}
  81. # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${PN}
  82. # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${PN}
  83. # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${PN}
  84. # @CODE
  85. restore_config() {
  86. case ${EBUILD_PHASE} in
  87. unpack|compile|configure|prepare) ;;
  88. *) die "Bad package! restore_config only for use in src_{unpack,compile,configure,prepare} functions!" ;;
  89. esac
  90. use savedconfig || return
  91. local found check configfile
  92. local base=${PORTAGE_CONFIGROOT}/etc/portage/savedconfig
  93. for check in {${CATEGORY}/${PF},${CATEGORY}/${P},${CATEGORY}/${PN}}; do
  94. configfile=${base}/${CTARGET}/${check}
  95. [[ -r ${configfile} ]] || configfile=${base}/${CHOST}/${check}
  96. [[ -r ${configfile} ]] || configfile=${base}/${check}
  97. einfo "Checking existence of ${configfile} ..."
  98. if [[ -r "${configfile}" ]]; then
  99. einfo "found ${configfile}"
  100. found=${configfile};
  101. break;
  102. fi
  103. done
  104. if [[ -f ${found} ]]; then
  105. elog "Building using saved configfile ${found}"
  106. if [ $# -gt 0 ]; then
  107. cp -pPR "${found}" "$1" || die "Failed to restore ${found} to $1"
  108. else
  109. die "need to know the restoration filename"
  110. fi
  111. elif [[ -d ${found} ]]; then
  112. elog "Building using saved config directory ${found}"
  113. local dest=${PWD}
  114. pushd "${found}" > /dev/null
  115. treecopy . "${dest}" || die "Failed to restore ${found} to $1"
  116. popd > /dev/null
  117. else
  118. # maybe the user is screwing around with perms they shouldnt #289168
  119. if [[ ! -r ${base} ]] ; then
  120. eerror "Unable to read ${base} -- please check its permissions."
  121. die "Reading config files failed"
  122. fi
  123. ewarn "No saved config to restore - please remove USE=savedconfig or"
  124. ewarn "provide a configuration file in ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${PN}"
  125. ewarn "Your config file(s) will not be used this time"
  126. fi
  127. }
  128. savedconfig_pkg_postinst() {
  129. # If the user has USE=savedconfig, then chances are they
  130. # are modifying these files, so keep them around. #396169
  131. # This might lead to cruft build up, but the alternatives
  132. # are worse :/.
  133. if use savedconfig ; then
  134. # Be lazy in our EAPI compat
  135. : ${EROOT:=${ROOT}}
  136. find "${EROOT}/etc/portage/savedconfig/${CATEGORY}/${PF}" \
  137. -exec touch {} + 2>/dev/null
  138. fi
  139. }
  140. EXPORT_FUNCTIONS pkg_postinst