1 | #!/usr/bin/python3 |
---|
2 | |
---|
3 | # Copyright (C) 2015 Martin Wimpress <code@ubuntu-mate.org> |
---|
4 | # Copyright (C) 2016 Luke Horwell <lukehorwell37+code@gmail.com> |
---|
5 | # |
---|
6 | # This program is free software; you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation; either version 2 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # This program is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with this program; if not, write to the |
---|
18 | # Free Software Foundation, Inc., |
---|
19 | # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | |
---|
21 | import os, sys, platform, subprocess, json, glob, urllib.request |
---|
22 | |
---|
23 | def del_apt_sources(listname): |
---|
24 | for filename in glob.glob(os.path.join('/','etc','apt','sources.list.d','*'+listname+'*.list*')): |
---|
25 | os.remove(filename) |
---|
26 | |
---|
27 | def add_apt_sources(source, listname): |
---|
28 | #subprocess.call(['aptdcon', '--add-repository=' + source, '--sources-file', listname + '.list']) |
---|
29 | sources_file = os.path.join('/','etc','apt','sources.list.d',listname + '.list') |
---|
30 | print('Writing: ' + sources_file) |
---|
31 | with open(sources_file, 'w') as f: |
---|
32 | f.write('\n'.join(source)) |
---|
33 | |
---|
34 | def add_apt_key_from_url(key_url): |
---|
35 | #subprocess.call(['aptdcon', '--add-vendor-key=' + os.path.join('/', 'tmp', 'pub.key')]) |
---|
36 | temp_key = os.path.join('/', 'tmp', 'pub.key') |
---|
37 | print('Processing: ' + key_url) |
---|
38 | if os.path.exists(temp_key): |
---|
39 | os.remove(temp_key) |
---|
40 | |
---|
41 | # Download the file from `url` and save it locally under `file_name`: |
---|
42 | with urllib.request.urlopen(key_url) as response, open(temp_key, 'wb') as out_file: |
---|
43 | data = response.read() # a `bytes` object |
---|
44 | out_file.write(data) |
---|
45 | subprocess.call(['apt-key', 'add', temp_key]) |
---|
46 | |
---|
47 | def add_apt_key_from_keyserver(keyserver, key): |
---|
48 | subprocess.call(['apt-key', 'adv', '--keyserver', keyserver, '--recv-keys', key]) |
---|
49 | |
---|
50 | def enable_ppa(ppa): |
---|
51 | subprocess.call(['apt-add-repository', '--enable-source', '--yes', ppa]) |
---|
52 | |
---|
53 | def enable_partner_repository(): |
---|
54 | subprocess.call(['apt-add-repository', '--enable-source', '--yes', 'http://archive.canonical.com/ubuntu partner']) |
---|
55 | |
---|
56 | |
---|
57 | def finish(err=0): |
---|
58 | ''' Close the application ''' |
---|
59 | print('--------------------------------------------------------------') |
---|
60 | sys.exit(err) |
---|
61 | |
---|
62 | if __name__ == "__main__": |
---|
63 | ''' |
---|
64 | This script requires the following variables: |
---|
65 | [0] = Path to the Application Index JSON |
---|
66 | [1] = Function to perform |
---|
67 | [2] = Program's category |
---|
68 | [3] = Program ID |
---|
69 | [4] = "Target" instructions to read. Contains a codename or "all" |
---|
70 | |
---|
71 | This script will directly read the Application Index JSON. |
---|
72 | ''' |
---|
73 | print('--- Repository Installer -------------------------------------') |
---|
74 | |
---|
75 | # Set important variables. |
---|
76 | os_version = platform.dist()[1] |
---|
77 | codename = platform.dist()[2] |
---|
78 | |
---|
79 | try: |
---|
80 | json_path = sys.argv[1] |
---|
81 | function = sys.argv[2] |
---|
82 | category = sys.argv[3] |
---|
83 | program_id = sys.argv[4] |
---|
84 | target = sys.argv[5] |
---|
85 | except: |
---|
86 | print('This script requires parameters that are retrieved via ubuntu-mate-welcome.') |
---|
87 | finish(1) |
---|
88 | |
---|
89 | # Load the Application Index |
---|
90 | if os.path.exists(json_path): |
---|
91 | with open(json_path) as data_file: |
---|
92 | index = json.load(data_file) |
---|
93 | else: |
---|
94 | print('The application index does not exist: "' + json_file + '".') |
---|
95 | finish(1) |
---|
96 | |
---|
97 | # Perform functions based on the parameters. |
---|
98 | if function == 'add_apt_key_from_keyserver': |
---|
99 | keyserver = index[category][program_id]['pre-install'][target]['apt-key-server'][0] |
---|
100 | key = index[category][program_id]['pre-install'][target]['apt-key-server'][1] |
---|
101 | add_apt_key_from_keyserver(keyserver, key) |
---|
102 | |
---|
103 | elif function == 'add_apt_key_from_url': |
---|
104 | url = index[category][program_id]['pre-install'][target]['apt-key-url'] |
---|
105 | add_apt_key_from_url(url) |
---|
106 | |
---|
107 | elif function == 'add_apt_sources': |
---|
108 | source_raw = index[category][program_id]['pre-install'][target]['apt-sources'] |
---|
109 | source_data = [] |
---|
110 | for line in source_raw: |
---|
111 | source_data.append(line.replace('OSVERSION',os_version).replace('CODENAME',codename)) |
---|
112 | list_name = index[category][program_id]['pre-install'][target]['source-file'] |
---|
113 | list_name = list_name.replace('OSVERSION',os_version).replace('CODENAME',codename) |
---|
114 | add_apt_sources(source_data, list_name) |
---|
115 | |
---|
116 | elif function == 'del_apt_sources': |
---|
117 | list_name = index[category][program_id]['pre-install'][target]['source-file'] |
---|
118 | list_name = list_name.replace('OSVERSION',os_version).replace('CODENAME',codename) |
---|
119 | del_apt_sources(list_name) |
---|
120 | |
---|
121 | elif function == 'enable_partner_repository': |
---|
122 | enable_partner_repository() |
---|
123 | |
---|
124 | elif function == 'enable_ppa': |
---|
125 | ppa = index[category][program_id]['pre-install'][target]['enable-ppa'] |
---|
126 | enable_ppa(ppa) |
---|
127 | |
---|
128 | else: |
---|
129 | print('Invalid function specified: ' + function) |
---|
130 | finish(1) |
---|
131 | |
---|
132 | print('Successfully performed function: "' + function + '" for "' + program_id + '".') |
---|
133 | finish(0) |
---|