forked from UbuntuBudgie/budgie-welcome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
budgie-welcome-privileged-actions
executable file
·208 lines (157 loc) · 5.7 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
#!/usr/bin/python3
# -*- coding:utf-8 -*-
#
# Copyright 2016 Ubuntu Mate
# Copyright 2016-2018 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
from softwareproperties.SoftwareProperties \
import SoftwareProperties, shortcut_handler
import aptsources
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
p = subprocess.Popen(['grep', '^deb\ http', filename])
retval = p.wait()
if retval == 0:
found = True
return found
def add_repos(repos, key_commands):
if not repos: return 0
release_code = platform.dist()[2] # → xenial, yakkety, zesty
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
distro = aptsources.distro.get_distro()
distro.get_sources(sp.sourceslist)
components = [comp.name for comp in distro.source_template.components]
print (components)
print (repos)
if repo in components:
if repo not in distro.enabled_comps:
force_update = True
distro.enable_component(repo)
else:
if check_if_installed_ppa( release_code, repo ) == False:
sp.add_source_from_shortcut(shortcut_handler(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")
packages = info.get("packages")
excludes = info.get("excludes")
key_commands = info.get("key-commands")
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()
return retval
def remove(fname, code):
info = pdata.getPackageInfo(fname, code)
packages = info["packages"]
process = subprocess.Popen(['apt', 'remove', '-y'] + packages)
retval = process.wait()
return retval
def run_script(fname):
process = subprocess.Popen(['sh', fname])
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 = platform.dist()[2] # → xenial, yakkety, zesty
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]
pdata = PackageData(json_path)
if action == 'INSTALL':
retval = install(fname, code)
elif action == 'REMOVE':
retval = remove(fname, code)
elif action == 'SCRIPT':
# fname -> script name, code -> requires root privilege?
retval = run_script(fname, code)
sys.exit(retval)