summaryrefslogtreecommitdiff
path: root/src/maintenance-tools/parabola-dependents
blob: 0e0f619d78eb9efba2cd35636f4ff94d74e98524 (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
#!/bin/bash

readonly BE_VERBOSE=$( [[ "$1" == '-v' ]] && echo 1 || echo 0 )
readonly DB_DIR="$(su $(logname) -c 'mktemp -d -t parabola-dependents-XXXXXXXXXX')"
readonly CFG_FILE=${DB_DIR}/pacman-all.conf
readonly OPTS="--dbpath=${DB_DIR} --config=${CFG_FILE}"
readonly REPOS=( nonprism{-testing,} nonsystemd{-testing,} libre{-testing,}
                 kernels{-testing,} testing core extra community{-testing,}
                 pcr{-testing,} nonprism-multilib{-testing,} nonsystemd-multilib
                 libre-multilib{-testing,} multilib{-testing,} pcr-multilib{-testing,} )
readonly DUMMY_PKGBUILD='pkgname=%s
pkgver=0.0.0
pkgrel=42
pkgdesc="dummy PKGBUILD for experimentation"
arch=(armv7h i686 x86_64)
url=https://nowhere.man
license=(GPL)
source=(PKGBUILD)
sha256sums=(SKIP)
package() { echo "created '"'\${pkgname}'"' package" ; }'
readonly USAGE='USAGE:
  parabola-dependents [-v] <PACKAGE_NAME>

  List all parabola packages which are dependents of a specified package.

  If <PACKAGE_NAME> does not exist, a dummy package will be created,
  in memory, for pactree to reflect upon.

  Options:
    -v
      Display the dependency-chain.'
readonly INVALID_ARG_MSG="no dependency package specified\n\n${USAGE}"
readonly UNPRIVILEGED_MSG="this script requires super-user privileges"
readonly CBLUE='\033[0;36m'
readonly CRED='\033[0;31m'
readonly CEND='\033[0m'


Log() # (log_fmt [args]*)
{
  local log_fmt=$1 ; shift ;
  local args="$@"

  printf "${CBLUE}${log_fmt}${CEND}\n" ${args}
}

LogError() # (source_file func_name line_n)
{
  local source_file="$1"
  local func_name=$2
  local line_n=$3

  printf "${CRED}ERROR: in ${func_name}\n${line_n}: %s${CEND}\n" \
  "$(awk "(( NR == ${line_n} )) { print }" ${source_file})"
}

Init() # (dep_pkgname)
{
  local dep_pkgname=$1
  local dummy_pkg=${dep_pkgname}-0.0.0-42-$(uname -m).pkg.tar.xz

  printf "[options]\nArchitecture = auto\n"                     > ${CFG_FILE}
  for repo in ${REPOS[@]}
  do  printf "[${repo}]\nInclude = /etc/pacman.d/mirrorlist\n" >> ${CFG_FILE}
  done
  printf "[parabola-dependents]\nServer = file://${DB_DIR}\n"  >> ${CFG_FILE}

  Log "updating database ...."
  pacman ${OPTS} -Sy &> /dev/null || true

  # create dummy for missing dependency package
  if   ! pacman ${OPTS} -Ss ^${dep_pkgname}$ &> /dev/null
  then Log "package '${dep_pkgname}' not found - creating dummy"
       cd ${DB_DIR}
       printf "${DUMMY_PKGBUILD}\n" ${dep_pkgname} > ./PKGBUILD
       su $(logname) -c 'makepkg --force &> /dev/null'

       [[ -f ./${dummy_pkg} ]]

       repo-add --new parabola-dependents.db.tar ./${dummy_pkg} &> /dev/null

       Log "dummy package '${dep_pkgname}' created"
  fi
  pacman ${OPTS} -Sy&> /dev/null
}

IsArchRepo() # (repo)
{
  local repo=$1 ; [[ "${repo}" =~ ^(community|core|extra|multilib|testing)$ ]]
}

ListParabolaDependents() # (dep_pkgname)
{
  local dep_pkgname=$1
  local dep_chain pkg repos

# TODO: --chain is a custom pactree feature
export PATH="/code/pacman-contrib/src:${PATH}"

  Log "searching ...." ; echo ;
  # TODO: --chain is a custom pactree feature
  pactree ${OPTS} --sync --reverse --unique --chain ${dep_pkgname} | sort | \
  while read dep_chain
  do    pkg=$(sed 's|.*<- ||' <<<${dep_chain})
        repos=$(pacman ${OPTS} -Si ${pkg} | grep Repository | cut -d ':' -f 2)

        for repo in ${REPOS}
        do  if   IsArchRepo ${repo}
            then echo    "${repo}/${pkg} (ignoring)" >&2
            else echo -n "${repo}/${pkg}"
                 (( $BE_VERBOSE )) && echo " [ ${dep_chain} ]" || echo ''
            fi
        done
  done
}

Cleanup()
{
  if   [[ -d "${DB_DIR}" && "${DB_DIR}" =~ parabola-dependents ]]
  then rm --force --recursive ${DB_DIR} ;
  fi
}


set -o errexit -o errtrace
trap 'Cleanup'                                                   EXIT INT TERM
trap 'LogError "${BASH_SOURCE[0]}" "${FUNCNAME[0]}" "${LINENO}"' ERR


(( $BE_VERBOSE )) && shift                           || true
(( ! EUID      )) || ! echo -e "${UNPRIVILEGED_MSG}" || false
(( $#          )) || ! echo -e "${INVALID_ARG_MSG}"  || false


Init                   $1
ListParabolaDependents $1
Cleanup