This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
forked from matu6968/assets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
installextension.py
264 lines (221 loc) · 11.3 KB
/
installextension.py
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
#!/usr/bin/python3
########################################################################
# #
# install-gnome-shell-extension.py #
# #
# In order to prevent the customized default schema files in #
# /usr/share/glib-2.0/schemas from being overridden, remove all #
# distribution specific schema override files in this directory by #
# moving them to a backup directory in /tmp. #
# #
# Copyright (C) 2019 PJ Singh <[email protected]> #
# #
########################################################################
########################################################################
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms 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. #
# #
# 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 General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses>. #
# #
########################################################################
"""
Install the specified gnome shell extension.
Run this program as root or using sudo.
Usage:
$ sudo ./install-gnome-shell-extension extension_id [shell_version]
Examples:
# Install Dash to Dock
$ sudo ./install-gnome-shell-extension 307
# Install Dash to Dock for Gnome Shell version 3.38
$ sudo ./install-gnome-shell-extension 307 3.38
Arguments:
extension_id (int)
The extension number
shell_version (str)
The Gnome shell version
Returns:
None
"""
import json
import shutil
import subprocess
import sys
import tempfile
import traceback
import urllib
import urllib.request
import xml.etree.ElementTree as ElementTree
import zipfile
########################################################################
# References
########################################################################
# https://docs.python.org/3/howto/urllib2.html
########################################################################
# Globals & Constants
########################################################################
EXTENSION_INFORMATION_URL_TEMPLATE = 'https://extensions.gnome.org/extension-info/?pk=%i&shell_version=%s'
EXTENSION_DOWNLOAD_URL_TEMPLATE = 'https://extensions.gnome.org%s'
EXTENSION_DIRECTORY_TEMPLATE = '/usr/share/gnome-shell/extensions/%s'
# EXTENSION_DIRECTORY_TEMPLATE = '/home/psingh/Temp/test/%s'
########################################################################
# Functions
########################################################################
#-----------------------------------------------------------------------
# Arguments
#-----------------------------------------------------------------------
def get_arguments():
extension_id = None
try:
extension_id = int(sys.argv[1])
except IndexError:
pass
except ValueError:
pass
shell_version = None
try:
shell_version = str(sys.argv[2])
except IndexError:
pass
except ValueError:
pass
return extension_id, shell_version
#-----------------------------------------------------------------------
# Gnome Shell Version
#-----------------------------------------------------------------------
def get_shell_version():
# Try to get the Gnome shell version from
# '/usr/share/gnome/gnome-version.xml'.
try:
filepath = '/usr/share/gnome/gnome-version.xml'
tree = ElementTree.parse(filepath)
root = tree.getroot()
platform = root.find('platform').text
shell_version = platform
minor = root.find('minor').text
shell_version = shell_version + '.' + minor
try:
micro = root.find('micro').text
shell_version = shell_version + '.' + micro
except AttributeError:
print('• Ignoring. Unable to get Gnome micro version from.')
print('• The Gnome Shell version is %s.' % shell_version)
return shell_version
except Exception as exception:
print('• Warning. Unable to get the Gnome shell version.')
print('• The exception is %s.' % exception)
# print('• The tracekback is %s.' % traceback.format_exc())
# Try to get the Gnome shell version using 'gnome-shell --version'.
try:
output = subprocess.check_output(['gnome-shell', '--version'])
output = output.strip()
output = output.decode("utf-8")
output = output.split()
shell_version = output[-1]
print('• The Gnome Shell version is %s.' % shell_version)
return shell_version
except Exception as exception:
print('• Warning. Unable to get the Gnome shell version.')
print('• The exception is %s.' % exception)
# print('• The tracekback is %s.' % traceback.format_exc())
return ''
#-----------------------------------------------------------------------
# Download the Extension
#-----------------------------------------------------------------------
def download_extension(extension_id, shell_version):
# Drop all trailing decimal zero(s) from the shell version.
if int(float(shell_version)) == float(shell_version):
shell_version = str(int(float(shell_version)))
else:
shell_version = str(float(shell_version))
url = EXTENSION_INFORMATION_URL_TEMPLATE % (extension_id, shell_version)
print('• The extension information url is %s.' % url)
try:
with urllib.request.urlopen(url) as response:
try:
# Get extension information.
data = response.read()
info = json.loads(data)
name = info.get('name')
print('• The extension name is %s.' % name)
uuid = info.get('uuid')
print('• The extension uuid is %s.' % uuid)
shell_versions = info.get('shell_version_map')
print('• Supported shell versions are %s.' % ', '.join(list(shell_versions.keys())))
if shell_version in shell_versions:
# Ex: url = 'https://extensions.gnome.org/download-extension/[email protected]?version_tag=15925'
url = EXTENSION_DOWNLOAD_URL_TEMPLATE % info.get('download_url')
print('• The extension download url is %s.' % url)
# Download the extension.
try:
with urllib.request.urlopen(url) as response:
try:
# Extract the extension
# extension_directory = EXTENSION_DIRECTORY_TEMPLATE % uuid
# extension_filepath = '%s/test.zip' % extension_directory
# with open(extension_filepath, 'wb') as target:
# shutil.copyfileobj(response, target)
# print('• Saved the extension as %s.' % extension_filepath)
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
shutil.copyfileobj(response, temp_file)
extension_directory = EXTENSION_DIRECTORY_TEMPLATE % uuid
print('• The extension directory is %s.' % extension_directory)
try:
# Unzip the temporary file to the target directory.
with zipfile.ZipFile(temp_file, 'r') as zip_file:
# The extension directory path will be created.
zip_file.extractall(extension_directory)
print(
'• Successfully extracted %s extension %d for Gnome %s to %s.' %
(uuid,
extension_id,
shell_version,
extension_directory))
except Exception as exception:
print('• Error. Unable to extract extension to %s.' % extension_directory)
print('• The exception is %s.' % exception)
# print('• The tracekback is %s.' % traceback.format_exc())
except Exception as exception:
print('• Error. Unable to save extension as a temporary file from %s.' % url)
print('• The exception is %s.' % exception)
# print('• The tracekback is %s.' % traceback.format_exc())
except urllib.error.URLError as exception:
print('• Error. Unable to access %s.' % url)
print('• The exception is %s.' % exception)
# print('• The tracekback is %s.' % traceback.format_exc())
else:
print('• Error. Shell version %s is not supported by this extension.' % shell_version)
except Exception as exception:
print('• Error. Unable to get extension information from %s.' % url)
print('• The exception is %s.' % exception)
print('• The tracekback is %s.' % traceback.format_exc())
except urllib.error.URLError as exception:
print('• Error. Unable to access %s.' % url)
print('• The exception is %s.' % exception)
# print('• The tracekback is %s.' % traceback.format_exc())
#-----------------------------------------------------------------------
# Execute
#-----------------------------------------------------------------------
extension_id, shell_version = get_arguments()
if extension_id:
print('Install extension %d...' % extension_id)
else:
print('• Error. An extension id number is required.')
exit()
system_shell_version = get_shell_version()
shell_version = shell_version or system_shell_version
if shell_version:
print('• Install extension for Gnome Shell version %s.' % shell_version)
else:
print('• Error. The Gnome Shell version is required.')
exit()
# Install the extension.
download_extension(extension_id, shell_version)