Move lisp/emacs-lisp/authors.el to admin/
[bpt/emacs.git] / lisp / cedet / inversion.el
CommitLineData
666fd2cc
CY
1;;; inversion.el --- When you need something in version XX.XX
2
ba318903 3;;; Copyright (C) 2002-2003, 2005-2014 Free Software Foundation, Inc.
666fd2cc
CY
4
5;; Author: Eric M. Ludlam <zappo@gnu.org>
268c041e 6;; Version: 1.3
666fd2cc
CY
7;; Keywords: OO, lisp
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software: you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24;;; Commentary:
25;;
26;; Keeping track of rapidly developing software is a tough thing to
27;; do, especially if you want to have co-dependent packages which all
28;; move at different rates.
29;;
30;; This library provides a framework for specifying version numbers
31;; and (as side effect) have a flexible way of getting a desired feature set.
32;;
33;; If you would like to use this package to satisfy dependency replace this:
34;;
35;; (require 'spiffy)
36;;
37;; with this:
38;;
39;; (require 'inversion)
40;; (inversion-require 'spiffy "1.0")
41;;
42;; If you feel the need to not throw errors, you can do this instead:
43;;
44;; (let ((err (inversion-test 'spiffy "1.0")))
45;; (if err (your-stuff-here)))
46;;
47;; If you new package (2.0) needs to make sure a load file from your
48;; package is compatible, use this test:
49;;
50;; (if (not (inversion-reverse-test 'spiffy version-from-file))
51;; ;; Everything ok
52;; (do stuff)
53;; ;; Out of date
54;; (import-old-code))
55;;
56;; If you would like to make inversion optional, do this:
57;;
58;; (or (require 'inversion nil t)
59;; (defun inversion-test (p v)
60;; (string= v (symbol-value
61;; (intern-soft (concat (symbol-string p) "-version"))))))
62;;
63;; Or modify to specify `inversion-require' instead.
64;;
65;; TODO:
66;; Offer to download newer versions of a package.
67
68;;; History:
69;;
70;; Sept 3, 2002: First general publication.
71
72;;; Code:
73
74(defvar inversion-version "1.3"
75 "Current version of InVersion.")
76
77(defvar inversion-incompatible-version "0.1alpha1"
78 "An earlier release which is incompatible with this release.")
79
80(defconst inversion-decoders
81 '(
62a81506
CY
82 (alpha "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.?\\([0-9]*\\)?\\s-*\\.?alpha\\([0-9]+\\)?$" 4)
83 (beta "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.?\\([0-9]*\\)?\\s-*\\.?beta\\([0-9]+\\)?$" 4)
84 (beta "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.?\\([0-9]*\\)?\\s-*\\.?(beta\\([0-9]+\\)?)$" 4)
85 (beta "^[^/]+/\\w+--\\w+--\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)--patch-\\([0-9]+\\)" 4)
86 (beta "^\\w+: v\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)-\\([0-9]+\\)-\\(.*\\)" 5)
67d3ffe4 87 (prerelease "^\\([0-9]+\\)\\.\\([0-9]+\\)\\s-*\\.?pre\\([0-9]+\\)?$" 3)
62a81506 88 (full "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?$" 3)
666fd2cc 89 (fullsingle "^\\([0-9]+\\)$" 1)
62a81506 90 (patch "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?\\s-*(patch \\([0-9]+\\))" 4)
666fd2cc 91 (point "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)$" 3)
62a81506 92 (point "^\\w+: v\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)-\\(0\\)-\\(.*\\)" 5)
666fd2cc 93 (build "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\).\\([0-9]+\\)$" 4)
62a81506
CY
94 (full "^[^/]+/\\w+--\\w+--\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)--version-\\([0-9]+\\)" 4)
95 (full "^\\w+: v\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)" 5)
666fd2cc
CY
96 )
97 "List of decoders for version strings.
98Each decoder is of the form:
99
100 ( RELEASE-TYPE REGEXP MAX )
101
102RELEASE-TYPE is a symbol specifying something like `beta' or `alpha'.
103REGEXP is the regular expression to match a version string.
104MAX is the maximum number of match-numbers in the release number.
105Decoders must be ordered to decode least stable versions before the
106more stable ones.")
107
108;;; Version Checking
109;;
110(defun inversion-decode-version (version-string)
111 "Decode VERSION-STRING into an encoded list.
112Return value is of the form:
113 (RELEASE MAJOR MINOR ...)
114where RELEASE is a symbol such as `full', or `beta'."
115 (let ((decoders inversion-decoders)
116 (result nil))
117 (while (and decoders (not result))
118 (if (string-match (nth 1 (car decoders)) version-string)
119 (let ((ver nil)
120 (num-left (nth 2 (car decoders)))
121 (count 1))
122 (while (<= count num-left)
123 (setq ver (cons
124 (if (match-beginning count)
125 (string-to-number
126 (substring version-string
127 (match-beginning count)
128 (match-end count)))
129 1)
130 ver)
131 count (1+ count)))
132 (setq result (cons (caar decoders) (nreverse ver))))
133 (setq decoders (cdr decoders))))
134 result))
135
136(defun inversion-package-version (package)
137 "Return the decoded version for PACKAGE."
138 (let ((ver (symbol-value
139 (intern-soft
140 (concat (symbol-name package)
141 "-version"))))
142 (code nil))
143 (unless ver
144 (error "Package %S does not define %S-version" package package))
145 ;; Decode the code
146 (setq code (inversion-decode-version ver))
147 (unless code
62a81506 148 (error "%S-version value (%s) cannot be decoded" package ver))
666fd2cc
CY
149 code))
150
151(defun inversion-package-incompatibility-version (package)
152 "Return the decoded incompatibility version for PACKAGE.
153The incompatibility version is specified by the programmer of
154a package when a package is not backward compatible. It is
155not an indication of new features or bug fixes."
156 (let ((ver (symbol-value
157 (intern-soft
158 (concat (symbol-name package)
159 "-incompatible-version")))))
160 (if (not ver)
161 nil
162 ;; Decode the code
163 (inversion-decode-version ver))))
164
165(defun inversion-recode (code)
166 "Convert CODE into a string."
167 (let ((r (nth 0 code)) ; release-type
168 (n (nth 1 code)) ; main number
169 (i (nth 2 code)) ; first increment
170 (p (nth 3 code))) ; second increment
171 (cond
172 ((eq r 'full)
173 (setq r "" p ""))
174 ((eq r 'point)
175 (setq r ".")))
176 (format "%s.%s%s%s" n i r p)))
177
178(defun inversion-release-to-number (release-symbol)
179 "Convert RELEASE-SYMBOL into a number."
180 (let* ((ra (assoc release-symbol inversion-decoders))
181 (rn (- (length inversion-decoders)
182 (length (member ra inversion-decoders)))))
183 rn))
184
185(defun inversion-= (ver1 ver2)
186 "Return non-nil if VER1 is equal to VER2."
187 (equal ver1 ver2))
188
189(defun inversion-< (ver1 ver2)
190 "Return non-nil if VER1 is less than VER2."
191 (let ((v1-0 (inversion-release-to-number (nth 0 ver1)))
192 (v1-1 (nth 1 ver1))
193 (v1-2 (nth 2 ver1))
194 (v1-3 (nth 3 ver1))
195 (v1-4 (nth 4 ver1))
196 ;; v2
197 (v2-0 (inversion-release-to-number (nth 0 ver2)))
198 (v2-1 (nth 1 ver2))
199 (v2-2 (nth 2 ver2))
200 (v2-3 (nth 3 ver2))
201 (v2-4 (nth 4 ver2))
202 )
62a81506
CY
203
204 (cond ((and (equal (list v1-1 v1-2 v1-3 v1-4)
205 (list v2-1 v2-2 v2-3 v2-4))
206 v1-0 v2-0)
207 (< v1-0 v2-0))
208 ((and (equal v1-1 v2-1)
209 (equal v1-2 v2-2)
210 (equal v1-3 v2-3)
211 v1-4 v2-4) ; all or nothing if elt - is =
666fd2cc 212 (< v1-4 v2-4))
62a81506
CY
213 ((and (equal v1-1 v2-1)
214 (equal v1-2 v2-2)
215 v1-3 v2-3) ; all or nothing if elt - is =
666fd2cc 216 (< v1-3 v2-3))
62a81506
CY
217 ((and (equal v1-1 v2-1)
218 v1-2 v2-2)
666fd2cc 219 (< v1-2 v2-2))
62a81506
CY
220 ((and v1-1 v2-1)
221 (< v1-1 v2-1))
666fd2cc
CY
222 )))
223
224(defun inversion-check-version (version incompatible-version
cd1181db 225 minimum &rest reserved)
666fd2cc
CY
226 "Check that a given version meets the minimum requirement.
227VERSION, INCOMPATIBLE-VERSION and MINIMUM are of similar format to
228return entries of `inversion-decode-version', or a classic version
229string. INCOMPATIBLE-VERSION can be nil.
230RESERVED arguments are kept for a later use.
231Return:
cd1181db 232- nil if everything is ok.
666fd2cc
CY
233- 'outdated if VERSION is less than MINIMUM.
234- 'incompatible if VERSION is not backward compatible with MINIMUM.
235- t if the check failed."
236 (let ((code (if (stringp version)
237 (inversion-decode-version version)
238 version))
239 (req (if (stringp minimum)
240 (inversion-decode-version minimum)
241 minimum))
242 )
243 ;; Perform a test.
244 (cond
245 ((inversion-= code req)
246 ;; Same version.. Yay!
247 nil)
248 ((inversion-< code req)
249 ;; Version is too old!
250 'outdated)
251 ((inversion-< req code)
252 ;; Newer is installed. What to do?
253 (let ((incompatible
254 (if (stringp incompatible-version)
255 (inversion-decode-version incompatible-version)
256 incompatible-version)))
257 (cond
258 ((not incompatible) nil)
259 ((or (inversion-= req incompatible)
260 (inversion-< req incompatible))
261 ;; The requested version is = or < than what the package
262 ;; maintainer says is incompatible.
263 'incompatible)
264 ;; Things are ok.
265 (t nil))))
266 ;; Check failed
267 (t t))))
268
269(defun inversion-test (package minimum &rest reserved)
270 "Test that PACKAGE meets the MINIMUM version requirement.
271PACKAGE is a symbol, similar to what is passed to `require'.
272MINIMUM is of similar format to return entries of
273`inversion-decode-version', or a classic version string.
274RESERVED arguments are kept for a later user.
275This depends on the symbols `PACKAGE-version' and optionally
276`PACKAGE-incompatible-version' being defined in PACKAGE.
277Return nil if everything is ok. Return an error string otherwise."
278 (let ((check (inversion-check-version
279 (inversion-package-version package)
280 (inversion-package-incompatibility-version package)
281 minimum reserved)))
282 (cond
283 ((null check)
284 ;; Same version.. Yay!
285 nil)
286 ((eq check 'outdated)
287 ;; Version is too old!
288 (format "You need to upgrade package %s to %s" package minimum))
289 ((eq check 'incompatible)
290 ;; Newer is installed but the requested version is = or < than
291 ;; what the package maintainer says is incompatible, then throw
292 ;; that error.
293 (format "Package %s version is not backward compatible with %s"
294 package minimum))
295 ;; Check failed
296 (t "Inversion version check failed."))))
297
298(defun inversion-reverse-test (package oldversion &rest reserved)
299 "Test that PACKAGE at OLDVERSION is still compatible.
300If something like a save file is loaded at OLDVERSION, this
301test will identify if OLDVERSION is compatible with the current version
302of PACKAGE.
303PACKAGE is a symbol, similar to what is passed to `require'.
304OLDVERSION is of similar format to return entries of
305`inversion-decode-version', or a classic version string.
306RESERVED arguments are kept for a later user.
307This depends on the symbols `PACKAGE-version' and optionally
308`PACKAGE-incompatible-version' being defined in PACKAGE.
309Return nil if everything is ok. Return an error string otherwise."
310 (let ((check (inversion-check-version
311 (inversion-package-version package)
312 (inversion-package-incompatibility-version package)
313 oldversion reserved)))
314 (cond
315 ((null check)
316 ;; Same version.. Yay!
317 nil)
318 ((eq check 'outdated)
319 ;; Version is too old!
320 (format "Package %s version %s is not compatible with current version"
321 package oldversion))
322 ((eq check 'incompatible)
323 ;; Newer is installed but the requested version is = or < than
324 ;; what the package maintainer says is incompatible, then throw
325 ;; that error.
326 (format "Package %s version is not backward compatible with %s"
327 package oldversion))
328 ;; Check failed
329 (t "Inversion version check failed."))))
330
331(defun inversion-require (package version &optional file directory
332 &rest reserved)
333 "Declare that you need PACKAGE with at least VERSION.
334PACKAGE might be found in FILE. (See `require'.)
335Throws an error if VERSION is incompatible with what is installed.
336Optional argument DIRECTORY is a location where new versions of
337this tool can be located. If there is a versioning problem and
338DIRECTORY is provided, inversion will offer to download the file.
339Optional argument RESERVED is saved for later use."
340 (require package file)
341 (let ((err (inversion-test package version)))
342 (when err
343 (if directory
344 (inversion-download-package-ask err package directory version)
345 (error err)))
346 ;; Return the package symbol that was required.
347 package))
348
62a81506
CY
349;;;###autoload
350(defun inversion-require-emacs (emacs-ver xemacs-ver sxemacs-ver)
735135f9 351 "Declare that you need either EMACS-VER, XEMACS-VER or SXEMACS-ver.
666fd2cc
CY
352Only checks one based on which kind of Emacs is being run."
353 (let ((err (inversion-test 'emacs
62a81506
CY
354 (cond ((featurep 'sxemacs)
355 sxemacs-ver)
356 ((featurep 'xemacs)
357 xemacs-ver)
358 (t
359 emacs-ver)))))
666fd2cc
CY
360 (if err (error err)
361 ;; Something nice...
362 t)))
363
364(defconst inversion-find-data
365 '("(def\\(var\\|const\\)\\s-+%s-%s\\s-+\"\\([^\"]+\\)" 2)
366 "Regexp template and match data index of a version string.")
367
368(defun inversion-find-version (package)
369 "Search for the version and incompatible version of PACKAGE.
370Does not load PACKAGE nor requires that it has been previously loaded.
371Search in the directories in `load-path' for a PACKAGE.el library.
372Visit the file found and search for the declarations of variables or
373constants `PACKAGE-version' and `PACKAGE-incompatible-version'. The
374value of these variables must be a version string.
375
376Return a pair (VERSION-STRING . INCOMPATIBLE-VERSION-STRING) where
377INCOMPATIBLE-VERSION-STRING can be nil.
378Return nil when VERSION-STRING was not found."
379 (let* ((file (locate-library (format "%s.el" package) t))
380 (tag (car inversion-find-data))
381 (idx (nth 1 inversion-find-data))
382 version)
383 (when file
384 (with-temp-buffer
385 ;; The 3000 is a bit arbitrary, but should cut down on
386 ;; fileio as version info usually is at the very top
cd1181db 387 ;; of a file. After a long commentary could be bad.
666fd2cc
CY
388 (insert-file-contents-literally file nil 0 3000)
389 (goto-char (point-min))
390 (when (re-search-forward (format tag package 'version) nil t)
391 (setq version (list (match-string idx)))
392 (goto-char (point-min))
393 (when (re-search-forward
394 (format tag package 'incompatible-version) nil t)
395 (setcdr version (match-string idx))))))
396 version))
397
398(defun inversion-add-to-load-path (package minimum
399 &optional installdir
400 &rest subdirs)
401 "Add the PACKAGE path to `load-path' if necessary.
402MINIMUM is the minimum version requirement of PACKAGE.
403Optional argument INSTALLDIR is the base directory where PACKAGE is
404installed. It defaults to `default-directory'/PACKAGE.
405SUBDIRS are sub-directories to add to `load-path', following the main
406INSTALLDIR path."
407 (let ((ver (inversion-find-version package)))
408 ;; If PACKAGE not found or a bad version already in `load-path',
409 ;; prepend the new PACKAGE path, so it will be loaded first.
410 (when (or (not ver)
411 (and
412 (inversion-check-version (car ver) (cdr ver) minimum)
413 (message "Outdated %s %s shadowed to meet minimum version %s"
414 package (car ver) minimum)
415 t))
416 (let* ((default-directory
417 (or installdir
418 (expand-file-name (format "./%s" package))))
419 subdir)
420 (when (file-directory-p default-directory)
421 ;; Add SUBDIRS
422 (while subdirs
423 (setq subdir (expand-file-name (car subdirs))
424 subdirs (cdr subdirs))
425 (when (file-directory-p subdir)
426 ;;(message "%S added to `load-path'" subdir)
427 (add-to-list 'load-path subdir)))
428 ;; Add the main path
429 ;;(message "%S added to `load-path'" default-directory)
430 (add-to-list 'load-path default-directory))
431 ;; We get to this point iff we do not accept or there is no
c7015153 432 ;; system file. Let's check the version of what we just
666fd2cc
CY
433 ;; installed... just to be safe.
434 (let ((newver (inversion-find-version package)))
435 (if (not newver)
436 (error "Failed to find version for newly installed %s"
437 package))
438 (if (inversion-check-version (car newver) (cdr newver) minimum)
439 (error "Outdated %s %s just installed" package (car newver)))
440 )))))
441
442;;; URL and downloading code
443;;
444(defun inversion-locate-package-files (package directory &optional version)
445 "Get a list of distributions of PACKAGE from DIRECTORY.
446DIRECTORY can be an ange-ftp compatible filename, such as:
447 \"/ftp@ftp1.sourceforge.net/pub/sourceforge/PACKAGE\"
448If it is a URL, wget will be used for download.
449Optional argument VERSION will restrict the list of available versions
450to the file matching VERSION exactly, or nil."
451;;DIRECTORY should also allow a URL:
452;; \"http://ftp1.sourceforge.net/PACKAGE\"
453;; but then I can get file listings easily.
454 (if (symbolp package) (setq package (symbol-name package)))
455 (directory-files directory t
456 (if version
457 (concat "^" package "-" version "\\>")
458 package)))
459
460(defvar inversion-package-common-tails '( ".tar.gz"
461 ".tar"
462 ".zip"
463 ".gz"
464 )
465 "Common distribution mechanisms for Emacs Lisp packages.")
466
467(defun inversion-locate-package-files-and-split (package directory &optional version)
468 "Use `inversion-locate-package-files' to get a list of PACKAGE files.
469DIRECTORY is the location where distributions of PACKAGE are.
470VERSION is an optional argument specifying a version to restrict to.
471The return list is an alist with the version string in the CAR,
472and the full path name in the CDR."
473 (if (symbolp package) (setq package (symbol-name package)))
474 (let ((f (inversion-locate-package-files package directory version))
475 (out nil))
476 (while f
477 (let* ((file (car f))
478 (dist (file-name-nondirectory file))
479 (tails inversion-package-common-tails)
480 (verstring nil))
481 (while (and tails (not verstring))
482 (when (string-match (concat (car tails) "$") dist)
483 (setq verstring
484 (substring dist (1+ (length package)) (match-beginning 0))))
485 (setq tails (cdr tails)))
486 (if (not verstring)
487 (error "Cannot decode version for %s" dist))
488 (setq out
489 (cons
490 (cons verstring file)
491 out))
492 (setq f (cdr f))))
493 out))
494
495(defun inversion-download-package-ask (err package directory version)
496 "Due to ERR, offer to download PACKAGE from DIRECTORY.
497The package should have VERSION available for download."
498 (if (symbolp package) (setq package (symbol-name package)))
499 (let ((files (inversion-locate-package-files-and-split
500 package directory version)))
501 (if (not files)
502 (error err)
503 (if (not (y-or-n-p (concat err ": Download update? ")))
504 (error err)
505 (let ((dest (read-directory-name (format "Download %s to: "
506 package)
507 t)))
508 (if (> (length files) 1)
509 (setq files
510 (list
511 "foo" ;; ignored
512 (read-file-name "Version to download: "
513 directory
514 files
515 t
516 (concat
517 (file-name-as-directory directory)
518 package)
519 nil))))
520
521 (copy-file (cdr (car files)) dest))))))
522
523;;; How we upgrade packages in Emacs has yet to be ironed out.
524
525;; (defun inversion-upgrade-package (package &optional directory)
526;; "Try to upgrade PACKAGE in DIRECTORY is available."
527;; (interactive "sPackage to upgrade: ")
528;; (if (stringp package) (setq package (intern package)))
529;; (if (not directory)
530;; ;; Hope that the package maintainer specified.
531;; (setq directory (symbol-value (or (intern-soft
532;; (concat (symbol-name package)
533;; "-url"))
534;; (intern-soft
535;; (concat (symbol-name package)
536;; "-directory"))))))
537;; (let ((files (inversion-locate-package-files-and-split
538;; package directory))
539;; (cver (inversion-package-version package))
540;; (newer nil))
541;; (mapc (lambda (f)
542;; (if (inversion-< cver (inversion-decode-version (car f)))
543;; (setq newer (cons f newer))))
544;; files)
545;; newer
546;; ))
547
548(provide 'inversion)
549
550;;; inversion.el ends here