nov-ruby-openid 2.1.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (203) hide show
  1. data/CHANGELOG +215 -0
  2. data/CHANGES-2.1.0 +36 -0
  3. data/INSTALL +47 -0
  4. data/LICENSE +210 -0
  5. data/NOTICE +2 -0
  6. data/README +81 -0
  7. data/Rakefile +98 -0
  8. data/UPGRADE +127 -0
  9. data/VERSION +1 -0
  10. data/contrib/google/ruby-openid-apps-discovery-1.0.gem +0 -0
  11. data/contrib/google/ruby-openid-apps-discovery-1.01.gem +0 -0
  12. data/examples/README +32 -0
  13. data/examples/active_record_openid_store/README +58 -0
  14. data/examples/active_record_openid_store/XXX_add_open_id_store_to_db.rb +24 -0
  15. data/examples/active_record_openid_store/XXX_upgrade_open_id_store.rb +26 -0
  16. data/examples/active_record_openid_store/init.rb +8 -0
  17. data/examples/active_record_openid_store/lib/association.rb +10 -0
  18. data/examples/active_record_openid_store/lib/nonce.rb +3 -0
  19. data/examples/active_record_openid_store/lib/open_id_setting.rb +4 -0
  20. data/examples/active_record_openid_store/lib/openid_ar_store.rb +57 -0
  21. data/examples/active_record_openid_store/test/store_test.rb +212 -0
  22. data/examples/discover +49 -0
  23. data/examples/rails_openid/README +153 -0
  24. data/examples/rails_openid/Rakefile +10 -0
  25. data/examples/rails_openid/app/controllers/application.rb +4 -0
  26. data/examples/rails_openid/app/controllers/consumer_controller.rb +122 -0
  27. data/examples/rails_openid/app/controllers/login_controller.rb +45 -0
  28. data/examples/rails_openid/app/controllers/server_controller.rb +265 -0
  29. data/examples/rails_openid/app/helpers/application_helper.rb +3 -0
  30. data/examples/rails_openid/app/helpers/login_helper.rb +2 -0
  31. data/examples/rails_openid/app/helpers/server_helper.rb +9 -0
  32. data/examples/rails_openid/app/views/consumer/index.rhtml +81 -0
  33. data/examples/rails_openid/app/views/layouts/server.rhtml +68 -0
  34. data/examples/rails_openid/app/views/login/index.rhtml +56 -0
  35. data/examples/rails_openid/app/views/server/decide.rhtml +26 -0
  36. data/examples/rails_openid/config/boot.rb +19 -0
  37. data/examples/rails_openid/config/database.yml +74 -0
  38. data/examples/rails_openid/config/environment.rb +54 -0
  39. data/examples/rails_openid/config/environments/development.rb +19 -0
  40. data/examples/rails_openid/config/environments/production.rb +19 -0
  41. data/examples/rails_openid/config/environments/test.rb +19 -0
  42. data/examples/rails_openid/config/routes.rb +24 -0
  43. data/examples/rails_openid/doc/README_FOR_APP +2 -0
  44. data/examples/rails_openid/public/.htaccess +40 -0
  45. data/examples/rails_openid/public/404.html +8 -0
  46. data/examples/rails_openid/public/500.html +8 -0
  47. data/examples/rails_openid/public/dispatch.cgi +12 -0
  48. data/examples/rails_openid/public/dispatch.fcgi +26 -0
  49. data/examples/rails_openid/public/dispatch.rb +12 -0
  50. data/examples/rails_openid/public/favicon.ico +0 -0
  51. data/examples/rails_openid/public/images/openid_login_bg.gif +0 -0
  52. data/examples/rails_openid/public/javascripts/controls.js +750 -0
  53. data/examples/rails_openid/public/javascripts/dragdrop.js +584 -0
  54. data/examples/rails_openid/public/javascripts/effects.js +854 -0
  55. data/examples/rails_openid/public/javascripts/prototype.js +1785 -0
  56. data/examples/rails_openid/public/robots.txt +1 -0
  57. data/examples/rails_openid/script/about +3 -0
  58. data/examples/rails_openid/script/breakpointer +3 -0
  59. data/examples/rails_openid/script/console +3 -0
  60. data/examples/rails_openid/script/destroy +3 -0
  61. data/examples/rails_openid/script/generate +3 -0
  62. data/examples/rails_openid/script/performance/benchmarker +3 -0
  63. data/examples/rails_openid/script/performance/profiler +3 -0
  64. data/examples/rails_openid/script/plugin +3 -0
  65. data/examples/rails_openid/script/process/reaper +3 -0
  66. data/examples/rails_openid/script/process/spawner +3 -0
  67. data/examples/rails_openid/script/process/spinner +3 -0
  68. data/examples/rails_openid/script/runner +3 -0
  69. data/examples/rails_openid/script/server +3 -0
  70. data/examples/rails_openid/test/functional/login_controller_test.rb +18 -0
  71. data/examples/rails_openid/test/functional/server_controller_test.rb +18 -0
  72. data/examples/rails_openid/test/test_helper.rb +28 -0
  73. data/lib/hmac/hmac.rb +112 -0
  74. data/lib/hmac/sha1.rb +11 -0
  75. data/lib/hmac/sha2.rb +25 -0
  76. data/lib/openid.rb +20 -0
  77. data/lib/openid/association.rb +249 -0
  78. data/lib/openid/consumer.rb +395 -0
  79. data/lib/openid/consumer/associationmanager.rb +344 -0
  80. data/lib/openid/consumer/checkid_request.rb +186 -0
  81. data/lib/openid/consumer/discovery.rb +497 -0
  82. data/lib/openid/consumer/discovery_manager.rb +123 -0
  83. data/lib/openid/consumer/html_parse.rb +134 -0
  84. data/lib/openid/consumer/idres.rb +523 -0
  85. data/lib/openid/consumer/responses.rb +148 -0
  86. data/lib/openid/cryptutil.rb +115 -0
  87. data/lib/openid/dh.rb +89 -0
  88. data/lib/openid/extension.rb +39 -0
  89. data/lib/openid/extensions/ax.rb +539 -0
  90. data/lib/openid/extensions/oauth.rb +91 -0
  91. data/lib/openid/extensions/pape.rb +179 -0
  92. data/lib/openid/extensions/sreg.rb +277 -0
  93. data/lib/openid/extensions/ui.rb +53 -0
  94. data/lib/openid/extras.rb +11 -0
  95. data/lib/openid/fetchers.rb +258 -0
  96. data/lib/openid/kvform.rb +136 -0
  97. data/lib/openid/kvpost.rb +58 -0
  98. data/lib/openid/message.rb +553 -0
  99. data/lib/openid/protocolerror.rb +8 -0
  100. data/lib/openid/server.rb +1544 -0
  101. data/lib/openid/store/filesystem.rb +271 -0
  102. data/lib/openid/store/interface.rb +75 -0
  103. data/lib/openid/store/memcache.rb +107 -0
  104. data/lib/openid/store/memory.rb +84 -0
  105. data/lib/openid/store/nonce.rb +68 -0
  106. data/lib/openid/trustroot.rb +349 -0
  107. data/lib/openid/urinorm.rb +75 -0
  108. data/lib/openid/util.rb +110 -0
  109. data/lib/openid/yadis/accept.rb +148 -0
  110. data/lib/openid/yadis/constants.rb +21 -0
  111. data/lib/openid/yadis/discovery.rb +153 -0
  112. data/lib/openid/yadis/filters.rb +205 -0
  113. data/lib/openid/yadis/htmltokenizer.rb +305 -0
  114. data/lib/openid/yadis/parsehtml.rb +45 -0
  115. data/lib/openid/yadis/services.rb +42 -0
  116. data/lib/openid/yadis/xrds.rb +155 -0
  117. data/lib/openid/yadis/xri.rb +90 -0
  118. data/lib/openid/yadis/xrires.rb +99 -0
  119. data/setup.rb +1551 -0
  120. data/test/data/accept.txt +124 -0
  121. data/test/data/dh.txt +29 -0
  122. data/test/data/example-xrds.xml +14 -0
  123. data/test/data/linkparse.txt +587 -0
  124. data/test/data/n2b64 +650 -0
  125. data/test/data/test1-discover.txt +137 -0
  126. data/test/data/test1-parsehtml.txt +152 -0
  127. data/test/data/test_discover/malformed_meta_tag.html +19 -0
  128. data/test/data/test_discover/openid.html +11 -0
  129. data/test/data/test_discover/openid2.html +11 -0
  130. data/test/data/test_discover/openid2_xrds.xml +12 -0
  131. data/test/data/test_discover/openid2_xrds_no_local_id.xml +11 -0
  132. data/test/data/test_discover/openid_1_and_2.html +11 -0
  133. data/test/data/test_discover/openid_1_and_2_xrds.xml +16 -0
  134. data/test/data/test_discover/openid_1_and_2_xrds_bad_delegate.xml +17 -0
  135. data/test/data/test_discover/openid_and_yadis.html +12 -0
  136. data/test/data/test_discover/openid_no_delegate.html +10 -0
  137. data/test/data/test_discover/openid_utf8.html +11 -0
  138. data/test/data/test_discover/yadis_0entries.xml +12 -0
  139. data/test/data/test_discover/yadis_2_bad_local_id.xml +15 -0
  140. data/test/data/test_discover/yadis_2entries_delegate.xml +22 -0
  141. data/test/data/test_discover/yadis_2entries_idp.xml +21 -0
  142. data/test/data/test_discover/yadis_another_delegate.xml +14 -0
  143. data/test/data/test_discover/yadis_idp.xml +12 -0
  144. data/test/data/test_discover/yadis_idp_delegate.xml +13 -0
  145. data/test/data/test_discover/yadis_no_delegate.xml +11 -0
  146. data/test/data/test_xrds/=j3h.2007.11.14.xrds +25 -0
  147. data/test/data/test_xrds/README +12 -0
  148. data/test/data/test_xrds/delegated-20060809-r1.xrds +34 -0
  149. data/test/data/test_xrds/delegated-20060809-r2.xrds +34 -0
  150. data/test/data/test_xrds/delegated-20060809.xrds +34 -0
  151. data/test/data/test_xrds/no-xrd.xml +7 -0
  152. data/test/data/test_xrds/not-xrds.xml +2 -0
  153. data/test/data/test_xrds/prefixsometimes.xrds +34 -0
  154. data/test/data/test_xrds/ref.xrds +109 -0
  155. data/test/data/test_xrds/sometimesprefix.xrds +34 -0
  156. data/test/data/test_xrds/spoof1.xrds +25 -0
  157. data/test/data/test_xrds/spoof2.xrds +25 -0
  158. data/test/data/test_xrds/spoof3.xrds +37 -0
  159. data/test/data/test_xrds/status222.xrds +9 -0
  160. data/test/data/test_xrds/subsegments.xrds +58 -0
  161. data/test/data/test_xrds/valid-populated-xrds.xml +39 -0
  162. data/test/data/trustroot.txt +153 -0
  163. data/test/data/urinorm.txt +79 -0
  164. data/test/discoverdata.rb +131 -0
  165. data/test/test_accept.rb +170 -0
  166. data/test/test_association.rb +266 -0
  167. data/test/test_associationmanager.rb +917 -0
  168. data/test/test_ax.rb +690 -0
  169. data/test/test_checkid_request.rb +294 -0
  170. data/test/test_consumer.rb +257 -0
  171. data/test/test_cryptutil.rb +119 -0
  172. data/test/test_dh.rb +86 -0
  173. data/test/test_discover.rb +852 -0
  174. data/test/test_discovery_manager.rb +262 -0
  175. data/test/test_extension.rb +46 -0
  176. data/test/test_extras.rb +35 -0
  177. data/test/test_fetchers.rb +565 -0
  178. data/test/test_filters.rb +270 -0
  179. data/test/test_idres.rb +963 -0
  180. data/test/test_kvform.rb +165 -0
  181. data/test/test_kvpost.rb +65 -0
  182. data/test/test_linkparse.rb +101 -0
  183. data/test/test_message.rb +1116 -0
  184. data/test/test_nonce.rb +89 -0
  185. data/test/test_oauth.rb +175 -0
  186. data/test/test_openid_yadis.rb +178 -0
  187. data/test/test_pape.rb +247 -0
  188. data/test/test_parsehtml.rb +80 -0
  189. data/test/test_responses.rb +63 -0
  190. data/test/test_server.rb +2457 -0
  191. data/test/test_sreg.rb +479 -0
  192. data/test/test_stores.rb +298 -0
  193. data/test/test_trustroot.rb +113 -0
  194. data/test/test_ui.rb +93 -0
  195. data/test/test_urinorm.rb +35 -0
  196. data/test/test_util.rb +145 -0
  197. data/test/test_xrds.rb +169 -0
  198. data/test/test_xri.rb +48 -0
  199. data/test/test_xrires.rb +63 -0
  200. data/test/test_yadis_discovery.rb +220 -0
  201. data/test/testutil.rb +127 -0
  202. data/test/util.rb +53 -0
  203. metadata +336 -0
