-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_packages.py
executable file
·347 lines (308 loc) · 12 KB
/
install_packages.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import os
import pathlib
import subprocess
import sys
from typing import List
def is_apt_repository_added(repo_url: str | None) -> bool:
if repo_url is None:
return False
try:
result = subprocess.run(['apt-cache', 'policy'], capture_output=True, text=True, check=True)
return repo_url in result.stdout
except subprocess.CalledProcessError:
return False
def add_apt_repository(repo: str, url: str | None, dry_run: bool) -> None:
if is_apt_repository_added(url):
print(f"APT repository '{repo}' is already added.")
return
if not dry_run:
subprocess.run(['sudo', 'add-apt-repository', '-y', repo])
else:
print(f"Would add APT repository: {repo}")
def update_apt(dry_run: bool) -> None:
if not dry_run:
subprocess.run(['sudo', 'apt', 'update'])
else:
print('Would update APT')
def install_apt_packages(packages: List[str], dry_run: bool) -> None:
if not dry_run:
cmd = ['sudo', 'apt', 'install', '-y']
cmd.extend(packages)
subprocess.run(cmd)
else:
package_str = '\n'.join(packages)
print(f'Would install packages:\n{package_str}')
def install_snap_packages(packages: List[str], dry_run: bool) -> None:
if not dry_run:
cmd = ['sudo', 'snap', 'install', '--classic']
cmd.extend(packages)
subprocess.run(cmd)
else:
package_str = '\n'.join(packages)
print(f'Would install packages:\n{package_str}')
def download_file(url: str, path: pathlib.Path, dry_run: bool,
use_sudo: bool = False) -> bool:
"""Returns True if was already cached."""
if path.exists():
return True
if not dry_run:
cmd = ['wget', '-O', path, url]
if use_sudo:
cmd = ['sudo'] + cmd
subprocess.run(cmd)
else:
print(f'Would download: {url} to {path}')
return False
def download_and_install_package(package_url: str, dry_run: bool, force: bool) -> None:
package_name = pathlib.Path(package_url).name
cache_dir = pathlib.Path(os.environ.get(
'XDG_CACHE_HOME', pathlib.Path.home() / '.cache')) / 'apt_packages'
cache_dir.mkdir(parents=True, exist_ok=True)
cached_package_path = cache_dir / package_name
was_cached = download_file(url=package_url, path=cached_package_path, dry_run=dry_run)
if force or not was_cached:
if not dry_run:
subprocess.run(['sudo', 'dpkg', '-i', cached_package_path])
subprocess.run(['sudo', 'apt', 'install', '-f', '-y'])
else:
print(f'Would install package: {cached_package_path}')
def download_and_install_app_image(app_image_url: str, dry_run: bool, force: bool) -> None:
package_name = pathlib.Path(app_image_url).name
# XXX: Should I move to a more permanent location?
cache_dir = pathlib.Path(os.environ.get(
'XDG_CACHE_HOME', pathlib.Path.home() / '.cache')) / 'app_images'
cache_dir.mkdir(parents=True, exist_ok=True)
cached_package_path = cache_dir / package_name
was_cached = download_file(url=app_image_url, path=cached_package_path, dry_run=dry_run)
if force or not was_cached:
os.chmod(cached_package_path, 0o775)
if not dry_run:
subprocess.run([cached_package_path, '--appimage-extract'])
else:
print(f'Would install package: {cached_package_path}')
def configure_fonts(dry_run: bool) -> None:
cache_dir = pathlib.Path(os.environ.get(
'XDG_CACHE_HOME', pathlib.Path.home() / '.cache')) / 'fonts'
cache_dir.mkdir(parents=True, exist_ok=True)
ubuntu_zip = cache_dir / 'Ubuntu_v3.1.1.zip'
ubuntu_mono_zip = cache_dir / 'UbuntuMono_v3.1.1.zip'
zip_files = [ubuntu_zip, ubuntu_mono_zip]
was_cached = True
was_cached &= download_file(
'https://github.com/ryanoasis/nerd-fonts/releases/download/v3.1.1/Ubuntu.zip',
ubuntu_zip, dry_run=dry_run)
was_cached &= download_file(
'https://github.com/ryanoasis/nerd-fonts/releases/download/v3.1.1/UbuntuMono.zip',
ubuntu_mono_zip, dry_run=dry_run)
font_dir = pathlib.Path(os.environ.get(
'XDG_DATA_HOME', pathlib.Path.home() / '.local/share')) / 'fonts'
font_dir.mkdir(parents=True, exist_ok=True)
if not was_cached:
# Unzip into font_dir
for zip_file in zip_files:
if not dry_run:
subprocess.run(['unzip', '-qq', zip_file, '-d', font_dir])
else:
print(f'Would have unzipped: {zip_file} to {font_dir}')
if not dry_run:
subprocess.run(['fc-cache', '-fv'])
else:
print('Would have reset font cache')
# Update gnome-tweaks
if not dry_run:
subprocess.run(['dconf', 'write',
'/org/gnome/desktop/interface/monospace-font-name',
'"UbuntuMono Nerd Font Mono 13"'])
else:
print('Would have changed monospace-font-name')
def miscellaneous_commands(dry_run: bool) -> None:
DESCRIPTION_W_CMD = [
('alias nvim to vim',
['sudo', 'snap', 'alias', 'nvim', 'vim']),
# NOTE: Requires reboot
('update locale for fonts',
['sudo', 'update-locale', 'LANG=en_US.UTF-8', 'LANGUAGE=en_US.UTF-8']),
]
for description, cmd in DESCRIPTION_W_CMD:
if not dry_run:
subprocess.run(cmd)
else:
print(f'Would: {description}')
# Define APT repositories
APT_REPOSITORIES_WITH_URL = [
# TODO(#30): Try out 8.0
('ppa:kicad/kicad-7.0-releases',
'http://ppa.launchpad.net/kicad/kicad-7.0-releases/ubuntu'),
('ppa:fish-shell/release-3',
'http://ppa.launchpad.net/fish-shell/release-3/ubuntu'),
# syncthing, necessitates setting this up
('deb [signed-by=/etc/apt/keyrings/syncthing-archive-keyring.gpg] https://apt.syncthing.net/ syncthing stable',
'https://apt.syncthing.net'),
('ppa:appimagelauncher-team/stable',
'https://ppa.launchpadcontent.net/appimagelauncher-team/stable/ubuntu'),
# github cli
# XXX: Not quite working
# ('deb [arch=amd64 signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main',
# 'https://cli.github.com/packages'),
]
# Define packages to install from APT repositories
APT_PACKAGES = [
# TODO: versions?
# Maybe ctags and clang-format?
'tmux',
'htop',
'xclip',
'curl',
# get say
'gnustep-gui-runtime',
# gcc
# TODO: Determine how to get bazel running without this installed
'build-essential',
# java
# TODO: same deal as build-essential
# - openjdk-19-jre-headless
# Switched from ^ to have gui
'openjdk-19-jre',
# For development purposes
'python3.10-venv',
# Directory Viewing Utilities
'tree',
'ranger',
# Searching
'silversearcher-ag',
# AppImage Support
# was originally fuse, but hopefully this is fine (fuse conflicts w/ ubuntu-desktop)
'fuse3',
'libfuse2',
# Sharing notes
# TODO: COnfigure as a service
'syncthing',
# XXX: waveforms is crashing when attempting to start
# NOTE: Can start fine from application launcher :shrug:
# TODO: digilent-agent isn't working though / no launcher
'libqt5serialport5-dev',
'xterm',
'libxcb-xinput-dev',
# - qt5-default # XXX: This should fix, but is unavailable
'arduino',
# Using blender 4.0, this is the 3.0 installation
# - blender
'kicad',
# https://code.visualstudio.com/download#
# Visualization of system metrics in toolbar
'gnome-system-monitor',
# post 22.04 use gnome-browser-connector
'chrome-gnome-shell',
'gir1.2-gtop-2.0',
'gir1.2-nm-1.0',
'gir1.2-clutter-1.0',
# Ensure usual settings, etc. are accessible, reruns every time ...
'ubuntu-desktop',
# Shells
'zsh',
'fish',
# For docker, see https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository
# There are a few manual steps to setup the keyrings
# TODO(#33): Couldn't install
# 'docker-ce',
# 'docker-ce-cli',
# 'containerd.io',
# 'docker-buildx-plugin',
# 'docker-compose-plugin',
# # For rootless docker
# 'uidmap',
# 'dbus-user-session',
# 'docker-ce-rootless-extras',
# For ssl usage / rust
'libssl-dev',
# For rust embedded
'gdb-multiarch',
'openocd',
'qemu-system-arm',
# Miscellaneous tools use it, eg) nvim obsidian
'ripgrep',
# 2 finger right click
'xserver-xorg-input-synaptics',
'gnome-tweaks',
# up/down workspace in Ubuntu
'gnome-shell-extension-manager',
# inotifywait, etc
'inotify-tools',
# obsidian app installed as appImage should be placed in favorites bar
'appimagelauncher',
# obsidian.nvim ObsidianPasteImage expects wl-paste
'wl-clipboard',
# dot / graphviz renderings
'graphviz',
# bambu video
'gstreamer1.0-plugins-bad',
# image editing
'gimp',
# github CLI for octo.nvim integration
'gh',
]
SNAP_PACKAGES = [
'nvim',
]
# Define other packages to download and install
OTHER_PACKAGES = [
# Install Digilent Adept for Waveforms
'https://digilent.s3.us-west-2.amazonaws.com/Software/Adept2+Runtime/2.27.9/digilent.adept.runtime_2.27.9-amd64.deb',
# Install Waveforms
'https://digilent.s3.us-west-2.amazonaws.com/Software/Waveforms2015/3.21.3/digilent.waveforms_3.21.3_amd64.deb',
# Install Waveforms Agent
'https://s3-us-west-2.amazonaws.com/digilent/Software/Digilent+Agent/1.0.1/digilent-agent_1.0.1-1_amd64.deb',
# Install vscode
'https://code.visualstudio.com/sha/download?build=stable&os=linux-deb-x64',
# CKAN for KSP
'https://github.com/KSP-CKAN/CKAN/releases/download/v1.34.4/ckan_1.34.4_all.deb',
# Rescuetime
'https://www.rescuetime.com/installers/rescuetime_current_amd64.deb',
# obsidian
'https://github.com/obsidianmd/obsidian-releases/releases/download/v1.5.8/obsidian_1.5.8_amd64.deb',
]
APP_IMAGES = [
'https://github.com/bambulab/BambuStudio/releases/download/v01.09.00.70/Bambu_Studio_linux_ubuntu-v01.09.00.70.AppImage',
]
def main():
parser = argparse.ArgumentParser(description="APT package management script")
parser.add_argument('--dry-run', action='store_true', help="Show what would be done without actually doing it")
parser.add_argument('--force', action='store_true', help="Re-install cached .deb even if already found")
args = parser.parse_args()
# Request sudo permissions if not running in dry-run mode
if not args.dry_run:
subprocess.run(['sudo', '-v'], check=True)
# Download keyrings
download_file(url='https://syncthing.net/release-key.gpg',
path=pathlib.Path('/etc/apt/keyrings/syncthing-archive-keyring.gpg'),
dry_run=args.dry_run,
use_sudo=True)
download_file(url='https://cli.github.com/packages/githubcli-archive-keyring.gpg',
path=pathlib.Path('/etc/apt/keyrings/githubcli-archive-keyring.gpg'),
dry_run=args.dry_run,
use_sudo=True)
# Add APT repositories
for repo, url in APT_REPOSITORIES_WITH_URL:
add_apt_repository(repo, url=url, dry_run=args.dry_run)
# Update APT
# TODO: Takes a significant amount of time
# update_apt(dry_run=args.dry_run)
# Install packages from APT & SNAP repositories
install_apt_packages(APT_PACKAGES, dry_run=args.dry_run)
install_snap_packages(SNAP_PACKAGES, dry_run=args.dry_run)
# Download and install other packages
for package in OTHER_PACKAGES:
download_and_install_package(package, dry_run=args.dry_run,
force=args.force)
# Download and install AppImages
for app_image in APP_IMAGES:
download_and_install_app_image(app_image, dry_run=args.dry_run,
force=args.force)
configure_fonts(dry_run=args.dry_run)
# Do other miscellaneous_commands
miscellaneous_commands(dry_run=args.dry_run)
if __name__ == '__main__':
main()