-
Notifications
You must be signed in to change notification settings - Fork 8
/
copr
151 lines (122 loc) · 5.22 KB
/
copr
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
#!/bin/bash
# assign variables for author, reponame and releaseversion
author="$(echo $2 | cut -d '/' -f1)"
reponame="$(echo $2 | cut -d '/' -f2)"
releasever="$(rpm -E %fedora)"
repofile="/etc/yum.repos.d/_copr_$author-$reponame.repo"
repofilebak="~/COPR/_copr_$author-$reponame.repo"
# the original copr warning message
question=$(cat <<EOF
Enabling a Copr repository. Please note that this repository is not part
of the main distribution, and quality may vary.
The Fedora Project does not exercise any power over the contents of
this repository beyond the rules outlined in the Copr FAQ at
<https://docs.pagure.org/copr.copr/user_documentation.html#what-i-can-build-in-copr>,
and packages are not held to any quality or security level.
Please do not file bug reports about these packages in Fedora
Bugzilla. In case of problems, contact the owner of this repository.
Do you really want to enable copr.fedorainfracloud.org/$author/$reponame? [y/N]:
EOF
)
# the helptext
helptext=$(cat << EOF
====== Experimental COPR Command in non-DNF Fedora Distributions ======
This Tool is not made or supported by the Fedora Project,
but aims to reproduce the "dnf copr" functionalities for easily adding COPRs.
Usage: ./copr [OPTION] [ARGUMENT]
Usage: copr [OPTION] [ARGUMENT]
Options:
enable Add COPR repository.
remove Remove COPR repository after backing it up.
list List all COPR repositories in your repo folder.
search Search for a COPR repository by name (in your Browser)
disable Keep a repo but disable it
enable Make a disabled repo active
help Display this help text.
Argument:
Name of the COPR repository (for search) or "author/repo" (for install and remove)
Examples:
copr enable kdesig/kde-nightly-qt6
copr remove kdesig/kde-nightly-qt6
copr list
copr search uutils
For help, visit https://discussion.fedoraproject.org.
EOF
)
# Main loop
if [[ "$1" == "enable" ]]; then
if [[ -f "$repofile" ]]; then
# If the repo file exists, enable it
sed -i 's/enabled=0/enabled=1/g' "$repofile" && echo "Repository $author/$reponame enabled" || echo "Already enabled or repo not found."
else
# If the repo file does not exist, download it
while true; do
read -p "$question" yn
case $yn in
[Yy]* )
echo "$author/$reponame -> $releasever"
curl -fsSL "https://copr.fedorainfracloud.org/coprs/$author/$reponame/repo/fedora-$releasever/$author-$reponame-fedora-.repo" | pkexec tee "$repofile" && break
;;
* )
echo "Unknown option, exiting." && break
;;
esac
done
fi
fi
# remove the named repo
elif [[ "$1" == "remove" ]]; then
# backup the repofile and change owner to user
mkdir ~/COPR
if [[ -e "$repofile" ]]; then
# Backup existing repo file and set ownership to the user
pkexec cp $repofile $repofilebak \
&& pkexec chown $USER "$backup_file" \
&& echo "Backup created in your home folder."
# Remove the repo file
pkexec rm $repofile \
&& echo "COPR Repository $author/$reponame removed." \
|| echo "ERROR removing COPR repository."
else
# search for the reponame
echo "Repo with name $author/$reponame not found, trying a search..."
result=$(grep -ril --include='*.repo' "$reponame" /etc/yum.repos.d/)
# if not found, exit
if [[ -z "$result" ]]; then
echo "No repositories containing '$reponame' were found." \
&& break
else
# if found, make sure it is the right one
read -rp "Repo '$result' was found. Do you want to backup and remove this repo? (Y/N) " answer
if [[ "${answer,,}" == "y" ]]; then
pkexec cp "$result" "$backup_file" \
&& pkexec chown "$(whoami)" "$backup_file" \
&& pkexec rm "$result" \
&& echo "COPR Repository '$reponame' removed." \
|| echo "ERROR removing COPR repository."
fi
fi
fi
# list only COPR repositories
elif [[ "$1" == "list" ]]; then
find /etc/yum.repos.d/ -type f -name '_copr*' -exec basename {} \; | sed 's/_copr_//; s/\.repo$//'
# search, this is just implemented through the browser
elif [[ "$1" == "search" ]]; then
xdg-open "https://copr.fedorainfracloud.org/coprs/fulltext/?fulltext=$2"
# disable a repo
elif [[ "$1" == "disable" ]]; then
sed 's/enabled=1/enabled=0/g' $repofile > /dev/null \
&& echo "Repository $author/$reponame disabled." \
|| echo "Already disabled or repo not found."
# display the helptext
elif [[ "$1" == "-h" || "$1" == "--h" || "$1" == "-help" || "$1" == "--help" || "$1" == "help" ]]; then
echo "$helptext"
fi
##### SECURE REPO FILES #####
# changing unix permissions to make them root-owned and read-only by world
# chang SELinux labels to "system_u:object_r:system_conf_t:s0" like the default Fedora repos
echo "Securing the repo files against manipulation. Please enter your password one last time."
pkexec sh -c "chown root:root /etc/yum.repos.d/* && \
chmod 744 /etc/yum.repos.d/* && \
chcon -R system_u:object_r:system_conf_t:s0 /etc/yum.repos.d/" || { echo "Failed to secure the repo files!"; exit 1; }
echo