| 1 | # Copyright 1999-2007 Gentoo Foundation |
1 | # Copyright 1999-2011 Gentoo Foundation |
| 2 | # Distributed under the terms of the GNU General Public License v2 |
2 | # Distributed under the terms of the GNU General Public License v2 |
| 3 | # $Header: /var/cvsroot/gentoo-x86/eclass/savedconfig.eclass,v 1.4 2007/02/21 20:49:45 dragonheart Exp $ |
3 | # $Header: /var/cvsroot/gentoo-x86/eclass/savedconfig.eclass,v 1.19 2012/01/04 07:45:16 vapier Exp $ |
| 4 | |
4 | |
| 5 | # Original Author: Daniel Black <dragonheart@gentoo.org> |
5 | # @ECLASS: savedconfig.eclass |
| 6 | # |
6 | # @MAINTAINER: |
| 7 | # Purpose: Define an interface for ebuilds to save and restore |
7 | # base-system@gentoo.org |
| 8 | # complex configuration that may be edited by users. |
8 | # @BLURB: common API for saving/restoring complex configuration files |
| 9 | # |
9 | # @DESCRIPTION: |
| 10 | # Thanks to Mike Frysinger <vapier@gentoo.org> for the suggestions. |
10 | # It is not uncommon to come across a package which has a very fine |
|
|
11 | # grained level of configuration options that go way beyond what |
|
|
12 | # USE flags can properly describe. For this purpose, a common API |
|
|
13 | # of saving and restoring the configuration files was developed |
|
|
14 | # so users can modify these config files and the ebuild will take it |
|
|
15 | # into account as needed. |
| 11 | |
16 | |
| 12 | inherit portability |
17 | inherit portability |
| 13 | |
18 | |
| 14 | IUSE="savedconfig" |
19 | IUSE="savedconfig" |
| 15 | |
20 | |
| 16 | # save_config |
21 | # @FUNCTION: save_config |
| 17 | # |
22 | # @USAGE: <config files to save> |
| 18 | # Saves the files and/or directories to |
23 | # @DESCRIPTION: |
| 19 | # /etc/portage/savedconfig/${CATEGORY}/${PF} |
24 | # Use this function to save the package's configuration file into the |
| 20 | # |
25 | # right location. You may specify any number of configuration files, |
| 21 | # If a single file is specified ${PF} is that file else it is a directory |
26 | # but just make sure you call save_config with all of them at the same |
| 22 | # containing all specified files and directories. |
27 | # time in order for things to work properly. |
| 23 | # |
|
|
| 24 | |
|
|
| 25 | save_config() { |
28 | save_config() { |
| 26 | if [[ ${EBUILD_PHASE} != "install" ]]; then |
29 | if [[ ${EBUILD_PHASE} != "install" ]]; then |
| 27 | die "Bad package! save_config only for use in src_install functions!" |
30 | die "Bad package! save_config only for use in src_install functions!" |
| 28 | fi |
31 | fi |
| 29 | case $# in |
32 | [[ $# -eq 0 ]] && die "Usage: save_config <files>" |
| 30 | 0) die "Tell me what to save" |
33 | |
| 31 | ;; |
34 | # Be lazy in our EAPI compat |
| 32 | 1) if [[ -f "$1" ]]; then |
35 | : ${ED:=${D}} |
|
|
36 | |
| 33 | dodir /etc/portage/savedconfig/${CATEGORY} |
37 | local dest="/etc/portage/savedconfig/${CATEGORY}" |
| 34 | cp "$1" "${D}"/etc/portage/savedconfig/${CATEGORY}/${PF} \ |
38 | if [[ $# -eq 1 && -f $1 ]] ; then |
| 35 | || die "Failed to save $1" |
39 | # Just one file, so have the ${PF} be that config file |
|
|
40 | dodir "${dest}" |
|
|
41 | cp "$@" "${ED}/${dest}/${PF}" || die "failed to save $*" |
| 36 | else |
42 | else |
| 37 | dodir /etc/portage/savedconfig/${CATEGORY}/${PF} |
43 | # A dir, or multiple files, so have the ${PF} be a dir |
| 38 | treecopy "$1" "${D}"/etc/portage/savedconfig/${CATEGORY}/${PF} \ |
44 | # with all the saved stuff below it |
| 39 | || die "Failed to save $1" |
45 | dodir "${dest}/${PF}" |
|
|
46 | treecopy "$@" "${ED}/${dest}/${PF}" || die "failed to save $*" |
| 40 | fi |
47 | fi |
| 41 | ;; |
48 | |
| 42 | *) |
49 | elog "Your configuration for ${CATEGORY}/${PF} has been saved in " |
|
|
50 | elog "/etc/portage/savedconfig/${CATEGORY}/${PF} for your editing pleasure." |
|
|
51 | elog "You can edit these files by hand and remerge this package with" |
|
|
52 | elog "USE=savedconfig to customise the configuration." |
|
|
53 | elog "You can rename this file/directory to one of the following for" |
|
|
54 | elog "its configuration to apply to multiple versions:" |
| 43 | dodir "${PORTAGE_CONFIGROOT}"/etc/portage/savedconfig/${CATEGORY}/${PF} |
55 | elog '${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/' |
| 44 | treecopy $* "${D}/${PORTAGE_CONFIGROOT}"/etc/portage/savedconfig/${CATEGORY}/${PF} \ |
56 | elog '[${CTARGET}|${CHOST}|""]/${CATEGORY}/[${PF}|${P}|${PN}]' |
| 45 | || die "Failed to save $1" |
|
|
| 46 | esac |
|
|
| 47 | } |
57 | } |
| 48 | |
58 | |
| 49 | |
59 | # @FUNCTION: restore_config |
| 50 | # restore_config |
60 | # @USAGE: <config files to restore> |
|
|
61 | # @DESCRIPTION: |
|
|
62 | # Restores the configuation saved ebuild previously potentially with user edits. |
|
|
63 | # You can restore a single file or a whole bunch, just make sure you call |
|
|
64 | # restore_config with all of the files to restore at the same time. |
| 51 | # |
65 | # |
| 52 | # Restores the configuation saved ebuild previously potentially with user edits |
66 | # Config files can be laid out as: |
| 53 | # |
67 | # @CODE |
| 54 | # Requires the name of the file to restore to if a single file was given to |
|
|
| 55 | # save_config. Otherwise it restores the directory structure. |
|
|
| 56 | # |
|
|
| 57 | # Looks for config files in the following order. |
|
|
| 58 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${PF} |
68 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${PF} |
| 59 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${PF} |
69 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${PF} |
| 60 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${PF} |
70 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${PF} |
| 61 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${P} |
71 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${P} |
| 62 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${P} |
72 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${P} |
| 63 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${P} |
73 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${P} |
| 64 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${PN} |
74 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${PN} |
| 65 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${PN} |
75 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${PN} |
| 66 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${PN} |
76 | # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${PN} |
| 67 | # |
77 | # @CODE |
| 68 | # |
78 | restore_config() { |
|
|
79 | case ${EBUILD_PHASE} in |
|
|
80 | unpack|compile|configure|prepare) ;; |
|
|
81 | *) die "Bad package! restore_config only for use in src_{unpack,compile,configure,prepare} functions!" ;; |
|
|
82 | esac |
| 69 | |
83 | |
| 70 | restore_config() { |
|
|
| 71 | use savedconfig || return |
84 | use savedconfig || return |
| 72 | |
85 | |
| 73 | case ${EBUILD_PHASE} in |
86 | local found check configfile |
| 74 | unpack|compile) |
|
|
| 75 | ;; |
|
|
| 76 | *) die "Bad package! restore_config only for use in src_unpack or src_compile functions!" |
|
|
| 77 | ;; |
|
|
| 78 | esac |
|
|
| 79 | local found; |
|
|
| 80 | local base=${PORTAGE_CONFIGROOT}/etc/portage/savedconfig |
87 | local base=${PORTAGE_CONFIGROOT}/etc/portage/savedconfig |
| 81 | for check in {${CATEGORY}/${PF},${CATEGORY}/${P},${CATEGORY}/${PN}}; do |
88 | for check in {${CATEGORY}/${PF},${CATEGORY}/${P},${CATEGORY}/${PN}}; do |
| 82 | configfile=${base}/${CTARGET}/${check} |
89 | configfile=${base}/${CTARGET}/${check} |
| 83 | [[ -r ${configfile} ]] || configfile=${base}/${CHOST}/${check} |
90 | [[ -r ${configfile} ]] || configfile=${base}/${CHOST}/${check} |
| 84 | [[ -r ${configfile} ]] || configfile=${base}/${check} |
91 | [[ -r ${configfile} ]] || configfile=${base}/${check} |
| 85 | einfo "Checking existence of ${configfile} ..." |
92 | einfo "Checking existence of ${configfile} ..." |
| 86 | if [[ -r "${configfile}" ]]; then |
93 | if [[ -r "${configfile}" ]]; then |
| 87 | einfo "found ${configfile}" |
94 | einfo "found ${configfile}" |
| 88 | found=${configfile}; |
95 | found=${configfile}; |
| 89 | break; |
96 | break; |
| 90 | fi |
97 | fi |
| 91 | done |
98 | done |
| 92 | if [[ -f ${found} ]]; then |
99 | if [[ -f ${found} ]]; then |
|
|
100 | elog "Building using saved configfile ${found}" |
| 93 | if [ $# -gt 0 ]; then |
101 | if [ $# -gt 0 ]; then |
| 94 | cp -pPR "${found}" "$1" || die "Failed to restore ${found} to $1" |
102 | cp -pPR "${found}" "$1" || die "Failed to restore ${found} to $1" |
| 95 | else |
103 | else |
| 96 | die "need to know the restoration filename" |
104 | die "need to know the restoration filename" |
| 97 | fi |
105 | fi |
| 98 | elif [[ -d ${found} ]]; then |
106 | elif [[ -d ${found} ]]; then |
|
|
107 | elog "Building using saved config directory ${found}" |
| 99 | dest=${PWD} |
108 | local dest=${PWD} |
| 100 | pushd "${found}" |
109 | pushd "${found}" > /dev/null |
| 101 | treecopy . "${dest}" \ |
|
|
| 102 | || die "Failed to restore ${found} to $1" |
110 | treecopy . "${dest}" || die "Failed to restore ${found} to $1" |
| 103 | popd |
111 | popd > /dev/null |
| 104 | elif [[ -a {found} ]]; then |
|
|
| 105 | die "do not know how to handle non-file/directory ${found}" |
|
|
| 106 | else |
112 | else |
|
|
113 | # maybe the user is screwing around with perms they shouldnt #289168 |
|
|
114 | if [[ ! -r ${base} ]] ; then |
|
|
115 | eerror "Unable to read ${base} -- please check its permissions." |
|
|
116 | die "Reading config files failed" |
|
|
117 | fi |
| 107 | eerror "No saved config to restore - please remove USE=saveconfig or" |
118 | ewarn "No saved config to restore - please remove USE=savedconfig or" |
| 108 | eerror "provide a configuration file in ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${PN}" |
119 | ewarn "provide a configuration file in ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${PN}" |
| 109 | die "config file needed when USE=savedconfig is specified" |
120 | ewarn "Your config file(s) will not be used this time" |
| 110 | fi |
121 | fi |
| 111 | } |
122 | } |