-
Notifications
You must be signed in to change notification settings - Fork 0
/
fur
executable file
·177 lines (161 loc) · 6.36 KB
/
fur
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Fedora User Repository CLI
Spec parsing limitations:
Sources and Patches must be added by url
"""
import sys
import os
import re
import getpass
import urllib.request
import platform
def get_fur_url():
"""
Loads Fedora User Repository url from /etc/fedora-user-repository.
"""
with open('/etc/fedora-user-repository', 'r') as url_file:
url = url_file.read().replace('\n', '')
return url
def get_fedora_version():
"""
Outputs Fedora version.
"""
with open('/etc/fedora-release', 'r') as release_file:
fedora_release = release_file.read().replace('\n', '')
fedora_release_num = ""
for single_char in fedora_release.split():
if single_char.isdigit():
fedora_release_num = fedora_release_num + single_char
return fedora_release_num
def get_spec(name, path=0):
"""
Accepts package name and path (optionally). Tries to get spec file from Fedora User Repository.
"""
url = get_fur_url()
user = getpass.getuser()
os.system("mkdir -p ~/.fur/" + name)
if path != 0:
try:
urllib.request.urlretrieve(url + name + ".spec", "/home/" + user + "/.fur/" + path + "/" + name + ".spec")
except:
os.system("rm -R ~/.fur/" + name)
raise NameError("UNK_PKG")
else:
try:
urllib.request.urlretrieve(url + name + ".spec", "/home/" + user + "/.fur/" + name + "/" + name + ".spec")
except:
os.system("rm -R ~/.fur/" + name)
raise NameError("UNK_PKG")
def get_sources(name):
"""
Accepts package name, tries to get all source files (and patches) with Fedora's spectool.
"""
user = getpass.getuser()
os.system("spectool -all --get-files --directory /home/" + user + "/.fur/" + name + "/ /home/" + user + "/.fur/" + name + "/" + name + ".spec")
def fur_dependencies(name):
"""
Accepts package name, runs trough entire spec file for ALL BuildRequires and Requires.
Try to install them from Fedora User Repository, ignores missing dependencies, which will be solved and installed by dnf later.
"""
user = getpass.getuser()
fur_requires_array = []
fur_build_requires_array = []
try:
spec_file = open("/home/" + user + "/.fur/" + name + "/" + name + ".spec", "r")
print("Solving dependencies from Fedora User Repository...")
for spec_line in spec_file:
if "#BuildRequires" in spec_line:
# Skip commented out BuildRequires
continue
elif "#Requires" in spec_line:
# Skip commented out Requires
continue
elif "BuildRequires" in spec_line:
spec_line = re.sub(r"BuildRequires: ", "", spec_line)
spec_line = spec_line.rstrip()
fur_build_requires_array.append(spec_line)
elif "Requires" in spec_line:
spec_line = re.sub(r"Requires: ", "", spec_line)
spec_line = spec_line.rstrip()
fur_requires_array.append(spec_line)
except:
raise NameError("BAD_SPEC")
for fur_requires in fur_requires_array:
# Try to get all the "Requires" from Fedora User Repository
os.system("fur --quiet install " + fur_requires)
for fur_build_requires in fur_build_requires_array:
os.system("fur --quiet source" + fur_build_requires + " /home/" + user + ".fur/" + name + "/SRPMS/")
def build_srpm(name, path=0):
"""
Accepts package name and (optionally) path where it'll store .src.rpm file made from spec and sources available.
"""
user = getpass.getuser()
if path != 0:
os.system("rpmbuild -bs " + "/home/" + user + "/.fur/" + name + "/" + name + ".spec --define '_sourcedir /home/" + user + "/.fur/" + path + "/' --define '_topdir /home/" + user + "/.fur/" + path + "'")
else:
os.system("rpmbuild -bs " + "/home/" + user + "/.fur/" + name + "/" + name + ".spec --define '_sourcedir /home/" + user + "/.fur/" + name + "/' --define '_topdir /home/" + user + "/.fur/" + name + "'")
def build_package(name):
"""
Accepts package name. Compiles all .src.rpm files to the rpm, recursively tries to satisfy build time dependencies.
"""
user = getpass.getuser()
arch = platform.machine()
srpms_dir = "/home/" + user + "/.fur/" + name + "/SRPMS/"
os.system("mockchain -r fedora-" + get_fedora_version() + "-" + arch + " -m --resultdir=/home/" + user + "/.fur/" + name + "/RPMS/ " + srpms_dir + "/*")
os.system("rm ~/.fur/" + name + "/*.src.rpm")
os.system("rm ~/.fur/" + name + "/RPMS/*.src.rpm")
def clean_debug(name):
"""
Accepts package name, removes all -debuginfo packages.
"""
os.system("rm ~/.fur/" + name + "/*-debuginfo*")
def install_package(name):
"""
Accepts package name, calls dnf to install all compiled rpm files.
"""
os.system("sudo dnf install ~/.fur/" + name + "/RPMS/*.rpm")
def postinst_clean(name):
"""
Accepts package name, removes entire working directory inside ~/.fur/ .
"""
os.system("rm -R ~/.fur/" + name)
if "install" in sys.argv:
package = sys.argv[(sys.argv.index("install") + 1)]
try:
get_spec(package)
except NameError:
if "--quiet" not in sys.argv:
print("Invalid package name or network problem, exiting...")
sys.exit()
try:
get_sources(package)
except NameError("SOURCE_MISS"):
print("Cannot download one or more source files, exiting...")
sys.exit()
except NameError("BAD_SPEC"):
print("Provided spec file is broken, exiting...")
sys.exit()
build_srpm(package)
fur_dependencies(package)
build_package(package)
clean_debug(package)
install_package(package)
if "--no-clean" not in sys.argv:
postinst_clean(package)
elif "source" in sys.argv:
package = sys.argv[(sys.argv.index("source") + 1)]
path = sys.argv[(sys.argv.index("source") + 2)]
try:
get_spec(package, path)
except NameError:
if "--quiet" not in sys.argv:
print("Invalid package name or network problem, exiting...")
sys.exit()
get_sources(package)
build_srpm(package, path)
else:
print("Use fur install [package] to install something or fur source [package] [path] to get srpm")
print("Other commands are not implemented yet...")
sys.exit()