1 ;;; inversion.el --- When you need something in version XX.XX
3 ;;; Copyright (C) 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010
4 ;;; Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Keeping track of rapidly developing software is a tough thing to
28 ;; do, especially if you want to have co-dependent packages which all
29 ;; move at different rates.
31 ;; This library provides a framework for specifying version numbers
32 ;; and (as side effect) have a flexible way of getting a desired feature set.
34 ;; If you would like to use this package to satisfy dependency replace this:
40 ;; (require 'inversion)
41 ;; (inversion-require 'spiffy "1.0")
43 ;; If you feel the need to not throw errors, you can do this instead:
45 ;; (let ((err (inversion-test 'spiffy "1.0")))
46 ;; (if err (your-stuff-here)))
48 ;; If you new package (2.0) needs to make sure a load file from your
49 ;; package is compatible, use this test:
51 ;; (if (not (inversion-reverse-test 'spiffy version-from-file))
57 ;; If you would like to make inversion optional, do this:
59 ;; (or (require 'inversion nil t)
60 ;; (defun inversion-test (p v)
61 ;; (string= v (symbol-value
62 ;; (intern-soft (concat (symbol-string p) "-version"))))))
64 ;; Or modify to specify `inversion-require' instead.
67 ;; Offer to download newer versions of a package.
71 ;; Sept 3, 2002: First general publication.
75 (defvar inversion-version
"1.3"
76 "Current version of InVersion.")
78 (defvar inversion-incompatible-version
"0.1alpha1"
79 "An earlier release which is incompatible with this release.")
81 (defconst inversion-decoders
83 (alpha "^\\([0-9]+\\)\\.\\([0-9]+\\)\\s-*\\.?alpha\\([0-9]+\\)?$" 3)
84 (beta "^\\([0-9]+\\)\\.\\([0-9]+\\)\\s-*\\.?beta\\([0-9]+\\)?$" 3)
85 (beta "^\\([0-9]+\\)\\.\\([0-9]+\\)\\s-*(beta\\([0-9]+\\)?)" 3)
86 (prerelease "^\\([0-9]+\\)\\.\\([0-9]+\\)\\s-*\\.?pre\\([0-9]+\\)?$" 3)
87 (full "^\\([0-9]+\\)\\.\\([0-9]+\\)$" 2)
88 (fullsingle "^\\([0-9]+\\)$" 1)
89 (patch "^\\([0-9]+\\)\\.\\([0-9]+\\) (patch \\([0-9]+\\))" 3)
90 (point "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)$" 3)
91 (build "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\).\\([0-9]+\\)$" 4)
93 "List of decoders for version strings.
94 Each decoder is of the form:
96 ( RELEASE-TYPE REGEXP MAX )
98 RELEASE-TYPE is a symbol specifying something like `beta' or `alpha'.
99 REGEXP is the regular expression to match a version string.
100 MAX is the maximum number of match-numbers in the release number.
101 Decoders must be ordered to decode least stable versions before the
106 (defun inversion-decode-version (version-string)
107 "Decode VERSION-STRING into an encoded list.
108 Return value is of the form:
109 (RELEASE MAJOR MINOR ...)
110 where RELEASE is a symbol such as `full', or `beta'."
111 (let ((decoders inversion-decoders
)
113 (while (and decoders
(not result
))
114 (if (string-match (nth 1 (car decoders
)) version-string
)
116 (num-left (nth 2 (car decoders
)))
118 (while (<= count num-left
)
120 (if (match-beginning count
)
122 (substring version-string
123 (match-beginning count
)
128 (setq result
(cons (caar decoders
) (nreverse ver
))))
129 (setq decoders
(cdr decoders
))))
132 (defun inversion-package-version (package)
133 "Return the decoded version for PACKAGE."
134 (let ((ver (symbol-value
136 (concat (symbol-name package
)
140 (error "Package %S does not define %S-version" package package
))
142 (setq code
(inversion-decode-version ver
))
144 (error "%S-version value cannot be decoded" package
))
147 (defun inversion-package-incompatibility-version (package)
148 "Return the decoded incompatibility version for PACKAGE.
149 The incompatibility version is specified by the programmer of
150 a package when a package is not backward compatible. It is
151 not an indication of new features or bug fixes."
152 (let ((ver (symbol-value
154 (concat (symbol-name package
)
155 "-incompatible-version")))))
159 (inversion-decode-version ver
))))
161 (defun inversion-recode (code)
162 "Convert CODE into a string."
163 (let ((r (nth 0 code
)) ; release-type
164 (n (nth 1 code
)) ; main number
165 (i (nth 2 code
)) ; first increment
166 (p (nth 3 code
))) ; second increment
172 (format "%s.%s%s%s" n i r p
)))
174 (defun inversion-release-to-number (release-symbol)
175 "Convert RELEASE-SYMBOL into a number."
176 (let* ((ra (assoc release-symbol inversion-decoders
))
177 (rn (- (length inversion-decoders
)
178 (length (member ra inversion-decoders
)))))
181 (defun inversion-= (ver1 ver2
)
182 "Return non-nil if VER1 is equal to VER2."
185 (defun inversion-< (ver1 ver2
)
186 "Return non-nil if VER1 is less than VER2."
187 (let ((v1-0 (inversion-release-to-number (nth 0 ver1
)))
193 (v2-0 (inversion-release-to-number (nth 0 ver2
)))
199 (or (and (= v1-0 v2-0
)
203 v1-4 v2-4
; all or nothin if elt - is =
208 v1-3 v2-3
; all or nothin if elt - is =
219 (defun inversion-check-version (version incompatible-version
220 minimum
&rest reserved
)
221 "Check that a given version meets the minimum requirement.
222 VERSION, INCOMPATIBLE-VERSION and MINIMUM are of similar format to
223 return entries of `inversion-decode-version', or a classic version
224 string. INCOMPATIBLE-VERSION can be nil.
225 RESERVED arguments are kept for a later use.
227 - nil if everything is ok
228 - 'outdated if VERSION is less than MINIMUM.
229 - 'incompatible if VERSION is not backward compatible with MINIMUM.
230 - t if the check failed."
231 (let ((code (if (stringp version
)
232 (inversion-decode-version version
)
234 (req (if (stringp minimum
)
235 (inversion-decode-version minimum
)
240 ((inversion-= code req
)
241 ;; Same version.. Yay!
243 ((inversion-< code req
)
244 ;; Version is too old!
246 ((inversion-< req code
)
247 ;; Newer is installed. What to do?
249 (if (stringp incompatible-version
)
250 (inversion-decode-version incompatible-version
)
251 incompatible-version
)))
253 ((not incompatible
) nil
)
254 ((or (inversion-= req incompatible
)
255 (inversion-< req incompatible
))
256 ;; The requested version is = or < than what the package
257 ;; maintainer says is incompatible.
264 (defun inversion-test (package minimum
&rest reserved
)
265 "Test that PACKAGE meets the MINIMUM version requirement.
266 PACKAGE is a symbol, similar to what is passed to `require'.
267 MINIMUM is of similar format to return entries of
268 `inversion-decode-version', or a classic version string.
269 RESERVED arguments are kept for a later user.
270 This depends on the symbols `PACKAGE-version' and optionally
271 `PACKAGE-incompatible-version' being defined in PACKAGE.
272 Return nil if everything is ok. Return an error string otherwise."
273 (let ((check (inversion-check-version
274 (inversion-package-version package
)
275 (inversion-package-incompatibility-version package
)
279 ;; Same version.. Yay!
281 ((eq check
'outdated
)
282 ;; Version is too old!
283 (format "You need to upgrade package %s to %s" package minimum
))
284 ((eq check
'incompatible
)
285 ;; Newer is installed but the requested version is = or < than
286 ;; what the package maintainer says is incompatible, then throw
288 (format "Package %s version is not backward compatible with %s"
291 (t "Inversion version check failed."))))
293 (defun inversion-reverse-test (package oldversion
&rest reserved
)
294 "Test that PACKAGE at OLDVERSION is still compatible.
295 If something like a save file is loaded at OLDVERSION, this
296 test will identify if OLDVERSION is compatible with the current version
298 PACKAGE is a symbol, similar to what is passed to `require'.
299 OLDVERSION is of similar format to return entries of
300 `inversion-decode-version', or a classic version string.
301 RESERVED arguments are kept for a later user.
302 This depends on the symbols `PACKAGE-version' and optionally
303 `PACKAGE-incompatible-version' being defined in PACKAGE.
304 Return nil if everything is ok. Return an error string otherwise."
305 (let ((check (inversion-check-version
306 (inversion-package-version package
)
307 (inversion-package-incompatibility-version package
)
308 oldversion reserved
)))
311 ;; Same version.. Yay!
313 ((eq check
'outdated
)
314 ;; Version is too old!
315 (format "Package %s version %s is not compatible with current version"
317 ((eq check
'incompatible
)
318 ;; Newer is installed but the requested version is = or < than
319 ;; what the package maintainer says is incompatible, then throw
321 (format "Package %s version is not backward compatible with %s"
324 (t "Inversion version check failed."))))
326 (defun inversion-require (package version
&optional file directory
328 "Declare that you need PACKAGE with at least VERSION.
329 PACKAGE might be found in FILE. (See `require'.)
330 Throws an error if VERSION is incompatible with what is installed.
331 Optional argument DIRECTORY is a location where new versions of
332 this tool can be located. If there is a versioning problem and
333 DIRECTORY is provided, inversion will offer to download the file.
334 Optional argument RESERVED is saved for later use."
335 (require package file
)
336 (let ((err (inversion-test package version
)))
339 (inversion-download-package-ask err package directory version
)
341 ;; Return the package symbol that was required.
344 (defun inversion-require-emacs (emacs-ver xemacs-ver
)
345 "Declare that you need either EMACS-VER, or XEMACS-VER.
346 Only checks one based on which kind of Emacs is being run."
347 (let ((err (inversion-test 'emacs
348 (if (featurep 'xemacs
)
355 (defconst inversion-find-data
356 '("(def\\(var\\|const\\)\\s-+%s-%s\\s-+\"\\([^\"]+\\)" 2)
357 "Regexp template and match data index of a version string.")
359 (defun inversion-find-version (package)
360 "Search for the version and incompatible version of PACKAGE.
361 Does not load PACKAGE nor requires that it has been previously loaded.
362 Search in the directories in `load-path' for a PACKAGE.el library.
363 Visit the file found and search for the declarations of variables or
364 constants `PACKAGE-version' and `PACKAGE-incompatible-version'. The
365 value of these variables must be a version string.
367 Return a pair (VERSION-STRING . INCOMPATIBLE-VERSION-STRING) where
368 INCOMPATIBLE-VERSION-STRING can be nil.
369 Return nil when VERSION-STRING was not found."
370 (let* ((file (locate-library (format "%s.el" package
) t
))
371 (tag (car inversion-find-data
))
372 (idx (nth 1 inversion-find-data
))
376 ;; The 3000 is a bit arbitrary, but should cut down on
377 ;; fileio as version info usually is at the very top
378 ;; of a file. AFter a long commentary could be bad.
379 (insert-file-contents-literally file nil
0 3000)
380 (goto-char (point-min))
381 (when (re-search-forward (format tag package
'version
) nil t
)
382 (setq version
(list (match-string idx
)))
383 (goto-char (point-min))
384 (when (re-search-forward
385 (format tag package
'incompatible-version
) nil t
)
386 (setcdr version
(match-string idx
))))))
389 (defun inversion-add-to-load-path (package minimum
392 "Add the PACKAGE path to `load-path' if necessary.
393 MINIMUM is the minimum version requirement of PACKAGE.
394 Optional argument INSTALLDIR is the base directory where PACKAGE is
395 installed. It defaults to `default-directory'/PACKAGE.
396 SUBDIRS are sub-directories to add to `load-path', following the main
398 (let ((ver (inversion-find-version package
)))
399 ;; If PACKAGE not found or a bad version already in `load-path',
400 ;; prepend the new PACKAGE path, so it will be loaded first.
403 (inversion-check-version (car ver
) (cdr ver
) minimum
)
404 (message "Outdated %s %s shadowed to meet minimum version %s"
405 package
(car ver
) minimum
)
407 (let* ((default-directory
409 (expand-file-name (format "./%s" package
))))
411 (when (file-directory-p default-directory
)
414 (setq subdir
(expand-file-name (car subdirs
))
415 subdirs
(cdr subdirs
))
416 (when (file-directory-p subdir
)
417 ;;(message "%S added to `load-path'" subdir)
418 (add-to-list 'load-path subdir
)))
420 ;;(message "%S added to `load-path'" default-directory)
421 (add-to-list 'load-path default-directory
))
422 ;; We get to this point iff we do not accept or there is no
423 ;; system file. Lets check the version of what we just
424 ;; installed... just to be safe.
425 (let ((newver (inversion-find-version package
)))
427 (error "Failed to find version for newly installed %s"
429 (if (inversion-check-version (car newver
) (cdr newver
) minimum
)
430 (error "Outdated %s %s just installed" package
(car newver
)))
433 ;;; URL and downloading code
435 (defun inversion-locate-package-files (package directory
&optional version
)
436 "Get a list of distributions of PACKAGE from DIRECTORY.
437 DIRECTORY can be an ange-ftp compatible filename, such as:
438 \"/ftp@ftp1.sourceforge.net/pub/sourceforge/PACKAGE\"
439 If it is a URL, wget will be used for download.
440 Optional argument VERSION will restrict the list of available versions
441 to the file matching VERSION exactly, or nil."
442 ;;DIRECTORY should also allow a URL:
443 ;; \"http://ftp1.sourceforge.net/PACKAGE\"
444 ;; but then I can get file listings easily.
445 (if (symbolp package
) (setq package
(symbol-name package
)))
446 (directory-files directory t
448 (concat "^" package
"-" version
"\\>")
451 (defvar inversion-package-common-tails
'( ".tar.gz"
456 "Common distribution mechanisms for Emacs Lisp packages.")
458 (defun inversion-locate-package-files-and-split (package directory
&optional version
)
459 "Use `inversion-locate-package-files' to get a list of PACKAGE files.
460 DIRECTORY is the location where distributions of PACKAGE are.
461 VERSION is an optional argument specifying a version to restrict to.
462 The return list is an alist with the version string in the CAR,
463 and the full path name in the CDR."
464 (if (symbolp package
) (setq package
(symbol-name package
)))
465 (let ((f (inversion-locate-package-files package directory version
))
468 (let* ((file (car f
))
469 (dist (file-name-nondirectory file
))
470 (tails inversion-package-common-tails
)
472 (while (and tails
(not verstring
))
473 (when (string-match (concat (car tails
) "$") dist
)
475 (substring dist
(1+ (length package
)) (match-beginning 0))))
476 (setq tails
(cdr tails
)))
478 (error "Cannot decode version for %s" dist
))
481 (cons verstring file
)
486 (defun inversion-download-package-ask (err package directory version
)
487 "Due to ERR, offer to download PACKAGE from DIRECTORY.
488 The package should have VERSION available for download."
489 (if (symbolp package
) (setq package
(symbol-name package
)))
490 (let ((files (inversion-locate-package-files-and-split
491 package directory version
)))
494 (if (not (y-or-n-p (concat err
": Download update? ")))
496 (let ((dest (read-directory-name (format "Download %s to: "
499 (if (> (length files
) 1)
503 (read-file-name "Version to download: "
508 (file-name-as-directory directory
)
512 (copy-file (cdr (car files
)) dest
))))))
514 ;;; How we upgrade packages in Emacs has yet to be ironed out.
516 ;; (defun inversion-upgrade-package (package &optional directory)
517 ;; "Try to upgrade PACKAGE in DIRECTORY is available."
518 ;; (interactive "sPackage to upgrade: ")
519 ;; (if (stringp package) (setq package (intern package)))
520 ;; (if (not directory)
521 ;; ;; Hope that the package maintainer specified.
522 ;; (setq directory (symbol-value (or (intern-soft
523 ;; (concat (symbol-name package)
526 ;; (concat (symbol-name package)
527 ;; "-directory"))))))
528 ;; (let ((files (inversion-locate-package-files-and-split
529 ;; package directory))
530 ;; (cver (inversion-package-version package))
533 ;; (if (inversion-< cver (inversion-decode-version (car f)))
534 ;; (setq newer (cons f newer))))
541 ;; arch-tag: 7239729c-3051-4d85-bb09-dcf92363aa3b
542 ;;; inversion.el ends here