@@ -0,0 +1,45 @@
1
+ require "openid/yadis/htmltokenizer"
2
+ require 'cgi'
3
+
4
+ module OpenID
5
+ module Yadis
6
+ def Yadis.html_yadis_location(html)
7
+ parser = HTMLTokenizer.new(html)
8
+
9
+ # to keep track of whether or not we are in the head element
10
+ in_head = false
11
+
12
+ begin
13
+ while el = parser.getTag('head', '/head', 'meta', 'body', '/body',
14
+ 'html', 'script')
15
+
16
+ # we are leaving head or have reached body, so we bail
17
+ return nil if ['/head', 'body', '/body'].member?(el.tag_name)
18
+
19
+ if el.tag_name == 'head'
20
+ unless el.to_s[-2] == ?/ # tag ends with a /: a short tag
21
+ in_head = true
22
+ end
23
+ end
24
+ next unless in_head
25
+
26
+ if el.tag_name == 'script'
27
+ unless el.to_s[-2] == ?/ # tag ends with a /: a short tag
28
+ parser.getTag('/script')
29
+ end
30
+ end
31
+
32
+ return nil if el.tag_name == 'html'
33
+
34
+ if el.tag_name == 'meta' and (equiv = el.attr_hash['http-equiv'])
35
+ if ['x-xrds-location','x-yadis-location'].member?(equiv.downcase) &&
36
+ el.attr_hash.member?('content')
37
+ return CGI::unescapeHTML(el.attr_hash['content'])
38
+ end
39
+ end
40
+ end
41
+ rescue HTMLTokenizerError # just stop parsing if there's an error
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,42 @@
1
+
2
+ require 'openid/yadis/filters'
3
+ require 'openid/yadis/discovery'
4
+ require 'openid/yadis/xrds'
5
+
6
+ module OpenID
7
+ module Yadis
8
+ def Yadis.get_service_endpoints(input_url, flt=nil)
9
+ # Perform the Yadis protocol on the input URL and return an
10
+ # iterable of resulting endpoint objects.
11
+ #
12
+ # @param flt: A filter object or something that is convertable
13
+ # to a filter object (using mkFilter) that will be used to
14
+ # generate endpoint objects. This defaults to generating
15
+ # BasicEndpoint objects.
16
+ result = Yadis.discover(input_url)
17
+ begin
18
+ endpoints = Yadis.apply_filter(result.normalized_uri,
19
+ result.response_text, flt)
20
+ rescue XRDSError => err
21
+ raise DiscoveryFailure.new(err.to_s, nil)
22
+ end
23
+
24
+ return [result.normalized_uri, endpoints]
25
+ end
26
+
27
+ def Yadis.apply_filter(normalized_uri, xrd_data, flt=nil)
28
+ # Generate an iterable of endpoint objects given this input data,
29
+ # presumably from the result of performing the Yadis protocol.
30
+
31
+ flt = Yadis.make_filter(flt)
32
+ et = Yadis.parseXRDS(xrd_data)
33
+
34
+ endpoints = []
35
+ each_service(et) { |service_element|
36
+ endpoints += flt.get_service_endpoints(normalized_uri, service_element)
37
+ }
38
+
39
+ return endpoints
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,155 @@
1
+ require 'rexml/document'
2
+ require 'rexml/element'
3
+ require 'rexml/xpath'
4
+
5
+ require 'openid/yadis/xri'
6
+
7
+ module OpenID
8
+ module Yadis
9
+
10
+ XRD_NS_2_0 = 'xri://$xrd*($v*2.0)'
11
+ XRDS_NS = 'xri://$xrds'
12
+
13
+ XRDS_NAMESPACES = {
14
+ 'xrds' => XRDS_NS,
15
+ 'xrd' => XRD_NS_2_0,
16
+ }
17
+
18
+ class XRDSError < StandardError; end
19
+
20
+ # Raised when there's an assertion in the XRDS that it does not
21
+ # have the authority to make.
22
+ class XRDSFraud < XRDSError
23
+ end
24
+
25
+ def Yadis::get_canonical_id(iname, xrd_tree)
26
+ # Return the CanonicalID from this XRDS document.
27
+ #
28
+ # @param iname: the XRI being resolved.
29
+ # @type iname: unicode
30
+ #
31
+ # @param xrd_tree: The XRDS output from the resolver.
32
+ #
33
+ # @returns: The XRI CanonicalID or None.
34
+ # @returntype: unicode or None
35
+
36
+ xrd_list = []
37
+ REXML::XPath::match(xrd_tree.root, '/xrds:XRDS/xrd:XRD', XRDS_NAMESPACES).each { |el|
38
+ xrd_list << el
39
+ }
40
+
41
+ xrd_list.reverse!
42
+
43
+ cid_elements = []
44
+
45
+ if !xrd_list.empty?
46
+ xrd_list[0].elements.each { |e|
47
+ if !e.respond_to?('name')
48
+ next
49
+ end
50
+ if e.name == 'CanonicalID'
51
+ cid_elements << e
52
+ end
53
+ }
54
+ end
55
+
56
+ cid_element = cid_elements[0]
57
+
58
+ if !cid_element
59
+ return nil
60
+ end
61
+
62
+ canonicalID = XRI.make_xri(cid_element.text)
63
+
64
+ childID = canonicalID.downcase
65
+
66
+ xrd_list[1..-1].each { |xrd|
67
+ parent_sought = childID[0...childID.rindex('!')]
68
+
69
+ parent = XRI.make_xri(xrd.elements["CanonicalID"].text)
70
+
71
+ if parent_sought != parent.downcase
72
+ raise XRDSFraud.new(sprintf("%s can not come from %s", parent_sought,
73
+ parent))
74
+ end
75
+
76
+ childID = parent_sought
77
+ }
78
+
79
+ root = XRI.root_authority(iname)
80
+ if not XRI.provider_is_authoritative(root, childID)
81
+ raise XRDSFraud.new(sprintf("%s can not come from root %s", childID, root))
82
+ end
83
+
84
+ return canonicalID
85
+ end
86
+
87
+ class XRDSError < StandardError
88
+ end
89
+
90
+ def Yadis::parseXRDS(text)
91
+ if text.nil?
92
+ raise XRDSError.new("Not an XRDS document.")
93
+ end
94
+
95
+ begin
96
+ d = REXML::Document.new(text)
97
+ rescue RuntimeError => why
98
+ raise XRDSError.new("Not an XRDS document. Failed to parse XML.")
99
+ end
100
+
101
+ if is_xrds?(d)
102
+ return d
103
+ else
104
+ raise XRDSError.new("Not an XRDS document.")
105
+ end
106
+ end
107
+
108
+ def Yadis::is_xrds?(xrds_tree)
109
+ xrds_root = xrds_tree.root
110
+ return (!xrds_root.nil? and
111
+ xrds_root.name == 'XRDS' and
112
+ xrds_root.namespace == XRDS_NS)
113
+ end
114
+
115
+ def Yadis::get_yadis_xrd(xrds_tree)
116
+ REXML::XPath.each(xrds_tree.root,
117
+ '/xrds:XRDS/xrd:XRD[last()]',
118
+ XRDS_NAMESPACES) { |el|
119
+ return el
120
+ }
121
+ raise XRDSError.new("No XRD element found.")
122
+ end
123
+
124
+ # aka iterServices in Python
125
+ def Yadis::each_service(xrds_tree, &block)
126
+ xrd = get_yadis_xrd(xrds_tree)
127
+ xrd.each_element('Service', &block)
128
+ end
129
+
130
+ def Yadis::services(xrds_tree)
131
+ s = []
132
+ each_service(xrds_tree) { |service|
133
+ s << service
134
+ }
135
+ return s
136
+ end
137
+
138
+ def Yadis::expand_service(service_element)
139
+ es = service_element.elements
140
+ uris = es.each('URI') { |u| }
141
+ uris = prio_sort(uris)
142
+ types = es.each('Type/text()')
143
+ # REXML::Text objects are not strings.
144
+ types = types.collect { |t| t.to_s }
145
+ uris.collect { |uri| [types, uri.text, service_element] }
146
+ end
147
+
148
+ # Sort a list of elements that have priority attributes.
149
+ def Yadis::prio_sort(elements)
150
+ elements.sort { |a,b|
151
+ a.attribute('priority').to_s.to_i <=> b.attribute('priority').to_s.to_i
152
+ }
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,90 @@
1
+ require 'openid/yadis/xrds'
2
+ require 'openid/fetchers'
3
+
4
+ module OpenID
5
+ module Yadis
6
+ module XRI
7
+
8
+ # The '(' is for cross-reference authorities, and hopefully has a
9
+ # matching ')' somewhere.
10
+ XRI_AUTHORITIES = ["!", "=", "@", "+", "$", "("]
11
+
12
+ def self.identifier_scheme(identifier)
13
+ if (!identifier.nil? and
14
+ identifier.length > 0 and
15
+ (identifier.match('^xri://') or
16
+ XRI_AUTHORITIES.member?(identifier[0].chr)))
17
+ return :xri
18
+ else
19
+ return :uri
20
+ end
21
+ end
22
+
23
+ # Transform an XRI reference to an IRI reference. Note this is
24
+ # not not idempotent, so do not apply this to an identifier more
25
+ # than once. XRI Syntax section 2.3.1
26
+ def self.to_iri_normal(xri)
27
+ iri = xri.dup
28
+ iri.insert(0, 'xri://') if not iri.match('^xri://')
29
+ return escape_for_iri(iri)
30
+ end
31
+
32
+ # Note this is not not idempotent, so do not apply this more than
33
+ # once. XRI Syntax section 2.3.2
34
+ def self.escape_for_iri(xri)
35
+ esc = xri.dup
36
+ # encode all %
37
+ esc.gsub!(/%/, '%25')
38
+ esc.gsub!(/\((.*?)\)/) { |xref_match|
39
+ xref_match.gsub(/[\/\?\#]/) { |char_match|
40
+ CGI::escape(char_match)
41
+ }
42
+ }
43
+ return esc
44
+ end
45
+
46
+ # Transform an XRI reference to a URI reference. Note this is not
47
+ # not idempotent, so do not apply this to an identifier more than
48
+ # once. XRI Syntax section 2.3.1
49
+ def self.to_uri_normal(xri)
50
+ return iri_to_uri(to_iri_normal(xri))
51
+ end
52
+
53
+ # RFC 3987 section 3.1
54
+ def self.iri_to_uri(iri)
55
+ uri = iri.dup
56
+ # for char in ucschar or iprivate
57
+ # convert each char to %HH%HH%HH (as many %HH as octets)
58
+ return uri
59
+ end
60
+
61
+ def self.provider_is_authoritative(provider_id, canonical_id)
62
+ lastbang = canonical_id.rindex('!')
63
+ return false unless lastbang
64
+ parent = canonical_id[0...lastbang]
65
+ return parent == provider_id
66
+ end
67
+
68
+ def self.root_authority(xri)
69
+ xri = xri[6..-1] if xri.index('xri://') == 0
70
+ authority = xri.split('/', 2)[0]
71
+ if authority[0].chr == '('
72
+ root = authority[0...authority.index(')')+1]
73
+ elsif XRI_AUTHORITIES.member?(authority[0].chr)
74
+ root = authority[0].chr
75
+ else
76
+ root = authority.split(/[!*]/)[0]
77
+ end
78
+
79
+ self.make_xri(root)
80
+ end
81
+
82
+ def self.make_xri(xri)
83
+ if xri.index('xri://') != 0
84
+ xri = 'xri://' + xri
85
+ end
86
+ return xri
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,99 @@
1
+ require "cgi"
2
+ require "openid/yadis/xri"
3
+ require "openid/yadis/xrds"
4
+ require "openid/fetchers"
5
+
6
+ module OpenID
7
+
8
+ module Yadis
9
+
10
+ module XRI
11
+
12
+ class XRIHTTPError < StandardError; end
13
+
14
+ class ProxyResolver
15
+
16
+ DEFAULT_PROXY = 'http://proxy.xri.net/'
17
+
18
+ def initialize(proxy_url=nil)
19
+ if proxy_url
20
+ @proxy_url = proxy_url
21
+ else
22
+ @proxy_url = DEFAULT_PROXY
23
+ end
24
+
25
+ @proxy_url += '/' unless @proxy_url.match('/$')
26
+ end
27
+
28
+ def query_url(xri, service_type=nil)
29
+ # URI normal form has a leading xri://, but we need to strip
30
+ # that off again for the QXRI. This is under discussion for
31
+ # XRI Resolution WD 11.
32
+ qxri = XRI.to_uri_normal(xri)[6..-1]
33
+ hxri = @proxy_url + qxri
34
+ args = {'_xrd_r' => 'application/xrds+xml'}
35
+ if service_type
36
+ args['_xrd_t'] = service_type
37
+ else
38
+ # don't perform service endpoint selection
39
+ args['_xrd_r'] += ';sep=false'
40
+ end
41
+
42
+ return XRI.append_args(hxri, args)
43
+ end
44
+
45
+ def query(xri)
46
+ # these can be query args or http headers, needn't be both.
47
+ # headers = {'Accept' => 'application/xrds+xml;sep=true'}
48
+ canonicalID = nil
49
+
50
+ url = self.query_url(xri)
51
+ begin
52
+ response = OpenID.fetch(url)
53
+ rescue
54
+ raise XRIHTTPError, "Could not fetch #{xri}, #{$!}"
55
+ end
56
+ raise XRIHTTPError, "Could not fetch #{xri}" if response.nil?
57
+
58
+ xrds = Yadis::parseXRDS(response.body)
59
+ canonicalID = Yadis::get_canonical_id(xri, xrds)
60
+
61
+ return canonicalID, Yadis::services(xrds)
62
+ # TODO:
63
+ # * If we do get hits for multiple service_types, we're almost
64
+ # certainly going to have duplicated service entries and
65
+ # broken priority ordering.
66
+ end
67
+ end
68
+
69
+ def self.urlencode(args)
70
+ a = []
71
+ args.each do |key, val|
72
+ a << (CGI::escape(key) + "=" + CGI::escape(val))
73
+ end
74
+ a.join("&")
75
+ end
76
+
77
+ def self.append_args(url, args)
78
+ return url if args.length == 0
79
+
80
+ # rstrip question marks
81
+ rstripped = url.dup
82
+ while rstripped[-1].chr == '?'
83
+ rstripped = rstripped[0...rstripped.length-1]
84
+ end
85
+
86
+ if rstripped.index('?')
87
+ sep = '&'
88
+ else
89
+ sep = '?'
90
+ end
91
+
92
+ return url + sep + XRI.urlencode(args)
93
+ end
94
+
95
+ end
96
+
97
+ end
98
+
99
+ end
@@ -0,0 +1,1551 @@
1
+ #
2
+ # setup.rb
3
+ #
4
+ # Copyright (c) 2000-2005 Minero Aoki
5
+ #
6
+ # This program is free software.
7
+ # You can distribute/modify this program under the terms of
8
+ # the GNU LGPL, Lesser General Public License version 2.1.
9
+ #
10
+
11
+ unless Enumerable.method_defined?(:map) # Ruby 1.4.6
12
+ module Enumerable
13
+ alias map collect
14
+ end
15
+ end
16
+
17
+ unless File.respond_to?(:read) # Ruby 1.6
18
+ def File.read(fname)
19
+ open(fname) {|f|
20
+ return f.read
21
+ }
22
+ end
23
+ end
24
+
25
+ unless Errno.const_defined?(:ENOTEMPTY) # Windows?
26
+ module Errno
27
+ class ENOTEMPTY
28
+ # We do not raise this exception, implementation is not needed.
29
+ end
30
+ end
31
+ end
32
+
33
+ def File.binread(fname)
34
+ open(fname, 'rb') {|f|
35
+ return f.read
36
+ }
37
+ end
38
+
39
+ # for corrupted Windows' stat(2)
40
+ def File.dir?(path)
41
+ File.directory?((path[-1,1] == '/') ? path : path + '/')
42
+ end
43
+
44
+
45
+ class ConfigTable
46
+
47
+ include Enumerable
48
+
49
+ def initialize(rbconfig)
50
+ @rbconfig = rbconfig
51
+ @items = []
52
+ @table = {}
53
+ # options
54
+ @install_prefix = nil
55
+ @config_opt = nil
56
+ @verbose = true
57
+ @no_harm = false
58
+ @libsrc_pattern = '*.rb'
59
+ end
60
+
61
+ attr_accessor :install_prefix
62
+ attr_accessor :config_opt
63
+
64
+ attr_writer :verbose
65
+
66
+ def verbose?
67
+ @verbose
68
+ end
69
+
70
+ attr_writer :no_harm
71
+
72
+ def no_harm?
73
+ @no_harm
74
+ end
75
+
76
+ attr_accessor :libsrc_pattern
77
+
78
+ def [](key)
79
+ lookup(key).resolve(self)
80
+ end
81
+
82
+ def []=(key, val)
83
+ lookup(key).set val
84
+ end
85
+
86
+ def names
87
+ @items.map {|i| i.name }
88
+ end
89
+
90
+ def each(&block)
91
+ @items.each(&block)
92
+ end
93
+
94
+ def key?(name)
95
+ @table.key?(name)
96
+ end
97
+
98
+ def lookup(name)
99
+ @table[name] or setup_rb_error "no such config item: #{name}"
100
+ end
101
+
102
+ def add(item)
103
+ @items.push item
104
+ @table[item.name] = item
105
+ end
106
+
107
+ def remove(name)
108
+ item = lookup(name)
109
+ @items.delete_if {|i| i.name == name }
110
+ @table.delete_if {|name, i| i.name == name }
111
+ item
112
+ end
113
+
114
+ def load_script(path, inst = nil)
115
+ if File.file?(path)
116
+ MetaConfigEnvironment.new(self, inst).instance_eval File.read(path), path
117
+ end
118
+ end
119
+
120
+ def savefile
121
+ '.config'
122
+ end
123
+
124
+ def load_savefile
125
+ begin
126
+ File.foreach(savefile()) do |line|
127
+ k, v = *line.split(/=/, 2)
128
+ self[k] = v.strip
129
+ end
130
+ rescue Errno::ENOENT
131
+ setup_rb_error $!.message + "\n#{File.basename($0)} config first"
132
+ end
133
+ end
134
+
135
+ def save
136
+ @items.each {|i| i.value }
137
+ File.open(savefile(), 'w') {|f|
138
+ @items.each do |i|
139
+ f.printf "%s=%s\n", i.name, i.value if i.value? and i.value
140
+ end
141
+ }
142
+ end
143
+
144
+ def load_standard_entries
145
+ standard_entries(@rbconfig).each do |ent|
146
+ add ent
147
+ end
148
+ end
149
+
150
+ def standard_entries(rbconfig)
151
+ c = rbconfig
152
+
153
+ rubypath = c['bindir'] + '/' + c['ruby_install_name']
154
+
155
+ major = c['MAJOR'].to_i
156
+ minor = c['MINOR'].to_i
157
+ teeny = c['TEENY'].to_i
158
+ version = "#{major}.#{minor}"
159
+
160
+ # ruby ver. >= 1.4.4?
161
+ newpath_p = ((major >= 2) or
162
+ ((major == 1) and
163
+ ((minor >= 5) or
164
+ ((minor == 4) and (teeny >= 4)))))
165
+
166
+ if c['rubylibdir']
167
+ # V > 1.6.3
168
+ libruby = "#{c['prefix']}/lib/ruby"
169
+ librubyver = c['rubylibdir']
170
+ librubyverarch = c['archdir']
171
+ siteruby = c['sitedir']
172
+ siterubyver = c['sitelibdir']
173
+ siterubyverarch = c['sitearchdir']
174
+ elsif newpath_p
175
+ # 1.4.4 <= V <= 1.6.3
176
+ libruby = "#{c['prefix']}/lib/ruby"
177
+ librubyver = "#{c['prefix']}/lib/ruby/#{version}"
178
+ librubyverarch = "#{c['prefix']}/lib/ruby/#{version}/#{c['arch']}"
179
+ siteruby = c['sitedir']
180
+ siterubyver = "$siteruby/#{version}"
181
+ siterubyverarch = "$siterubyver/#{c['arch']}"
182
+ else
183
+ # V < 1.4.4
184
+ libruby = "#{c['prefix']}/lib/ruby"
185
+ librubyver = "#{c['prefix']}/lib/ruby/#{version}"
186
+ librubyverarch = "#{c['prefix']}/lib/ruby/#{version}/#{c['arch']}"
187
+ siteruby = "#{c['prefix']}/lib/ruby/#{version}/site_ruby"
188
+ siterubyver = siteruby
189
+ siterubyverarch = "$siterubyver/#{c['arch']}"
190
+ end
191
+ parameterize = lambda {|path|
192
+ path.sub(/\A#{Regexp.quote(c['prefix'])}/, '$prefix')
193
+ }
194
+
195
+ if arg = c['configure_args'].split.detect {|arg| /--with-make-prog=/ =~ arg }
196
+ makeprog = arg.sub(/'/, '').split(/=/, 2)[1]
197
+ else
198
+ makeprog = 'make'
199
+ end
200
+
201
+ [
202
+ ExecItem.new('installdirs', 'std/site/home',
203
+ 'std: install under libruby; site: install under site_ruby; home: install under $HOME')\
204
+ {|val, table|
205
+ case val
206
+ when 'std'
207
+ table['rbdir'] = '$librubyver'
208
+ table['sodir'] = '$librubyverarch'
209
+ when 'site'
210
+ table['rbdir'] = '$siterubyver'
211
+ table['sodir'] = '$siterubyverarch'
212
+ when 'home'
213
+ setup_rb_error '$HOME was not set' unless ENV['HOME']
214
+ table['prefix'] = ENV['HOME']
215
+ table['rbdir'] = '$libdir/ruby'
216
+ table['sodir'] = '$libdir/ruby'
217
+ end
218
+ },
219
+ PathItem.new('prefix', 'path', c['prefix'],
220
+ 'path prefix of target environment'),
221
+ PathItem.new('bindir', 'path', parameterize.call(c['bindir']),
222
+ 'the directory for commands'),
223
+ PathItem.new('libdir', 'path', parameterize.call(c['libdir']),
224
+ 'the directory for libraries'),
225
+ PathItem.new('datadir', 'path', parameterize.call(c['datadir']),
226
+ 'the directory for shared data'),
227
+ PathItem.new('mandir', 'path', parameterize.call(c['mandir']),
228
+ 'the directory for man pages'),
229
+ PathItem.new('sysconfdir', 'path', parameterize.call(c['sysconfdir']),
230
+ 'the directory for system configuration files'),
231
+ PathItem.new('localstatedir', 'path', parameterize.call(c['localstatedir']),
232
+ 'the directory for local state data'),
233
+ PathItem.new('libruby', 'path', libruby,
234
+ 'the directory for ruby libraries'),
235
+ PathItem.new('librubyver', 'path', librubyver,
236
+ 'the directory for standard ruby libraries'),
237
+ PathItem.new('librubyverarch', 'path', librubyverarch,
238
+ 'the directory for standard ruby extensions'),
239
+ PathItem.new('siteruby', 'path', siteruby,
240
+ 'the directory for version-independent aux ruby libraries'),
241
+ PathItem.new('siterubyver', 'path', siterubyver,
242
+ 'the directory for aux ruby libraries'),
243
+ PathItem.new('siterubyverarch', 'path', siterubyverarch,
244
+ 'the directory for aux ruby binaries'),
245
+ PathItem.new('rbdir', 'path', '$siterubyver',
246
+ 'the directory for ruby scripts'),
247
+ PathItem.new('sodir', 'path', '$siterubyverarch',
248
+ 'the directory for ruby extentions'),
249
+ PathItem.new('rubypath', 'path', rubypath,
250
+ 'the path to set to #! line'),
251
+ ProgramItem.new('rubyprog', 'name', rubypath,
252
+ 'the ruby program using for installation'),
253
+ ProgramItem.new('makeprog', 'name', makeprog,
254
+ 'the make program to compile ruby extentions'),
255
+ SelectItem.new('shebang', 'all/ruby/never', 'ruby',
256
+ 'shebang line (#!) editing mode'),
257
+ BoolItem.new('without-ext', 'yes/no', 'no',
258
+ 'does not compile/install ruby extentions')
259
+ ]
260
+ end
261
+ private :standard_entries
262
+
263
+ def load_multipackage_entries
264
+ multipackage_entries().each do |ent|
265
+ add ent
266
+ end
267
+ end
268
+
269
+ def multipackage_entries
270
+ [
271
+ PackageSelectionItem.new('with', 'name,name...', '', 'ALL',
272
+ 'package names that you want to install'),
273
+ PackageSelectionItem.new('without', 'name,name...', '', 'NONE',
274
+ 'package names that you do not want to install')
275
+ ]
276
+ end
277
+ private :multipackage_entries
278
+
279
+ ALIASES = {
280
+ 'std-ruby' => 'librubyver',
281
+ 'stdruby' => 'librubyver',
282
+ 'rubylibdir' => 'librubyver',
283
+ 'archdir' => 'librubyverarch',
284
+ 'site-ruby-common' => 'siteruby', # For backward compatibility
285
+ 'site-ruby' => 'siterubyver', # For backward compatibility
286
+ 'bin-dir' => 'bindir',
287
+ 'bin-dir' => 'bindir',
288
+ 'rb-dir' => 'rbdir',
289
+ 'so-dir' => 'sodir',
290
+ 'data-dir' => 'datadir',
291
+ 'ruby-path' => 'rubypath',
292
+ 'ruby-prog' => 'rubyprog',
293
+ 'ruby' => 'rubyprog',
294
+ 'make-prog' => 'makeprog',
295
+ 'make' => 'makeprog'
296
+ }
297
+
298
+ def fixup
299
+ ALIASES.each do |ali, name|
300
+ @table[ali] = @table[name]
301
+ end
302
+ @items.freeze
303
+ @table.freeze
304
+ @options_re = /\A--(#{@table.keys.join('|')})(?:=(.*))?\z/
305
+ end
306
+
307
+ def parse_opt(opt)
308
+ m = @options_re.match(opt) or setup_rb_error "config: unknown option #{opt}"
309
+ m.to_a[1,2]
310
+ end
311
+
312
+ def dllext
313
+ @rbconfig['DLEXT']
314
+ end
315
+
316
+ def value_config?(name)
317
+ lookup(name).value?
318
+ end
319
+
320
+ class Item
321
+ def initialize(name, template, default, desc)
322
+ @name = name.freeze
323
+ @template = template
324
+ @value = default
325
+ @default = default
326
+ @description = desc
327
+ end
328
+
329
+ attr_reader :name
330
+ attr_reader :description
331
+
332
+ attr_accessor :default
333
+ alias help_default default
334
+
335
+ def help_opt
336
+ "--#{@name}=#{@template}"
337
+ end
338
+
339
+ def value?
340
+ true
341
+ end
342
+
343
+ def value
344
+ @value
345
+ end
346
+
347
+ def resolve(table)
348
+ @value.gsub(%r<\$([^/]+)>) { table[$1] }
349
+ end
350
+
351
+ def set(val)
352
+ @value = check(val)
353
+ end
354
+
355
+ private
356
+
357
+ def check(val)
358
+ setup_rb_error "config: --#{name} requires argument" unless val
359
+ val
360
+ end
361
+ end
362
+
363
+ class BoolItem < Item
364
+ def config_type
365
+ 'bool'
366
+ end
367
+
368
+ def help_opt
369
+ "--#{@name}"
370
+ end
371
+
372
+ private
373
+
374
+ def check(val)
375
+ return 'yes' unless val
376
+ unless /\A(y(es)?|n(o)?|t(rue)?|f(alse))\z/i =~ val
377
+ setup_rb_error "config: --#{@name} accepts only yes/no for argument"
378
+ end
379
+ (/\Ay(es)?|\At(rue)/i =~ value) ? 'yes' : 'no'
380
+ end
381
+ end
382
+
383
+ class PathItem < Item
384
+ def config_type
385
+ 'path'
386
+ end
387
+
388
+ private
389
+
390
+ def check(path)
391
+ setup_rb_error "config: --#{@name} requires argument" unless path
392
+ path[0,1] == '$' ? path : File.expand_path(path)
393
+ end
394
+ end
395
+
396
+ class ProgramItem < Item
397
+ def config_type
398
+ 'program'
399
+ end
400
+ end
401
+
402
+ class SelectItem < Item
403
+ def initialize(name, selection, default, desc)
404
+ super
405
+ @ok = selection.split('/')
406
+ end
407
+
408
+ def config_type
409
+ 'select'
410
+ end
411
+
412
+ private
413
+
414
+ def check(val)
415
+ unless @ok.include?(val.strip)
416
+ setup_rb_error "config: use --#{@name}=#{@template} (#{val})"
417
+ end
418
+ val.strip
419
+ end
420
+ end
421
+
422
+ class ExecItem < Item
423
+ def initialize(name, selection, desc, &block)
424
+ super name, selection, nil, desc
425
+ @ok = selection.split('/')
426
+ @action = block
427
+ end
428
+
429
+ def config_type
430
+ 'exec'
431
+ end
432
+
433
+ def value?
434
+ false
435
+ end
436
+
437
+ def resolve(table)
438
+ setup_rb_error "$#{name()} wrongly used as option value"
439
+ end
440
+
441
+ undef set
442
+
443
+ def evaluate(val, table)
444
+ v = val.strip.downcase
445
+ unless @ok.include?(v)
446
+ setup_rb_error "invalid option --#{@name}=#{val} (use #{@template})"
447
+ end
448
+ @action.call v, table
449
+ end
450
+ end
451
+
452
+ class PackageSelectionItem < Item
453
+ def initialize(name, template, default, help_default, desc)
454
+ super name, template, default, desc
455
+ @help_default = help_default
456
+ end
457
+
458
+ attr_reader :help_default
459
+
460
+ def config_type
461
+ 'package'
462
+ end
463
+
464
+ private
465
+
466
+ def check(val)
467
+ unless File.dir?("packages/#{val}")
468
+ setup_rb_error "config: no such package: #{val}"
469
+ end
470
+ val
471
+ end
472
+ end
473
+
474
+ class MetaConfigEnvironment
475
+ def intiailize(config, installer)
476
+ @config = config
477
+ @installer = installer
478
+ end
479
+
480
+ def config_names
481
+ @config.names
482
+ end
483
+
484
+ def config?(name)
485
+ @config.key?(name)
486
+ end
487
+
488
+ def bool_config?(name)
489
+ @config.lookup(name).config_type == 'bool'
490
+ end
491
+
492
+ def path_config?(name)
493
+ @config.lookup(name).config_type == 'path'
494
+ end
495
+
496
+ def value_config?(name)
497
+ @config.lookup(name).config_type != 'exec'
498
+ end
499
+
500
+ def add_config(item)
501
+ @config.add item
502
+ end
503
+
504
+ def add_bool_config(name, default, desc)
505
+ @config.add BoolItem.new(name, 'yes/no', default ? 'yes' : 'no', desc)
506
+ end
507
+
508
+ def add_path_config(name, default, desc)
509
+ @config.add PathItem.new(name, 'path', default, desc)
510
+ end
511
+
512
+ def set_config_default(name, default)
513
+ @config.lookup(name).default = default
514
+ end
515
+
516
+ def remove_config(name)
517
+ @config.remove(name)
518
+ end
519
+
520
+ # For only multipackage
521
+ def packages
522
+ raise '[setup.rb fatal] multi-package metaconfig API packages() called for single-package; contact application package vendor' unless @installer
523
+ @installer.packages
524
+ end
525
+
526
+ # For only multipackage
527
+ def declare_packages(list)
528
+ raise '[setup.rb fatal] multi-package metaconfig API declare_packages() called for single-package; contact application package vendor' unless @installer
529
+ @installer.packages = list
530
+ end
531
+ end
532
+
533
+ end # class ConfigTable
534
+
535
+
536
+ # This module requires: #verbose?, #no_harm?
537
+ module FileOperations
538
+
539
+ def mkdir_p(dirname, prefix = nil)
540
+ dirname = prefix + File.expand_path(dirname) if prefix
541
+ $stderr.puts "mkdir -p #{dirname}" if verbose?
542
+ return if no_harm?
543
+
544
+ # Does not check '/', it's too abnormal.
545
+ dirs = File.expand_path(dirname).split(%r<(?=/)>)
546
+ if /\A[a-z]:\z/i =~ dirs[0]
547
+ disk = dirs.shift
548
+ dirs[0] = disk + dirs[0]
549
+ end
550
+ dirs.each_index do |idx|
551
+ path = dirs[0..idx].join('')
552
+ Dir.mkdir path unless File.dir?(path)
553
+ end
554
+ end
555
+
556
+ def rm_f(path)
557
+ $stderr.puts "rm -f #{path}" if verbose?
558
+ return if no_harm?
559
+ force_remove_file path
560
+ end
561
+
562
+ def rm_rf(path)
563
+ $stderr.puts "rm -rf #{path}" if verbose?
564
+ return if no_harm?
565
+ remove_tree path
566
+ end
567
+
568
+ def remove_tree(path)
569
+ if File.symlink?(path)
570
+ remove_file path
571
+ elsif File.dir?(path)
572
+ remove_tree0 path
573
+ else
574
+ force_remove_file path
575
+ end
576
+ end
577
+
578
+ def remove_tree0(path)
579
+ Dir.foreach(path) do |ent|
580
+ next if ent == '.'
581
+ next if ent == '..'
582
+ entpath = "#{path}/#{ent}"
583
+ if File.symlink?(entpath)
584
+ remove_file entpath
585
+ elsif File.dir?(entpath)
586
+ remove_tree0 entpath
587
+ else
588
+ force_remove_file entpath
589
+ end
590
+ end
591
+ begin
592
+ Dir.rmdir path
593
+ rescue Errno::ENOTEMPTY
594
+ # directory may not be empty
595
+ end
596
+ end
597
+
598
+ def move_file(src, dest)
599
+ force_remove_file dest
600
+ begin
601
+ File.rename src, dest
602
+ rescue
603
+ File.open(dest, 'wb') {|f|
604
+ f.write File.binread(src)
605
+ }
606
+ File.chmod File.stat(src).mode, dest
607
+ File.unlink src
608
+ end
609
+ end
610
+
611
+ def force_remove_file(path)
612
+ begin
613
+ remove_file path
614
+ rescue
615
+ end
616
+ end
617
+
618
+ def remove_file(path)
619
+ File.chmod 0777, path
620
+ File.unlink path
621
+ end
622
+
623
+ def install(from, dest, mode, prefix = nil)
624
+ $stderr.puts "install #{from} #{dest}" if verbose?
625
+ return if no_harm?
626
+
627
+ realdest = prefix ? prefix + File.expand_path(dest) : dest
628
+ realdest = File.join(realdest, File.basename(from)) if File.dir?(realdest)
629
+ str = File.binread(from)
630
+ if diff?(str, realdest)
631
+ verbose_off {
632
+ rm_f realdest if File.exist?(realdest)
633
+ }
634
+ File.open(realdest, 'wb') {|f|
635
+ f.write str
636
+ }
637
+ File.chmod mode, realdest
638
+
639
+ File.open("#{objdir_root()}/InstalledFiles", 'a') {|f|
640
+ if prefix
641
+ f.puts realdest.sub(prefix, '')
642
+ else
643
+ f.puts realdest
644
+ end
645
+ }
646
+ end
647
+ end
648
+
649
+ def diff?(new_content, path)
650
+ return true unless File.exist?(path)
651
+ new_content != File.binread(path)
652
+ end
653
+
654
+ def command(*args)
655
+ $stderr.puts args.join(' ') if verbose?
656
+ system(*args) or raise RuntimeError,
657
+ "system(#{args.map{|a| a.inspect }.join(' ')}) failed"
658
+ end
659
+
660
+ def ruby(*args)
661
+ command config('rubyprog'), *args
662
+ end
663
+
664
+ def make(task = nil)
665
+ command(*[config('makeprog'), task].compact)
666
+ end
667
+
668
+ def extdir?(dir)
669
+ File.exist?("#{dir}/MANIFEST") or File.exist?("#{dir}/extconf.rb")
670
+ end
671
+
672
+ def files_of(dir)
673
+ Dir.open(dir) {|d|
674
+ return d.select {|ent| File.file?("#{dir}/#{ent}") }
675
+ }
676
+ end
677
+
678
+ DIR_REJECT = %w( . .. CVS SCCS RCS CVS.adm .svn )
679
+
680
+ def directories_of(dir)
681
+ Dir.open(dir) {|d|
682
+ return d.select {|ent| File.dir?("#{dir}/#{ent}") } - DIR_REJECT
683
+ }
684
+ end
685
+
686
+ end
687
+
688
+
689
+ # This module requires: #srcdir_root, #objdir_root, #relpath
690
+ module HookScriptAPI
691
+
692
+ def get_config(key)
693
+ @config[key]
694
+ end
695
+
696
+ alias config get_config
697
+
698
+ # obsolete: use metaconfig to change configuration
699
+ def set_config(key, val)
700
+ @config[key] = val
701
+ end
702
+
703
+ #
704
+ # srcdir/objdir (works only in the package directory)
705
+ #
706
+
707
+ def curr_srcdir
708
+ "#{srcdir_root()}/#{relpath()}"
709
+ end
710
+
711
+ def curr_objdir
712
+ "#{objdir_root()}/#{relpath()}"
713
+ end
714
+
715
+ def srcfile(path)
716
+ "#{curr_srcdir()}/#{path}"
717
+ end
718
+
719
+ def srcexist?(path)
720
+ File.exist?(srcfile(path))
721
+ end
722
+
723
+ def srcdirectory?(path)
724
+ File.dir?(srcfile(path))
725
+ end
726
+
727
+ def srcfile?(path)
728
+ File.file?(srcfile(path))
729
+ end
730
+
731
+ def srcentries(path = '.')
732
+ Dir.open("#{curr_srcdir()}/#{path}") {|d|
733
+ return d.to_a - %w(. ..)
734
+ }
735
+ end
736
+
737
+ def srcfiles(path = '.')
738
+ srcentries(path).select {|fname|
739
+ File.file?(File.join(curr_srcdir(), path, fname))
740
+ }
741
+ end
742
+
743
+ def srcdirectories(path = '.')
744
+ srcentries(path).select {|fname|
745
+ File.dir?(File.join(curr_srcdir(), path, fname))
746
+ }
747
+ end
748
+
749
+ end
750
+
751
+
752
+ class ToplevelInstaller
753
+
754
+ Version = '3.4.0'
755
+ Copyright = 'Copyright (c) 2000-2005 Minero Aoki'
756
+
757
+ TASKS = [
758
+ [ 'all', 'do config, setup, then install' ],
759
+ [ 'config', 'saves your configurations' ],
760
+ [ 'show', 'shows current configuration' ],
761
+ [ 'setup', 'compiles ruby extentions and others' ],
762
+ [ 'install', 'installs files' ],
763
+ [ 'test', 'run all tests in test/' ],
764
+ [ 'clean', "does `make clean' for each extention" ],
765
+ [ 'distclean',"does `make distclean' for each extention" ]
766
+ ]
767
+
768
+ def ToplevelInstaller.invoke
769
+ config = ConfigTable.new(load_rbconfig())
770
+ config.load_standard_entries
771
+ config.load_multipackage_entries if multipackage?
772
+ config.fixup
773
+ klass = (multipackage?() ? ToplevelInstallerMulti : ToplevelInstaller)
774
+ klass.new(File.dirname($0), config).invoke
775
+ end
776
+
777
+ def ToplevelInstaller.multipackage?
778
+ File.dir?(File.dirname($0) + '/packages')
779
+ end
780
+
781
+ def ToplevelInstaller.load_rbconfig
782
+ if arg = ARGV.detect {|arg| /\A--rbconfig=/ =~ arg }
783
+ ARGV.delete(arg)
784
+ load File.expand_path(arg.split(/=/, 2)[1])
785
+ $".push 'rbconfig.rb'
786
+ else
787
+ require 'rbconfig'
788
+ end
789
+ ::Config::CONFIG
790
+ end
791
+
792
+ def initialize(ardir_root, config)
793
+ @ardir = File.expand_path(ardir_root)
794
+ @config = config
795
+ # cache
796
+ @valid_task_re = nil
797
+ end
798
+
799
+ def config(key)
800
+ @config[key]
801
+ end
802
+
803
+ def inspect
804
+ "#<#{self.class} #{__id__()}>"
805
+ end
806
+
807
+ def invoke
808
+ run_metaconfigs
809
+ case task = parsearg_global()
810
+ when nil, 'all'
811
+ parsearg_config
812
+ init_installers
813
+ exec_config
814
+ exec_setup
815
+ exec_install
816
+ else
817
+ case task
818
+ when 'config', 'test'
819
+ ;
820
+ when 'clean', 'distclean'
821
+ @config.load_savefile if File.exist?(@config.savefile)
822
+ else
823
+ @config.load_savefile
824
+ end
825
+ __send__ "parsearg_#{task}"
826
+ init_installers
827
+ __send__ "exec_#{task}"
828
+ end
829
+ end
830
+
831
+ def run_metaconfigs
832
+ @config.load_script "#{@ardir}/metaconfig"
833
+ end
834
+
835
+ def init_installers
836
+ @installer = Installer.new(@config, @ardir, File.expand_path('.'))
837
+ end
838
+
839
+ #
840
+ # Hook Script API bases
841
+ #
842
+
843
+ def srcdir_root
844
+ @ardir
845
+ end
846
+
847
+ def objdir_root
848
+ '.'
849
+ end
850
+
851
+ def relpath
852
+ '.'
853
+ end
854
+
855
+ #
856
+ # Option Parsing
857
+ #
858
+
859
+ def parsearg_global
860
+ while arg = ARGV.shift
861
+ case arg
862
+ when /\A\w+\z/
863
+ setup_rb_error "invalid task: #{arg}" unless valid_task?(arg)
864
+ return arg
865
+ when '-q', '--quiet'
866
+ @config.verbose = false
867
+ when '--verbose'
868
+ @config.verbose = true
869
+ when '--help'
870
+ print_usage $stdout
871
+ exit 0
872
+ when '--version'
873
+ puts "#{File.basename($0)} version #{Version}"
874
+ exit 0
875
+ when '--copyright'
876
+ puts Copyright
877
+ exit 0
878
+ else
879
+ setup_rb_error "unknown global option '#{arg}'"
880
+ end
881
+ end
882
+ nil
883
+ end
884
+
885
+ def valid_task?(t)
886
+ valid_task_re() =~ t
887
+ end
888
+
889
+ def valid_task_re
890
+ @valid_task_re ||= /\A(?:#{TASKS.map {|task,desc| task }.join('|')})\z/
891
+ end
892
+
893
+ def parsearg_no_options
894
+ unless ARGV.empty?
895
+ setup_rb_error "#{task}: unknown options: #{ARGV.join(' ')}"
896
+ end
897
+ end
898
+
899
+ alias parsearg_show parsearg_no_options
900
+ alias parsearg_setup parsearg_no_options
901
+ alias parsearg_test parsearg_no_options
902
+ alias parsearg_clean parsearg_no_options
903
+ alias parsearg_distclean parsearg_no_options
904
+
905
+ def parsearg_config
906
+ evalopt = []
907
+ set = []
908
+ @config.config_opt = []
909
+ while i = ARGV.shift
910
+ if /\A--?\z/ =~ i
911
+ @config.config_opt = ARGV.dup
912
+ break
913
+ end
914
+ name, value = *@config.parse_opt(i)
915
+ if @config.value_config?(name)
916
+ @config[name] = value
917
+ else
918
+ evalopt.push [name, value]
919
+ end
920
+ set.push name
921
+ end
922
+ evalopt.each do |name, value|
923
+ @config.lookup(name).evaluate value, @config
924
+ end
925
+ # Check if configuration is valid
926
+ set.each do |n|
927
+ @config[n] if @config.value_config?(n)
928
+ end
929
+ end
930
+
931
+ def parsearg_install
932
+ @config.no_harm = false
933
+ @config.install_prefix = ''
934
+ while a = ARGV.shift
935
+ case a
936
+ when '--no-harm'
937
+ @config.no_harm = true
938
+ when /\A--prefix=/
939
+ path = a.split(/=/, 2)[1]
940
+ path = File.expand_path(path) unless path[0,1] == '/'
941
+ @config.install_prefix = path
942
+ else
943
+ setup_rb_error "install: unknown option #{a}"
944
+ end
945
+ end
946
+ end
947
+
948
+ def print_usage(out)
949
+ out.puts 'Typical Installation Procedure:'
950
+ out.puts " $ ruby #{File.basename $0} config"
951
+ out.puts " $ ruby #{File.basename $0} setup"
952
+ out.puts " # ruby #{File.basename $0} install (may require root privilege)"
953
+ out.puts
954
+ out.puts 'Detailed Usage:'
955
+ out.puts " ruby #{File.basename $0} <global option>"
956
+ out.puts " ruby #{File.basename $0} [<global options>] <task> [<task options>]"
957
+
958
+ fmt = " %-24s %s\n"
959
+ out.puts
960
+ out.puts 'Global options:'
961
+ out.printf fmt, '-q,--quiet', 'suppress message outputs'
962
+ out.printf fmt, ' --verbose', 'output messages verbosely'
963
+ out.printf fmt, ' --help', 'print this message'
964
+ out.printf fmt, ' --version', 'print version and quit'
965
+ out.printf fmt, ' --copyright', 'print copyright and quit'
966
+ out.puts
967
+ out.puts 'Tasks:'
968
+ TASKS.each do |name, desc|
969
+ out.printf fmt, name, desc
970
+ end
971
+
972
+ fmt = " %-24s %s [%s]\n"
973
+ out.puts
974
+ out.puts 'Options for CONFIG or ALL:'
975
+ @config.each do |item|
976
+ out.printf fmt, item.help_opt, item.description, item.help_default
977
+ end
978
+ out.printf fmt, '--rbconfig=path', 'rbconfig.rb to load',"running ruby's"
979
+ out.puts
980
+ out.puts 'Options for INSTALL:'
981
+ out.printf fmt, '--no-harm', 'only display what to do if given', 'off'
982
+ out.printf fmt, '--prefix=path', 'install path prefix', ''
983
+ out.puts
984
+ end
985
+
986
+ #
987
+ # Task Handlers
988
+ #
989
+
990
+ def exec_config
991
+ @installer.exec_config
992
+ @config.save # must be final
993
+ end
994
+
995
+ def exec_setup
996
+ @installer.exec_setup
997
+ end
998
+
999
+ def exec_install
1000
+ @installer.exec_install
1001
+ end
1002
+
1003
+ def exec_test
1004
+ @installer.exec_test
1005
+ end
1006
+
1007
+ def exec_show
1008
+ @config.each do |i|
1009
+ printf "%-20s %s\n", i.name, i.value if i.value?
1010
+ end
1011
+ end
1012
+
1013
+ def exec_clean
1014
+ @installer.exec_clean
1015
+ end
1016
+
1017
+ def exec_distclean
1018
+ @installer.exec_distclean
1019
+ end
1020
+
1021
+ end # class ToplevelInstaller
1022
+
1023
+
1024
+ class ToplevelInstallerMulti < ToplevelInstaller
1025
+
1026
+ include FileOperations
1027
+
1028
+ def initialize(ardir_root, config)
1029
+ super
1030
+ @packages = directories_of("#{@ardir}/packages")
1031
+ raise 'no package exists' if @packages.empty?
1032
+ @root_installer = Installer.new(@config, @ardir, File.expand_path('.'))
1033
+ end
1034
+
1035
+ def run_metaconfigs
1036
+ @config.load_script "#{@ardir}/metaconfig", self
1037
+ @packages.each do |name|
1038
+ @config.load_script "#{@ardir}/packages/#{name}/metaconfig"
1039
+ end
1040
+ end
1041
+
1042
+ attr_reader :packages
1043
+
1044
+ def packages=(list)
1045
+ raise 'package list is empty' if list.empty?
1046
+ list.each do |name|
1047
+ raise "directory packages/#{name} does not exist"\
1048
+ unless File.dir?("#{@ardir}/packages/#{name}")
1049
+ end
1050
+ @packages = list
1051
+ end
1052
+
1053
+ def init_installers
1054
+ @installers = {}
1055
+ @packages.each do |pack|
1056
+ @installers[pack] = Installer.new(@config,
1057
+ "#{@ardir}/packages/#{pack}",
1058
+ "packages/#{pack}")
1059
+ end
1060
+ with = extract_selection(config('with'))
1061
+ without = extract_selection(config('without'))
1062
+ @selected = @installers.keys.select {|name|
1063
+ (with.empty? or with.include?(name)) \
1064
+ and not without.include?(name)
1065
+ }
1066
+ end
1067
+
1068
+ def extract_selection(list)
1069
+ a = list.split(/,/)
1070
+ a.each do |name|
1071
+ setup_rb_error "no such package: #{name}" unless @installers.key?(name)
1072
+ end
1073
+ a
1074
+ end
1075
+
1076
+ def print_usage(f)
1077
+ super
1078
+ f.puts 'Inluded packages:'
1079
+ f.puts ' ' + @packages.sort.join(' ')
1080
+ f.puts
1081
+ end
1082
+
1083
+ #
1084
+ # Task Handlers
1085
+ #
1086
+
1087
+ def exec_config
1088
+ run_hook 'pre-config'
1089
+ each_selected_installers {|inst| inst.exec_config }
1090
+ run_hook 'post-config'
1091
+ @config.save # must be final
1092
+ end
1093
+
1094
+ def exec_setup
1095
+ run_hook 'pre-setup'
1096
+ each_selected_installers {|inst| inst.exec_setup }
1097
+ run_hook 'post-setup'
1098
+ end
1099
+
1100
+ def exec_install
1101
+ run_hook 'pre-install'
1102
+ each_selected_installers {|inst| inst.exec_install }
1103
+ run_hook 'post-install'
1104
+ end
1105
+
1106
+ def exec_test
1107
+ run_hook 'pre-test'
1108
+ each_selected_installers {|inst| inst.exec_test }
1109
+ run_hook 'post-test'
1110
+ end
1111
+
1112
+ def exec_clean
1113
+ rm_f @config.savefile
1114
+ run_hook 'pre-clean'
1115
+ each_selected_installers {|inst| inst.exec_clean }
1116
+ run_hook 'post-clean'
1117
+ end
1118
+
1119
+ def exec_distclean
1120
+ rm_f @config.savefile
1121
+ run_hook 'pre-distclean'
1122
+ each_selected_installers {|inst| inst.exec_distclean }
1123
+ run_hook 'post-distclean'
1124
+ end
1125
+
1126
+ #
1127
+ # lib
1128
+ #
1129
+
1130
+ def each_selected_installers
1131
+ Dir.mkdir 'packages' unless File.dir?('packages')
1132
+ @selected.each do |pack|
1133
+ $stderr.puts "Processing the package `#{pack}' ..." if verbose?
1134
+ Dir.mkdir "packages/#{pack}" unless File.dir?("packages/#{pack}")
1135
+ Dir.chdir "packages/#{pack}"
1136
+ yield @installers[pack]
1137
+ Dir.chdir '../..'
1138
+ end
1139
+ end
1140
+
1141
+ def run_hook(id)
1142
+ @root_installer.run_hook id
1143
+ end
1144
+
1145
+ # module FileOperations requires this
1146
+ def verbose?
1147
+ @config.verbose?
1148
+ end
1149
+
1150
+ # module FileOperations requires this
1151
+ def no_harm?
1152
+ @config.no_harm?
1153
+ end
1154
+
1155
+ end # class ToplevelInstallerMulti
1156
+
1157
+
1158
+ class Installer
1159
+
1160
+ FILETYPES = %w( bin lib ext data conf man )
1161
+
1162
+ include FileOperations
1163
+ include HookScriptAPI
1164
+
1165
+ def initialize(config, srcroot, objroot)
1166
+ @config = config
1167
+ @srcdir = File.expand_path(srcroot)
1168
+ @objdir = File.expand_path(objroot)
1169
+ @currdir = '.'
1170
+ end
1171
+
1172
+ def inspect
1173
+ "#<#{self.class} #{File.basename(@srcdir)}>"
1174
+ end
1175
+
1176
+ #
1177
+ # Hook Script API base methods
1178
+ #
1179
+
1180
+ def srcdir_root
1181
+ @srcdir
1182
+ end
1183
+
1184
+ def objdir_root
1185
+ @objdir
1186
+ end
1187
+
1188
+ def relpath
1189
+ @currdir
1190
+ end
1191
+
1192
+ #
1193
+ # Config Access
1194
+ #
1195
+
1196
+ # module FileOperations requires this
1197
+ def verbose?
1198
+ @config.verbose?
1199
+ end
1200
+
1201
+ # module FileOperations requires this
1202
+ def no_harm?
1203
+ @config.no_harm?
1204
+ end
1205
+
1206
+ def verbose_off
1207
+ begin
1208
+ save, @config.verbose = @config.verbose?, false
1209
+ yield
1210
+ ensure
1211
+ @config.verbose = save
1212
+ end
1213
+ end
1214
+
1215
+ #
1216
+ # TASK config
1217
+ #
1218
+
1219
+ def exec_config
1220
+ exec_task_traverse 'config'
1221
+ end
1222
+
1223
+ def config_dir_bin(rel)
1224
+ end
1225
+
1226
+ def config_dir_lib(rel)
1227
+ end
1228
+
1229
+ def config_dir_man(rel)
1230
+ end
1231
+
1232
+ def config_dir_ext(rel)
1233
+ extconf if extdir?(curr_srcdir())
1234
+ end
1235
+
1236
+ def extconf
1237
+ ruby "#{curr_srcdir()}/extconf.rb", *@config.config_opt
1238
+ end
1239
+
1240
+ def config_dir_data(rel)
1241
+ end
1242
+
1243
+ def config_dir_conf(rel)
1244
+ end
1245
+
1246
+ #
1247
+ # TASK setup
1248
+ #
1249
+
1250
+ def exec_setup
1251
+ exec_task_traverse 'setup'
1252
+ end
1253
+
1254
+ def setup_dir_bin(rel)
1255
+ files_of(curr_srcdir()).each do |fname|
1256
+ adjust_shebang "#{curr_srcdir()}/#{fname}"
1257
+ end
1258
+ end
1259
+
1260
+ def adjust_shebang(path)
1261
+ return if no_harm?
1262
+ tmpfile = File.basename(path) + '.tmp'
1263
+ begin
1264
+ File.open(path, 'rb') {|r|
1265
+ first = r.gets
1266
+ return unless File.basename(first.sub(/\A\#!/, '').split[0].to_s) == 'ruby'
1267
+ $stderr.puts "adjusting shebang: #{File.basename(path)}" if verbose?
1268
+ File.open(tmpfile, 'wb') {|w|
1269
+ w.print first.sub(/\A\#!\s*\S+/, '#! ' + config('rubypath'))
1270
+ w.write r.read
1271
+ }
1272
+ }
1273
+ move_file tmpfile, File.basename(path)
1274
+ ensure
1275
+ File.unlink tmpfile if File.exist?(tmpfile)
1276
+ end
1277
+ end
1278
+
1279
+ def setup_dir_lib(rel)
1280
+ end
1281
+
1282
+ def setup_dir_man(rel)
1283
+ end
1284
+
1285
+ def setup_dir_ext(rel)
1286
+ make if extdir?(curr_srcdir())
1287
+ end
1288
+
1289
+ def setup_dir_data(rel)
1290
+ end
1291
+
1292
+ def setup_dir_conf(rel)
1293
+ end
1294
+
1295
+ #
1296
+ # TASK install
1297
+ #
1298
+
1299
+ def exec_install
1300
+ rm_f 'InstalledFiles'
1301
+ exec_task_traverse 'install'
1302
+ end
1303
+
1304
+ def install_dir_bin(rel)
1305
+ install_files targetfiles(), "#{config('bindir')}/#{rel}", 0755
1306
+ end
1307
+
1308
+ def install_dir_lib(rel)
1309
+ install_files rubyscripts(), "#{config('rbdir')}/#{rel}", 0644
1310
+ end
1311
+
1312
+ def install_dir_ext(rel)
1313
+ return unless extdir?(curr_srcdir())
1314
+ install_files rubyextentions('.'),
1315
+ "#{config('sodir')}/#{File.dirname(rel)}",
1316
+ 0555
1317
+ end
1318
+
1319
+ def install_dir_data(rel)
1320
+ install_files targetfiles(), "#{config('datadir')}/#{rel}", 0644
1321
+ end
1322
+
1323
+ def install_dir_conf(rel)
1324
+ # FIXME: should not remove current config files
1325
+ # (rename previous file to .old/.org)
1326
+ install_files targetfiles(), "#{config('sysconfdir')}/#{rel}", 0644
1327
+ end
1328
+
1329
+ def install_dir_man(rel)
1330
+ install_files targetfiles(), "#{config('mandir')}/#{rel}", 0644
1331
+ end
1332
+
1333
+ def install_files(list, dest, mode)
1334
+ mkdir_p dest, @config.install_prefix
1335
+ list.each do |fname|
1336
+ install fname, dest, mode, @config.install_prefix
1337
+ end
1338
+ end
1339
+
1340
+ def rubyscripts
1341
+ glob_select(@config.libsrc_pattern, targetfiles())
1342
+ end
1343
+
1344
+ def rubyextentions(dir)
1345
+ ents = glob_select("*.#{@config.dllext}", targetfiles())
1346
+ if ents.empty?
1347
+ setup_rb_error "no ruby extention exists: 'ruby #{$0} setup' first"
1348
+ end
1349
+ ents
1350
+ end
1351
+
1352
+ def targetfiles
1353
+ mapdir(existfiles() - hookfiles())
1354
+ end
1355
+
1356
+ def mapdir(ents)
1357
+ ents.map {|ent|
1358
+ if File.exist?(ent)
1359
+ then ent # objdir
1360
+ else "#{curr_srcdir()}/#{ent}" # srcdir
1361
+ end
1362
+ }
1363
+ end
1364
+
1365
+ # picked up many entries from cvs-1.11.1/src/ignore.c
1366
+ JUNK_FILES = %w(
1367
+ core RCSLOG tags TAGS .make.state
1368
+ .nse_depinfo #* .#* cvslog.* ,* .del-* *.olb
1369
+ *~ *.old *.bak *.BAK *.orig *.rej _$* *$
1370
+
1371
+ *.org *.in .*
1372
+ )
1373
+
1374
+ def existfiles
1375
+ glob_reject(JUNK_FILES, (files_of(curr_srcdir()) | files_of('.')))
1376
+ end
1377
+
1378
+ def hookfiles
1379
+ %w( pre-%s post-%s pre-%s.rb post-%s.rb ).map {|fmt|
1380
+ %w( config setup install clean ).map {|t| sprintf(fmt, t) }
1381
+ }.flatten
1382
+ end
1383
+
1384
+ def glob_select(pat, ents)
1385
+ re = globs2re([pat])
1386
+ ents.select {|ent| re =~ ent }
1387
+ end
1388
+
1389
+ def glob_reject(pats, ents)
1390
+ re = globs2re(pats)
1391
+ ents.reject {|ent| re =~ ent }
1392
+ end
1393
+
1394
+ GLOB2REGEX = {
1395
+ '.' => '\.',
1396
+ '$' => '\$',
1397
+ '#' => '\#',
1398
+ '*' => '.*'
1399
+ }
1400
+
1401
+ def globs2re(pats)
1402
+ /\A(?:#{
1403
+ pats.map {|pat| pat.gsub(/[\.\$\#\*]/) {|ch| GLOB2REGEX[ch] } }.join('|')
1404
+ })\z/
1405
+ end
1406
+
1407
+ #
1408
+ # TASK test
1409
+ #
1410
+
1411
+ TESTDIR = 'test'
1412
+
1413
+ def exec_test
1414
+ unless File.directory?('test')
1415
+ $stderr.puts 'no test in this package' if verbose?
1416
+ return
1417
+ end
1418
+ $stderr.puts 'Running tests...' if verbose?
1419
+ require 'test/unit'
1420
+ runner = Test::Unit::AutoRunner.new(true)
1421
+ runner.to_run << TESTDIR
1422
+ runner.run
1423
+ end
1424
+
1425
+ #
1426
+ # TASK clean
1427
+ #
1428
+
1429
+ def exec_clean
1430
+ exec_task_traverse 'clean'
1431
+ rm_f @config.savefile
1432
+ rm_f 'InstalledFiles'
1433
+ end
1434
+
1435
+ def clean_dir_bin(rel)
1436
+ end
1437
+
1438
+ def clean_dir_lib(rel)
1439
+ end
1440
+
1441
+ def clean_dir_ext(rel)
1442
+ return unless extdir?(curr_srcdir())
1443
+ make 'clean' if File.file?('Makefile')
1444
+ end
1445
+
1446
+ def clean_dir_data(rel)
1447
+ end
1448
+
1449
+ def clean_dir_conf(rel)
1450
+ end
1451
+
1452
+ #
1453
+ # TASK distclean
1454
+ #
1455
+
1456
+ def exec_distclean
1457
+ exec_task_traverse 'distclean'
1458
+ rm_f @config.savefile
1459
+ rm_f 'InstalledFiles'
1460
+ end
1461
+
1462
+ def distclean_dir_bin(rel)
1463
+ end
1464
+
1465
+ def distclean_dir_lib(rel)
1466
+ end
1467
+
1468
+ def distclean_dir_ext(rel)
1469
+ return unless extdir?(curr_srcdir())
1470
+ make 'distclean' if File.file?('Makefile')
1471
+ end
1472
+
1473
+ def distclean_dir_data(rel)
1474
+ end
1475
+
1476
+ def distclean_dir_conf(rel)
1477
+ end
1478
+
1479
+ #
1480
+ # lib
1481
+ #
1482
+
1483
+ def exec_task_traverse(task)
1484
+ run_hook "pre-#{task}"
1485
+ FILETYPES.each do |type|
1486
+ if config('without-ext') == 'yes' and type == 'ext'
1487
+ $stderr.puts 'skipping ext/* by user option' if verbose?
1488
+ next
1489
+ end
1490
+ traverse task, type, "#{task}_dir_#{type}"
1491
+ end
1492
+ run_hook "post-#{task}"
1493
+ end
1494
+
1495
+ def traverse(task, rel, mid)
1496
+ dive_into(rel) {
1497
+ run_hook "pre-#{task}"
1498
+ __send__ mid, rel.sub(%r[\A.*?(?:/|\z)], '')
1499
+ directories_of(curr_srcdir()).each do |d|
1500
+ traverse task, "#{rel}/#{d}", mid
1501
+ end
1502
+ run_hook "post-#{task}"
1503
+ }
1504
+ end
1505
+
1506
+ def dive_into(rel)
1507
+ return unless File.dir?("#{@srcdir}/#{rel}")
1508
+
1509
+ dir = File.basename(rel)
1510
+ Dir.mkdir dir unless File.dir?(dir)
1511
+ prevdir = Dir.pwd
1512
+ Dir.chdir dir
1513
+ $stderr.puts '---> ' + rel if verbose?
1514
+ @currdir = rel
1515
+ yield
1516
+ Dir.chdir prevdir
1517
+ $stderr.puts '<--- ' + rel if verbose?
1518
+ @currdir = File.dirname(rel)
1519
+ end
1520
+
1521
+ def run_hook(id)
1522
+ path = [ "#{curr_srcdir()}/#{id}",
1523
+ "#{curr_srcdir()}/#{id}.rb" ].detect {|cand| File.file?(cand) }
1524
+ return unless path
1525
+ begin
1526
+ instance_eval File.read(path), path, 1
1527
+ rescue
1528
+ raise if $DEBUG
1529
+ setup_rb_error "hook #{path} failed:\n" + $!.message
1530
+ end
1531
+ end
1532
+
1533
+ end # class Installer
1534
+
1535
+
1536
+ class SetupError < StandardError; end
1537
+
1538
+ def setup_rb_error(msg)
1539
+ raise SetupError, msg
1540
+ end
1541
+
1542
+ if $0 == __FILE__
1543
+ begin
1544
+ ToplevelInstaller.invoke
1545
+ rescue SetupError
1546
+ raise if $DEBUG
1547
+ $stderr.puts $!.message
1548
+ $stderr.puts "Try 'ruby #{$0} --help' for detailed usage."
1549
+ exit 1
1550
+ end
1551
+ end