This repository has been archived by the owner on Jun 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsystem-packages.el
490 lines (437 loc) · 18.3 KB
/
system-packages.el
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
;;; system-packages.el --- functions to manage system packages -*- lexical-binding: t; -*-
;; Copyright (C) 2016-2018 J. Alexander Branham
;; Author: J. Alexander Branham <[email protected]>
;; Maintainer: J. Alexander Branham <[email protected]>
;; URL: https://github.com/jabranham/system-packages
;; Package-Requires: ((emacs "24.3"))
;; Version: 1.0.5
;; This file is not part of GNU Emacs.
;;; License:
;;
;; 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
;; <http://www.gnu.org/licenses/>
;;; Commentary:
;; This is a package to manage installed system packages. Useful
;; functions include installing packages, removing packages, listing
;; installed packages, and others.
;; Helm users might also be interested in helm-system-packages.el
;; <https://github.com/emacs-helm/helm-system-packages>
;; Usage:
;; (require 'system-packages)
;;
;;; Code:
(eval-when-compile
(require 'cl-lib))
(defgroup system-packages nil
"Manages system packages"
:tag "System Packages"
:prefix "system-packages"
:group 'packages)
(defvar system-packages-supported-package-managers
'(
;; guix
(guix .
((default-sudo . nil)
(install . "guix package -i")
(search . "guix package -s")
(uninstall . "guix package -r")
(update . ("guix package --upgrade"))
(clean-cache . nil)
(log . nil)
(get-info . nil)
(get-info-remote . nil)
(list-files-provided-by . nil)
(verify-all-packages . nil)
(verify-all-dependencies . nil)
(remove-orphaned . nil)
(list-installed-packages . "guix package -I")
(list-installed-packages-all . "guix package -I")
(list-dependencies-of . nil)
(noconfirm . nil)))
;; nix
(nix .
((default-sudo . nil)
(install . "nix-env -i")
(search . "nix search")
(uninstall . "nix-env -e")
(update . ("nix-env -u" ))
(clean-cache . nil)
(log . nil)
(get-info . nil)
(get-info-remote . nil)
(list-files-provided-by . nil)
(verify-all-packages . nil)
(verify-all-dependencies . nil)
(remove-orphaned . nil)
(list-installed-packages . "nix-env -q")
(list-installed-packages-all . "nix-env -q")
(list-dependencies-of . nil)
(noconfirm . nil)))
;; Mac
(brew .
((default-sudo . nil)
(install . "brew install")
(search . "brew search")
(uninstall . "brew uninstall")
(update . ("brew update" "brew upgrade"))
(clean-cache . "brew cleanup")
(log . nil)
(get-info . nil)
(get-info-remote . nil)
(list-files-provided-by . "brew ls --verbose")
(verify-all-packages . nil)
(verify-all-dependencies . nil)
(remove-orphaned . nil)
(list-installed-packages . "brew list")
(list-installed-packages-all . "brew list")
(list-dependencies-of . "brew deps")
(noconfirm . nil)))
(port .
((default-sudo . t)
(install . "port install")
(search . "port search")
(uninstall . "port uninstall")
(update . ("port sync" "port upgrade outdated"))
(clean-cache . "port clean --all")
(log . "port log")
(get-info . "port info")
(get-info-remote . nil)
(list-files-provided-by . "port contents")
(verify-all-packages . nil)
(verify-all-dependencies . nil)
(remove-orphaned . "port uninstall leaves")
(list-installed-packages . "port installed")
(list-installed-packages-all . "port installed")
(list-dependencies-of . "port deps")
(noconfirm . nil)))
;; Arch-based systems
(pacman .
((default-sudo . t)
(install . "pacman -S")
(search . "pacman -Ss")
(uninstall . "pacman -Rns")
(update . "pacman -Syu")
(clean-cache . "pacman -Sc")
(log . "cat /var/log/pacman.log")
(get-info . "pacman -Qi")
(get-info-remote . "pacman -Si")
(list-files-provided-by . "pacman -Ql")
(verify-all-packages . "pacman -Qkk")
(verify-all-dependencies . "pacman -Dk")
(remove-orphaned . "pacman -Rns $(pacman -Qtdq)")
(list-installed-packages . "pacman -Qe")
(list-installed-packages-all . "pacman -Q")
(list-dependencies-of . "pacman -Qi")
(noconfirm . "--noconfirm")))
;; Debian (and Ubuntu) based systems
(apt .
((default-sudo . t)
(install . "apt-get install")
(search . "apt-cache search")
(uninstall . "apt-get --purge remove")
(update . ("apt-get update" "apt-get upgrade"))
(clean-cache . "apt-get clean")
(log . "cat /var/log/dpkg.log")
(get-info . "dpkg -s")
(get-info-remote . "apt-cache show")
(list-files-provided-by . "dpkg -L")
(verify-all-packages . "debsums")
(verify-all-dependencies . "apt-get check")
(remove-orphaned . "apt-get autoremove")
(list-installed-packages . "dpkg -l")
(list-installed-packages-all . "dpkg -l")
(list-dependencies-of . "apt-cache deps")
(noconfirm . "-y")))
(aptitude .
((default-sudo . t)
(install . "aptitude install")
(search . "aptitude search")
(uninstall . "aptitude remove")
(update . ("apt update" "aptitude safe-upgrade"))
(clean-cache . "aptitude clean")
(log . "cat /var/log/dpkg.log")
(get-info . "aptitude show")
(get-info-remote . "aptitude show")
(list-files-provided-by . "dpkg -L")
(verify-all-packages . "debsums")
(verify-all-dependencies . "apt-get check")
(remove-orphaned . nil) ; aptitude does this automatically
(list-installed-packages . "aptitude search '~i!~M'")
(list-installed-packages-all . "aptitude search '~i!~M'")
(list-dependencies-of . "apt-cache deps")
(noconfirm . "-y")))
;; Gentoo
(emerge .
((default-sudo . t)
(install . "emerge")
(search . "emerge -S")
(uninstall . "emerge -C")
(update . "emerge -u world")
(clean-cache . "eclean distfiles")
(log . "cat /var/log/portage")
(get-info . "emerge -pv")
(get-info-remote . "emerge -S")
(list-files-provided-by . "equery files")
(verify-all-packages . "equery check")
(verify-all-dependencies . "emerge -uDN world")
(remove-orphaned . "emerge --depclean")
(list-installed-packages . nil)
(list-installed-packages-all . nil)
(list-dependencies-of . "emerge -ep")
(noconfirm . nil)))
;; openSUSE
(zypper .
((default-sudo . t)
(install . "zypper install")
(search . "zypper search")
(uninstall . "zypper remove")
(update . "zypper update")
(clean-cache . "zypper clean")
(log . "cat /var/log/zypp/history")
(get-info . "zypper info")
(get-info-remote . "zypper info")
(list-files-provided-by . "rpm -Ql")
(verify-all-packages . "rpm -Va")
(verify-all-dependencies . "zypper verify")
(remove-orphaned . "zypper rm -u")
(list-installed-packages . nil)
(list-installed-packages-all . nil)
(list-dependencies-of . "zypper info --requires")
(noconfirm . nil)))
;; Fedora
(dnf .
((default-sudo . t)
(install . "dnf install")
(search . "dnf search")
(uninstall . "dnf remove")
(update . ("dnf upgrade"))
(clean-cache . "dnf clean all")
(log . "dnf history")
(get-info . "rpm -qi")
(get-info-remote . "dnf info")
(list-files-provided-by . "rpm -ql")
(verify-all-packages . "rpm -Va")
(verify-all-dependencies . "dnf repoquery --requires")
(remove-orphaned . "dnf autoremove")
(list-installed-packages . "dnf list --installed")
(list-installed-packages-all . nil)
(list-dependencies-of . "rpm -qR")
(noconfirm . nil)))
;; Void
;; xbps is the name of the package manager, but that doesn't appear as an
;; executable, so let's just call it xbps-install:
(xbps-install .
((default-sudo . t)
(install . "xbps-install")
(search . "xbps-query -Rs")
(uninstall . "xbps-remove -R")
(update . ("xbps-install -Su"))
(clean-cache . "xbps-remove -O")
(log . nil)
(get-info . "xbps-query")
(get-info-remote . "xbps-query -R")
(list-files-provided-by . "xbps-query -f")
(verify-all-packages . nil)
(verify-all-dependencies . "xbps-pkgdb -a")
(remove-orphaned . "dnf autoremove")
(list-installed-packages . "xbps-query -l ")
(list-installed-packages-all . "xbps-query -l ")
(list-dependencies-of . "xbps-query -x")
(noconfirm . nil))))
"An alist of package manager commands.
The key is the package manager and values (usually) commands.")
(put 'system-packages-supported-package-managers 'risky-local-variable t)
(defcustom system-packages-package-manager
(cl-loop for (name . prop) in system-packages-supported-package-managers
for path = (executable-find (symbol-name name))
when path
return name)
"Symbol containing the package manager to use.
See `system-packages-supported-package-managers' for a list of
supported software. Tries to be smart about selecting the
default."
:type '(choice
(const guix)
(const nix-env)
(const brew)
(const port)
(const pacman)
(const apt)
(const aptitude)
(const emerge)
(const zypper)
(const dnf)
(const xbps-install)))
(define-obsolete-variable-alias 'system-packages-packagemanager
'system-packages-package-manager "2017-12-25")
(defcustom system-packages-use-sudo
(cdr (assoc 'default-sudo (cdr (assoc system-packages-package-manager
system-packages-supported-package-managers))))
"If non-nil, system-packages uses sudo for appropriate commands.
Tries to be smart for selecting the default."
:type 'boolean)
(define-obsolete-variable-alias 'system-packages-usesudo
'system-packages-use-sudo "2017-12-25")
(defcustom system-packages-noconfirm nil
"If non-nil, bypass prompts asking the user to confirm package upgrades."
:type 'boolean)
(defun system-packages-get-command (action &optional pack args)
"Return a command to run as a string.
ACTION should be in
`system-packages-supported-package-managers' (e.g. 'install).
PACK is used to operate on a specific package, and ARGS is a way
of passing additional arguments to the package manager."
(let ((command
(cdr (assoc action (cdr (assoc system-packages-package-manager
system-packages-supported-package-managers)))))
(noconfirm (when system-packages-noconfirm
(cdr (assoc 'noconfirm
(cdr (assoc system-packages-package-manager
system-packages-supported-package-managers)))))))
(unless command
(error (format "%S not supported in %S" action system-packages-package-manager)))
(unless (listp command)
(setq command (list command)))
(setq command (mapconcat 'identity command " && "))
(setq command (mapconcat 'identity (list command pack) " "))
(when noconfirm
(setq args (concat args (and pack " ") noconfirm)))
(concat command args)))
(defun system-packages--run-command (action &optional pack args)
"Run a command asynchronously using the system's package manager.
See `system-packages-get-command' for how to use ACTION, PACK,
and ARGS."
(let ((command (system-packages-get-command action pack args))
(default-directory (if system-packages-use-sudo
"/sudo::"
default-directory)))
(async-shell-command command "*system-packages*")))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; functions on named packages
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;###autoload
(defun system-packages-install (pack &optional args)
"Install system packages.
Use the package manager from `system-packages-package-manager' to
install PACK. You may use ARGS to pass options to the package
manger."
(interactive "sPackage to install: ")
(system-packages--run-command 'install pack args))
;;;###autoload
(defun system-packages-ensure (pack &optional args)
"Ensure PACK is installed on system.
Search for PACK with `system-packages-package-installed-p', and
install the package if not found. Use ARGS to pass options to
the package manager."
(interactive "sPackage to ensure is present: ")
(if (system-packages-package-installed-p pack)
t
(system-packages-install pack args)))
;;;###autoload
(defalias 'system-packages-package-installed-p #'executable-find
"Return t if PACK is installed.
Currently an alias for `executable-find', so it will give wrong
results if the package and executable names are different.")
;;;###autoload
(defun system-packages-search (pack &optional args)
"Search for system packages.
Use the package manager named in `system-packages-package-manager'
to search for PACK. You may use ARGS to pass options to the
package manager."
(interactive "sSearch string: ")
(system-packages--run-command 'search pack args))
;;;###autoload
(defun system-packages-uninstall (pack &optional args)
"Uninstall system packages.
Uses the package manager named in
`system-packages-package-manager' to uninstall PACK. You may use
ARGS to pass options to the package manager."
(interactive "sWhat package to uninstall: ")
(system-packages--run-command 'uninstall pack args))
;;;###autoload
(defun system-packages-list-dependencies-of (pack &optional args)
"List the dependencies of PACK.
You may use ARGS to pass options to the package manager."
(interactive "sWhat package to list dependencies of: ")
(system-packages--run-command 'list-dependencies-of pack args))
;;;###autoload
(defun system-packages-get-info (pack &optional args)
"Display local package information of PACK.
With a prefix argument, display remote package information. You
may use ARGS to pass options to the package manager."
(interactive "sWhat package to list info for: ")
(if current-prefix-arg
(system-packages--run-command 'get-info-remote pack args)
(system-packages--run-command 'get-info pack args)))
;;;###autoload
(defun system-packages-list-files-provided-by (pack &optional args)
"List the files provided by PACK.
You may use ARGS to pass options to the package manager."
(interactive "sWhat package to list provided files of: ")
(system-packages--run-command 'list-files-provided-by pack args))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; functions that don't take a named package
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;###autoload
(defun system-packages-update (&optional args)
"Update system packages.
Use the package manager `system-packages-package-manager'. You
may use ARGS to pass options to the package manger."
(interactive)
(system-packages--run-command 'update nil args))
;;;###autoload
(defun system-packages-remove-orphaned (&optional args)
"Remove orphaned packages.
Uses the package manager named in
`system-packages-package-manager'. You may use ARGS to pass
options to the package manger."
(interactive)
(system-packages--run-command 'remove-orphaned nil args))
;;;###autoload
(defun system-packages-list-installed-packages (all &optional args)
"List explicitly installed packages.
Uses the package manager named in
`system-packages-package-manager'. With
\\[universal-argument] (for ALL), list all installed packages.
You may use ARGS to pass options to the package manger."
(interactive "P")
(if all
(system-packages--run-command 'list-installed-packages-all nil args)
(system-packages--run-command 'list-installed-packages nil args)))
;;;###autoload
(defun system-packages-clean-cache (&optional args)
"Clean the cache of the package manager.
You may use ARGS to pass options to the package manger."
(interactive)
(system-packages--run-command 'clean-cache nil args))
;;;###autoload
(defun system-packages-log (&optional args)
"Show a log from `system-packages-package-manager'.
You may use ARGS to pass options to the package manger."
(interactive)
(system-packages--run-command 'log args))
;;;###autoload
(defun system-packages-verify-all-packages (&optional args)
"Check that files owned by packages are present on the system.
You may use ARGS to pass options to the package manger."
(interactive)
(system-packages--run-command 'verify-all-packages nil args))
;;;###autoload
(defun system-packages-verify-all-dependencies (&optional args)
"Verify that all required dependencies are installed on the system.
You may use ARGS to pass options to the package manger."
(interactive)
(system-packages--run-command 'verify-all-dependencies nil args))
(provide 'system-packages)
;;; system-packages.el ends here