1 ;;; package-get.el --- Retrieve SXEmacs package
3 ;; Copyright (C) 1998 by Pete Ware
4 ;; Copyright (C) 2002 Ben Wing.
5 ;; Copyright (C) 2003 - 2015 Steve Youngs
7 ;; Author: Pete Ware <ware@cis.ohio-state.edu>
8 ;; Heavy-Modifications: Greg Klanderman <greg@alphatech.com>
9 ;; Jan Vroonhof <vroonhof@math.ethz.ch>
10 ;; Steve Youngs <steve@sxemacs.org>
13 ;; This file is part of SXEmacs.
15 ;; SXEmacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; SXEmacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
28 ;;; Synched up with: Not in FSF
33 ;; Retrieve a package and any other required packages from an archive
36 ;; Note (JV): Most of this no longer applies!
37 ;; Note (SY): Definitely no longer applies, but I'm leaving these
38 ;; comments here because there are some nifty ideas here.
41 ;; A new XEmacs lisp-only release is generated with the following steps:
42 ;; 1. The maintainer runs some yet to be written program that
43 ;; generates all the dependency information. This should
44 ;; determine all the require and provide statements and associate
45 ;; them with a package.
46 ;; 2. All the packages are then bundled into their own tar balls
47 ;; (or whatever format)
48 ;; 3. Maintainer automatically generates a new `package-get-base'
49 ;; data structure which contains information such as the
50 ;; package name, the file to be retrieved, an md5 checksum,
51 ;; etc (see `package-get-base').
52 ;; 4. The maintainer posts an announcement with the new version
53 ;; of `package-get-base'.
54 ;; 5. A user/system manager saves this posting and runs
55 ;; `package-get-update' which uses the previously saved list
56 ;; of packages, `package-get-here' that the user/site
57 ;; wants to determine what new versions to download and
60 ;; A user/site manager can generate a new `package-get-here' structure
61 ;; by using `package-get-setup' which generates a customize like
62 ;; interface to the list of packages. The buffer looks something
65 ;; gnus - a mail and news reader
68 ;; [] Required by other [packages]
74 ;; [] Required by other [packages]
76 ;; Where `[]' indicates a toggle box
78 ;; - Clicking on "Always install" puts this into
79 ;; `package-get-here' list. "Needs updating" indicates a new
80 ;; version is available. Anything already in
81 ;; `package-get-here' has this enabled.
82 ;; - "Required by other" means some other packages are going to force
83 ;; this to be installed. Clicking on [packages] gives a list
84 ;; of packages that require this.
86 ;; The `package-get-base' should be installed in a file in
87 ;; `data-directory'. The `package-get-here' should be installed in
88 ;; site-lisp. Both are then read at run time.
91 ;; - Implement `package-get-setup'
92 ;; - Actually put `package-get-base' and `package-get-here' into
93 ;; files that are read.
94 ;; - SOMEONE needs to write the programs that generate the
95 ;; provides/requires database and makes it into a lisp data
96 ;; structure suitable for `package-get-base'
97 ;; - Handle errors such as no package providing a required symbol.
98 ;; - Tie this into the `require' function to download packages
105 (require 'package-admin)
106 ;; (require 'package-get-base)
108 (defgroup package-tools nil
109 "Tools to manipulate packages."
112 (defgroup package-get nil
113 "Automatic Package Fetcher and Installer."
114 :prefix "package-get"
115 :group 'package-tools)
118 (defvar package-get-base nil
119 "List of packages that are installed at this site.
120 For each element in the alist, car is the package name and the cdr is
121 a plist containing information about the package. Typical fields
122 kept in the plist are:
124 version - version of this package
125 provides - list of symbols provided
126 requires - list of symbols that are required.
127 These in turn are provided by other packages.
128 filename - name of the file.
129 size - size of the file (aka the bundled package)
130 md5sum - computed md5 checksum
131 description - What this package is for.
132 type - Whether this is a 'binary (default) or 'single file package
134 More fields may be added as needed. An example:
138 (version \"<version 2>\"
140 description \"what this package is about.\"
147 (version \"<version 1>\"
149 description \"what this package is about.\"
159 For version information, it is assumed things are listed in most
160 recent to least recent -- in other words, the version names don't have to
161 be lexically ordered. It is debatable if it makes sense to have more than
162 one version of a package available.")
164 (defcustom package-get-dir (temp-directory)
165 "*Where to store temporary files for staging."
166 :tag "Temporary directory"
171 (defcustom package-get-package-index-file-location
172 (car (split-path (or (getenv "EMACSPACKAGEPATH") user-packages-topdir)))
173 "*The directory where the package-index file can be found."
178 (defcustom package-get-install-to-user-directory nil
179 "*If non-nil install packages under `user-packages-topdir'."
183 (defvaralias 'package-get-install-to-user-init-directory
184 'package-get-install-to-user-directory)
186 (define-widget 'host-name 'string
190 (define-widget 'url-scheme 'string
191 "A URL protocol scheme."
194 (defcustom package-get-remote nil
195 "*The remote site to contact for downloading packages.
197 Format is '(site-name directory-on-site scheme). As a special case,
198 `site-name' can be `nil', in which case `directory-on-site' is treated
199 as a local directory."
200 :tag "Package repository"
201 :type '(set (choice (const :tag "None" nil)
202 (list :tag "Local" (const :tag "Local" nil) directory)
203 (list :tag "Remote" host-name directory url-scheme)))
207 (defvar package-get-have-curl (ignore-errors (require 'ffi-curl))
208 "Non-nil when FFI and curl is available.")
211 (defcustom package-get-download-sites
212 `(,@(when package-get-have-curl
213 '(("SXEmacs XE pkg mirror"
214 "downloads.sxemacs.org" "xemacs-pkgs/packages" "http")))
215 ("US (Main XEmacs Site)"
216 "ftp.xemacs.org" "pub/xemacs/packages" "ftp")
217 ;; In alphabetical order of Country, our mirrors...
219 ("Belgium (be.xemacs.org)"
220 "ftp.be.xemacs.org" "xemacs/packages" "ftp")
221 ("Canada (ca.xemacs.org)"
222 "ftp.ca.xemacs.org" "pub/Mirror/xemacs/packages" "ftp")
223 ("Denmark (dk.xemacs.org)"
224 "ftp.dk.xemacs.org" "xemacs/packages" "ftp")
225 ("France (fr.xemacs.org)"
226 "ftp.fr.xemacs.org" "pub/xemacs/packages" "ftp")
228 ("France (mirror.cict.fr)"
229 "mirror.cict.fr" "xemacs/packages" "ftp")
230 ("France (pasteur.fr)"
231 "ftp.pasteur.fr" "pub/computing/xemacs/packages" "ftp")
232 ;; Very outdated, experimental not updated since 2013
233 ("Germany (de.xemacs.org)"
234 "ftp.de.xemacs.org" "pub/ftp.xemacs.org/tux/xemacs/packages" "ftp")
236 ("Greece (gr.xemacs.org)"
237 "ftp.gr.xemacs.org" "mirrors/XEmacs/ftp/packages" "ftp")
238 ("Ireland (heanet.ie)"
239 "ftp.heanet.ie" "mirrors/ftp.xemacs.org/packages" "ftp")
241 ("Italy (it.xemacs.org)"
242 "ftp.it.xemacs.org" "unix/packages/XEMACS/packages" "ftp")
245 "ftp.dti.ad.jp" "pub/unix/editor/xemacs/packages" "ftp")
246 ("Norway (no.xemacs.org)"
247 "ftp.no.xemacs.org" "pub/xemacs/packages" "ftp")
248 ("Portugal (pt.xemacs.org)"
249 "ftp.pt.xemacs.org" "pub/MIRRORS/ftp.xemacs.org/packages" "ftp")
251 ("Russia (ru.xemacs.org)"
252 "ftp.ru.xemacs.org" "pub/emacs/xemacs/packages" "ftp")
253 ("Saudi Arabia (sa.xemacs.org)"
254 "ftp.sa.xemacs.org" "pub/xemacs.org/packages" "ftp")
255 ("Sweden (se.xemacs.org)"
256 "ftp.se.xemacs.org" "pub/gnu/xemacs/packages" "ftp")
257 ("Switzerland (ch.xemacs.org)"
258 "ftp.ch.xemacs.org" "mirror/xemacs/packages" "ftp")
259 ("Taiwan (ftp.tw.xemacs.org)"
260 "ftp.tw.xemacs.org" "Unix/Editors/XEmacs/packages" "ftp")
261 ("UK (uk.xemacs.org)"
262 "ftp.uk.xemacs.org" "sites/ftp.xemacs.org/pub/xemacs/packages" "ftp")
264 "mirrors.ibiblio.org" "pub/mirrors/xemacs/packages" "ftp")
266 "*List of remote sites available for downloading packages.
268 List format is '(site-description site-name directory-on-site url-scheme).
269 SITE-DESCRIPTION is a textual description of the site. SITE-NAME is
270 the internet address of the download site. DIRECTORY-ON-SITE is the
271 directory on the site in which packages may be found. URL-SCHEME is
272 the protocol such as `http', `ftp', etc. This variable is used to
273 initialize `package-get-remote', the variable actually used to specify
274 package download sites."
275 :tag "Package download sites"
276 :type '(repeat (list (string :tag "Name")
277 host-name directory url-scheme))
281 (defcustom package-get-pre-release-download-sites
282 `(,@(when package-get-have-curl
283 '(("SXEmacs XE pkg Pre-Releases" "downloads.sxemacs.org"
284 "xemacs-pkgs/beta/experimental/packages" "http")))
285 ("US Pre-Releases (Main XEmacs Site)" "ftp.xemacs.org"
286 "pub/xemacs/beta/experimental/packages" "ftp")
287 ;; In alphabetical order of Country, our mirrors...
289 ("Belgium Pre-Releases (be.xemacs.org)" "ftp.be.xemacs.org"
290 "xemacs/beta/experimental/packages" "ftp")
291 ("Canada Pre-Releases (ca.xemacs.org)" "ftp.ca.xemacs.org"
292 "pub/Mirror/xemacs/beta/experimental/packages" "ftp")
293 ("Denmark Pre-Releases (dk.xemacs.org)" "ftp.dk.xemacs.org"
294 "xemacs/beta/experimental/packages" "ftp")
295 ("France Pre-Releases (fr.xemacs.org)" "ftp.fr.xemacs.org"
296 "pub/xemacs/beta/experimental/packages" "ftp")
297 ; Was a month out of date as of 2013-04-12, hopefully not a sign
298 ; it is no longer updating.
299 ("France Pre-Releases (mirror.cict.fr)" "mirror.cict.fr"
300 "xemacs/beta/experimental/packages" "ftp")
301 ("France Pre-Releases (pasteur.fr)" "ftp.pasteur.fr"
302 "pub/computing/xemacs/beta/experimental/packages" "ftp")
303 ;; Very outdated, experimental not updated since 2013
304 ("Germany Pre-Releases (de.xemacs.org)" "ftp.de.xemacs.org"
305 "pub/ftp.xemacs.org/tux/xemacs/beta/experimental/packages" "ftp")
307 ("Greece Pre-Releases (gr.xemacs.org)" "ftp.gr.xemacs.org"
308 "mirrors/XEmacs/ftp/beta/experimental/packages" "ftp")
309 ("Ireland Pre-Releases (heanet.ie)" "ftp.heanet.ie"
310 "mirrors/ftp.xemacs.org/beta/experimental/packages" "ftp")
312 ("Italy Pre-Releases (it.xemacs.org)" "ftp.it.xemacs.org"
313 "unix/packages/XEMACS/beta/experimental/packages" "ftp")
314 ; Was out of date as at 2013-04-12. Hopefully not a sign they are
315 ; no longer updating.
317 ("Japan Pre-Releases (dti.ad.jp)" "ftp.dti.ad.jp"
318 "pub/unix/editor/xemacs/beta/experimental/packages" "ftp")
319 ("Norway Pre-Releases (no.xemacs.org)" "ftp.no.xemacs.org"
320 "pub/xemacs/beta/experimental/packages" "ftp")
321 ("Portugal Pre-Releases (pt.xemacs.org)" "ftp.pt.xemacs.org"
322 "pub/MIRRORS/ftp.xemacs.org/beta/experimental/packages" "ftp")
323 ("Saudi Arabia Pre-Releases (sa.xemacs.org)" "ftp.sa.xemacs.org"
324 "pub/xemacs.org/beta/experimental/packages" "ftp")
325 ("Sweden Pre-Releases (se.xemacs.org)" "ftp.se.xemacs.org"
326 "pub/gnu/xemacs/beta/experimental/packages" "ftp")
327 ("Switzerland Pre-Releases (ch.xemacs.org)" "ftp.ch.xemacs.org"
328 "mirror/xemacs/beta/experimental/packages" "ftp")
329 ("Taiwan Pre-Releases (ftp.tw.xemacs.org)" "ftp.tw.xemacs.org"
330 "Unix/Editors/XEmacs/beta/experimental/packages" "ftp")
331 ("UK Pre-Releases (uk.xemacs.org)" "ftp.uk.xemacs.org"
332 "sites/ftp.xemacs.org/pub/xemacs/beta/experimental/packages" "ftp")
333 ("US Pre-Releases (ibiblio.org)" "mirrors.ibiblio.org"
334 "pub/mirrors/xemacs/beta/experimental/packages" "ftp")
336 "*List of remote sites available for downloading \"Pre-Release\" packages.
338 List format is '(site-description site-name directory-on-site url-scheme).
339 SITE-DESCRIPTION is a textual description of the site. SITE-NAME is
340 the internet address of the download site. DIRECTORY-ON-SITE is the
341 directory on the site in which packages may be found. URL-SCHEME is
342 the protocol such as `http', `ftp', etc. This variable is used to
343 initialize `package-get-remote', the variable actually used to specify
344 package download sites."
345 :tag "Pre-Release Package download sites"
346 :type '(repeat (list (string :tag "Name")
347 host-name directory url-scheme))
351 (defcustom package-get-site-release-download-sites nil
352 "*List of remote sites available for downloading \"Site Release\" packages.
354 List format is '(site-description site-name directory-on-site url-scheme).
355 SITE-DESCRIPTION is a textual description of the site. SITE-NAME is
356 the internet address of the download site. DIRECTORY-ON-SITE is the
357 directory on the site in which packages may be found. URL-SCHEME is
358 the protocol such as `http', `ftp', etc. This variable is used to
359 initialize `package-get-remote', the variable actually used to specify
360 package download sites."
361 :tag "Site Release Package download sites"
362 :type '(repeat (list (string :tag "Name")
363 host-name directory url-scheme))
366 (defcustom package-get-remove-copy t
367 "*After copying and installing a package, if this is t, then remove the
368 copy. Otherwise, keep it around."
372 ;; #### it may make sense for this to be a list of names.
373 ;; #### also, should we rename "*base*" to "*index*" or "*db*"?
374 ;; "base" is a pretty poor name.
375 (defcustom package-get-base-filename "package-index.LATEST.gpg"
376 "*Name of the default package-get database file.
377 This may either be a relative path, in which case it is interpreted
378 with respect to `package-get-remote', or an absolute path."
382 (defcustom package-get-always-update nil
383 "*If Non-nil always make sure we are using the latest package index (base).
384 Otherwise respect the `force-current' argument of `package-get-require-base'."
388 (defvar package-get-was-current nil
389 "Non-nil we did our best to fetch a current database.")
392 (defun package-get-require-base (&optional force-current)
393 "Require that a package-get database has been loaded.
394 If the optional FORCE-CURRENT argument or the value of
395 `package-get-always-update' is Non-nil, try to update the database
396 from a location in `package-get-remote'. Otherwise a local copy is used
397 if available and remote access is never done.
399 Please use FORCE-CURRENT only when the user is explictly dealing with packages
400 and remote access is likely in the near future."
401 (setq force-current (or force-current package-get-always-update))
402 (unless (and (boundp 'package-get-base)
404 (or (not force-current) package-get-was-current))
405 (package-get-update-base nil force-current))
406 (if (or (not (boundp 'package-get-base))
407 (not package-get-base))
408 (error 'void-variable
409 "Package-get database not loaded")
410 (setq package-get-was-current force-current)))
413 (defun package-get-update-base-entry (entry)
414 "Update an entry in `package-get-base'."
415 (let ((existing (assq (car entry) package-get-base)))
417 (setcdr existing (cdr entry))
418 (setq package-get-base (cons entry package-get-base)))))
420 (defun package-get-locate-file (file &optional nil-if-not-found no-remote)
421 "Locate an existing FILE with respect to `package-get-remote'.
422 If FILE is an absolute path or is not found, simply return FILE.
423 If optional argument NIL-IF-NOT-FOUND is non-nil, return nil
424 if FILE can not be located.
425 If NO-REMOTE is non-nil never search remote locations."
426 (if (file-name-absolute-p file)
428 (let ((site package-get-remote)
431 (unless (and no-remote (caar (list site)))
432 (let ((expn (package-get-remote-filename (car (list site)) file)))
433 (if (and expn (file-exists-p expn))
437 (and (not nil-if-not-found)
440 (defun package-get-locate-index-file (no-remote)
441 "Locate the package-get index file.
443 Do not return remote paths if NO-REMOTE is non-nil. If the index
444 file doesn't exist in `package-get-package-index-file-location', ask
445 the user if one should be created using the index file in core as a
447 (or (package-get-locate-file package-get-base-filename t no-remote)
448 (if (file-exists-p (expand-file-name package-get-base-filename
449 package-get-package-index-file-location))
450 (expand-file-name package-get-base-filename
451 package-get-package-index-file-location)
452 (if (y-or-n-p (format "No index file, shall I create one in %s? "
453 package-get-package-index-file-location))
457 (find-file-noselect (expand-file-name
458 package-get-base-filename
459 package-get-package-index-file-location)))
460 (let ((coding-system-for-write 'binary))
462 (insert-file-contents-literally
463 (locate-data-file package-get-base-filename))
464 (save-buffer (current-buffer))
465 (kill-buffer (current-buffer))))
466 (expand-file-name package-get-base-filename
467 package-get-package-index-file-location))
468 (error 'search-failed
469 "Can't locate a package index file.")))))
471 (defun package-get-maybe-save-index (filename)
472 "Offer to save the current buffer as the local package index file,
474 (let ((location (package-get-locate-index-file t)))
475 (unless (and filename (equal filename location))
476 (unless (and location
477 (equal (md5 (current-buffer))
479 (insert-file-contents-literally location)
480 (md5 (current-buffer)))))
481 (when (not (file-writable-p location))
482 (if (y-or-n-p (format "Sorry, %s is read-only, can I use %s? "
483 location user-packages-topdir))
484 (setq location (expand-file-name
485 package-get-base-filename
486 package-get-package-index-file-location))
488 (format "%s is read-only" location))))
489 (when (y-or-n-p (concat "Update package index in " location "? "))
490 (let ((coding-system-for-write 'binary))
491 (write-file location)))))))
494 (defun package-get-update-base (&optional db-file force-current)
495 "Update the package-get database file with entries from DB-FILE.
496 Unless FORCE-CURRENT is non-nil never try to update the database."
498 (let* ((dflt (package-get-locate-index-file nil))
499 (match (not (string-match #r"^\(https?\|s?ftp\)://" dflt))))
500 (list (read-file-name "Load package-get database: "
501 (file-name-directory dflt)
504 (file-name-nondirectory dflt)))))
505 (setq db-file (expand-file-name (or db-file
506 (package-get-locate-index-file
507 (not force-current)))))
508 (if (not (file-exists-p db-file))
510 (format "Package-get database file `%s' does not exist" db-file)))
511 (if (not (file-readable-p db-file))
513 (format "Package-get database file `%s' not readable" db-file)))
514 (let ((buf (get-buffer-create "*package database*")))
519 (insert-file-contents-literally db-file)
520 (package-get-update-base-from-buffer buf)
521 (if (or (file-remote-p db-file)
522 (and (string-match #r"^\(https?\|s?ftp\)://" db-file)
523 package-get-have-curl))
524 (package-get-maybe-save-index db-file)))
527 ;; This is here because the `process-error' datum doesn't exist in
529 (define-error 'process-error "Process error")
532 (defun package-get-update-base-from-buffer (&optional buf)
533 "Update the package-get database with entries from BUFFER.
534 BUFFER defaults to the current buffer. This command can be
535 used interactively, for example from a mail or news buffer."
537 (setq buf (or buf (current-buffer)))
538 (let ((coding-system-for-read 'binary)
539 (coding-system-for-write 'binary)
540 content-beg content-end)
543 (goto-char (point-min))
544 (setq content-beg (point))
545 (setq content-end (save-excursion (goto-char (point-max)) (point)))
546 (package-get-update-base-entries content-beg content-end)
547 (message "Updated package database"))))
549 (defun package-get-update-base-entries (start end)
550 "Update the package-get database with the entries found between
551 START and END in the current buffer."
554 (if (not (re-search-forward "^(package-get-update-base-entry" nil t))
555 (error 'search-failed
556 "Buffer does not contain package-get database entries"))
559 (while (and (< (point) end)
560 (re-search-forward "^(package-get-update-base-entry" nil t))
562 (let ((entry (read (current-buffer))))
563 (if (or (not (consp entry))
564 (not (eq (car entry) 'package-get-update-base-entry)))
566 "Invalid package-get database entry found"))
567 (package-get-update-base-entry
568 (car (cdr (car (cdr entry)))))
569 (setq count (1+ count))))
570 (message "Got %d package-get database entries" count))))
573 (defun package-get-save-base (file)
574 "Write the package-get database to FILE.
576 Note: This database will be unsigned of course."
577 (interactive "FSave package-get database to: ")
578 (package-get-require-base t)
579 (let ((buf (get-buffer-create "*package database*")))
584 (goto-char (point-min))
585 (let ((entries package-get-base) entry plist)
586 (insert ";; Package Index file -- Do not edit manually.\n")
589 (setq entry (car entries))
590 (setq plist (car (cdr entry)))
591 (insert "(package-get-update-base-entry (quote\n")
592 (insert (format "(%s\n" (symbol-name (car entry))))
594 (insert (format " %s%s %S\n"
595 (if (eq plist (car (cdr entry))) "(" " ")
596 (symbol-name (car plist))
598 (setq plist (cdr (cdr plist))))
599 (insert "))\n))\n;;;@@@\n")
600 (setq entries (cdr entries))))
601 (insert ";; Package Index file ends here\n")
602 (write-region (point-min) (point-max) file))
605 (defun package-get-interactive-package-query (get-version package-symbol)
606 "Perform interactive querying for package and optional version.
607 Query for a version if GET-VERSION is non-nil. Return package name as
608 a symbol instead of a string if PACKAGE-SYMBOL is non-nil.
609 The return value is suitable for direct passing to `interactive'."
610 (package-get-require-base t)
611 (let ((table (mapcar #'(lambda (item)
612 (let ((name (symbol-name (car item))))
615 package package-symbol default-version version)
616 (save-window-excursion
617 (setq package (completing-read "Package: " table nil t))
618 (setq package-symbol (intern package))
621 (setq default-version
622 (package-get-info-prop
623 (package-get-info-version
624 (package-get-info-find-package package-get-base
628 (setq version (read-string "Version: " default-version))
631 (list package-symbol version)
632 (list package version)))
634 (list package-symbol)
638 (defun package-get-delete-package (package &optional pkg-topdir)
639 "Delete an installation of PACKAGE below directory PKG-TOPDIR.
640 PACKAGE is a symbol, not a string.
641 This is just an interactive wrapper for `package-admin-delete-binary-package'."
642 (interactive (package-get-interactive-package-query nil t))
643 (package-admin-delete-binary-package package pkg-topdir))
646 (defun package-get-update-all ()
647 "Fetch and install the latest versions of all currently installed packages."
649 (package-get-require-base t)
652 (mapcar (lambda (pkg)
653 (if (not (package-get (car pkg) nil 'never))
654 (throw 'exit nil))) ;; Bail out if error detected
655 packages-package-list)))
658 (defun package-get-all (package version &optional fetched-packages install-dir)
659 "Fetch PACKAGE with VERSION and all other required packages.
660 Uses `package-get-base' to determine just what is required and what
661 package provides that functionality. If VERSION is nil, retrieves
662 latest version. Optional argument FETCHED-PACKAGES is used to keep
663 track of packages already fetched. Optional argument INSTALL-DIR,
664 if non-nil, specifies the package directory where fetched packages
667 Returns nil upon error."
668 (interactive (package-get-interactive-package-query t nil))
669 (let* ((the-package (package-get-info-find-package package-get-base
671 (this-package (package-get-info-version
672 the-package version))
673 (this-requires (package-get-info-prop this-package 'requires)))
675 (setq version (package-get-info-prop this-package 'version))
676 (unless (package-get-installedp package version)
677 (if (not (package-get package version nil install-dir))
679 (setq fetched-packages nil)
681 (setq fetched-packages
682 (append (list package)
683 (package-get-info-prop this-package 'provides)
685 ;; grab everything that this package requires plus recursively
686 ;; grab everything that the requires require. Keep track
687 ;; in `fetched-packages' the list of things provided -- this
688 ;; keeps us from going into a loop
690 (if (not (member (car this-requires) fetched-packages))
691 (let* ((reqd-package (package-get-package-provider
692 (car this-requires) t))
693 (reqd-version (cadr reqd-package))
694 (reqd-name (car reqd-package)))
696 (error 'search-failed
697 (format "Unable to find a provider for %s"
698 (car this-requires))))
699 (if (not (setq fetched-packages
700 (package-get-all reqd-name reqd-version
704 (setq this-requires (cdr this-requires))))
708 (defun package-get-dependencies (packages)
709 "Compute dependencies for PACKAGES.
710 Uses `package-get-base' to determine just what is required and what
711 package provides that functionality. Returns the list of packages
712 required by PACKAGES."
713 (package-get-require-base t)
714 (let ((orig-packages packages)
715 dependencies provided)
717 (let* ((package (car packages))
718 (the-package (package-get-info-find-package
719 package-get-base package))
720 (this-package (package-get-info-version
722 (this-requires (package-get-info-prop this-package 'requires))
723 (new-depends (set-difference
726 (let* ((reqd-package (package-get-package-provider reqd))
727 (reqd-name (car reqd-package)))
729 (error 'search-failed
730 (format "Unable to find a provider for %s" reqd)))
734 (this-provides (package-get-info-prop this-package 'provides)))
736 (union dependencies new-depends))
738 (union provided (union (list package) this-provides)))
740 (union new-depends (cdr packages)))))
741 (set-difference dependencies orig-packages)))
743 (defun package-get-load-package-file (lispdir file)
745 (setq pathname (expand-file-name file lispdir))
751 (message "Error loading package file \"%s\" %s!" pathname err)
755 (defun package-get-init-package (lispdir)
756 "Initialize the package.
757 This really assumes that the package has never been loaded. Updating
758 a newer package can cause problems, due to old, obsolete functions in
761 Return `t' upon complete success, `nil' if any errors occurred."
764 (file-accessible-directory-p lispdir))
766 ;; Add lispdir to load-path if it doesn't already exist.
767 ;; NOTE: this does not take symlinks, etc., into account.
768 (if (let ((dirs load-path))
771 (if (string-equal (car dirs) lispdir)
773 (setq dirs (cdr dirs)))
775 (setq load-path (cons lispdir load-path)))
776 (if (not (package-get-load-package-file lispdir "auto-autoloads"))
777 (package-get-load-package-file lispdir "_pkg"))
782 (defun package-get-info (package information &optional arg remote)
783 "Get information about a package.
785 Quite similar to `package-get-info-prop', but can retrieve a lot more
788 Argument PACKAGE is the name of an XEmacs package (a symbol). It must
789 be a valid package, ie, a member of `package-get-base'.
791 Argument INFORMATION is a symbol that can be any one of:
793 standards-version Package system version (not used).
794 version Version of the XEmacs package.
795 author-version The upstream version of the package.
796 date The date the package was last modified.
797 build-date The date the package was last built.
798 maintainer The maintainer of the package.
799 distribution Will always be \"xemacs\" (not used).
800 priority \"low\", \"medium\", or \"high\" (not used).
801 category Either \"standard\", \"mule\", or \"unsupported\"..
802 dump Is the package dumped (not used).
803 description A description of the package.
804 filename The filename of the binary tarball of the package.
805 md5sum The md5sum of filename.
806 size The size in bytes of filename.
807 provides A list of symbols that this package provides.
808 requires A list of packages that this package requires.
809 type Can be either \"regular\" or \"single-file\".
811 If optional argument ARG is non-nil insert INFORMATION into current
812 buffer at point. This is very useful for doing things like inserting
813 a maintainer's email address into a mail buffer.
815 If optional argument REMOTE is non-nil use a package list from a
816 remote site. For this to work `package-get-remote' must be non-nil.
818 If this function is called interactively it will display INFORMATION
820 (interactive "SPackage: \nSInfo: \nP")
822 (package-get-require-base t)
823 (package-get-require-base nil))
824 (let ((all-pkgs package-get-base)
826 (loop until (equal package (caar all-pkgs))
827 do (setq all-pkgs (cdr all-pkgs))
828 do (if (not all-pkgs)
829 (error 'invalid-argument
830 (format "%s is not a valid package" package))))
831 (setq info (plist-get (cadar all-pkgs) information))
834 (insert (format "%s" info))
835 (if (package-get-key package :version)
837 (message "%s (Package: %s is not installed)" info package)))
839 (insert (format "%s" info))
843 (defun package-get-list-packages-where (item field &optional arg)
844 "Return a list of packages that fulfill certain criteria.
846 Argument ITEM, a symbol, is what you want to check for. ITEM must be a
847 symbol even when it doesn't make sense to be a symbol \(think, searching
848 maintainers, descriptions, etc\). The function will convert the symbol
849 to a string if a string is what is needed. The downside to this is that
850 ITEM can only ever be a single word.
852 Argument FIELD, a symbol, is the field to check in. You can specify
855 Field Sane or Allowable Content
856 description any single word
857 category `standard' or `mule'
858 maintainer any single word
859 build-date yyyy-mm-dd
861 type `regular' or `single'
862 requires any package name
864 priority `low', `medium', or `high'
866 Optional Argument ARG, a prefix arg, insert output at point in the
868 (interactive "SList packages that have (item): \nSin their (field): \nP")
869 (package-get-require-base nil)
870 (let ((pkgs package-get-base)
871 (strings '(description category maintainer build-date date))
872 (symbols '(type requires provides priority))
874 (cond ((memq field strings)
875 (setq item (symbol-name item))
877 (when (string-match item (package-get-info (caar pkgs) field))
878 (setq results (push (caar pkgs) results)))
879 (setq pkgs (cdr pkgs))))
880 ((memq field symbols)
881 (if (or (eq field 'type)
882 (eq field 'priority))
884 (when (eq item (package-get-info (caar pkgs) field))
885 (setq results (push (caar pkgs) results)))
886 (setq pkgs (cdr pkgs)))
888 (when (memq item (package-get-info (caar pkgs) field))
889 (setq results (push (caar pkgs) results)))
890 (setq pkgs (cdr pkgs)))))
892 (error 'wrong-type-argument field)))
895 (insert (format "%s" results))
896 (message "%s" results)))
900 (defun package-get (package &optional version conflict install-dir)
901 "Fetch PACKAGE from remote site.
902 Optional arguments VERSION indicates which version to retrieve, nil
903 means most recent version. CONFLICT indicates what happens if the
904 package is already installed. Valid values for CONFLICT are:
905 'always always retrieve the package even if it is already installed
906 'never do not retrieve the package if it is installed.
907 INSTALL-DIR, if non-nil, specifies the package directory where
908 fetched packages should be installed.
910 The value of `package-get-base' is used to determine what files should
911 be retrieved. The value of `package-get-remote' is used to determine
912 where a package should be retrieved from.
914 Once the package is retrieved, its md5 checksum is computed. If that
915 sum does not match that stored in `package-get-base' for this version
916 of the package, an error is signalled.
918 Returns `t' upon success, the symbol `error' if the package was
919 successfully installed but errors occurred during initialization, or
921 (interactive (package-get-interactive-package-query nil t))
924 (package-get-info-version
925 (package-get-info-find-package package-get-base
927 (latest (package-get-info-prop this-package 'version))
928 (installed (package-get-key package :version))
931 (search-dir package-get-remote)
932 (base-filename (package-get-info-prop this-package 'filename))
934 filenames full-package-filename)
935 (if (and (equal (package-get-info package 'category) "mule")
936 (not (featurep 'mule)))
937 (error 'invalid-state
938 "Mule packages can't be installed with a non-Mule SXEmacs"))
939 (if (null this-package)
940 (if package-get-remote
941 (error 'search-failed
942 (format "Couldn't find package %s with version %s"
945 "No download site or local package location specified.")))
946 (if (null base-filename)
948 (format "No filename associated with package %s, version %s"
950 (setq install-dir (package-admin-get-install-dir package install-dir))
952 ;; If they asked for the latest using version=nil, don't get an older
953 ;; version than we already have.
955 (if (> (if (stringp installed)
956 (string-to-number installed)
959 (string-to-number latest)
961 (if (not (null version))
962 (warn "Installing %s package version %s, you had a newer version %s"
963 package latest installed)
964 (warn "Skipping %s package, you have a newer version %s"
966 (throw 'skip-update t))))
968 ;; Contrive a list of possible package filenames.
969 ;; Ugly. Is there a better way to do this?
970 (setq filenames (cons base-filename nil))
971 (if (string-match #r"^\(..*\)\.tar\.gz$" base-filename)
972 (setq filenames (append filenames
973 (list (concat (match-string 1 base-filename)
976 (setq version latest)
977 (unless (and (eq conflict 'never)
978 (package-get-installedp package version))
979 ;; Find the package from the search list in package-get-remote
980 ;; and copy it into the staging directory. Then validate
981 ;; the checksum. Finally, install the package.
983 (let (search-filenames dir current-filename dest-filename)
984 ;; In each search directory ...
986 (setq host (car search-dir)
987 dir (car (cdr search-dir))
988 search-filenames filenames)
990 ;; Look for one of the possible package filenames ...
991 (while search-filenames
992 (setq current-filename (car search-filenames)
993 dest-filename (package-get-staging-dir current-filename))
995 ;; No host means look on the current system.
997 (setq full-package-filename
998 (substitute-in-file-name
999 (expand-file-name current-filename
1000 (file-name-as-directory dir)))))
1002 ;; If it's already on the disk locally, and the size is
1004 ((and (file-exists-p dest-filename)
1005 (eq (nth 7 (file-attributes dest-filename))
1006 (package-get-info package 'size)))
1007 (setq full-package-filename dest-filename))
1009 ;; If the file exists on the remote system ...
1010 ((file-exists-p (package-get-remote-filename
1011 search-dir current-filename))
1013 (setq full-package-filename dest-filename)
1014 (message "Retrieving package `%s' ..."
1017 (copy-file (package-get-remote-filename search-dir
1019 full-package-filename t)))
1021 ;; If we found it, we're done.
1022 (if (and full-package-filename
1023 (file-exists-p full-package-filename))
1025 ;; Didn't find it. Try the next possible filename.
1026 (setq search-filenames (cdr search-filenames))))))
1028 (if (or (not full-package-filename)
1029 (not (file-exists-p full-package-filename)))
1030 (if package-get-remote
1031 (error 'search-failed
1032 (format "Unable to find file %s" base-filename))
1033 (error 'syntax-error
1034 "No download sites or local package locations specified.")))
1035 ;; Validate the md5 checksum
1036 ;; Doing it with SXEmacs removes the need for an external md5 program
1037 (message "Validating checksum for `%s'..." package) (sit-for 0)
1039 (insert-file-contents-literally full-package-filename)
1040 (if (not (string= (md5 (current-buffer))
1041 (package-get-info-prop this-package
1045 (delete-file full-package-filename))
1046 (error 'process-error
1047 (format "Package %s does not match md5 checksum %s has been deleted"
1048 base-filename full-package-filename)))))
1050 (package-admin-delete-binary-package package install-dir)
1052 (message "Installing package `%s' ..." package) (sit-for 0)
1054 (package-admin-add-binary-package full-package-filename
1058 ;; clear messages so that only messages from
1059 ;; package-get-init-package are seen, below.
1061 (if (package-get-init-package (package-admin-get-lispdir
1062 install-dir package))
1064 (run-hook-with-args 'package-install-hook package install-dir)
1065 (message "Added package `%s'" package)
1068 ;; display message only if there isn't already one.
1069 (if (not (current-message))
1071 (message "Added package `%s' (errors occurred)"
1075 (setq package-status 'errors)))))
1076 (message "Installation of package %s failed." base-filename)
1078 (switch-to-buffer package-admin-temp-buffer)
1079 ;; null host means a local package mirror
1081 (delete-file full-package-filename))
1082 (setq package-status nil)))
1084 (if (and found package-get-remove-copy (not (null host)))
1085 (delete-file full-package-filename))
1088 (defun package-get-info-find-package (which name)
1089 "Look in WHICH for the package called NAME and return all the info
1090 associated with it. See `package-get-base' for info on the format
1093 To access fields returned from this, use
1094 `package-get-info-version' to return information about particular a
1095 version. Use `package-get-info-find-prop' to find particular property
1096 from a version returned by `package-get-info-version'."
1097 (interactive "xPackage list: \nsPackage Name: ")
1099 (if (eq (caar which) name)
1102 (package-get-info-find-package (cdr which) name)))))
1104 (defun package-get-info-version (package version)
1105 "In PACKAGE, return the plist associated with a particular VERSION of the
1106 package. PACKAGE is typically as returned by
1107 `package-get-info-find-package'. If VERSION is nil, then return the
1108 first (aka most recent) version. Use `package-get-info-find-prop'
1109 to retrieve a particular property from the value returned by this."
1110 (interactive (package-get-interactive-package-query t t))
1111 (while (and version package (not (string= (plist-get (car package) 'version) version)))
1112 (setq package (cdr package)))
1113 (if package (car package)))
1115 (defun package-get-info-prop (package-version property)
1116 "In PACKAGE-VERSION, return the value associated with PROPERTY.
1117 PACKAGE-VERSION is typically returned by `package-get-info-version'
1118 and PROPERTY is typically (although not limited to) one of the
1121 version - version of this package
1122 provides - list of symbols provided
1123 requires - list of symbols that are required.
1124 These in turn are provided by other packages.
1125 size - size of the bundled package
1126 md5sum - computed md5 checksum"
1127 (interactive "xPackage Version: \nSProperty")
1128 (plist-get package-version property))
1130 (defun package-get-info-version-prop (package-list package version property)
1131 "In PACKAGE-LIST, search for PACKAGE with this VERSION and return
1133 (package-get-info-prop
1134 (package-get-info-version
1135 (package-get-info-find-package package-list package) version) property))
1137 (defun package-get-staging-dir (filename)
1138 "Return a good place to stash FILENAME when it is retrieved.
1139 Use `package-get-dir' for directory to store stuff.
1140 Creates `package-get-dir' if it doesn't exist."
1141 (interactive "FPackage filename: ")
1142 (if (not (file-exists-p package-get-dir))
1143 (make-directory package-get-dir))
1145 (file-name-nondirectory (or (and-fboundp 'efs-ftp-path
1146 (nth 2 (efs-ftp-path filename)))
1148 (file-name-as-directory package-get-dir)))
1150 (defun package-get-remote-filename (search filename)
1151 "Return FILENAME as a remote filename.
1152 It first checks if FILENAME already is a remote filename. If it is
1153 not, then it uses the (car search) as the remote site-name and the (cadr
1154 search) as the remote-directory and concatenates filename. In other
1157 site-name:remote-directory/filename.
1159 If ffi-curl has been loaded then this will return a URL style name,
1162 http://site-name/remote-directory/filename
1164 The url scheme to use in this case is from (third search).
1166 If (car search) is nil, (cadr search is interpreted as a local
1168 (if (or (file-remote-p filename)
1169 (and (string-match #r"^\(https?\|s?ftp\)://" filename)
1170 package-get-have-curl))
1172 (let ((site (car search))
1174 (scheme (third search)))
1175 (if (and site package-get-have-curl)
1176 (concat scheme "://" site "/" dir "/" filename)
1179 (if (string-match "@" site)
1183 (if (string-match "/$" dir)
1188 (defun package-get-installedp (package version)
1189 "Determine if PACKAGE with VERSION has already been installed.
1190 I'm not sure if I want to do this by searching directories or checking
1191 some built in variables. For now, use packages-package-list."
1192 ;; Use packages-package-list which contains name and version
1194 (package-get-info-find-package packages-package-list
1196 (if (floatp version)
1198 (string-to-number version))))
1201 (defun package-get-package-provider (sym &optional force-current)
1202 "Search for a package that provides SYM and return the name and
1203 version. Searches in `package-get-base' for SYM. If SYM is a
1204 consp, then it must match a corresponding (provide (SYM VERSION)) from
1207 If FORCE-CURRENT is non-nil make sure the database is up to date. This might
1208 lead to Emacs accessing remote sites."
1209 (interactive "SSymbol: ")
1210 (package-get-require-base force-current)
1211 (let ((packages package-get-base)
1214 (while (and (not done) packages)
1215 (let* ((this-name (caar packages))
1216 (this-package (cdr (car packages)))) ;strip off package name
1217 (while (and (not done) this-package)
1218 (if (or (eq this-name sym)
1220 (package-get-info-prop (car this-package) 'version))
1223 (package-get-info-prop (car this-package) 'provides)))
1224 (progn (setq done t)
1226 (list (caar packages)
1227 (package-get-info-prop (car this-package) 'version))))
1228 (setq this-package (cdr this-package)))))
1229 (setq packages (cdr packages)))
1230 (when (interactive-p)
1232 (message "%S" found)
1233 (message "No appropriate package found")))
1236 (defun package-get-ever-installed-p (pkg &optional notused)
1237 (string-match "-package$" (symbol-name pkg))
1238 (custom-initialize-set
1240 (if (package-get-info-find-package
1241 packages-package-list
1242 (intern (substring (symbol-name pkg) 0 (match-beginning 0))))
1245 (provide 'package-get)
1248 (unless (and (featurep 'package-ui)
1250 (require 'package-ui)
1253 ;;; package-get.el ends here