inversion.el 19 KB

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