Ticket #32506: bump_dep_revs.sh

File bump_dep_revs.sh, 3.2 KB (added by marin.saric@…, 12 years ago)

A tool to find all ports that depend on a given port and produce a patch file that bumps their revisions. The local Portfile whitespace style is emulated.

Line 
1#!/bin/bash
2# A tool for bumping the revision number of dependent ports
3# Author: Marin Saric <marin.saric@gmail.com>
4
5if [[ "$1" == "" ]]; then
6    cat <<EOF
7Usage:
8   bump_dep_revs.sh port-name
9
10   This tool will generate a patchfile that will bump up the revision of all
11   the portfiles that depend on the port port-name.
12EOF
13    exit 1
14fi
15
16port_name="$1"
17
18ports_dir=$( cd $( port dir coreutils ) && cd ../.. && pwd )
19
20if [[ "$( port dir ${port_name} )" == "" ]]; then
21    echo "Error: Can't find the port ${port_name}" >&2
22    exit 1
23fi
24
25dependent_portfiles=$( find "${ports_dir}" -name Portfile \
26    | xargs grep -l -w port:"${port_name}" )
27
28if [[ "${dependent_portfiles}" == "" ]]; then
29    echo "Done. No dependencies for ${port_name}" >&2
30    exit 0
31fi
32
33has_field() {
34    [[ x"$1" == x"" || x"$2" == x"" ]] && echo "ERROR" >&2 && return 1
35
36    local portfile="$1"
37    local field_name="$2"
38    local has_rev=$( cat "$portfile" | \
39        awk '{ if ($1 == "'"${field_name}"'") { print "yes" } }' )
40    [[ x"${has_rev}" == x"yes" ]]
41}
42
43bump_existing_revision() {
44    [[ x"$1" == x"" ]] && echo "ERROR" >&2 && return 1
45    local portfile="$1"
46    awkcode=$( cat <<"EOF"
47{
48    if ($1 == "revision") {
49        rev_num = $2;
50        rev_pos = index($0, rev_num);
51        print substr($0, 1, rev_pos - 1)""(rev_num + 1);
52    } else {
53        print $0;
54    }
55}
56EOF
57    )
58
59    cat "$portfile" | awk "$awkcode"
60}
61
62add_revision_tag() {
63    [[ x"$1" == x"" ]] && echo "ERROR" >&2 && return 1
64    local portfile="$1"
65
66    local field_to_emulate="version"
67
68    if ! has_field "$portfile" version; then
69        field_to_emulate="name"
70        if ! has_field "$portfile" name; then
71            echo "ERROR: Port $portfile does not have a name field" >&2
72            return 1
73        fi
74    fi
75
76    awkcode=$( cat <<"EOF"
77{
78    if ($1 == field_name) {
79         param_2 = $2;
80         param_2_pos = index($0, param_2);
81         num_spaces = param_2_pos - length(field_name) - 1;
82         space_delta = length(field_name) - length("revision");
83         num_spaces = num_spaces + space_delta;
84         if (num_spaces < 1) num_spaces = 1;
85         spaces = "";
86         for (i = 0; i < num_spaces; ++i) spaces = spaces" ";
87         print $0;
88         print "revision"spaces"2";
89    } else {
90         print $0;
91    }
92}
93EOF
94    )
95    cat "$portfile" | awk "$awkcode" field_name="${field_to_emulate}"
96}
97
98bump_port_revision() {
99    [[ x"$1" == x"" ]] && echo "ERROR" >&2 && return 1
100    local portfile="$1"
101
102    if has_field "$portfile" revision; then
103        bump_existing_revision "$portfile"
104    else
105        add_revision_tag "$portfile"
106    fi
107}
108
109for portfile in ${dependent_portfiles}; do
110    port_name_dir=$( basename $( dirname "${portfile}" ) )
111    port_category_dir=$( basename $( dirname $( dirname "${portfile}" ) ) )
112    port_namecat_dir="${port_category_dir}/${port_name_dir}"
113
114    tmpdir=$( mktemp -d )
115    tmp_port_dir="${tmpdir}/${port_namecat_dir}"
116
117    mkdir -p "${tmp_port_dir}"
118    cp "${portfile}" "${tmp_port_dir}/Portfile.orig"
119
120    bump_port_revision "${portfile}" >"${tmp_port_dir}/Portfile"
121    cd "${tmpdir}" && \
122        diff -u "${port_namecat_dir}/Portfile.orig" "${port_namecat_dir}/Portfile"
123
124    rm "${tmp_port_dir}"/*
125    rmdir "${tmpdir}/${port_namecat_dir}"
126    rmdir "${tmpdir}/${port_category_dir}"
127    rmdir "${tmpdir}"
128done