-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
budgie-welcome-privileged-actions
executable file
·266 lines (207 loc) · 7.52 KB
/
budgie-welcome-privileged-actions
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/python3
# -*- coding:utf-8 -*-
#
# Copyright 2016 Ubuntu Mate
# Copyright 2016-2020 Ubuntu Budgie Developers
#
# budgie-welcome is free software: you can redistribute it and/or modify
# it under the temms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ubuntu Budgie welcome 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ubuntu Budgie welcome. If not, see <http://www.gnu.org/licenses/>.
#
import subprocess
import sys
import json
import os
from softwareproperties.SoftwareProperties \
import SoftwareProperties, shortcut_handler
import aptsources
try:
import distro
except:
import platform
import glob
'''
Should replace apt command with python-apt module.
'''
def check_if_installed_source(release_code, source_name):
source_part = source_name[4:]
for file in glob.glob("/etc/apt/sources.list/*.list"):
with open (file) as f:
for line in f:
if source_part in line and line.startswith("deb"):
return True
with open ('/etc/apt/sources.list') as f:
for line in f:
if line.startswith(source_name):
return True
return False
def check_if_installed_ppa(release_code, ppa_name):
found = False
# check if first three letters is "deb" or "ppa"
# deb can be found in both /etc/apt/sources.list as well as
# in /etc/apt/sources.list.d
if ppa_name.startswith("deb"):
return check_if_installed_source(release_code, ppa_name)
# for the first element of the codename (e.g. zesty)
# repo split by the / character to get an array
# first element is the ppa organisation and second
# is the ppa name
ppa = ppa_name[4:].split('/')
filename = ppa[0] + "-ubuntu-" + ppa[1] + "-" + \
release_code + ".list"
filename = '/etc/apt/sources.list.d/' + filename
if os.path.isfile(filename):
p = subprocess.Popen(['grep', r'^deb\ http', filename])
retval = p.wait()
if retval == 0:
return True
filename = ppa[0] + "-ubuntu-" + ppa[1] + "-" + \
release_code + ".sources"
filename = '/etc/apt/sources.list.d/' + filename
if os.path.isfile(filename):
found = True
p = subprocess.Popen(['grep', 'Enabled: no', filename])
retval = p.wait()
if retval == 0:
found = False
if os.stat(filename).st_size <= 1:
found = False
return found
def get_release_code():
release_code = ""
try:
release_code = distro.codename().split(' ')[0].lower()
except:
release_code = platform.dist()[2] # → trusty, wily, xenial
return release_code
def add_repos(repos, key_commands):
if not repos: return 0
release_code = get_release_code()
if "all" in repos : repos = repos["all"]
else : repos = repos.get(release_code)
# possibly repo is not required for this release
if not repos: return 0
sp = SoftwareProperties()
force_update = False
for repo in repos:
# check if its a component that should be added/removed
aptdistro = aptsources.distro.get_distro()
aptdistro.get_sources(sp.sourceslist)
components = [comp.name for comp in aptdistro.source_template.components]
if repo in components:
if repo not in aptdistro.enabled_comps:
force_update = True
aptdistro.enable_component(repo)
else:
if check_if_installed_ppa( release_code, repo ) == False:
#sp.add_source_from_shortcut(shortcut_handler(repo))
subprocess.call(['add-apt-repository', '--yes', '--no-update', repo])
force_update = True
sp.sourceslist.save()
if key_commands:
for key_command in key_commands:
subprocess.call(key_command, shell=True)
if force_update:
p = subprocess.Popen(['apt', 'update'])
return p.wait()
return False
def install(fname, code):
info = pdata.getPackageInfo(fname, code)
repos = info.get("repos")
snaps = info.get("snap")
packages = info.get("packages")
excludes = info.get("excludes")
key_commands = info.get("key-commands")
retval = 0
if snaps and len(snaps) > 0:
# we are dealing with snap installs
for snap in snaps:
process = subprocess.Popen(['snap', 'install', snap])
retval += process.wait()
return retval
# we are dealing with repo and package installs
retval = add_repos(repos, key_commands)
if excludes != None:
for exclude in excludes:
packages.append(exclude + '-')
if packages:
process = subprocess.Popen(['apt', 'install', '--allow-unauthenticated', '-y'] + packages)
retval += process.wait()
if retval != 0:
process = subprocess.Popen(['apt', 'update'])
retval = process.wait()
process = subprocess.Popen(['apt', 'install', '--allow-unauthenticated', '-y'] + packages)
retval += process.wait()
return retval
def remove(fname, code):
info = pdata.getPackageInfo(fname, code)
snaps = info.get("snap")
removekey_commands = info.get("removekey-commands")
retval = 0
if snaps and len(snaps) > 0:
# we are dealing with snap removals
for snap in snaps:
process = subprocess.Popen(['snap', 'remove', snap])
retval += process.wait()
return retval
# we are dealing with package removals
packages = info["packages"]
process = subprocess.Popen(['apt', 'remove', '-y'] + packages)
retval = process.wait()
if removekey_commands:
for removekey_command in removekey_commands:
subprocess.call(removekey_command, shell=True)
return retval
def run_script(fname, code):
process = subprocess.Popen(['sh', fname, code])
retval = process.wait()
return retval
class PackageData(object):
def __init__(self, json_path):
# app._data_path + "/config/packages.json"
with open(json_path) as file:
self.data = json.load(file)
def getPackageInfo(self, filename, code):
release_code = get_release_code()
full_code = code + "#" + release_code
try:
if full_code in self.data[filename]:
return self.data[filename][full_code]
return self.data[filename][code]
except KeyError as e:
print(e)
return None
if __name__ == "__main__":
'''
args[1] action to perform (INSTALL/REMOVE/SCRIPT)
args[2] file name in which package is listed
args[3] code name for package
args[4] path to json file containing package details
'''
args = sys.argv
if len(args) < 5:
print("Expected arguments not found")
sys.exit(-1)
action = args[1]
fname = args[2]
code = args[3]
json_path = args[4]
if action == 'INSTALL':
pdata = PackageData(json_path)
retval = install(fname, code)
elif action == 'REMOVE':
pdata = PackageData(json_path)
retval = remove(fname, code)
elif action == 'SCRIPT':
# fname -> script name, code -> requires root privilege?
retval = run_script(fname, code)
sys.exit(retval)