OSDN Git Service

The options passed to portsreinstall in the builder chroot environment by portsreinst...
[portsreinstall/current.git] / lib / libconf.sh
1 #!/bin/sh -e
2 # ==============================================================================
3 # portsreinstall library script
4 # - Parsing of configuration files -
5 # Copyright (C) 2013-2018 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
6 # This software is distributed under the 2-Clause BSD License.
7 # ==============================================================================
8
9 # ============= Set up environment variables =============
10 conf_setup_ports_envs ()
11 {
12         local tmp_work
13         tmp_work=${TMPDIR}/conf_setup_ports_envs:work
14         rm -rf "$tmp_work"
15         mkdir "$tmp_work"
16         PORTSDIR=${PORTSDIR:-`[ -e /etc/make.conf ] && make -C "$tmp_work" -f /etc/make.conf -V PORTSDIR 2> /dev/null`} || :
17         PORTSDIR=${PORTSDIR:-/usr/ports}
18         pkgsys_chk_ports_tree_implementation
19         PORTS_MOVED_DB=${PORTSDIR}/MOVED
20         PORTS_INDEX_DB=${PORTSDIR}/INDEX-`uname -r | cut -d . -f 1`
21         if [ ! -e "${PORTS_INDEX_DB}" ]
22         then
23                 message_echo "INFO: The ports index for the current OS version is not found, so fetching it now."
24                 if ! make -C "${PORTSDIR}" fetchindex || [ ! -e "${PORTS_INDEX_DB}" ]
25                 then
26                         message_echo "WARNING: Failed to fetch the ports index, so trying to build it locally." >&2
27                         make -C "${PORTSDIR}" index
28                 fi
29         fi
30 }
31
32 # ============= Set up environment variables =============
33 conf_setup_packages_envs ()
34 {
35         local var
36         for var in LOCALBASE PORT_DBDIR LINUXBASE DISTDIR PACKAGES PKGREPOSITORYSUBDIR PKGREPOSITORY
37         do
38                 val=`pkgsys_sysvar $var`
39                 eval $var=\$val
40         done
41         PKGREPOSITORY=${PKG_PATH:-${PKGREPOSITORY}}
42         PACKAGECHECKSUMROOTS=${PACKAGECHECKSUMROOTS:-'ftp://ftp.FreeBSD.org/|'\
43 'ftp://ftp1.FreeBSD.org/|ftp://ftp2.FreeBSD.org/|ftp://ftp3.FreeBSD.org/|'\
44 'ftp://ftp4.FreeBSD.org/|ftp://ftp5.FreeBSD.org/|ftp://ftp6.FreeBSD.org/|'\
45 'ftp://ftp7.FreeBSD.org/|ftp://ftp8.FreeBSD.org/|ftp://ftp9.FreeBSD.org/|'\
46 'ftp://ftp10.FreeBSD.org/|ftp://ftp11.FreeBSD.org/|ftp://ftp12.FreeBSD.org/|'\
47 'ftp://ftp13.FreeBSD.org/|ftp://ftp14.FreeBSD.org/'}
48         PACKAGECHECKSUMDIR=${PACKAGEDIR:-'pub/FreeBSD/ports/%s/packages-%s/All/'}
49         PACKAGEROOTS=${PACKAGEROOTS:-${PACKAGECHECKSUMROOTS}}
50         PACKAGEDIR=${PACKAGEDIR:-${PACKAGECHECKSUMDIR}}
51 }
52
53 # ============= Parse configuration variables given for each port glob =============
54 conf_parse_vars_for_each_port_glob ()
55 {
56         local section
57         section=$1
58         misc_get_all_vardefs | grep -e "^_CONF_${section}_" | cut -d = -f 1 | while read var
59         do
60                 eval glob_pattern=\${$var}
61                 pkgsys_eval_ports_glob "$glob_pattern"
62         done > ${DBDIR}/conf/${section}:PORTS.parsed
63 }
64
65 # ============= Parse configuration variables given for each port glob with a bound value =============
66 conf_parse_vars_for_each_port_glob_with_bound_val ()
67 {
68         local section tag_target tag_val
69         section=$1
70         tag_target=$2
71         tag_val=$3
72         misc_get_all_vardefs | grep -e "^_CONF_${section}_${tag_target}_" | cut -d = -f 1 | while read var
73         do
74                 eval glob_pattern=\${$var}
75                 eval val=\$\{`echo "$var" | sed "s/^_CONF_${section}_${tag_target}_/_CONF_${section}_${tag_val}_/"`\}
76                 pkgsys_eval_ports_glob "$glob_pattern" | while read origin
77                 do
78                         path=${DBDIR}/conf/each_port/$origin
79                         mkdir -p "$path"
80                         echo "$val" > $path/${section}.conf
81                 done
82         done
83 }
84
85 # ============= Parse pkgtools.conf(5) =============
86 conf_parse_pkgtools_conf ()
87 {
88         local PORTUPGRADE tmp_script_crop portupgrade_pkg
89         tmp_script_crop=${TMPDIR}/conf_parse_pkgtools_conf:crop
90         PORTUPGRADE=`which portupgrade | head -n 1 || :`
91         if [ ! -f "$PORTUPGRADE" ]
92         then
93                 message_echo "WARNING: portupgrade is not found, pkgtools.conf is ignored" >&2
94                 return
95         fi
96         portupgrade_pkg=`pkgsys_get_installed_pkg_from_origin ports-mgmt/portupgrade`
97         [ -n "$portupgrade_pkg" ] || portupgrade_pkg=`pkgsys_get_installed_pkg_from_origin ports-mgmt/portupgrade-devel`
98         [ `expr "$portupgrade_pkg" : '^portupgrade-devel-'` -eq 0 ] \
99                 || message_echo "WARNING: Combination with portupgrade-devel-* has not tested." >&2
100         sed -n '/^def init_global$/,/^end$/p' "$PORTUPGRADE" > $tmp_script_crop
101         [ `wc -l < $tmp_script_crop` -gt 0 ] || \
102                 { message_echo "ERROR: The current installed version of portupgrade is unsupported." >&2; }
103         ruby > ${DBDIR}/conf/pkgtools.parsed << eof
104 MYNAME = 'portupgrade'
105 require 'pkgtools'
106 `cat "$tmp_script_crop"`
107 init_global
108 init_pkgtools_global
109 load_config
110 alt_moved = config_value(:ALT_MOVED)
111 hold_pkgs = config_value(:HOLD_PKGS)
112 ignore_moved = config_value(:IGNORE_MOVED)
113 alt_pkgdep = config_value(:ALT_PKGDEP)
114 make_args = config_value(:MAKE_ARGS)
115 make_env = config_value(:MAKE_ENV)
116 beforebuild = config_value(:BEFOREBUILD)
117 beforedeinstall = config_value(:BEFOREDEINSTALL)
118 afterinstall = config_value(:AFTERINSTALL)
119
120 keys = ['LOCALBASE', 'LINUXBASE', 'PORT_DBDIR', 'PORTSDIR', 'DISTDIR', 'PACKAGES', 'PKG_PATH', 'PKGREPOSITORY']
121 keys.each{|key|
122         if ENV.has_key?(key) then
123                 printf("ENV_%s=%s\n", key, ENV[key])
124         end
125 }
126
127 i = 0
128 alt_moved.each do |val|
129         printf("ALT_MOVED_pkgtoolsconf_%d_='%s'\n", i, val)
130         i = i + 1
131 end
132
133 i = 0
134 hold_pkgs.each do |val|
135         printf("HOLD_pkgtoolsconf_%d_='%s'\n", i, val)
136         i = i + 1
137 end
138 ignore_moved.each do |val|
139         printf("HOLD_pkgtoolsconf_%d_='%s'\n", i, val)
140         i = i + 1
141 end
142
143 i = 0
144 alt_pkgdep.each do |pat, alt|
145         printf("REPLACE_FROM_pkgtoolsconf_%d_='%s'\n", i, pat)
146         printf("REPLACE_TO_pkgtoolsconf_%d_='%s'\n", i, alt)
147         i = i + 1
148 end
149
150 i = 0
151 make_args.each do |target, definition|
152         printf("MARG_TARGET_pkgtoolsconf_%d_='%s'\n", i, target)
153         printf("MARG_DEF_pkgtoolsconf_%d_='%s'\n", i, definition)
154         i = i + 1
155 end
156
157 i = 0
158 make_env.each do |target, definition|
159         printf("MENV_TARGET_pkgtoolsconf_%d_='%s'\n", i, target)
160         printf("MENV_DEF_pkgtoolsconf_%d_='%s'\n", i, definition)
161         i = i + 1
162 end
163
164 i = 0
165 beforebuild.each do |target, command|
166         printf("BEFOREBUILD_TARGET_pkgtoolsconf_%d_='%s'\n", i, target)
167         printf("BEFOREBUILD_COMMAND_pkgtoolsconf_%d_='%s'\n", i, command)
168         i = i + 1
169 end
170
171 i = 0
172 beforedeinstall.each do |target, command|
173         printf("BEFOREDEINSTALL_TARGET_pkgtoolsconf_%d_='%s'\n", i, target)
174         printf("BEFOREDEINSTALL_COMMAND_pkgtoolsconf_%d_='%s'\n", i, command)
175         i = i + 1
176 end
177
178 i = 0
179 afterinstall.each do |target, command|
180         printf("AFTERINSTALL_TARGET_pkgtoolsconf_%d_='%s'\n", i, target)
181         printf("AFTERINSTALL_COMMAND_pkgtoolsconf_%d_='%s'\n", i, command)
182         i = i + 1
183 end
184 eof
185 }
186
187 # ============= Get complete configuration variable definitions by importing pkgtools.conf(5) if available =============
188 conf_get_complete_var_defs ()
189 {
190         case `options_get_effective_opt_load_pkgtoolsconf` in
191         default )
192                 conf_parse_pkgtools_conf
193                 cat "${DBDIR}/conf/pkgtools.parsed" "${CONFFILE}" 2> /dev/null || :
194                 ;;
195         override )
196                 conf_parse_pkgtools_conf
197                 cat "${CONFFILE}" "${DBDIR}/conf/pkgtools.parsed" 2> /dev/null || :
198                 ;;
199         no )
200                 cat "${CONFFILE}" 2> /dev/null || :
201                 ;;
202         esac
203 }
204
205 # ============= Manipulate available variable definitions =============
206 conf_manipulate_available_var_defs ()
207 {
208         (set -e
209                 tmpfile_unsetvars=${TMPDIR}/conf_manipulate_available_var_defs::unsetvars.sh
210                 _CONFVARS='ENV ALT_MOVED HOLD TABOO FREEZE NOPKG REPLACE_FROM REPLACE_TO CONFLICT_TARGET CONFLICT_DEF BUILDCONFLICT_TARGET BUILDCONFLICT_DEF INSTCONFLICT_TARGET INSTCONFLICT_DEF MARG_TARGET MARG_DEF MENV_TARGET MENV_DEF BEFOREBUILD BEFOREDEINSTALL AFTERINSTALL'
211                 for item in ${_CONFVARS}
212                 do
213                         misc_get_all_vardefs | grep -e "^${item}_" | cut -d = -f 1 | sed 's/^/unset /'
214                 done > $tmpfile_unsetvars
215                 . "$tmpfile_unsetvars"
216                 . "${DBDIR}/conf/complete_setup.sh"
217                 for item in ${_CONFVARS}
218                 do
219                         misc_get_all_vardefs | grep -e "^${item}_" | sed 's/^/_CONF_/'
220                 done | sort -u > ${DBDIR}/conf/manipulated_defs.sh
221         ) || { message_echo "ERROR: Failed to manipulate available variable definitions from the configuration file." >&2; exit 1; }
222 }
223
224 # ============= Build an effective MOVED database =============
225 conf_build_effective_MOVED ()
226 {
227         grep -v -e '^[[:space:]]*#' -e '^[[:space:]]*$' "${PORTS_MOVED_DB}" > ${DBDIR}/conf/MOVED.parsed
228         misc_get_all_vardefs | grep -e '^_CONF_ALT_MOVED_' | cut -d = -f 1 | while read var
229         do
230                 eval movedsb_path=\${$var}
231                 cat "$movedsb_path"
232         done | grep -v -e '^[[:space:]]*#' -e '^[[:space:]]*$' > ${DBDIR}/conf/MOVED_ALT.parsed || :
233         cat "${DBDIR}/conf/MOVED_ALT.parsed" >> ${DBDIR}/conf/MOVED.parsed
234 }
235
236 # ============= Set up effective environment values =============
237 conf_setup_effective_env ()
238 {
239         (
240                 for key in LOCALBASE LINUXBASE PORT_DBDIR PORTSDIR DISTDIR PACKAGES PKG_PATH PKGREPOSITORY PACKAGECHECKSUMROOTS PACKAGECHECKSUMDIR PACKAGEROOTS PACKAGEDIR
241                 do
242                         eval $key=\$\{_CONF_ENV_$key:-\$\{$key\}\}
243                 done
244                 misc_get_all_vardefs | grep -e '^LOCALBASE=' -e '^LINUXBASE=' -e '^PORT_DBDIR=' -e '^PORTSDIR=' -e '^DISTDIR=' -e 'PACKAGES=' -e '^PKG_PATH=' -e '^PKGREPOSITORY=' -e '^PACKAGECHECKSUMROOTS='  -e '^PACKAGECHECKSUMDIR=' -e '^PACKAGEROOTS=' -e '^PACKAGEDIR='
245         )
246         echo '[ -n "$PKG_PATH" ] && PKGREPOSITORY=$PKG_PATH'
247         echo 'conf_setup_ports_envs'
248         [ "${DBDIR}/conf/MOVED.parsed" -nt "${PORTS_MOVED_DB}" ] || return 0
249         echo PORTS_MOVED_DB=${DBDIR}/conf/MOVED.parsed
250 }
251
252 # ============= Build replacement patterns from REPLACE variables =============
253 conf_build_replacement_patterns_from_REPLACE ()
254 {
255         local tmp_evaluated_origins
256         tmp_matching_origins=${TMPDIR}/conf_build_replacement_patterns_from_REPLACE:origins
257         cp /dev/null "${DBDIR}/conf/REPLACE.filter"
258         cp /dev/null "${DBDIR}/conf/REPLACE.sed_pattern"
259         cp /dev/null "${DBDIR}/conf/REPLACE.csv"
260         misc_get_all_vardefs | grep -e '^_CONF_REPLACE_FROM_' | cut -d = -f 1 | while read var
261         do
262                 eval glob_pattern=\$\{$var\}
263                 eval to=\$\{`echo "$var" | sed 's/^_CONF_REPLACE_FROM_/_CONF_REPLACE_TO_/'`\}
264                 pkgsys_eval_ports_glob_even_if_nonexistent "$glob_pattern" > ${DBDIR}/conf/REPLACE.filter || :
265                 if [ `wc -l < ${DBDIR}/conf/REPLACE.filter` -eq 0 ]
266                 then
267                         message_echo "WARNING: Original package to be replaced [$glob_pattern] is obsolete." >&2
268                         message_echo "If still required, use a pattern for flavored port origins instead." >&2
269                         continue
270                 fi
271                 if [ -z "$to" -o "$to" = delete ]
272                 then
273                         to=
274                 else
275                         to_port_path=`pkgsys_get_portpath_from_origin "$to"`
276                         [ -d "$to_port_path" ] || message_echo "WARNING: replacement port [$to] is obsolete." >&2
277                 fi
278                 str_escape_regexp_filter < ${DBDIR}/conf/REPLACE.filter \
279                         | sed -E "s|^|s:^|; s/$/$:`str_escape_replaceval \"$to\"`:/" \
280                         >> ${DBDIR}/conf/REPLACE.sed_pattern
281                 while read from
282                 do
283                         printf '%s\t%s\n' "$from" "$to"
284                 done < ${DBDIR}/conf/REPLACE.filter >> ${DBDIR}/conf/REPLACE.csv
285         done
286 }
287