Welcome to mirror list, hosted at ThFree Co, Russian Federation.

distclean.sh « poudriere « share « src - github.com/freebsd/poudriere.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dea0c8e242b50c9883000c2cfcbf0d1a11f76dd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/sh
# 
# Copyright (c) 2012-2013 Baptiste Daroussin <bapt@FreeBSD.org>
# Copyright (c) 2012-2013 Bryan Drewery <bdrewery@FreeBSD.org>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
set -e

usage() {
	cat <<EOF
poudriere distclean [options]

Options:
    -J n        -- Run n jobs in parallel (Defaults to the number of CPUs)
    -p tree     -- Specify which ports tree to use for comparing to distfiles
                   (Defaults to the 'default' tree)
    -n          -- Do not actually remove anything, just show what would be
                   removed
    -v          -- Be verbose; show more information. Use twice to enable
                   debug output
    -y          -- Assume yes when deleting and do not prompt for confirmation
EOF
	exit 1
}

SCRIPTPATH=`realpath $0`
SCRIPTPREFIX=`dirname ${SCRIPTPATH}`
PTNAME=default
DRY_RUN=0
ALL=1

. ${SCRIPTPREFIX}/common.sh

while getopts "J:np:vy" FLAG; do
	case "${FLAG}" in
		J)
			PARALLEL_JOBS=${OPTARG}
			;;
		n)
			DRY_RUN=1
			;;
		p)
			PTNAME=${OPTARG}
			;;
		v)
			VERBOSE=$((${VERBOSE} + 1))
			;;
		y)
			answer=yes
			;;
		*)
			usage
			;;
	esac
done

shift $((OPTIND-1))

export PORTSDIR=$(pget ${PTNAME} mnt)
[ -d "${PORTSDIR}/ports" ] && PORTSDIR="${PORTSDIR}/ports"
[ -z "${PORTSDIR}" ] && err 1 "No such ports tree: ${PTNAME}"
[ -d ${DISTFILES_CACHE:-/nonexistent} ] ||
	err 1 "The DISTFILES_CACHE directory does not exist (c.f. poudriere.conf)"

DISTFILES_LIST=$(mktemp -t poudriere_distfiles)
CLEANUP_HOOK=distfiles_cleanup
distfiles_cleanup() {
	rm -f ${DISTFILES_LIST} ${DISTFILES_LIST}.expected \
		${DISTFILES_LIST}.actual ${DISTFILES_LIST}.unexpected \
		2>/dev/null
}

gather_distfiles() {
	local origin="$1"
	local distinfo_file="$(make -C ${PORTSDIR}/${origin} -V DISTINFO_FILE)"

	[ -f "${distinfo_file}" ] || return 0

	msg_verbose "Gathering distfiles for: ${origin}"

	awk -v distdir="${DISTFILES_CACHE%/}" '/SIZE/ {print distdir "/" substr($2, 2, length($2) - 2)}' \
		"${distinfo_file}" >> ${DISTFILES_LIST}
}

msg "Gathering all expected distfiles"
parallel_start
for origin in $(listed_ports); do
	parallel_run gather_distfiles ${origin}
done
parallel_stop

# Remove duplicates
sort -u ${DISTFILES_LIST} > ${DISTFILES_LIST}.expected

# Gather list of actual files
msg "Gathering list of actual distfiles"
[ -n "${DISTFILES_CACHE}" ] || err 1 "DISTFILES_CACHE must be set (c.f. poudriere.conf)"
find -x -s ${DISTFILES_CACHE}/ -type f > ${DISTFILES_LIST}.actual

comm -1 -3 ${DISTFILES_LIST}.expected ${DISTFILES_LIST}.actual \
	> ${DISTFILES_LIST}.unexpected

file_cnt=$(wc -l ${DISTFILES_LIST}.unexpected | awk '{print $1}')

if [ ${file_cnt} -eq 0 ]; then
	msg "No stale distfiles to cleanup"
	exit 0
fi

[ -s "${DISTFILES_LIST}.expected" ] || \
	err 1 "Something went wrong. All distfiles would have been removed."

hsize=$(cat ${DISTFILES_LIST}.unexpected | xargs stat -f %z | \
	awk '{total += $1} END {print total}' | \
	awk -f ${AWKPREFIX}/humanize.awk
)

msg "Files to be deleted:"
cat ${DISTFILES_LIST}.unexpected
msg "Cleaning these will free: ${hsize}"

if [ ${DRY_RUN} -eq 1 ];  then
	msg "Dry run: not cleaning anything."
	exit 0
fi

if [ -z "${answer}" ]; then
	msg_n "Proceed? [y/N] "
	read answer
	case $answer in
		[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
			answer=yes
			;;
		*)
			answer=no
			;;
	esac
fi

if [ "${answer}" = "yes" ]; then
	msg "Cleaning files"
	cat ${DISTFILES_LIST}.unexpected | xargs rm -f
fi