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,57 @@
1
+ ###
2
+ ### $Rev$
3
+ ### 0.7.2
4
+ ### $COPYRIGHT$
5
+ ###
6
+
7
+ module Kwalify
8
+
9
+ module Util
10
+
11
+ class OrderedHash < Hash
12
+
13
+ def initialize(*args, &block)
14
+ super
15
+ @_keys = []
16
+ end
17
+
18
+ alias __set__ []=
19
+
20
+ def put(key, val)
21
+ @_keys << key unless self.key?(key)
22
+ __set__(key, val)
23
+ end
24
+
25
+ def add(key, val)
26
+ @_keys.delete_at(@_keys.index(key)) if self.key?(key)
27
+ @_keys << key
28
+ __set__(key, val)
29
+ end
30
+
31
+ alias []= put
32
+ #alias []= add
33
+
34
+ def keys
35
+ return @_keys.dup
36
+ end
37
+
38
+ def values
39
+ return @_keys.collect {|key| self[key] }
40
+ end
41
+
42
+ def delete(key)
43
+ @_keys.delete_at(@_keys.index(key)) if self.key?(key)
44
+ super
45
+ end
46
+
47
+ def each
48
+ @_keys.each do |key|
49
+ yield key, self[key]
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,112 @@
1
+ ###
2
+ ### $Rev$
3
+ ### $Release: 0.7.2 $
4
+ ### copyright(c) 2005-2010 kuwata-lab all rights reserved.
5
+ ###
6
+
7
+ require 'yaml'
8
+ require 'test/unit/testcase'
9
+
10
+
11
+ class Test::Unit::TestCase # :nodoc:
12
+
13
+
14
+ def self._untabify(str, width=8) # :nodoc:
15
+ sb = []
16
+ str.scan(/(.*?)\t/m) do |s, |
17
+ len = (n = s.rindex(?\n)) ? s.length - n - 1 : s.length
18
+ sb << s << (" " * (width - len % width))
19
+ end
20
+ str = (sb << $').join if $'
21
+ return str
22
+ end
23
+
24
+
25
+ def self.load_yaml_documents(filename, options={}) # :nodoc:
26
+ str = File.read(filename)
27
+ if filename =~ /\.rb$/
28
+ str =~ /^__END__$/ or raise "*** error: __END__ is not found in '#{filename}'."
29
+ str = $'
30
+ end
31
+ str = _untabify(str) unless options[:tabify] == false
32
+ #
33
+ identkey = options[:identkey] || 'name'
34
+ list = []
35
+ table = {}
36
+ YAML.load_documents(str) do |ydoc|
37
+ if ydoc.is_a?(Hash)
38
+ list << ydoc
39
+ elsif ydoc.is_a?(Array)
40
+ list += ydoc
41
+ else
42
+ raise "*** invalid ydoc: #{ydoc.inspect}"
43
+ end
44
+ end
45
+ #
46
+ list.each do |ydoc|
47
+ ident = ydoc[identkey]
48
+ ident or raise "*** #{identkey} is not found."
49
+ table[ident] and raise "*** #{identkey} '#{ident}' is duplicated."
50
+ table[ident] = ydoc
51
+ yield(ydoc) if block_given?
52
+ end
53
+ #
54
+ return list
55
+ end
56
+
57
+
58
+ def self.load_yaml_testdata(filename, options={}) # :nodoc:
59
+ identkey = options[:identkey] || 'name'
60
+ testmethod = options[:testmethod] || '_test'
61
+ lang = options[:lang]
62
+ load_yaml_documents(filename, options) do |ydoc|
63
+ ident = ydoc[identkey]
64
+ s = "def test_#{ident}\n"
65
+ ydoc.each do |key, val|
66
+ if key[-1] == ?*
67
+ key = key[0, key.length-1]
68
+ val = val[lang]
69
+ end
70
+ s << " @#{key} = #{val.inspect}\n"
71
+ end
72
+ s << " #{testmethod}\n"
73
+ s << "end\n"
74
+ #$stderr.puts "*** #{method_name()}(): eval_str=<<'END'\n#{s}END" if $DEBUG
75
+ module_eval s # not eval!
76
+ end
77
+ end
78
+
79
+
80
+ def self.method_name # :nodoc:
81
+ return (caller[0] =~ /in `(.*?)'/) && $1
82
+ end
83
+
84
+
85
+ def self.load_yaml_testdata_with_each_lang(filename, options={}) # :nodoc:
86
+ identkey = options[:identkey] || 'name'
87
+ testmethod = options[:testmethod] || '_test'
88
+ langs = defined?($lang) && $lang ? [ $lang ] : options[:langs]
89
+ langs or raise "*** #{method_name()}(): option ':langs' is required."
90
+ #
91
+ load_yaml_documents(filename, options) do |ydoc|
92
+ ident = ydoc[identkey]
93
+ langs.each do |lang|
94
+ s = "def test_#{ident}_#{lang}\n"
95
+ s << " @lang = #{lang.inspect}\n"
96
+ ydoc.each do |key, val|
97
+ if key[-1] == ?*
98
+ key = key[0, key.length-1]
99
+ val = val[lang]
100
+ end
101
+ s << " @#{key} = #{val.inspect}\n"
102
+ end
103
+ s << " #{testmethod}\n"
104
+ s << "end\n"
105
+ #$stderr.puts "*** #{method_name()}(): eval_str=<<'END'\n#{s}END" if $DEBUG
106
+ module_eval s # not eval!
107
+ end
108
+ end
109
+ end
110
+
111
+
112
+ end
@@ -0,0 +1,282 @@
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
+ require 'kwalify/rule'
11
+
12
+ module Kwalify
13
+
14
+ ##
15
+ ## validate YAML document
16
+ ##
17
+ ## ex1. validate yaml document
18
+ ## schema = YAML.load_file('schema.yaml')
19
+ ## validator = Kwalify::Validator.new(schema)
20
+ ## document = YAML.load_file('document.yaml')
21
+ ## erros = validator.validate(document)
22
+ ## if errors && !errors.empty?
23
+ ## errors.each do |err|
24
+ ## puts "- [#{err.path}] #{err.message}"
25
+ ## end
26
+ ## end
27
+ ##
28
+ ## ex2. validate with parsing
29
+ ## schema = YAML.load_file('schema.yaml')
30
+ ## validator = Kwalify::Validator.new(schema)
31
+ ## parser = Kwalify::Yaml::Parser.new(validator)
32
+ ## document = parser.parse(File.read('document.yaml'))
33
+ ## errors = parser.errors
34
+ ## if errors && errors.empty?
35
+ ## errors.each do |e|
36
+ ## puts "#{e.linenum}:#{e.column} [#{e.path}] #{e.message}"
37
+ ## end
38
+ ## end
39
+ ##
40
+ class Validator
41
+ include Kwalify::ErrorHelper
42
+
43
+
44
+ def initialize(hash_or_rule, &block)
45
+ obj = hash_or_rule
46
+ @rule = (obj.nil? || obj.is_a?(Rule)) ? obj : Rule.new(obj)
47
+ @block = block
48
+ end
49
+ attr_reader :rule
50
+
51
+
52
+ def _inspect
53
+ @rule._inspect
54
+ end
55
+
56
+
57
+ def validate(value)
58
+ path = ''; errors = []; done = {}; uniq_table = nil
59
+ _validate(value, @rule, path, errors, done, uniq_table)
60
+ return errors
61
+ end
62
+
63
+
64
+ protected
65
+
66
+
67
+ def validate_hook(value, rule, path, errors)
68
+ ## may be overrided by subclass
69
+ end
70
+
71
+
72
+ public
73
+
74
+
75
+ def _validate(value, rule, path, errors, done, uniq_table, recursive=true)
76
+ #if Types.collection?(value)
77
+ if !Types.scalar?(value)
78
+ #if done[value.__id__]
79
+ # rule2 = done[value.__id__]
80
+ # if rule2.is_a?(Rule)
81
+ # return if rule.equal?(rule2)
82
+ # done[value.__id__] = [rule2, rule]
83
+ # elsif rule2.is_a?(Array)
84
+ # return if rule2.any? {|r| r.equal?(rule)}
85
+ # done[value.__id__] << rule
86
+ # else
87
+ # raise "unreachable"
88
+ # end
89
+ #end
90
+ return if done[value.__id__] # avoid infinite loop
91
+ done[value.__id__] = rule
92
+ end
93
+ if rule.required && value.nil?
94
+ #* key=:required_novalue msg="value required but none."
95
+ errors << validate_error(:required_novalue, rule, path, value)
96
+ return
97
+ end
98
+ if rule.type_class && !value.nil? && !value.is_a?(rule.type_class)
99
+ unless rule.classobj && value.is_a?(rule.classobj)
100
+ #* key=:type_unmatch msg="not a %s."
101
+ errors << validate_error(:type_unmatch, rule, path, value, [Kwalify.word(rule.type)])
102
+ return
103
+ end
104
+ end
105
+ #
106
+ n = errors.length
107
+ if rule.sequence
108
+ _validate_sequence(value, rule, path, errors, done, uniq_table, recursive)
109
+ elsif rule.mapping
110
+ _validate_mapping(value, rule, path, errors, done, uniq_table, recursive)
111
+ else
112
+ _validate_scalar(value, rule, path, errors, done, uniq_table)
113
+ end
114
+ return unless errors.length == n
115
+ #
116
+ #path_str = path.is_a?(Array) ? '/'+path.join('/') : path
117
+ #validate_hook(value, rule, path_str, errors)
118
+ #@block.call(value, rule, path_str, errors) if @block
119
+ validate_hook(value, rule, path, errors)
120
+ @block.call(value, rule, path, errors) if @block
121
+ end
122
+
123
+
124
+ private
125
+
126
+
127
+ def _validate_sequence(list, seq_rule, path, errors, done, uniq_table, recursive=true)
128
+ assert_error("seq_rule.sequence.class==#{seq_rule.sequence.class.name} (expected Array)") unless seq_rule.sequence.is_a?(Array)
129
+ assert_error("seq_rule.sequence.length==#{seq_rule.sequence.length} (expected 1)") unless seq_rule.sequence.length == 1
130
+ return if list.nil? || !recursive
131
+ rule = seq_rule.sequence[0]
132
+ uniq_table = rule._uniqueness_check_table()
133
+ list.each_with_index do |val, i|
134
+ child_path = path.is_a?(Array) ? path + [i] : "#{path}/#{i}"
135
+ _validate(val, rule, child_path, errors, done, uniq_table) ## validate recursively
136
+ end
137
+ end
138
+
139
+
140
+ def _validate_mapping(hash, map_rule, path, errors, done, uniq_table, recursive=true)
141
+ assert_error("map_rule.mapping.class==#{map_rule.mapping.class.name} (expected Hash)") unless map_rule.mapping.is_a?(Hash)
142
+ return if hash.nil?
143
+ return if !recursive
144
+ _validate_mapping_required_keys(hash, map_rule, path, errors)
145
+ hash.each do |key, val|
146
+ rule = map_rule.mapping[key]
147
+ child_path = path.is_a?(Array) ? path+[key] : "#{path}/#{key}"
148
+ unless rule
149
+ #* key=:key_undefined msg="key '%s' is undefined."
150
+ errors << validate_error(:key_undefined, rule, child_path, hash, ["#{key}:"])
151
+ ##* key=:key_undefined msg="undefined key."
152
+ #errors << validate_error(:key_undefined, rule, child_path, "#{key}:")
153
+ else
154
+ _validate(val, rule, child_path, errors, done,
155
+ uniq_table ? uniq_table[key] : nil) ## validate recursively
156
+ end
157
+ end
158
+ end
159
+
160
+
161
+ def _validate_mapping_required_keys(hash, map_rule, path, errors) #:nodoc:
162
+ map_rule.mapping.each do |key, rule|
163
+ #next unless rule.required
164
+ #val = hash.is_a?(Hash) ? hash[key] : hash.instance_variable_get("@#{key}")
165
+ #if val.nil?
166
+ if rule.required && hash[key].nil? # or !hash.key?(key)
167
+ #* key=:required_nokey msg="key '%s:' is required."
168
+ errors << validate_error(:required_nokey, rule, path, hash, [key])
169
+ end
170
+ end
171
+ end
172
+ public :_validate_mapping_required_keys
173
+
174
+
175
+ def _validate_scalar(value, rule, path, errors, done, uniq_table)
176
+ assert_error("rule.sequence.class==#{rule.sequence.class.name} (expected NilClass)") if rule.sequence
177
+ assert_error("rule.mapping.class==#{rule.mapping.class.name} (expected NilClass)") if rule.mapping
178
+ _validate_assert( value, rule, path, errors) if rule.assert_proc
179
+ _validate_enum( value, rule, path, errors) if rule.enum
180
+ return if value.nil?
181
+ _validate_pattern(value, rule, path, errors) if rule.pattern
182
+ _validate_range( value, rule, path, errors) if rule.range
183
+ _validate_length( value, rule, path, errors) if rule.length
184
+ _validate_unique( value, rule, path, errors, uniq_table) if uniq_table
185
+ end
186
+
187
+
188
+ def _validate_unique(value, rule, path, errors, uniq_table)
189
+ assert_error "uniq_table=#{uniq_table.inspect}" unless rule.unique || rule.ident
190
+ if uniq_table.key?(value)
191
+ exist_at = uniq_table[value]
192
+ exist_at = "/#{exist_at.join('/')}" if exist_at.is_a?(Array)
193
+ #* key=:value_notunique msg="is already used at '%s'."
194
+ errors << validate_error(:value_notunique, rule, path, value, exist_at)
195
+ else
196
+ uniq_table[value] = path.dup
197
+ end
198
+ end
199
+ public :_validate_unique
200
+
201
+
202
+ def _validate_assert(value, rule, path, errors)
203
+ assert_error("rule=#{rule._inspect}") unless rule.assert_proc
204
+ unless rule.assert_proc.call(value)
205
+ #* key=:assert_failed msg="assertion expression failed (%s)."
206
+ errors << validate_error(:assert_failed, rule, path, value, [rule.assert])
207
+ end
208
+ end
209
+
210
+
211
+ def _validate_enum(value, rule, path, errors)
212
+ assert_error("rule=#{rule._inspect}") unless rule.enum
213
+ unless rule.enum.include?(value)
214
+ keyname = path.is_a?(Array) ? path[-1] : File.basename(path)
215
+ keyname = 'enum' if keyname =~ /\A\d+\z/
216
+ #* key=:enum_notexist msg="invalid %s value."
217
+ errors << validate_error(:enum_notexist, rule, path, value, [keyname])
218
+ end
219
+ end
220
+
221
+
222
+ def _validate_pattern(value, rule, path, errors)
223
+ assert_error("rule=#{rule._inspect}") unless rule.pattern
224
+ unless value.to_s =~ rule.regexp
225
+ #* key=:pattern_unmatch msg="not matched to pattern %s."
226
+ errors << validate_error(:pattern_unmatch, rule, path, value, [rule.pattern])
227
+ end
228
+ end
229
+
230
+
231
+ def _validate_range(value, rule, path, errors)
232
+ assert_error("rule=#{rule._inspect}") unless rule.range
233
+ assert_error("value.class=#{value.class.name}") unless Types.scalar?(value)
234
+ h = rule.range
235
+ max, min, max_ex, min_ex = h['max'], h['min'], h['max-ex'], h['min-ex']
236
+ if max && max < value
237
+ #* key=:range_toolarge msg="too large (> max %s)."
238
+ errors << validate_error(:range_toolarge, rule, path, value, [max.to_s])
239
+ end
240
+ if min && min > value
241
+ #* key=:range_toosmall msg="too small (< min %s)."
242
+ errors << validate_error(:range_toosmall, rule, path, value, [min.to_s])
243
+ end
244
+ if max_ex && max_ex <= value
245
+ #* key=:range_toolargeex msg="too large (>= max %s)."
246
+ errors << validate_error(:range_toolargeex, rule, path, value, [max_ex.to_s])
247
+ end
248
+ if min_ex && min_ex >= value
249
+ #* key=:range_toosmallex msg="too small (<= min %s)."
250
+ errors << validate_error(:range_toosmallex, rule, path, value, [min_ex.to_s])
251
+ end
252
+ end
253
+
254
+
255
+ def _validate_length(value, rule, path, errors)
256
+ assert_error("rule=#{rule._inspect}") unless rule.length
257
+ assert_error("value.class=#{value.class.name}") unless value.is_a?(String) || value.is_a?(Text)
258
+ len = value.to_s.length
259
+ h = rule.length
260
+ max, min, max_ex, min_ex = h['max'], h['min'], h['max-ex'], h['min-ex']
261
+ if max && max < len
262
+ #* key=:length_toolong msg="too long (length %d > max %d)."
263
+ errors << validate_error(:length_toolong, rule, path, value, [len, max])
264
+ end
265
+ if min && min > len
266
+ #* key=:length_tooshort msg="too short (length %d < min %d)."
267
+ errors << validate_error(:length_tooshort, rule, path, value, [len, min])
268
+ end
269
+ if max_ex && max_ex <= len
270
+ #* key=:length_toolongex msg="too long (length %d >= max %d)."
271
+ errors << validate_error(:length_toolongex, rule, path, value, [len, max_ex])
272
+ end
273
+ if min_ex && min_ex >= len
274
+ #* key=:length_tooshortex msg="too short (length %d <= min %d)."
275
+ errors << validate_error(:length_tooshortex, rule, path, value, [len, min_ex])
276
+ end
277
+ end
278
+
279
+
280
+ end
281
+
282
+ end