/[gentoo-x86]/eclass/savedconfig.eclass
Gentoo

Diff of /eclass/savedconfig.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.1 Revision 1.19
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.1 2007/02/04 13:43:34 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# 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.
10 16
11# TODO 17inherit portability
12#
13# - Move away from cp --parents because BSD doesn't like it
14
15# Should I be using: $PORTAGE_CONFIGROOT ???? Whatever it means, whereever it is
16# documented.
17 18
18IUSE="savedconfig" 19IUSE="savedconfig"
19 20
21# @FUNCTION: save_config
22# @USAGE: <config files to save>
23# @DESCRIPTION:
24# Use this function to save the package's configuration file into the
25# right location. You may specify any number of configuration files,
26# but just make sure you call save_config with all of them at the same
27# time in order for things to work properly.
20# save_config 28save_config() {
21# 29 if [[ ${EBUILD_PHASE} != "install" ]]; then
22# Saves the files and/or directories to 30 die "Bad package! save_config only for use in src_install functions!"
23# /etc/portage/savedconfig/${CATEGORY}/${PF} 31 fi
24# If a single file is specified ${PF} is that file else it is a directory 32 [[ $# -eq 0 ]] && die "Usage: save_config <files>"
25# containing all specified files and directories.
26#
27 33
28save_config() { 34 # Be lazy in our EAPI compat
29 case ${EBUILD_PHASE} in 35 : ${ED:=${D}}
30 preinst|install) 36
31 ;;
32 *) die "Bad package! save_config only for use in pkg_preinst or src_install functions!"
33 ;;
34 esac
35 case $# in
36 0) die "Tell me what to save"
37 ;;
38 1) if [[ -f "$1" ]]; then
39 dodir /etc/portage/savedconfig/${CATEGORY} 37 local dest="/etc/portage/savedconfig/${CATEGORY}"
40 cp "$1" "${D}"/etc/portage/savedconfig/${CATEGORY}/${PF} \ 38 if [[ $# -eq 1 && -f $1 ]] ; then
41 || 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 $*"
42 else 42 else
43 dodir /etc/portage/savedconfig/${CATEGORY}/${PF} 43 # A dir, or multiple files, so have the ${PF} be a dir
44 cp --parents -pPR "$1" "${D}"/etc/portage/savedconfig/${CATEGORY}/${PF} \ 44 # with all the saved stuff below it
45 || die "Failed to save $1" 45 dodir "${dest}/${PF}"
46 treecopy "$@" "${ED}/${dest}/${PF}" || die "failed to save $*"
46 fi 47 fi
47 ;; 48
48 *) 49 elog "Your configuration for ${CATEGORY}/${PF} has been saved in "
49 dodir /etc/portage/savedconfig/${CATEGORY}/${PF} 50 elog "/etc/portage/savedconfig/${CATEGORY}/${PF} for your editing pleasure."
50 while [ "$1" ]; do 51 elog "You can edit these files by hand and remerge this package with"
51 cp --parents -pPR "$1" "${D}"/etc/portage/savedconfig/${CATEGORY}/${PF} \ 52 elog "USE=savedconfig to customise the configuration."
52 || die "Failed to save $1" 53 elog "You can rename this file/directory to one of the following for"
53 shift 54 elog "its configuration to apply to multiple versions:"
54 done 55 elog '${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/'
55 esac 56 elog '[${CTARGET}|${CHOST}|""]/${CATEGORY}/[${PF}|${P}|${PN}]'
56} 57}
57 58
59# @FUNCTION: 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.
65#
66# Config files can be laid out as:
67# @CODE
68# ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${PF}
69# ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${PF}
70# ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${PF}
71# ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${P}
72# ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${P}
73# ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${P}
74# ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${PN}
75# ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${PN}
76# ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${PN}
77# @CODE
78restore_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
58 83
59# restore_config
60#
61# Restores the configuation saved ebuild previously potentially with user edits
62#
63# Requires the name of the file to restore to if a single file was given to
64# save_config. Otherwise it restores the directory structure.
65#
66# Looks for config files in the following order.
67# ${ROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${PF}
68# ${ROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${PF}
69# ${ROOT}/etc/portage/savedconfig/${CATEGORY}/${PF}
70# ${ROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${P}
71# ${ROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${P}
72# ${ROOT}/etc/portage/savedconfig/${CATEGORY}/${P}
73# ${ROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${PN}
74# ${ROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${PN}
75# ${ROOT}/etc/portage/savedconfig/${CATEGORY}/${PN}
76#
77#
78
79restore_config() {
80 use savedconfig || return 84 use savedconfig || return
81 85
82 case ${EBUILD_PHASE} in 86 local found check configfile
83 unpack|compile)
84 ;;
85 *) die "Bad package! save_config only for use in pkg_preinst or src_install functions!"
86 ;;
87 esac
88 local found;
89 local base=${ROOT}/etc/portage/savedconfig 87 local base=${PORTAGE_CONFIGROOT}/etc/portage/savedconfig
90 for check in {${CATEGORY}/${PF},${CATEGORY}/${P},${CATEGORY}/${PN}}; do 88 for check in {${CATEGORY}/${PF},${CATEGORY}/${P},${CATEGORY}/${PN}}; do
91 configfile=${base}/${CTARGET}/${check} 89 configfile=${base}/${CTARGET}/${check}
92 [[ -r ${configfile} ]] || configfile=${base}/${CHOST}/${check} 90 [[ -r ${configfile} ]] || configfile=${base}/${CHOST}/${check}
93 [[ -r ${configfile} ]] || configfile=${base}/${check} 91 [[ -r ${configfile} ]] || configfile=${base}/${check}
94 einfo "Checking existence of ${configfile} ..." 92 einfo "Checking existence of ${configfile} ..."
95 if [[ -r "${configfile}" ]]; then 93 if [[ -r "${configfile}" ]]; then
96 einfo "found ${configfile}" 94 einfo "found ${configfile}"
97 found=${configfile}; 95 found=${configfile};
98 break; 96 break;
99 fi 97 fi
100 done 98 done
101 if [[ -f ${found} ]]; then 99 if [[ -f ${found} ]]; then
100 elog "Building using saved configfile ${found}"
102 if [ $# -gt 0 ]; then 101 if [ $# -gt 0 ]; then
103 cp -pPR "${found}" "$1" || die "Failed to restore ${found} to $1" 102 cp -pPR "${found}" "$1" || die "Failed to restore ${found} to $1"
104 else 103 else
105 die "need to know the restoration filename" 104 die "need to know the restoration filename"
106 fi 105 fi
107 elif [[ -d ${found} ]]; then 106 elif [[ -d ${found} ]]; then
107 elog "Building using saved config directory ${found}"
108 dest=${PWD} 108 local dest=${PWD}
109 pushd "${found}" 109 pushd "${found}" > /dev/null
110 cp --parents . "${DEST}" \
111 || die "Failed to restore ${found} to $1" 110 treecopy . "${dest}" || die "Failed to restore ${found} to $1"
112 popd 111 popd > /dev/null
113 elif [[ -a {found} ]]; then
114 die "do not know how to handle non-file/directory ${found}"
115 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
116 eerror "No saved config to restore - please remove USE=saveconfig or" 118 ewarn "No saved config to restore - please remove USE=savedconfig or"
117 die "provide a configuration file in /etc/portage/savedconfig/${CATEGORY}/${PN}" 119 ewarn "provide a configuration file in ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${PN}"
120 ewarn "Your config file(s) will not be used this time"
118 fi 121 fi
119} 122}
120
121
122#warn_config() {
123#
124#}

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.19

  ViewVC Help
Powered by ViewVC 1.1.20