tpkg 2.3.3 → 2.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (205) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE +20 -0
  5. data/Portfile +39 -0
  6. data/Portfile.template +39 -0
  7. data/README.md +43 -0
  8. data/Rakefile +468 -18
  9. data/bin/gem2tpkg +2 -2
  10. data/bin/tpkg +18 -13
  11. data/bin/tpkg_uploader +132 -0
  12. data/ca.pem +1 -0
  13. data/control +7 -0
  14. data/depend +3 -0
  15. data/externals-etch/authorized_keys +40 -0
  16. data/externals-etch/group +9 -0
  17. data/externals-etch/iptables +38 -0
  18. data/externals-etch/limits +30 -0
  19. data/externals-etch/nfs +30 -0
  20. data/externals-etch/sudo +30 -0
  21. data/externals-etch/supplemental_groups +8 -0
  22. data/externals-etch/sysctl +30 -0
  23. data/externals-etch/user +41 -0
  24. data/externals/group +39 -0
  25. data/externals/supplemental_groups +48 -0
  26. data/externals/user +39 -0
  27. data/lib/tpkg.rb +260 -991
  28. data/lib/tpkg/os.rb +164 -0
  29. data/lib/tpkg/os/debian.rb +159 -0
  30. data/lib/tpkg/os/freebsd.rb +113 -0
  31. data/lib/tpkg/os/macosx.rb +113 -0
  32. data/lib/tpkg/os/redhat.rb +173 -0
  33. data/lib/tpkg/os/solaris.rb +101 -0
  34. data/lib/tpkg/os/windows.rb +26 -0
  35. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify.rb +67 -0
  36. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/errors.rb +127 -0
  37. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/kwalify.schema.yaml +58 -0
  38. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/main.rb +442 -0
  39. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/messages.rb +173 -0
  40. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/meta-validator.rb +275 -0
  41. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/parser/base.rb +127 -0
  42. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/parser/yaml.rb +841 -0
  43. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/rule.rb +559 -0
  44. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/templates/genclass-java.eruby +222 -0
  45. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/templates/genclass-php.eruby +104 -0
  46. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/templates/genclass-ruby.eruby +113 -0
  47. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/types.rb +156 -0
  48. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util.rb +158 -0
  49. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util/assert-text-equal.rb +46 -0
  50. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util/hash-interface.rb +18 -0
  51. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util/hashlike.rb +51 -0
  52. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util/option-parser.rb +220 -0
  53. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util/ordered-hash.rb +57 -0
  54. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util/testcase-helper.rb +112 -0
  55. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/validator.rb +282 -0
  56. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/yaml-parser.rb +870 -0
  57. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh.rb +219 -0
  58. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/agent.rb +179 -0
  59. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/constants.rb +18 -0
  60. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/key_manager.rb +219 -0
  61. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/methods/abstract.rb +60 -0
  62. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/methods/hostbased.rb +71 -0
  63. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/methods/keyboard_interactive.rb +66 -0
  64. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/methods/password.rb +39 -0
  65. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/methods/publickey.rb +92 -0
  66. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/pageant.rb +183 -0
  67. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/session.rb +134 -0
  68. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/buffer.rb +340 -0
  69. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/buffered_io.rb +198 -0
  70. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/config.rb +205 -0
  71. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/connection/channel.rb +630 -0
  72. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/connection/constants.rb +33 -0
  73. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/connection/session.rb +597 -0
  74. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/connection/term.rb +178 -0
  75. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/errors.rb +85 -0
  76. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/key_factory.rb +102 -0
  77. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/known_hosts.rb +129 -0
  78. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/loggable.rb +61 -0
  79. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/packet.rb +102 -0
  80. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/prompt.rb +93 -0
  81. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/proxy/command.rb +75 -0
  82. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/proxy/errors.rb +14 -0
  83. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/proxy/http.rb +94 -0
  84. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/proxy/socks4.rb +70 -0
  85. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/proxy/socks5.rb +142 -0
  86. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/ruby_compat.rb +43 -0
  87. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/service/forward.rb +288 -0
  88. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test.rb +89 -0
  89. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test/channel.rb +129 -0
  90. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test/extensions.rb +152 -0
  91. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test/kex.rb +44 -0
  92. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test/local_packet.rb +51 -0
  93. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test/packet.rb +81 -0
  94. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test/remote_packet.rb +38 -0
  95. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test/script.rb +157 -0
  96. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test/socket.rb +64 -0
  97. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/algorithms.rb +384 -0
  98. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/cipher_factory.rb +97 -0
  99. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/constants.rb +30 -0
  100. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/hmac.rb +31 -0
  101. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/hmac/abstract.rb +79 -0
  102. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/hmac/md5.rb +12 -0
  103. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/hmac/md5_96.rb +11 -0
  104. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/hmac/none.rb +15 -0
  105. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/hmac/sha1.rb +13 -0
  106. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/hmac/sha1_96.rb +11 -0
  107. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/identity_cipher.rb +55 -0
  108. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/kex.rb +13 -0
  109. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb +208 -0
  110. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +77 -0
  111. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/openssl.rb +127 -0
  112. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/packet_stream.rb +235 -0
  113. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/server_version.rb +71 -0
  114. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/session.rb +276 -0
  115. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/state.rb +206 -0
  116. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/verifiers/lenient.rb +30 -0
  117. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/verifiers/null.rb +12 -0
  118. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/verifiers/strict.rb +53 -0
  119. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/version.rb +62 -0
  120. data/lib/tpkg/version.rb +3 -0
  121. data/man/man1/cpan2tpkg.1 +82 -0
  122. data/man/man1/gem2tpkg.1 +120 -0
  123. data/man/man1/tpkg.1 +411 -0
  124. data/pkginfo +8 -0
  125. data/postinstall.solaris +11 -0
  126. data/postremove.solaris +16 -0
  127. data/schema/schema-1.0.5.yml +0 -0
  128. data/schema/schema-1.0.6.yml +0 -0
  129. data/schema/schema-1.0.7.yml +0 -0
  130. data/schema/schema-1.0.8.yml +0 -0
  131. data/schema/schema-1.0.9.yml +0 -0
  132. data/schema/schema.yml +0 -0
  133. data/schema/tpkg-1.0.0.dtd +0 -0
  134. data/schema/tpkg-1.0.1.dtd +0 -0
  135. data/schema/tpkg-1.0.2.dtd +0 -0
  136. data/schema/tpkg-1.0.3.dtd +0 -0
  137. data/schema/tpkg-1.0.4.dtd +0 -0
  138. data/schema/tpkg-1.0.5.dtd +0 -0
  139. data/schema/tpkg-1.0.6.dtd +0 -0
  140. data/schema/tpkg-1.0.7.dtd +0 -0
  141. data/schema/tpkg-1.0.8.dtd +0 -0
  142. data/schema/tpkg-1.0.9.dtd +0 -0
  143. data/schema/tpkg.dtd +0 -0
  144. data/test/TODO +30 -0
  145. data/test/premadetestpkg/pkg_without_file_metadata-1.0-1.tpkg +0 -0
  146. data/test/test_checksum.rb +53 -0
  147. data/test/test_compress.rb +55 -0
  148. data/test/test_conflict.rb +41 -0
  149. data/test/test_crontabs.rb +398 -0
  150. data/test/test_dependency.rb +1113 -0
  151. data/test/test_downgrade.rb +80 -0
  152. data/test/test_download.rb +95 -0
  153. data/test/test_encrypt.rb +136 -0
  154. data/test/test_filemetadata.rb +131 -0
  155. data/test/test_initscript.rb +93 -0
  156. data/test/test_install.rb +186 -0
  157. data/test/test_lock.rb +82 -0
  158. data/test/test_make.rb +410 -0
  159. data/test/test_metadata.rb +805 -0
  160. data/test/test_misc.rb +379 -0
  161. data/test/test_options.rb +1711 -0
  162. data/test/test_os.rb +193 -0
  163. data/test/test_os_debian.rb +99 -0
  164. data/test/test_os_freebsd.rb +89 -0
  165. data/test/test_os_macosx.rb +79 -0
  166. data/test/test_os_redhat.rb +124 -0
  167. data/test/test_os_solaris.rb +85 -0
  168. data/test/test_os_windows.rb +26 -0
  169. data/test/test_query.rb +134 -0
  170. data/test/test_remove.rb +539 -0
  171. data/test/test_tar.rb +99 -0
  172. data/test/test_unpack.rb +977 -0
  173. data/test/test_upgrade.rb +398 -0
  174. data/test/test_version.rb +54 -0
  175. data/test/testcmds/crontab +14 -0
  176. data/test/testcmds/debian/apt-cache +145 -0
  177. data/test/testcmds/debian/dpkg-query +16 -0
  178. data/test/testcmds/freebsd/pkg_info +13 -0
  179. data/test/testcmds/macosx/port +35 -0
  180. data/test/testcmds/redhat/rpmbuild +6 -0
  181. data/test/testcmds/redhat/yum +90 -0
  182. data/test/testcmds/solaris/pkginfo +25 -0
  183. data/test/testcmds/solaris/pkgutil +36 -0
  184. data/test/testpkg/reloc/encfile +2 -0
  185. data/test/testpkg/reloc/file +2 -0
  186. data/test/testpkg/reloc/precryptfile +1 -0
  187. data/test/testpkg/reloc/precryptfile.plaintext +3 -0
  188. data/test/testpkg/tpkg-bad-ownergroup.xml +25 -0
  189. data/test/testpkg/tpkg-bad-ownergroup.yml +18 -0
  190. data/test/testpkg/tpkg-default-perms.xml +28 -0
  191. data/test/testpkg/tpkg-default-perms.yml +20 -0
  192. data/test/testpkg/tpkg-good-ownergroup.xml +25 -0
  193. data/test/testpkg/tpkg-good-ownergroup.yml +18 -0
  194. data/test/testpkg/tpkg-nativedeps.yml +13 -0
  195. data/test/testpkg/tpkg-nofiles.xml +14 -0
  196. data/test/testpkg/tpkg-nofiles.yml +9 -0
  197. data/test/testpkg/tpkg.xml +35 -0
  198. data/test/testpkg/tpkg.yml +25 -0
  199. data/test/tpkgtest.rb +300 -0
  200. data/tpkg.conf +16 -0
  201. data/tpkg.gemspec +24 -0
  202. data/tpkg.spec +28 -0
  203. data/tpkg.xml +17 -0
  204. data/tpkg_profile.sh +32 -0
  205. metadata +306 -31
