Add some font-locking for Python 2.7.
[emacs.git] / lisp / cedet / inversion.el
blobc79914139afcf1db561b830694065a7e08276129
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>
7 ;; Version: 0.2
8 ;; Keywords: OO, lisp
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/>.
25 ;;; Commentary:
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:
36 ;; (require 'spiffy)
38 ;; with 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))
52 ;; ;; Everything ok
53 ;; (do stuff)
54 ;; ;; Out of date
55 ;; (import-old-code))
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.
66 ;; TODO:
67 ;; Offer to download newer versions of a package.
69 ;;; History:
71 ;; Sept 3, 2002: First general publication.
73 ;;; Code:
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
102 more stable ones.")
104 ;;; Version Checking
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)
112 (result nil))
113 (while (and decoders (not result))
114 (if (string-match (nth 1 (car decoders)) version-string)
115 (let ((ver nil)
116 (num-left (nth 2 (car decoders)))
117 (count 1))
118 (while (<= count num-left)
119 (setq ver (cons
120 (if (match-beginning count)
121 (string-to-number
122 (substring version-string
123 (match-beginning count)
124 (match-end count)))
126 ver)
127 count (1+ count)))
128 (setq result (cons (caar decoders) (nreverse ver))))
129 (setq decoders (cdr decoders))))
130 result))
132 (defun inversion-package-version (package)
133 "Return the decoded version for PACKAGE."
134 (let ((ver (symbol-value
135 (intern-soft
136 (concat (symbol-name package)
137 "-version"))))
138 (code nil))
139 (unless ver
140 (error "Package %S does not define %S-version" package package))
141 ;; Decode the code
142 (setq code (inversion-decode-version ver))
143 (unless code
144 (error "%S-version value cannot be decoded" package))
145 code))
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
153 (intern-soft
154 (concat (symbol-name package)
155 "-incompatible-version")))))
156 (if (not ver)
158 ;; Decode the code
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
167 (cond
168 ((eq r 'full)
169 (setq r "" p ""))
170 ((eq r 'point)
171 (setq r ".")))
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)))))
179 rn))
181 (defun inversion-= (ver1 ver2)
182 "Return non-nil if VER1 is equal to VER2."
183 (equal ver1 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)))
188 (v1-1 (nth 1 ver1))
189 (v1-2 (nth 2 ver1))
190 (v1-3 (nth 3 ver1))
191 (v1-4 (nth 4 ver1))
192 ;; v2
193 (v2-0 (inversion-release-to-number (nth 0 ver2)))
194 (v2-1 (nth 1 ver2))
195 (v2-2 (nth 2 ver2))
196 (v2-3 (nth 3 ver2))
197 (v2-4 (nth 4 ver2))
199 (or (and (= v1-0 v2-0)
200 (= v1-1 v2-1)
201 (= v1-2 v2-2)
202 (= v1-3 v2-3)
203 v1-4 v2-4 ; all or nothin if elt - is =
204 (< v1-4 v2-4))
205 (and (= v1-0 v2-0)
206 (= v1-1 v2-1)
207 (= v1-2 v2-2)
208 v1-3 v2-3 ; all or nothin if elt - is =
209 (< v1-3 v2-3))
210 (and (= v1-1 v2-1)
211 (< v1-2 v2-2))
212 (and (< v1-1 v2-1))
213 (and (< v1-0 v2-0)
214 (= v1-1 v2-1)
215 (= v1-2 v2-2)
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.
226 Return:
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)
233 version))
234 (req (if (stringp minimum)
235 (inversion-decode-version minimum)
236 minimum))
238 ;; Perform a test.
239 (cond
240 ((inversion-= code req)
241 ;; Same version.. Yay!
242 nil)
243 ((inversion-< code req)
244 ;; Version is too old!
245 'outdated)
246 ((inversion-< req code)
247 ;; Newer is installed. What to do?
248 (let ((incompatible
249 (if (stringp incompatible-version)
250 (inversion-decode-version incompatible-version)
251 incompatible-version)))
252 (cond
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.
258 'incompatible)
259 ;; Things are ok.
260 (t nil))))
261 ;; Check failed
262 (t t))))
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)
276 minimum reserved)))
277 (cond
278 ((null check)
279 ;; Same version.. Yay!
280 nil)
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
287 ;; that error.
288 (format "Package %s version is not backward compatible with %s"
289 package minimum))
290 ;; Check failed
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
297 of PACKAGE.
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)))
309 (cond
310 ((null check)
311 ;; Same version.. Yay!
312 nil)
313 ((eq check 'outdated)
314 ;; Version is too old!
315 (format "Package %s version %s is not compatible with current version"
316 package oldversion))
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
320 ;; that error.
321 (format "Package %s version is not backward compatible with %s"
322 package oldversion))
323 ;; Check failed
324 (t "Inversion version check failed."))))
326 (defun inversion-require (package version &optional file directory
327 &rest reserved)
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)))
337 (when err
338 (if directory
339 (inversion-download-package-ask err package directory version)
340 (error err)))
341 ;; Return the package symbol that was required.
342 package))
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)
349 xemacs-ver
350 emacs-ver))))
351 (if err (error err)
352 ;; Something nice...
353 t)))
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))
373 version)
374 (when file
375 (with-temp-buffer
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))))))
387 version))
389 (defun inversion-add-to-load-path (package minimum
390 &optional installdir
391 &rest subdirs)
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
397 INSTALLDIR path."
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.
401 (when (or (not ver)
402 (and
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
408 (or installdir
409 (expand-file-name (format "./%s" package))))
410 subdir)
411 (when (file-directory-p default-directory)
412 ;; Add SUBDIRS
413 (while subdirs
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)))
419 ;; Add the main path
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)))
426 (if (not newver)
427 (error "Failed to find version for newly installed %s"
428 package))
429 (if (inversion-check-version (car newver) (cdr newver) minimum)
430 (error "Outdated %s %s just installed" package (car newver)))
431 )))))
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
447 (if version
448 (concat "^" package "-" version "\\>")
449 package)))
451 (defvar inversion-package-common-tails '( ".tar.gz"
452 ".tar"
453 ".zip"
454 ".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))
466 (out nil))
467 (while f
468 (let* ((file (car f))
469 (dist (file-name-nondirectory file))
470 (tails inversion-package-common-tails)
471 (verstring nil))
472 (while (and tails (not verstring))
473 (when (string-match (concat (car tails) "$") dist)
474 (setq verstring
475 (substring dist (1+ (length package)) (match-beginning 0))))
476 (setq tails (cdr tails)))
477 (if (not verstring)
478 (error "Cannot decode version for %s" dist))
479 (setq out
480 (cons
481 (cons verstring file)
482 out))
483 (setq f (cdr f))))
484 out))
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)))
492 (if (not files)
493 (error err)
494 (if (not (y-or-n-p (concat err ": Download update? ")))
495 (error err)
496 (let ((dest (read-directory-name (format "Download %s to: "
497 package)
498 t)))
499 (if (> (length files) 1)
500 (setq files
501 (list
502 "foo" ;; ignored
503 (read-file-name "Version to download: "
504 directory
505 files
507 (concat
508 (file-name-as-directory directory)
509 package)
510 nil))))
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)
524 ;; "-url"))
525 ;; (intern-soft
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))
531 ;; (newer nil))
532 ;; (mapc (lambda (f)
533 ;; (if (inversion-< cver (inversion-decode-version (car f)))
534 ;; (setq newer (cons f newer))))
535 ;; files)
536 ;; newer
537 ;; ))
539 (provide 'inversion)
541 ;; arch-tag: 7239729c-3051-4d85-bb09-dcf92363aa3b
542 ;;; inversion.el ends here