tm.tcl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. # -*- tcl -*-
  2. #
  3. # Searching for Tcl Modules. Defines a procedure, declares it as the primary
  4. # command for finding packages, however also uses the former 'package unknown'
  5. # command as a fallback.
  6. #
  7. # Locates all possible packages in a directory via a less restricted glob. The
  8. # targeted directory is derived from the name of the requested package, i.e.
  9. # the TM scan will look only at directories which can contain the requested
  10. # package. It will register all packages it found in the directory so that
  11. # future requests have a higher chance of being fulfilled by the ifneeded
  12. # database without having to come to us again.
  13. #
  14. # We do not remember where we have been and simply rescan targeted directories
  15. # when invoked again. The reasoning is this:
  16. #
  17. # - The only way we get back to the same directory is if someone is trying to
  18. # [package require] something that wasn't there on the first scan.
  19. #
  20. # Either
  21. # 1) It is there now: If we rescan, you get it; if not you don't.
  22. #
  23. # This covers the possibility that the application asked for a package
  24. # late, and the package was actually added to the installation after the
  25. # application was started. It shoukld still be able to find it.
  26. #
  27. # 2) It still is not there: Either way, you don't get it, but the rescan
  28. # takes time. This is however an error case and we dont't care that much
  29. # about it
  30. #
  31. # 3) It was there the first time; but for some reason a "package forget" has
  32. # been run, and "package" doesn't know about it anymore.
  33. #
  34. # This can be an indication that the application wishes to reload some
  35. # functionality. And should work as well.
  36. #
  37. # Note that this also strikes a balance between doing a glob targeting a
  38. # single package, and thus most likely requiring multiple globs of the same
  39. # directory when the application is asking for many packages, and trying to
  40. # glob for _everything_ in all subdirectories when looking for a package,
  41. # which comes with a heavy startup cost.
  42. #
  43. # We scan for regular packages only if no satisfying module was found.
  44. namespace eval ::tcl::tm {
  45. # Default paths. None yet.
  46. variable paths {}
  47. # The regex pattern a file name has to match to make it a Tcl Module.
  48. set pkgpattern {^([_[:alpha:]][:_[:alnum:]]*)-([[:digit:]].*)[.]tm$}
  49. # Export the public API
  50. namespace export path
  51. namespace ensemble create -command path -subcommands {add remove list}
  52. }
  53. # ::tcl::tm::path implementations --
  54. #
  55. # Public API to the module path. See specification.
  56. #
  57. # Arguments
  58. # cmd - The subcommand to execute
  59. # args - The paths to add/remove. Must not appear querying the
  60. # path with 'list'.
  61. #
  62. # Results
  63. # No result for subcommands 'add' and 'remove'. A list of paths for
  64. # 'list'.
  65. #
  66. # Sideeffects
  67. # The subcommands 'add' and 'remove' manipulate the list of paths to
  68. # search for Tcl Modules. The subcommand 'list' has no sideeffects.
  69. proc ::tcl::tm::add {args} {
  70. # PART OF THE ::tcl::tm::path ENSEMBLE
  71. #
  72. # The path is added at the head to the list of module paths.
  73. #
  74. # The command enforces the restriction that no path may be an ancestor
  75. # directory of any other path on the list. If the new path violates this
  76. # restriction an error wil be raised.
  77. #
  78. # If the path is already present as is no error will be raised and no
  79. # action will be taken.
  80. variable paths
  81. # We use a copy of the path as source during validation, and extend it as
  82. # well. Because we not only have to detect if the new paths are bogus with
  83. # respect to the existing paths, but also between themselves. Otherwise we
  84. # can still add bogus paths, by specifying them in a single call. This
  85. # makes the use of the new paths simpler as well, a trivial assignment of
  86. # the collected paths to the official state var.
  87. set newpaths $paths
  88. foreach p $args {
  89. if {$p in $newpaths} {
  90. # Ignore a path already on the list.
  91. continue
  92. }
  93. # Search for paths which are subdirectories of the new one. If there
  94. # are any then the new path violates the restriction about ancestors.
  95. set pos [lsearch -glob $newpaths ${p}/*]
  96. # Cannot use "in", we need the position for the message.
  97. if {$pos >= 0} {
  98. return -code error \
  99. "$p is ancestor of existing module path [lindex $newpaths $pos]."
  100. }
  101. # Now look for existing paths which are ancestors of the new one. This
  102. # reverse question forces us to loop over the existing paths, as each
  103. # element is the pattern, not the new path :(
  104. foreach ep $newpaths {
  105. if {[string match ${ep}/* $p]} {
  106. return -code error \
  107. "$p is subdirectory of existing module path $ep."
  108. }
  109. }
  110. set newpaths [linsert $newpaths 0 $p]
  111. }
  112. # The validation of the input is complete and successful, and everything
  113. # in newpaths is either an old path, or added. We can now extend the
  114. # official list of paths, a simple assignment is sufficient.
  115. set paths $newpaths
  116. return
  117. }
  118. proc ::tcl::tm::remove {args} {
  119. # PART OF THE ::tcl::tm::path ENSEMBLE
  120. #
  121. # Removes the path from the list of module paths. The command is silently
  122. # ignored if the path is not on the list.
  123. variable paths
  124. foreach p $args {
  125. set pos [lsearch -exact $paths $p]
  126. if {$pos >= 0} {
  127. set paths [lreplace $paths $pos $pos]
  128. }
  129. }
  130. }
  131. proc ::tcl::tm::list {} {
  132. # PART OF THE ::tcl::tm::path ENSEMBLE
  133. variable paths
  134. return $paths
  135. }
  136. # ::tcl::tm::UnknownHandler --
  137. #
  138. # Unknown handler for Tcl Modules, i.e. packages in module form.
  139. #
  140. # Arguments
  141. # original - Original [package unknown] procedure.
  142. # name - Name of desired package.
  143. # version - Version of desired package. Can be the
  144. # empty string.
  145. # exact - Either -exact or ommitted.
  146. #
  147. # Name, version, and exact are used to determine satisfaction. The
  148. # original is called iff no satisfaction was achieved. The name is also
  149. # used to compute the directory to target in the search.
  150. #
  151. # Results
  152. # None.
  153. #
  154. # Sideeffects
  155. # May populate the package ifneeded database with additional provide
  156. # scripts.
  157. proc ::tcl::tm::UnknownHandler {original name args} {
  158. # Import the list of paths to search for packages in module form.
  159. # Import the pattern used to check package names in detail.
  160. variable paths
  161. variable pkgpattern
  162. # Without paths to search we can do nothing. (Except falling back to the
  163. # regular search).
  164. if {[llength $paths]} {
  165. set pkgpath [string map {:: /} $name]
  166. set pkgroot [file dirname $pkgpath]
  167. if {$pkgroot eq "."} {
  168. set pkgroot ""
  169. }
  170. # We don't remember a copy of the paths while looping. Tcl Modules are
  171. # unable to change the list while we are searching for them. This also
  172. # simplifies the loop, as we cannot get additional directories while
  173. # iterating over the list. A simple foreach is sufficient.
  174. set satisfied 0
  175. foreach path $paths {
  176. if {![interp issafe] && ![file exists $path]} {
  177. continue
  178. }
  179. set currentsearchpath [file join $path $pkgroot]
  180. if {![interp issafe] && ![file exists $currentsearchpath]} {
  181. continue
  182. }
  183. set strip [llength [file split $path]]
  184. # We can't use glob in safe interps, so enclose the following in a
  185. # catch statement, where we get the module files out of the
  186. # subdirectories. In other words, Tcl Modules are not-functional
  187. # in such an interpreter. This is the same as for the command
  188. # "tclPkgUnknown", i.e. the search for regular packages.
  189. catch {
  190. # We always look for _all_ possible modules in the current
  191. # path, to get the max result out of the glob.
  192. foreach file [glob -nocomplain -directory $currentsearchpath *.tm] {
  193. set pkgfilename [join [lrange [file split $file] $strip end] ::]
  194. if {![regexp -- $pkgpattern $pkgfilename --> pkgname pkgversion]} {
  195. # Ignore everything not matching our pattern for
  196. # package names.
  197. continue
  198. }
  199. try {
  200. package vcompare $pkgversion 0
  201. } on error {} {
  202. # Ignore everything where the version part is not
  203. # acceptable to "package vcompare".
  204. continue
  205. }
  206. if {[package ifneeded $pkgname $pkgversion] ne {}} {
  207. # There's already a provide script registered for
  208. # this version of this package. Since all units of
  209. # code claiming to be the same version of the same
  210. # package ought to be identical, just stick with
  211. # the one we already have.
  212. continue
  213. }
  214. # We have found a candidate, generate a "provide script"
  215. # for it, and remember it. Note that we are using ::list
  216. # to do this; locally [list] means something else without
  217. # the namespace specifier.
  218. # NOTE. When making changes to the format of the provide
  219. # command generated below CHECK that the 'LOCATE'
  220. # procedure in core file 'platform/shell.tcl' still
  221. # understands it, or, if not, update its implementation
  222. # appropriately.
  223. #
  224. # Right now LOCATE's implementation assumes that the path
  225. # of the package file is the last element in the list.
  226. package ifneeded $pkgname $pkgversion \
  227. "[::list package provide $pkgname $pkgversion];[::list source -encoding utf-8 $file]"
  228. # We abort in this unknown handler only if we got a
  229. # satisfying candidate for the requested package.
  230. # Otherwise we still have to fallback to the regular
  231. # package search to complete the processing.
  232. if {($pkgname eq $name)
  233. && [package vsatisfies $pkgversion {*}$args]} {
  234. set satisfied 1
  235. # We do not abort the loop, and keep adding provide
  236. # scripts for every candidate in the directory, just
  237. # remember to not fall back to the regular search
  238. # anymore.
  239. }
  240. }
  241. }
  242. }
  243. if {$satisfied} {
  244. return
  245. }
  246. }
  247. # Fallback to previous command, if existing. See comment above about
  248. # ::list...
  249. if {[llength $original]} {
  250. uplevel 1 $original [::linsert $args 0 $name]
  251. }
  252. }
  253. # ::tcl::tm::Defaults --
  254. #
  255. # Determines the default search paths.
  256. #
  257. # Arguments
  258. # None
  259. #
  260. # Results
  261. # None.
  262. #
  263. # Sideeffects
  264. # May add paths to the list of defaults.
  265. proc ::tcl::tm::Defaults {} {
  266. global env tcl_platform
  267. lassign [split [info tclversion] .] major minor
  268. set exe [file normalize [info nameofexecutable]]
  269. # Note that we're using [::list], not [list] because [list] means
  270. # something other than [::list] in this namespace.
  271. roots [::list \
  272. [file dirname [info library]] \
  273. [file join [file dirname [file dirname $exe]] lib] \
  274. ]
  275. if {$tcl_platform(platform) eq "windows"} {
  276. set sep ";"
  277. } else {
  278. set sep ":"
  279. }
  280. for {set n $minor} {$n >= 0} {incr n -1} {
  281. foreach ev [::list \
  282. TCL${major}.${n}_TM_PATH \
  283. TCL${major}_${n}_TM_PATH \
  284. ] {
  285. if {![info exists env($ev)]} continue
  286. foreach p [split $env($ev) $sep] {
  287. path add $p
  288. }
  289. }
  290. }
  291. return
  292. }
  293. # ::tcl::tm::roots --
  294. #
  295. # Public API to the module path. See specification.
  296. #
  297. # Arguments
  298. # paths - List of 'root' paths to derive search paths from.
  299. #
  300. # Results
  301. # No result.
  302. #
  303. # Sideeffects
  304. # Calls 'path add' to paths to the list of module search paths.
  305. proc ::tcl::tm::roots {paths} {
  306. regexp {^(\d+)\.(\d+)} [package present Tcl] - major minor
  307. foreach pa $paths {
  308. set p [file join $pa tcl$major]
  309. for {set n $minor} {$n >= 0} {incr n -1} {
  310. set px [file join $p ${major}.${n}]
  311. if {![interp issafe]} {set px [file normalize $px]}
  312. path add $px
  313. }
  314. set px [file join $p site-tcl]
  315. if {![interp issafe]} {set px [file normalize $px]}
  316. path add $px
  317. }
  318. return
  319. }
  320. # Initialization. Set up the default paths, then insert the new handler into
  321. # the chain.
  322. if {![interp issafe]} {::tcl::tm::Defaults}