summaryrefslogtreecommitdiff
path: root/blacklist-diffs
blob: de46db4267789f7c760f6b0e9397d54ada1c46df (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
#!/bin/bash

# blacklist-diffs - locates the upstream repo in which each blacklist entry exists;
# and identifies blacklist entries which do not exist upstream
#
# Copyright (C) 2023 bill-auger <bill-auger@programmer.net>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
#
# TODO: determine which non-existing packages should exist on a per-arch basis,
#       perhaps with a cross-arch compare filter, or per-arch supplemental blacklists
#       (eg: blacklist.txt (all arches) , blacklist-armv7h.txt (armv7h only) , ...)


## constants ##

readonly DEBUG=0

readonly BLACKLIST=/usr/share/doc/your-freedom/blacklist.txt
readonly ARCH_MIRROR=http://mirror.cyberbits.eu/archlinux
readonly ARCH_REPOS=( community{,-testing} core extra testing multilib{,-testing} )
readonly TEMP_DB_NAME=blacklist-diffs

readonly BLACKLIST_ERR_MSG="this script requires the 'your-freedom' package"
readonly UNPRIVILEGED_ERR_MSG="this script requires super-user privileges"
readonly CBLUE='\033[0;36m'
readonly CRED='\033[0;31m'
readonly CYELLOW='\033[0;33m'
readonly CEND='\033[0m'

BlacklistedPkgs=( $(sed 's|:.*||' ${BLACKLIST}) )
DbTempdirs=() # Init()


## helpers ##

DBG() # (log_fmt [fmt_args]*)
{
  local log_fmt=$1
  local fmt_args=( "${@:2}" )

  (( DEBUG )) && printf "${CYELLOW}${log_fmt}${CEND}\n" "${fmt_args[@]}" >&2 || :
}

Log() # (log_fmt [fmt_args]*)
{
  local log_fmt=$1
  local fmt_args=( "${@:2}" )

  printf "${CBLUE}${log_fmt}${CEND}\n" "${fmt_args[@]}" >&2
}

LogError() # (log_fmt [fmt_args]*)
{
  local log_fmt=$1
  local fmt_args=( "${@:2}" )

  printf "${CRED}ERROR: ${log_fmt}${CEND}\n" "${fmt_args[@]}" >&2
}

Exit() # (log_fmt [fmt_args]*)
{
  LogError "$@" ; exit 1 ;
}

PacmanOpts() # (db_tempdir)
{
  local db_tempdir=$1

  echo "--dbpath=${db_tempdir} --config=${db_tempdir}/pacman.conf"
}


## business ##

Init() # (upstream_arch mirror_url repos*)
{
  local upstream_arch=$1
  local mirror_url=$2
  local repos=( ${@:3} )

  # Setup the temporary directory
  local db_tempdir="$(mktemp --tmpdir -d "${TEMP_DB_NAME}-${upstream_arch}.XXXXXXXXXX")"
  DbTempdirs+=( ${db_tempdir} )

  # populate the temporary package database
  Log "updating database ...."
  cd ${db_tempdir}
  printf "[options]\nArchitecture = ${upstream_arch}\n"              > pacman.conf
  for repo in ${repos[@]}
  do  printf "[${repo}]\nServer = ${mirror_url}/\$repo/os/\$arch\n" >> pacman.conf
  done
  sudo pacman $(PacmanOpts ${db_tempdir}) -Sy &> /dev/null

  echo ${db_tempdir}


DBG "Init() db_tempdir=${db_tempdir}=$(printf "\n\t%s" $(ls ${db_tempdir}))"
}

Compare() # (db_tempdir)
{
  local db_tempdir=$1
  local upstream_arch=$(sed "s|.*/${TEMP_DB_NAME}-\([^\.]*\)\..*|\1|" <<<${db_tempdir})
  local upstream_pkgs=( $(pacman $(PacmanOpts ${db_tempdir}) -Ss | cut -d ' ' -f 1) )
  local blacklisted_pkg upstream_pkg


DBG "Compare() db_tempdir=${db_tempdir}"
DBG "Compare() PacmanOpts=$(PacmanOpts ${db_tempdir})"
DBG "Compare() upstream_arch=${upstream_arch}"
DBG "Compare() upstream_pkgs=${upstream_pkgs[*]:0:4}"
DBG "Compare() n_blacklisted_pkgs=${#BlacklistedPkgs[*]}"
DBG "Compare() n_upstream_pkgs=${#upstream_pkgs[*]}"


  Log "comparing (${#BlacklistedPkgs[*]}) blacklisted packages to (${#upstream_pkgs[*]}) ${upstream_arch} packages ...."
  for blacklisted_pkg in ${BlacklistedPkgs[*]}
  do  upstream_pkg="$(grep ^[^/]*/${blacklisted_pkg}$ <(printf "%s\n" ${upstream_pkgs[*]}) || \
                      echo '<NONE>' | cut -d ' ' -f 1               )"
      echo "${upstream_pkg} ${blacklisted_pkg}"
  done | sort | column -t
}


trap "for tmpdir in ${DbTempdirs[*]} ; do rm -rf -- ${tmpdir} ; done ;" EXIT


[[ -f "${BLACKLIST}"             ]] || Exit "${BLACKLIST_ERR_MSG}"
(( ${#BlacklistedPkgs[*]}        )) || Exit "${BLACKLIST_ERR_MSG}"
sudo pacman -Ss pacman &> /dev/null || Exit "${UNPRIVILEGED_ERR_MSG}"


Compare $(Init x86_64 ${ARCH_MIRROR} ${ARCH_REPOS[*]})