@@ -0,0 +1,559 @@
1
+ ###
2
+ ### $Rev$
3
+ ### $Release: 0.7.2 $
4
+ ### copyright(c) 2005-2010 kuwata-lab all rights reserved.
5
+ ###
6
+
7
+ require 'kwalify/messages'
8
+ require 'kwalify/errors'
9
+ require 'kwalify/types'
10
+
11
+
12
+ module Kwalify
13
+
14
+
15
+ class Rule
16
+ include Kwalify::ErrorHelper
17
+
18
+ attr_accessor :parent
19
+ attr_reader :name
20
+ attr_reader :desc
21
+ attr_reader :enum
22
+ attr_reader :required
23
+ attr_reader :type
24
+ attr_reader :type_class
25
+ attr_reader :pattern
26
+ attr_reader :regexp
27
+ attr_reader :sequence
28
+ attr_reader :mapping
29
+ attr_reader :assert
30
+ attr_reader :assert_proc
31
+ attr_reader :range
32
+ attr_reader :length
33
+ attr_reader :ident
34
+ attr_reader :unique
35
+ attr_reader :default
36
+ attr_reader :classname
37
+ attr_reader :classobj
38
+
39
+
40
+ def initialize(hash=nil, parent=nil)
41
+ _init(hash, "", {}) if hash
42
+ @parent = parent
43
+ end
44
+
45
+
46
+ def _init(hash, path="", rule_table={})
47
+ unless hash.is_a?(Hash)
48
+ #* key=:schema_notmap msg="schema definition is not a mapping."
49
+ raise Kwalify.schema_error(:schema_notmap, nil, (!path || path.empty? ? "/" : path), nil)
50
+ end
51
+ rule = self
52
+ rule_table[hash.__id__] = rule
53
+ ## 'type:' entry
54
+ curr_path = "#{path}/type"
55
+ _init_type_value(hash['type'], rule, curr_path)
56
+ ## other entries
57
+ hash.each do |key, val|
58
+ curr_path = "#{path}/#{key}"
59
+ sym = key.intern
60
+ method = get_init_method(sym)
61
+ unless method
62
+ #* key=:key_unknown msg="unknown key."
63
+ raise schema_error(:key_unknown, rule, curr_path, "#{key}:")
64
+ end
65
+ if sym == :sequence || sym == :mapping
66
+ __send__(method, val, rule, curr_path, rule_table)
67
+ else
68
+ __send__(method, val, rule, curr_path)
69
+ end
70
+ end
71
+ _check_confliction(hash, rule, path)
72
+ return self
73
+ end
74
+
75
+
76
+ keys = %w[type name desc required pattern enum assert range length
77
+ ident unique default sequence mapping class]
78
+ #table = keys.inject({}) {|h, k| h[k.intern] = "_init_#{k}_value".intern; h }
79
+ table = {}; keys.each {|k| table[k.intern] = "_init_#{k}_value".intern }
80
+ @@dispatch_table = table
81
+
82
+
83
+ protected
84
+
85
+
86
+ def get_init_method(sym)
87
+ @_dispatch_table ||= @@dispatch_table
88
+ return @_dispatch_table[sym]
89
+ end
90
+
91
+
92
+ private
93
+
94
+
95
+ def _init_type_value(val, rule, path)
96
+ @type = val
97
+ @type = Types::DEFAULT_TYPE if @type.nil?
98
+ unless @type.is_a?(String)
99
+ #* key=:type_notstr msg="not a string."
100
+ raise schema_error(:type_notstr, rule, path, @type.to_s)
101
+ end
102
+ @type_class = Types.type_class(@type)
103
+ #if @type_class.nil?
104
+ # begin
105
+ # @type_class = Kernel.const_get(@type)
106
+ # rescue NameError
107
+ # end
108
+ #end
109
+ unless @type_class
110
+ #* key=:type_unknown msg="unknown type."
111
+ raise schema_error(:type_unknown, rule, path, @type.to_s)
112
+ end
113
+ end
114
+
115
+
116
+ def _init_class_value(val, rule, path)
117
+ @classname = val
118
+ unless @type == 'map'
119
+ #* key=:class_notmap msg="available only with map type."
120
+ raise schema_error(:class_notmap, rule, path, 'class:')
121
+ end
122
+ begin
123
+ @classobj = Util.get_class(val)
124
+ rescue NameError
125
+ @classobj = nil
126
+ end
127
+ end
128
+
129
+
130
+ def _init_name_value(val, rule, path)
131
+ @name = val
132
+ end
133
+
134
+
135
+ def _init_desc_value(val, rule, path)
136
+ @desc = val
137
+ end
138
+
139
+
140
+ def _init_required_value(val, rule, path)
141
+ @required = val
142
+ unless val.is_a?(Boolean) #|| val.nil?
143
+ #* key=:required_notbool msg="not a boolean."
144
+ raise schema_error(:required_notbool, rule, path, val)
145
+ end
146
+ end
147
+
148
+
149
+ def _init_pattern_value(val, rule, path)
150
+ @pattern = val
151
+ unless val.is_a?(String) || val.is_a?(Regexp)
152
+ #* key=:pattern_notstr msg="not a string (or regexp)"
153
+ raise schema_error(:pattern_notstr, rule, path, val)
154
+ end
155
+ unless val =~ /\A\/(.*)\/([mi]?[mi]?)\z/
156
+ #* key=:pattern_notmatch msg="should be '/..../'."
157
+ raise schema_error(:pattern_notmatch, rule, path, val)
158
+ end
159
+ pat = $1; opt = $2
160
+ flag = 0
161
+ flag += Regexp::IGNORECASE if opt.include?("i")
162
+ flag += Regexp::MULTILINE if opt.include?("m")
163
+ begin
164
+ @regexp = Regexp.compile(pat, flag)
165
+ rescue RegexpError => ex
166
+ #* key=:pattern_syntaxerr msg="has regexp error."
167
+ raise schema_error(:pattern_syntaxerr, rule, path, val)
168
+ end
169
+ end
170
+
171
+
172
+ def _init_enum_value(val, rule, path)
173
+ @enum = val
174
+ unless val.is_a?(Array)
175
+ #* key=:enum_notseq msg="not a sequence."
176
+ raise schema_error(:enum_notseq, rule, path, val)
177
+ end
178
+ if Types.collection_type?(@type) # unless Kwalify.scalar_class?(@type_class)
179
+ #* key=:enum_notscalar msg="not available with seq or map."
180
+ raise schema_error(:enum_notscalar, rule, File.dirname(path), 'enum:')
181
+ end
182
+ elem_table = {}
183
+ @enum.each do |elem|
184
+ unless elem.is_a?(@type_class)
185
+ #* key=:enum_type_unmatch msg="%s type expected."
186
+ raise schema_error(:enum_type_unmatch, rule, path, elem, [Kwalify.word(@type)])
187
+ end
188
+ if elem_table[elem]
189
+ #* key=:enum_duplicate msg="duplicated enum value."
190
+ raise schema_error(:enum_duplicate, rule, path, elem.to_s)
191
+ end
192
+ elem_table[elem] = true
193
+ end
194
+ end
195
+
196
+
197
+ def _init_assert_value(val, rule, path)
198
+ @assert = val
199
+ unless val.is_a?(String)
200
+ #* key=:assert_notstr msg="not a string."
201
+ raise schema_error(:assert_notstr, rule, path, val)
202
+ end
203
+ unless val =~ /\bval\b/
204
+ #* key=:assert_noval msg="'val' is not used."
205
+ raise schema_error(:assert_noval, rule, path, val)
206
+ end
207
+ begin
208
+ @assert_proc = eval "proc { |val| #{val} }"
209
+ rescue ::SyntaxError => ex
210
+ #* key=:assert_syntaxerr msg="expression syntax error."
211
+ raise schema_error(:assert_syntaxerr, rule, path, val)
212
+ end
213
+ end
214
+
215
+
216
+ def _init_range_value(val, rule, path)
217
+ @range = val
218
+ unless val.is_a?(Hash)
219
+ #* key=:range_notmap msg="not a mapping."
220
+ raise schema_error(:range_notmap, rule, path, val)
221
+ end
222
+ if Types.collection_type?(@type) || @type == 'bool'
223
+ #* key=:range_notscalar msg="is available only with scalar type."
224
+ raise schema_error(:range_notscalar, rule, File.dirname(path), 'range:')
225
+ end
226
+ val.each do |k, v|
227
+ case k
228
+ when 'max', 'min', 'max-ex', 'min-ex'
229
+ unless v.is_a?(@type_class)
230
+ typename = Kwalify.word(@type) || @type
231
+ #* key=:range_type_unmatch msg="not a %s."
232
+ raise schema_error(:range_type_unmatch, rule, "#{path}/#{k}", v, [typename])
233
+ end
234
+ else
235
+ #* key=:range_undefined msg="undefined key."
236
+ raise schema_error(:range_undefined, rule, "#{path}/#{k}", "#{k}:")
237
+ end
238
+ end
239
+ if val.key?('max') && val.key?('max-ex')
240
+ #* key=:range_twomax msg="both 'max' and 'max-ex' are not available at once."
241
+ raise schema_error(:range_twomax, rule, path, nil)
242
+ end
243
+ if val.key?('min') && val.key?('min-ex')
244
+ #* key=:range_twomin msg="both 'min' and 'min-ex' are not available at once."
245
+ raise schema_error(:range_twomin, rule, path, nil)
246
+ end
247
+ max, min, max_ex, min_ex = val['max'], val['min'], val['max-ex'], val['min-ex']
248
+ if max
249
+ if min && max < min
250
+ #* key=:range_maxltmin msg="max '%s' is less than min '%s'."
251
+ raise validate_error(:range_maxltmin, rule, path, nil, [max, min])
252
+ elsif min_ex && max <= min_ex
253
+ #* key=:range_maxleminex msg="max '%s' is less than or equal to min-ex '%s'."
254
+ raise validate_error(:range_maxleminex, rule, path, nil, [max, min_ex])
255
+ end
256
+ elsif max_ex
257
+ if min && max_ex <= min
258
+ #* key=:range_maxexlemin msg="max-ex '%s' is less than or equal to min '%s'."
259
+ raise validate_error(:range_maxexlemin, rule, path, nil, [max_ex, min])
260
+ elsif min_ex && max_ex <= min_ex
261
+ #* key=:range_maxexleminex msg="max-ex '%s' is less than or equal to min-ex '%s'."
262
+ raise validate_error(:range_maxexleminex, rule, path, nil, [max_ex, min_ex])
263
+ end
264
+ end
265
+ end
266
+
267
+
268
+ def _init_length_value(val, rule, path)
269
+ @length = val
270
+ unless val.is_a?(Hash)
271
+ #* key=:length_notmap msg="not a mapping."
272
+ raise schema_error(:length_notmap, rule, path, val)
273
+ end
274
+ unless @type == 'str' || @type == 'text'
275
+ #* key=:length_nottext msg="is available only with string or text."
276
+ raise schema_error(:length_nottext, rule, File.dirname(path), 'length:')
277
+ end
278
+ val.each do |k, v|
279
+ case k
280
+ when 'max', 'min', 'max-ex', 'min-ex'
281
+ unless v.is_a?(Integer)
282
+ #* key=:length_notint msg="not an integer."
283
+ raise schema_error(:length_notint, rule, "#{path}/#{k}", v)
284
+ end
285
+ else
286
+ #* key=:length_undefined msg="undefined key."
287
+ raise schema_error(:length_undefined, rule, "#{path}/#{k}", "#{k}:")
288
+ end
289
+ end
290
+ if val.key?('max') && val.key?('max-ex')
291
+ #* key=:length_twomax msg="both 'max' and 'max-ex' are not available at once."
292
+ raise schema_error(:length_twomax, rule, path, nil)
293
+ end
294
+ if val.key?('min') && val.key?('min-ex')
295
+ #* key=:length_twomin msg="both 'min' and 'min-ex' are not available at once."
296
+ raise schema_error(:length_twomin, rule, path, nil)
297
+ end
298
+ max, min, max_ex, min_ex = val['max'], val['min'], val['max-ex'], val['min-ex']
299
+ if max
300
+ if min && max < min
301
+ #* key=:length_maxltmin msg="max '%s' is less than min '%s'."
302
+ raise validate_error(:length_maxltmin, rule, path, nil, [max, min])
303
+ elsif min_ex && max <= min_ex
304
+ #* key=:length_maxleminex msg="max '%s' is less than or equal to min-ex '%s'."
305
+ raise validate_error(:length_maxleminex, rule, path, nil, [max, min_ex])
306
+ end
307
+ elsif max_ex
308
+ if min && max_ex <= min
309
+ #* key=:length_maxexlemin msg="max-ex '%s' is less than or equal to min '%s'."
310
+ raise validate_error(:length_maxexlemin, rule, path, nil, [max_ex, min])
311
+ elsif min_ex && max_ex <= min_ex
312
+ #* key=:length_maxexleminex msg="max-ex '%s' is less than or equal to min-ex '%s'."
313
+ raise validate_error(:length_maxexleminex, rule, path, nil, [max_ex, min_ex])
314
+ end
315
+ end
316
+ end
317
+
318
+
319
+ def _init_ident_value(val, rule, path)
320
+ @ident = val
321
+ @required = true
322
+ unless val.is_a?(Boolean)
323
+ #* key=:ident_notbool msg="not a boolean."
324
+ raise schema_error(:ident_notbool, rule, path, val)
325
+ end
326
+ if @type == 'map' || @type == 'seq'
327
+ #* key=:ident_notscalar msg="is available only with a scalar type."
328
+ raise schema_error(:ident_notscalar, rule, File.dirname(path), "ident:")
329
+ end
330
+ if File.dirname(path) == "/"
331
+ #* key=:ident_onroot msg="is not available on root element."
332
+ raise schema_error(:ident_onroot, rule, "/", "ident:")
333
+ end
334
+ unless @parent && @parent.type == 'map'
335
+ #* key=:ident_notmap msg="is available only with an element of mapping."
336
+ raise schema_error(:ident_notmap, rule, File.dirname(path), "ident:")
337
+ end
338
+ end
339
+
340
+
341
+ def _init_unique_value(val, rule, path)
342
+ @unique = val
343
+ unless val.is_a?(Boolean)
344
+ #* key=:unique_notbool msg="not a boolean."
345
+ raise schema_error(:unique_notbool, rule, path, val)
346
+ end
347
+ if @type == 'map' || @type == 'seq'
348
+ #* key=:unique_notscalar msg="is available only with a scalar type."
349
+ raise schema_error(:unique_notscalar, rule, File.dirname(path), "unique:")
350
+ end
351
+ if File.dirname(path) == "/"
352
+ #* key=:unique_onroot msg="is not available on root element."
353
+ raise schema_error(:unique_onroot, rule, "/", "unique:")
354
+ end
355
+ end
356
+
357
+
358
+ def _init_default_value(val, rule, path)
359
+ @default = val
360
+ unless Types.scalar?(val)
361
+ #* key=:default_nonscalarval msg="not a scalar."
362
+ raise schema_error(:default_nonscalarval, rule, path, val)
363
+ end
364
+ if @type == 'map' || @type == 'seq'
365
+ #* key=:default_notscalar msg="is available only with a scalar type."
366
+ raise schema_error(:default_notscalar, rule, File.dirname(path), "default:")
367
+ end
368
+ unless val.nil? || val.is_a?(@type_class)
369
+ #* key=:default_unmatch msg="not a %s."
370
+ raise schema_error(:default_unmatch, rule, path, val, [Kwalify.word(@type)])
371
+ end
372
+ end
373
+
374
+
375
+ def _init_sequence_value(val, rule, path, rule_table)
376
+ if !val.nil? && !val.is_a?(Array)
377
+ #* key=:sequence_notseq msg="not a sequence."
378
+ raise schema_error(:sequence_notseq, rule, path, val)
379
+ elsif val.nil? || val.empty?
380
+ #* key=:sequence_noelem msg="required one element."
381
+ raise schema_error(:sequence_noelem, rule, path, val)
382
+ elsif val.length > 1
383
+ #* key=:sequence_toomany msg="required just one element."
384
+ raise schema_error(:sequence_toomany, rule, path, val)
385
+ else
386
+ elem = val[0]
387
+ elem ||= {}
388
+ i = 0 # or 1? *index*
389
+ rule = rule_table[elem.__id__]
390
+ rule ||= Rule.new(nil, self)._init(elem, "#{path}/#{i}", rule_table)
391
+ @sequence = [ rule ]
392
+ end
393
+ end
394
+
395
+
396
+ def _init_mapping_value(val, rule, path, rule_table)
397
+ if !val.nil? && !val.is_a?(Hash)
398
+ #* key=:mapping_notmap msg="not a mapping."
399
+ raise schema_error(:mapping_notmap, rule, path, val)
400
+ elsif val.nil? || (val.empty? && !val.default)
401
+ #* key=:mapping_noelem msg="required at least one element."
402
+ raise schema_error(:mapping_noelem, rule, path, val)
403
+ else
404
+ @mapping = {}
405
+ if val.default
406
+ elem = val.default # hash
407
+ rule = rule_table[elem.__id__]
408
+ rule ||= Rule.new(nil, self)._init(elem, "#{path}/=", rule_table)
409
+ @mapping.default = rule
410
+ end
411
+ val.each do |k, v|
412
+ ##* key=:key_duplicate msg="key duplicated."
413
+ #raise schema_error(:key_duplicate, rule, path, key) if @mapping.key?(key)
414
+ v ||= {}
415
+ rule = rule_table[v.__id__]
416
+ rule ||= Rule.new(nil, self)._init(v, "#{path}/#{k}", rule_table)
417
+ if k == '='
418
+ @mapping.default = rule
419
+ else
420
+ @mapping[k] = rule
421
+ end
422
+ end if val
423
+ end
424
+ end
425
+
426
+
427
+ def _check_confliction(hash, rule, path)
428
+ if @type == 'seq'
429
+ #* key=:seq_nosequence msg="type 'seq' requires 'sequence:'."
430
+ raise schema_error(:seq_nosequence, rule, path, nil) unless hash.key?('sequence')
431
+ #* key=:seq_conflict msg="not available with sequence."
432
+ raise schema_error(:seq_conflict, rule, path, 'enum:') if @enum
433
+ raise schema_error(:seq_conflict, rule, path, 'pattern:') if @pattern
434
+ raise schema_error(:seq_conflict, rule, path, 'mapping:') if @mapping
435
+ raise schema_error(:seq_conflict, rule, path, 'range:') if @range
436
+ raise schema_error(:seq_conflict, rule, path, 'length:') if @length
437
+ elsif @type == 'map'
438
+ #* key=:map_nomapping msg="type 'map' requires 'mapping:'."
439
+ raise schema_error(:map_nomapping, rule, path, nil) unless hash.key?('mapping')
440
+ #* key=:map_conflict msg="not available with mapping."
441
+ raise schema_error(:map_conflict, rule, path, 'enum:') if @enum
442
+ raise schema_error(:map_conflict, rule, path, 'pattern:') if @pattern
443
+ raise schema_error(:map_conflict, rule, path, 'sequence:') if @sequence
444
+ raise schema_error(:map_conflict, rule, path, 'range:') if @range
445
+ raise schema_error(:map_conflict, rule, path, 'length:') if @length
446
+ else
447
+ #* key=:scalar_conflict msg="not available with scalar type."
448
+ raise schema_error(:scalar_conflict, rule, path, 'sequence:') if @sequence
449
+ raise schema_error(:scalar_conflict, rule, path, 'mapping:') if @mapping
450
+ if @enum
451
+ #* key=:enum_conflict msg="not available with 'enum:'."
452
+ raise schema_error(:enum_conflict, rule, path, 'range:') if @range
453
+ raise schema_error(:enum_conflict, rule, path, 'length:') if @length
454
+ raise schema_error(:enum_conflict, rule, path, 'pattern:') if @pattern
455
+ end
456
+ unless @default.nil?
457
+ #* key=:default_conflict msg="not available when 'required:' is true."
458
+ raise schema_error(:default_conflict, rule, path, 'default:') if @required
459
+ end
460
+ end
461
+ end
462
+
463
+ #def inspect()
464
+ # str = ""; level = 0; done = {}
465
+ # _inspect(str, level, done)
466
+ # return str
467
+ #end
468
+
469
+
470
+ protected
471
+
472
+
473
+ def _inspect(str="", level=0, done={})
474
+ done[self.__id__] = true
475
+ str << " " * level << "name: #{@name}\n" unless @name.nil?
476
+ str << " " * level << "desc: #{@desc}\n" unless @desc.nil?
477
+ str << " " * level << "type: #{@type}\n" unless @type.nil?
478
+ str << " " * level << "klass: #{@type_class.name}\n" unless @type_class.nil?
479
+ str << " " * level << "required: #{@required}\n" unless @required.nil?
480
+ str << " " * level << "pattern: #{@regexp.inspect}\n" unless @pattern.nil?
481
+ str << " " * level << "assert: #{@assert}\n" unless @assert.nil?
482
+ str << " " * level << "ident: #{@ident}\n" unless @ident.nil?
483
+ str << " " * level << "unique: #{@unique}\n" unless @unique.nil?
484
+ if !@enum.nil?
485
+ str << " " * level << "enum:\n"
486
+ @enum.each do |item|
487
+ str << " " * (level+1) << "- #{item}\n"
488
+ end
489
+ end
490
+ if !@range.nil?
491
+ str << " " * level
492
+ str << "range: { "
493
+ colon = ""
494
+ %w[max max-ex min min-ex].each do |key|
495
+ val = @range[key]
496
+ unless val.nil?
497
+ str << colon << "#{key}: #{val.inspect}"
498
+ colon = ", "
499
+ end
500
+ end
501
+ str << " }\n"
502
+ end
503
+ if !@length.nil?
504
+ str << " " * level
505
+ str << "length: { "
506
+ colon = ""
507
+ %w[max max-ex min min-ex].each do |key|
508
+ val = @length[key]
509
+ if !val.nil?
510
+ str << colon << "#{key}: #{val.inspect}"
511
+ colon = ", "
512
+ end
513
+ end
514
+ str << " }\n"
515
+ end
516
+ @sequence.each do |rule|
517
+ if done[rule.__id__]
518
+ str << " " * (level+1) << "- ...\n"
519
+ else
520
+ str << " " * (level+1) << "- \n"
521
+ rule._inspect(str, level+2, done)
522
+ end
523
+ end if @sequence
524
+ @mapping.each do |key, rule|
525
+ if done[rule.__id__]
526
+ str << ' ' * (level+1) << '"' << key << "\": ...\n"
527
+ else
528
+ str << ' ' * (level+1) << '"' << key << "\":\n"
529
+ rule._inspect(str, level+2, done)
530
+ end
531
+ end if @mapping
532
+ return str
533
+ end
534
+
535
+
536
+ public
537
+
538
+
539
+ def _uniqueness_check_table() # :nodoc:
540
+ uniq_table = nil
541
+ if @type == 'map'
542
+ @mapping.keys.each do |key|
543
+ rule = @mapping[key]
544
+ if rule.unique || rule.ident
545
+ uniq_table ||= {}
546
+ uniq_table[key] = {}
547
+ end
548
+ end
549
+ elsif @unique || @ident
550
+ uniq_table = {}
551
+ end
552
+ return uniq_table
553
+ end
554
+
555
+
556
+ end
557
+
558
+
559
+ end