1 ;;; inversion.el --- When you need something in version XX.XX
3 ;;; Copyright (C) 2002, 2003, 2005, 2006, 2007 Eric M. Ludlam
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; X-RCS: $Id: inversion.el,v 1.1 2007-11-26 15:06:41 michaels Exp $
9 (defvar inversion-version "1.3"
10 "Current version of InVersion.")
11 (defvar inversion-incompatible-version "0.1alpha1"
12 "An earlier release which is incompatible with this release.")
14 ;; InVersion is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; This software is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
31 ;; Keeping track of rapidly developing software is a tough thing to
32 ;; do, especially if you want to have co-dependent packages which all
33 ;; move at different rates.
35 ;; This library provides a framework for specifying version numbers
36 ;; and (as side effect) have a flexible way of getting a desired feature set.
38 ;; If you would like to use this package to satisfy dependency replace this:
44 ;; (require 'inversion)
45 ;; (inversion-require 'spiffy "1.0")
47 ;; If you feel the need to not throw errors, you can do this instead:
49 ;; (let ((err (inversion-test 'spiffy "1.0")))
50 ;; (if err (your-stuff-here)))
52 ;; If you new package (2.0) needs to make sure a load file from your
53 ;; package is compatible, use this test:
55 ;; (if (not (inversion-reverse-test 'spiffy version-from-file))
61 ;; If you would like to make inversion optional, do this:
63 ;; (or (require 'inversion nil t)
64 ;; (defun inversion-test (p v)
65 ;; (string= v (symbol-value
66 ;; (intern-soft (concat (symbol-string p) "-version"))))))
68 ;; Or modify to specify `inversion-require' instead.
71 ;; Offer to download newer versions of a package.
75 ;; Sept 3, 2002: First general publication.
77 (defconst inversion-decoders
79 (alpha "^\\([0-9]+\\)\\.\\([0-9]+\\)\\s-*alpha\\([0-9]+\\)?$" 3)
80 (beta "^\\([0-9]+\\)\\.\\([0-9]+\\)\\s-*beta\\([0-9]+\\)?$" 3)
81 (prerelease "^\\([0-9]+\\)\\.\\([0-9]+\\)\\s-*pre\\([0-9]+\\)?$" 3)
82 (full "^\\([0-9]+\\)\\.\\([0-9]+\\)$" 2)
83 (point "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)$" 3)
85 "List of decoders for version strings.
86 Each decoder is of the form:
88 ( RELEASE-TYPE REGEXP MAX )
90 RELEASE-TYPE is a symbol specifying something like `beta' or `alpha'.
91 REGEXP is the regular expression to match a version string.
92 MAX is the maximum number of match-numbers in the release number.
93 Decoders must be ordered to decode least stable versions before the
98 (defun inversion-decode-version (version-string)
99 "Decode VERSION-STRING into an encoded list.
100 Return value is of the form:
101 (RELEASE MAJOR MINOR ...)
102 where RELEASE is a symbol such as `full', or `beta'."
103 (let ((decoders inversion-decoders)
105 (while (and decoders (not result))
106 (if (string-match (nth 1 (car decoders)) version-string)
108 (num-left (nth 2 (car decoders)))
110 (while (<= count num-left)
112 (if (match-beginning count)
114 (substring version-string
115 (match-beginning count)
120 (setq result (cons (caar decoders) (nreverse ver))))
121 (setq decoders (cdr decoders))))
124 (defun inversion-package-version (package)
125 "Return the decoded version for PACKAGE."
126 (let ((ver (symbol-value
128 (concat (symbol-name package)
132 (error "Package %S does not define %S-version" package package))
134 (setq code (inversion-decode-version ver))
136 (error "%S-version value cannot be decoded" package))
139 (defun inversion-package-incompatibility-version (package)
140 "Return the decoded incompatibility version for PACKAGE.
141 The incompatibility version is specified by the programmer of
142 a package when a package is not backward compatible. It is
143 not an indication of new features or bug fixes."
144 (let ((ver (symbol-value
146 (concat (symbol-name package)
147 "-incompatible-version")))))
151 (inversion-decode-version ver))))
153 (defun inversion-recode (code)
154 "Convert CODE into a string."
155 (let ((r (nth 0 code)) ; release-type
156 (n (nth 1 code)) ; main number
157 (i (nth 2 code)) ; first increment
158 (p (nth 3 code))) ; second increment
164 (format "%s.%s%s%s" n i r p)))
166 (defun inversion-release-to-number (release-symbol)
167 "Convert RELEASE-SYMBOL into a number."
168 (let* ((ra (assoc release-symbol inversion-decoders))
169 (rn (- (length inversion-decoders)
170 (length (member ra inversion-decoders)))))
173 (defun inversion-= (ver1 ver2)
174 "Return non-nil if VER1 is equal to VER2."
177 (defun inversion-< (ver1 ver2)
178 "Return non-nil if VER1 is less than VER2."
179 (let ((v1-0 (inversion-release-to-number (nth 0 ver1)))
184 (v2-0 (inversion-release-to-number (nth 0 ver2)))
188 (or (and (= v1-0 v2-0)
191 v1-3 v2-3 ; all or nothin if elt - is =
202 (defun inversion-check-version (version incompatible-version
203 minimum &rest reserved)
204 "Check that a given version meets the minimum requirement.
205 VERSION, INCOMPATIBLE-VERSION and MINIMUM are of similar format to
206 return entries of `inversion-decode-version', or a classic version
207 string. INCOMPATIBLE-VERSION can be nil.
208 RESERVED arguments are kept for a later use.
210 - nil if everything is ok
211 - 'outdated if VERSION is less than MINIMUM.
212 - 'incompatible if VERSION is not backward compatible with MINIMUM.
213 - t if the check failed."
214 (let ((code (if (stringp version)
215 (inversion-decode-version version)
217 (req (if (stringp minimum)
218 (inversion-decode-version minimum)
224 ((inversion-= code req)
225 ;; Same version.. Yay!
227 ((inversion-< code req)
228 ;; Version is too old!
230 ((inversion-< req code)
231 ;; Newer is installed. What to do?
233 (if (stringp incompatible-version)
234 (inversion-decode-version incompatible-version)
235 incompatible-version)))
237 ((not incompatible) nil)
238 ((or (inversion-= req incompatible)
239 (inversion-< req incompatible))
240 ;; The requested version is = or < than what the package
241 ;; maintainer says is incompatible.
248 (defun inversion-test (package minimum &rest reserved)
249 "Test that PACKAGE meets the MINIMUM version requirement.
250 PACKAGE is a symbol, similar to what is passed to `require'.
251 MINIMUM is of similar format to return entries of
252 `inversion-decode-version', or a classic version string.
253 RESERVED arguments are kept for a later user.
254 This depends on the symbols `PACKAGE-version' and optionally
255 `PACKAGE-incompatible-version' being defined in PACKAGE.
256 Return nil if everything is ok. Return an error string otherwise."
257 (let ((check (inversion-check-version
258 (inversion-package-version package)
259 (inversion-package-incompatibility-version package)
263 ;; Same version.. Yay!
265 ((eq check 'outdated)
266 ;; Version is too old!
267 (format "You need to upgrade package %s to %s" package minimum))
268 ((eq check 'incompatible)
269 ;; Newer is installed but the requested version is = or < than
270 ;; what the package maintainer says is incompatible, then throw
272 (format "Package %s version is not backward compatible with %s"
275 (t "Inversion version check failed."))))
277 (defun inversion-reverse-test (package oldversion &rest reserved)
278 "Test that PACKAGE at OLDVERSION is still compatible.
279 If something like a save file is loaded at OLDVERSION, this
280 test will identify if OLDVERSION is compatible with the current version
282 PACKAGE is a symbol, similar to what is passed to `require'.
283 OLDVERSION is of similar format to return entries of
284 `inversion-decode-version', or a classic version string.
285 RESERVED arguments are kept for a later user.
286 This depends on the symbols `PACKAGE-version' and optionally
287 `PACKAGE-incompatible-version' being defined in PACKAGE.
288 Return nil if everything is ok. Return an error string otherwise."
289 (let ((check (inversion-check-version
290 (inversion-package-version package)
291 (inversion-package-incompatibility-version package)
292 oldversion reserved)))
295 ;; Same version.. Yay!
297 ((eq check 'outdated)
298 ;; Version is too old!
299 (format "Package %s version %s is not compatible with current version"
301 ((eq check 'incompatible)
302 ;; Newer is installed but the requested version is = or < than
303 ;; what the package maintainer says is incompatible, then throw
305 (format "Package %s version is not backward compatible with %s"
308 (t "Inversion version check failed."))))
311 (defun inversion-require (package version &optional file directory
313 "Declare that you need PACKAGE with at least VERSION.
314 PACKAGE might be found in FILE. (See `require'.)
315 Throws an error if VERSION is incompatible with what is installed.
316 Optional argument DIRECTORY is a location where new versions of
317 this tool can be located. If there is a versioning problem and
318 DIRECTORY is provided, inversion will offer to download the file.
319 Optional argument RESERVED is saved for later use."
320 (require package file)
321 (let ((err (inversion-test package version)))
324 (inversion-download-package-ask err package directory version)
327 (defconst inversion-find-data
328 '("(def\\(var\\|const\\)\\s-+%s-%s\\s-+\"\\([^\"]+\\)" 2)
329 "Regexp template and match data index of a version string.")
332 (defun inversion-find-version (package)
333 "Search for the version and incompatible version of PACKAGE.
334 Does not load PACKAGE nor requires that it has been previously loaded.
335 Search in the directories in `load-path' for a PACKAGE.el library.
336 Visit the file found and search for the declarations of variables or
337 constants `PACKAGE-version' and `PACKAGE-incompatible-version'. The
338 value of these variables must be a version string.
340 Return a pair (VERSION-STRING . INCOMPATIBLE-VERSION-STRING) where
341 INCOMPATIBLE-VERSION-STRING can be nil.
342 Return nil when VERSION-STRING was not found."
343 (let* ((file (locate-library (format "%s.el" package) t))
344 (tag (car inversion-find-data))
345 (idx (nth 1 inversion-find-data))
349 ;; The 3000 is a bit arbitrary, but should cut down on
350 ;; fileio as version info usually is at the very top
351 ;; of a file. AFter a long commentary could be bad.
352 (insert-file-contents-literally file nil 0 3000)
353 (goto-char (point-min))
354 (when (re-search-forward (format tag package 'version) nil t)
355 (setq version (list (match-string idx)))
356 (goto-char (point-min))
357 (when (re-search-forward
358 (format tag package 'incompatible-version) nil t)
359 (setcdr version (match-string idx))))))
363 (defun inversion-add-to-load-path (package minimum
366 "Add the PACKAGE path to `load-path' if necessary.
367 MINIMUM is the minimum version requirement of PACKAGE.
368 Optional argument INSTALLDIR is the base directory where PACKAGE is
369 installed. It defaults to `default-directory'/PACKAGE.
370 SUBDIRS are sub-directories to add to `load-path', following the main
372 (let ((ver (inversion-find-version package)))
373 ;; If PACKAGE not found or a bad version already in `load-path',
374 ;; prepend the new PACKAGE path, so it will be loaded first.
377 (inversion-check-version (car ver) (cdr ver) minimum)
378 (message "Outdated %s %s shadowed to meet minimum version %s"
379 package (car ver) minimum)
381 (let* ((default-directory
383 (expand-file-name (format "./%s" package))))
385 (when (file-directory-p default-directory)
388 (setq subdir (expand-file-name (car subdirs))
389 subdirs (cdr subdirs))
390 (when (file-directory-p subdir)
391 (message "%S added to `load-path'" subdir)
392 (add-to-list 'load-path subdir)))
394 (message "%S added to `load-path'" default-directory)
395 (add-to-list 'load-path default-directory))
396 ;; We get to this point iff we do not accept or there is no
397 ;; system file. Lets check the version of what we just
398 ;; installed... just to be safe.
399 (let ((newver (inversion-find-version package)))
401 (error "Failed to find version for newly installed %s"
403 (if (inversion-check-version (car newver) (cdr newver) minimum)
404 (error "Outdated %s %s just installed" package (car newver)))
409 (defun inversion-unit-test ()
410 "Test inversion to make sure it can identify different version strings."
412 (let ((c1 (inversion-package-version 'inversion))
413 (c1i (inversion-package-incompatibility-version 'inversion))
414 (c2 (inversion-decode-version "1.3alpha2"))
415 (c3 (inversion-decode-version "1.3beta4"))
416 (c4 (inversion-decode-version "1.3 beta5"))
417 (c5 (inversion-decode-version "1.3.4"))
418 (c6 (inversion-decode-version "2.3alpha"))
419 (c7 (inversion-decode-version "1.3"))
420 (c8 (inversion-decode-version "1.3pre1")))
441 (not (inversion-< c3 c2))
442 (not (inversion-< c4 c3))
443 (not (inversion-< c5 c4))
444 (not (inversion-< c6 c5))
445 (not (inversion-< c7 c2))
446 (not (inversion-< c7 c8))
447 ;; Test the tester on inversion
448 (not (inversion-test 'inversion inversion-version))
449 ;; Test that we throw an error
450 (inversion-test 'inversion "0.0.0")
451 (inversion-test 'inversion "1000.0")
453 (error "Inversion tests failed")
454 (message "Inversion tests passed."))))
456 ;;; URL and downloading code
458 (defun inversion-locate-package-files (package directory &optional version)
459 "Get a list of distributions of PACKAGE from DIRECTORY.
460 DIRECTORY can be an ange-ftp compatible filename, such as:
461 \"/ftp@ftp1.sourceforge.net/pub/sourceforge/PACKAGE\"
462 If it is a URL, wget will be used for download.
463 Optional argument VERSION will restrict the list of available versions
464 to the file matching VERSION exactly, or nil."
465 ;;DIRECTORY should also allow a URL:
466 ;; \"http://ftp1.sourceforge.net/PACKAGE\"
467 ;; but then I can get file listings easily.
468 (if (symbolp package) (setq package (symbol-name package)))
469 (directory-files directory t
471 (concat "^" package "-" version "\\>")
474 (defvar inversion-package-common-tails '( ".tar.gz"
479 "Common distribution mechanisms for Emacs Lisp packages.")
481 (defun inversion-locate-package-files-and-split (package directory &optional version)
482 "Use `inversion-locate-package-files' to get a list of PACKAGE files.
483 DIRECTORY is the location where distributions of PACKAGE are.
484 VERSION is an optional argument specifying a version to restrict to.
485 The return list is an alist with the version string in the CAR,
486 and the full path name in the CDR."
487 (if (symbolp package) (setq package (symbol-name package)))
488 (let ((f (inversion-locate-package-files package directory version))
489 (prefix (concat (file-name-as-directory directory)
493 (let* ((file (car f))
494 (dist (file-name-nondirectory file))
495 (tails inversion-package-common-tails)
497 (while (and tails (not verstring))
498 (when (string-match (concat (car tails) "$") dist)
500 (substring dist (1+ (length package)) (match-beginning 0))))
501 (setq tails (cdr tails)))
503 (error "Cannot decode version for %s" dist))
506 (cons verstring file)
511 (defun inversion-download-package-ask (err package directory version)
512 "Due to ERR, offer to download PACKAGE from DIRECTORY.
513 The package should have VERSION available for download."
514 (if (symbolp package) (setq package (symbol-name package)))
515 (let ((files (inversion-locate-package-files-and-split
516 package directory version)))
519 (if (not (y-or-n-p (concat err ": Download update? ")))
521 (let ((dest (read-directory-name (format "Download %s to: "
524 (if (> (length files) 1)
528 (read-file-name "Version to download: "
533 (file-name-as-directory directory)
537 (copy-file (cdr (car files)) dest))))))
540 (defun inversion-upgrade-package (package &optional directory)
541 "Try to upgrade PACKAGE in DIRECTORY is available."
542 (interactive "sPackage to upgrade: ")
543 (if (stringp package) (setq package (intern package)))
545 ;; Hope that the package maintainer specified.
546 (setq directory (symbol-value (or (intern-soft
547 (concat (symbol-name package)
550 (concat (symbol-name package)
552 (let ((files (inversion-locate-package-files-and-split
554 (cver (inversion-package-version package))
557 (if (inversion-< cver (inversion-decode-version (car f)))
558 (setq newer (cons f newer))))
563 ;; (inversion-upgrade-package
565 ;; "/ftp@ftp1.sourceforge.net:/pub/sourceforge/cedet")
567 ;; "/ftp@ftp1.sourceforge.net:/pub/sourceforge/cedet"
570 ;;; inversion.el ends here