pione 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (304) hide show
  1. data/.gitignore +2 -0
  2. data/.s3cfg +42 -0
  3. data/.travis.yml +18 -4
  4. data/.yardopts +1 -0
  5. data/Gemfile +3 -0
  6. data/History.txt +14 -0
  7. data/Procfile +7 -0
  8. data/Rakefile +25 -0
  9. data/bin/pione-interactive +6 -0
  10. data/bin/{pione-compiler → pione-notification-listener} +1 -1
  11. data/bin/{pione-broker → pione-task-worker-broker} +1 -1
  12. data/doc/man/pione-list-param.md +36 -0
  13. data/example/ActionError/ActionError.pione +9 -0
  14. data/example/ActionError/pione-package.json +15 -0
  15. data/example/CountChar/CountChar.pione +1 -1
  16. data/example/ScoreAggregation/ScoreAggregation.pione +1 -1
  17. data/example/Touch/pione-package.json +15 -0
  18. data/lib/pione.rb +12 -0
  19. data/lib/pione/agent.rb +3 -3
  20. data/lib/pione/agent/basic-agent.rb +10 -4
  21. data/lib/pione/agent/input-generator.rb +3 -3
  22. data/lib/pione/agent/{process-manager.rb → job-manager.rb} +10 -7
  23. data/lib/pione/agent/job-terminator.rb +2 -2
  24. data/lib/pione/agent/logger.rb +3 -3
  25. data/lib/pione/agent/messenger.rb +20 -9
  26. data/lib/pione/agent/notification-listener.rb +88 -0
  27. data/lib/pione/agent/task-worker-broker.rb +84 -0
  28. data/lib/pione/agent/task-worker.rb +3 -2
  29. data/lib/pione/agent/tuple-space-client.rb +1 -0
  30. data/lib/pione/agent/tuple-space-provider.rb +17 -35
  31. data/lib/pione/command.rb +33 -15
  32. data/lib/pione/command/action.rb +105 -0
  33. data/lib/pione/command/basic-command.rb +34 -376
  34. data/lib/pione/command/command-exception.rb +12 -11
  35. data/lib/pione/command/common.rb +198 -0
  36. data/lib/pione/command/option.rb +159 -204
  37. data/lib/pione/command/pione-action-exec.rb +85 -0
  38. data/lib/pione/command/pione-action-list.rb +43 -19
  39. data/lib/pione/command/pione-action-print.rb +79 -0
  40. data/lib/pione/command/pione-action.rb +8 -67
  41. data/lib/pione/command/pione-clean.rb +88 -68
  42. data/lib/pione/command/pione-client.rb +475 -332
  43. data/lib/pione/command/pione-command.rb +14 -31
  44. data/lib/pione/command/pione-compile.rb +90 -0
  45. data/lib/pione/command/pione-config-get.rb +53 -0
  46. data/lib/pione/command/pione-config-list.rb +64 -0
  47. data/lib/pione/command/pione-config-set.rb +59 -0
  48. data/lib/pione/command/pione-config-unset.rb +50 -0
  49. data/lib/pione/command/pione-config.rb +61 -0
  50. data/lib/pione/command/pione-diagnosis-notification.rb +235 -0
  51. data/lib/pione/command/pione-diagnosis.rb +21 -0
  52. data/lib/pione/command/pione-interactive.rb +188 -0
  53. data/lib/pione/command/pione-lang-check-syntax.rb +163 -0
  54. data/lib/pione/command/pione-lang-interactive.rb +146 -0
  55. data/lib/pione/command/pione-lang.rb +22 -0
  56. data/lib/pione/command/pione-log-format.rb +163 -0
  57. data/lib/pione/command/pione-log-list-id.rb +48 -0
  58. data/lib/pione/command/pione-log.rb +19 -101
  59. data/lib/pione/command/pione-notification-listener.rb +95 -0
  60. data/lib/pione/command/pione-package-add.rb +137 -0
  61. data/lib/pione/command/pione-package-build.rb +122 -0
  62. data/lib/pione/command/pione-package-show.rb +96 -0
  63. data/lib/pione/command/pione-package-update.rb +59 -0
  64. data/lib/pione/command/pione-package.rb +11 -139
  65. data/lib/pione/command/pione-task-worker-broker.rb +88 -0
  66. data/lib/pione/command/pione-task-worker.rb +148 -98
  67. data/lib/pione/command/pione-tuple-space-provider.rb +62 -54
  68. data/lib/pione/command/pione-tuple-space-viewer.rb +105 -83
  69. data/lib/pione/command/pione-val.rb +39 -39
  70. data/lib/pione/command/spawner.rb +34 -27
  71. data/lib/pione/front.rb +4 -2
  72. data/lib/pione/front/basic-front.rb +86 -23
  73. data/lib/pione/front/client-front.rb +2 -2
  74. data/lib/pione/front/diagnosis-notification-front.rb +40 -0
  75. data/lib/pione/front/front-exception.rb +7 -1
  76. data/lib/pione/front/notification-listener-front.rb +36 -0
  77. data/lib/pione/front/notification-recipient-interface.rb +19 -0
  78. data/lib/pione/front/relay-front.rb +4 -4
  79. data/lib/pione/front/task-worker-broker-front.rb +19 -0
  80. data/lib/pione/front/task-worker-front.rb +2 -2
  81. data/lib/pione/front/tuple-space-provider-front.rb +3 -2
  82. data/lib/pione/global.rb +3 -1
  83. data/lib/pione/global/client-variable.rb +1 -1
  84. data/lib/pione/global/config.rb +63 -7
  85. data/lib/pione/global/diagnosis-variable.rb +26 -0
  86. data/lib/pione/global/item.rb +28 -5
  87. data/lib/pione/global/log-variable.rb +6 -5
  88. data/lib/pione/global/network-variable.rb +75 -4
  89. data/lib/pione/global/path-variable.rb +1 -1
  90. data/lib/pione/global/system-variable.rb +12 -12
  91. data/lib/pione/global/task-worker-broker-variable.rb +43 -0
  92. data/lib/pione/global/tuple-space-notifier-variable.rb +3 -55
  93. data/lib/pione/lang/boolean.rb +4 -0
  94. data/lib/pione/lang/data-expr.rb +1 -1
  95. data/lib/pione/lang/integer.rb +1 -1
  96. data/lib/pione/lang/string.rb +4 -0
  97. data/lib/pione/lang/type.rb +1 -1
  98. data/lib/pione/location.rb +1 -0
  99. data/lib/pione/location/data-location.rb +41 -13
  100. data/lib/pione/location/dropbox-location.rb +175 -56
  101. data/lib/pione/location/ftp-location.rb +4 -1
  102. data/lib/pione/location/http-location.rb +5 -3
  103. data/lib/pione/location/https-location.rb +4 -2
  104. data/lib/pione/location/local-location.rb +6 -3
  105. data/lib/pione/location/location-exception.rb +12 -0
  106. data/lib/pione/location/notification-scheme.rb +46 -0
  107. data/lib/pione/log.rb +8 -7
  108. data/lib/pione/log/debug.rb +9 -9
  109. data/lib/pione/log/domain-log.rb +6 -1
  110. data/lib/pione/log/message-log-receiver.rb +32 -0
  111. data/lib/pione/log/system-log.rb +62 -141
  112. data/lib/pione/model.rb +7 -0
  113. data/lib/pione/model/notification-listener-model.rb +29 -0
  114. data/lib/pione/model/task-worker-broker-model.rb +129 -0
  115. data/lib/pione/notification.rb +13 -0
  116. data/lib/pione/notification/address.rb +104 -0
  117. data/lib/pione/notification/exception.rb +10 -0
  118. data/lib/pione/notification/message.rb +109 -0
  119. data/lib/pione/notification/receiver.rb +90 -0
  120. data/lib/pione/notification/recipient.rb +68 -0
  121. data/lib/pione/notification/task-worker-broker-recipient.rb +85 -0
  122. data/lib/pione/notification/transmitter.rb +84 -0
  123. data/lib/pione/pnml.rb +35 -0
  124. data/lib/pione/pnml/annotation-extractor.rb +47 -0
  125. data/lib/pione/pnml/compiler.rb +176 -0
  126. data/lib/pione/pnml/input-merge-complement.rb +78 -0
  127. data/lib/pione/pnml/input-parallelization-complement.rb +75 -0
  128. data/lib/pione/pnml/input-reduction.rb +80 -0
  129. data/lib/pione/pnml/invalid-arc-elimination.rb +41 -0
  130. data/lib/pione/pnml/io-expansion.rb +88 -0
  131. data/lib/pione/pnml/isolated-element-elimination.rb +32 -0
  132. data/lib/pione/pnml/net-rewriter.rb +44 -0
  133. data/lib/pione/pnml/output-decomposition-complement.rb +77 -0
  134. data/lib/pione/pnml/output-reduction.rb +86 -0
  135. data/lib/pione/pnml/output-synchronization-complement.rb +77 -0
  136. data/lib/pione/pnml/pione-model.rb +417 -0
  137. data/lib/pione/pnml/pnml-exception.rb +23 -0
  138. data/lib/pione/pnml/pnml-model.rb +368 -0
  139. data/lib/pione/pnml/reader.rb +51 -0
  140. data/lib/pione/rule-engine/action-handler.rb +8 -6
  141. data/lib/pione/rule-engine/basic-handler.rb +5 -5
  142. data/lib/pione/rule-engine/engine-exception.rb +7 -6
  143. data/lib/pione/rule-engine/flow-handler.rb +27 -18
  144. data/lib/pione/system.rb +3 -1
  145. data/lib/pione/system/domain-dump.rb +34 -0
  146. data/lib/pione/system/file-cache.rb +92 -13
  147. data/lib/pione/system/init.rb +3 -0
  148. data/lib/pione/system/normalizer.rb +40 -0
  149. data/lib/pione/system/status.rb +5 -5
  150. data/lib/pione/system/system-exception.rb +14 -1
  151. data/lib/pione/task-worker-broker.rb +7 -0
  152. data/lib/pione/task-worker-broker/basic-provider.rb +20 -0
  153. data/lib/pione/task-worker-broker/easy-provider.rb +65 -0
  154. data/lib/pione/test-helper.rb +27 -0
  155. data/lib/pione/test-helper/command-helper.rb +9 -101
  156. data/lib/pione/tuple-space/basic-tuple.rb +1 -1
  157. data/lib/pione/tuple-space/tuple-definition.yml +4 -6
  158. data/lib/pione/tuple-space/tuple-space-interface.rb +3 -45
  159. data/lib/pione/tuple-space/tuple-space-server.rb +45 -0
  160. data/lib/pione/util.rb +1 -2
  161. data/lib/pione/util/boolean-value.rb +62 -0
  162. data/lib/pione/util/completion.rb +111 -0
  163. data/lib/pione/util/evaluatable.rb +13 -27
  164. data/lib/pione/util/package-parameters-list.rb +15 -27
  165. data/lib/pione/util/zip.rb +8 -3
  166. data/lib/pione/version.rb +1 -1
  167. data/lib/rootage.rb +20 -0
  168. data/lib/rootage/action.rb +114 -0
  169. data/lib/rootage/argument.rb +46 -0
  170. data/lib/rootage/command.rb +218 -0
  171. data/lib/rootage/core.rb +532 -0
  172. data/lib/rootage/exception.rb +107 -0
  173. data/lib/rootage/help.rb +148 -0
  174. data/lib/rootage/help.txt.erb +31 -0
  175. data/lib/rootage/log.rb +226 -0
  176. data/lib/rootage/normalizer.rb +184 -0
  177. data/lib/rootage/option.rb +152 -0
  178. data/lib/rootage/scenario-test-result.erb +39 -0
  179. data/lib/rootage/scenario.rb +362 -0
  180. data/lib/rootage/test-helper.rb +115 -0
  181. data/man/pione-list-param.1 +44 -0
  182. data/misc/clock.rb +9 -0
  183. data/misc/machine-info.sh +21 -0
  184. data/misc/pione-completion.bash +238 -0
  185. data/misc/pione-completion.erb +53 -0
  186. data/misc/pione-completion.zsh +238 -0
  187. data/misc/pione.god +22 -0
  188. data/misc/ui.xml +23 -0
  189. data/pione.gemspec +3 -1
  190. data/test/agent/spec_basic-agent.rb +1 -1
  191. data/test/agent/spec_input-generator.rb +2 -2
  192. data/test/agent/spec_messenger.rb +6 -9
  193. data/test/agent/spec_notification-listener.rb +80 -0
  194. data/test/agent/{spec_broker.rb → spec_task-worker-broker.rb} +13 -10
  195. data/test/agent/spec_tuple-space-provider.rb +10 -6
  196. data/test/command/command-behavior.rb +3 -11
  197. data/test/command/data/pione-list-param/AdvancedParameters.pione +12 -0
  198. data/test/command/data/pione-list-param/BasicParameters.pione +12 -0
  199. data/test/command/spec_pione-action-exec.rb +16 -0
  200. data/test/command/spec_pione-action-list.rb +15 -10
  201. data/test/command/spec_pione-action-print.rb +14 -0
  202. data/test/command/spec_pione-action.rb +6 -19
  203. data/test/command/spec_pione-clean.rb +29 -46
  204. data/test/command/spec_pione-client.rb +29 -36
  205. data/test/command/spec_pione-command.rb +6 -6
  206. data/test/command/{spec_pione-compiler.rb → spec_pione-compile.rb} +11 -13
  207. data/test/command/spec_pione-config-get.rb +47 -0
  208. data/test/command/spec_pione-config-list.rb +42 -0
  209. data/test/command/spec_pione-config-set.rb +38 -0
  210. data/test/command/spec_pione-config-unset.rb +44 -0
  211. data/test/command/spec_pione-config.rb +11 -0
  212. data/test/command/spec_pione-diagnosis-notification.rb +23 -0
  213. data/test/command/spec_pione-diagnosis.rb +11 -0
  214. data/test/command/spec_pione-lang-check-syntax.rb +12 -0
  215. data/test/command/spec_pione-lang.rb +11 -0
  216. data/test/command/spec_pione-log-format.rb +29 -0
  217. data/test/command/spec_pione-log-list-id.rb +17 -0
  218. data/test/command/spec_pione-log.rb +6 -20
  219. data/test/command/spec_pione-package-add.rb +55 -0
  220. data/test/command/spec_pione-package-build.rb +57 -0
  221. data/test/command/spec_pione-package-show.rb +72 -0
  222. data/test/command/{spec_pione-update-package-info.rb → spec_pione-package-update.rb} +12 -13
  223. data/test/command/spec_pione-package.rb +4 -104
  224. data/test/command/spec_pione-val.rb +10 -7
  225. data/test/global/spec_config.rb +50 -0
  226. data/test/global/spec_item.rb +1 -1
  227. data/test/literate-action/data/HelloWorld.md +1 -1
  228. data/test/location/location-behavior.rb +1 -1
  229. data/test/location/spec_dropbox-location.rb +39 -0
  230. data/test/location/spec_notification-scheme.rb +37 -0
  231. data/test/log/spec_debug.rb +5 -4
  232. data/test/log/spec_message-log-receiver.rb +13 -0
  233. data/test/log/spec_message-log.rb +6 -9
  234. data/test/log/spec_system-log.rb +5 -3
  235. data/test/notification/spec_address.rb +229 -0
  236. data/test/notification/spec_message.rb +30 -0
  237. data/test/notification/spec_receiver.rb +36 -0
  238. data/test/notification/spec_transmitter.rb +37 -0
  239. data/test/pnml/data/ConditionalBranchIf.pnml +270 -0
  240. data/test/pnml/data/ConditionalBranchIfElse.pnml +309 -0
  241. data/test/pnml/data/IOExpansionComplex.pnml +363 -0
  242. data/test/pnml/data/IOExpansionSimple.pnml +140 -0
  243. data/test/pnml/data/InputMergeComplementComplex.pnml +381 -0
  244. data/test/pnml/data/InputMergeComplementSimple.pnml +248 -0
  245. data/test/pnml/data/InputParallelizationComplementComplex.pnml +433 -0
  246. data/test/pnml/data/InputParallelizationComplementSimple.pnml +288 -0
  247. data/test/pnml/data/InputReductionComplex.pnml +192 -0
  248. data/test/pnml/data/InputReductionLong.pnml +344 -0
  249. data/test/pnml/data/InputReductionSimple.pnml +140 -0
  250. data/test/pnml/data/IsolatedElementElimination.pnml +171 -0
  251. data/test/pnml/data/OutputDecompositionComplementComplex.pnml +381 -0
  252. data/test/pnml/data/OutputDecompositionComplementSimple.pnml +242 -0
  253. data/test/pnml/data/OutputReductionComplex.pnml +186 -0
  254. data/test/pnml/data/OutputReductionLong.pnml +344 -0
  255. data/test/pnml/data/OutputReductionSimple.pnml +140 -0
  256. data/test/pnml/data/OutputSynchronizationComplementComplex.pnml +498 -0
  257. data/test/pnml/data/OutputSynchronizationComplementSimple.pnml +347 -0
  258. data/test/pnml/data/SampleNet.pnml +238 -0
  259. data/test/pnml/spec_input-merge-complement.rb +40 -0
  260. data/test/pnml/spec_input-parallelization-complement.rb +50 -0
  261. data/test/pnml/spec_input-reduction.rb +113 -0
  262. data/test/pnml/spec_invalid-arc-elimination.rb +33 -0
  263. data/test/pnml/spec_io-expansion.rb +126 -0
  264. data/test/pnml/spec_isolated-element-elimination.rb +25 -0
  265. data/test/pnml/spec_output-decomposition-complement.rb +40 -0
  266. data/test/pnml/spec_output-reduction.rb +114 -0
  267. data/test/pnml/spec_output-synchronization-complement.rb +62 -0
  268. data/test/pnml/spec_pione-element.rb +144 -0
  269. data/test/pnml/spec_pnml-element.rb +373 -0
  270. data/test/pnml/spec_reader.rb +16 -0
  271. data/test/rootage/spec_argument.rb +18 -0
  272. data/test/rootage/spec_command.rb +239 -0
  273. data/test/rootage/spec_core.rb +198 -0
  274. data/test/rootage/spec_scenario.rb +149 -0
  275. data/test/system/{spec_domain-info.rb → spec_domain-dump.rb} +6 -6
  276. data/test/system/spec_file-cache.rb +6 -9
  277. data/test/tuple-space/spec_finished-tuple.rb +1 -1
  278. data/test/util/{spec_package-parameters-list_1.pione → data/package-parameters-list/Param1.pione} +0 -0
  279. data/test/util/{spec_package-parameters-list_2.pione → data/package-parameters-list/Param2.pione} +0 -0
  280. data/test/util/{spec_package-parameters-list_3.pione → data/package-parameters-list/Param3.pione} +0 -0
  281. data/test/util/{spec_package-parameters-list_4.pione → data/package-parameters-list/Param4.pione} +0 -0
  282. data/test/util/spec_boolean-value.rb +32 -0
  283. data/test/util/spec_completion.rb +22 -0
  284. data/test/util/spec_package-parameters-list.rb +39 -52
  285. data/test/util/spec_zip.rb +28 -1
  286. metadata +288 -47
  287. data/bin/pione-tuple-space-receiver +0 -5
  288. data/lib/pione/agent/broker.rb +0 -304
  289. data/lib/pione/agent/tuple-space-receiver.rb +0 -137
  290. data/lib/pione/command/pione-broker.rb +0 -104
  291. data/lib/pione/command/pione-compiler.rb +0 -57
  292. data/lib/pione/command/pione-relay-account-db.rb +0 -141
  293. data/lib/pione/command/pione-relay-client-db.rb +0 -118
  294. data/lib/pione/command/pione-relay.rb +0 -59
  295. data/lib/pione/command/pione-syntax-checker.rb +0 -214
  296. data/lib/pione/command/pione-tuple-space-receiver.rb +0 -111
  297. data/lib/pione/command/pione-update-package-info.rb +0 -53
  298. data/lib/pione/front/broker-front.rb +0 -22
  299. data/lib/pione/front/tuple-space-receiver-front.rb +0 -11
  300. data/lib/pione/global/broker-variable.rb +0 -33
  301. data/lib/pione/system/domain-info.rb +0 -25
  302. data/lib/pione/util/pnml-compiler.rb +0 -168
  303. data/test/agent/spec_tuple-space-receiver.rb +0 -47
  304. data/test/util/spec_pnml-compiler.rb +0 -32
@@ -0,0 +1,184 @@
1
+ module Rootage
2
+ # `Normalizer` is a utility module that normalizes values into normalization
3
+ # types. If values cannot normalize, this method raises
4
+ # `NormalizerValueError`. Normalization types are followings:
5
+ #
6
+ # - boolean
7
+ # - string
8
+ # - integer
9
+ # - positive_integer
10
+ # - float
11
+ # - date
12
+ #
13
+ # And array is treated as special type, this type means the value should be
14
+ # included in the array.
15
+ module Normalizer
16
+ class << self
17
+ # Normalize the value as the normalization type.
18
+ #
19
+ # @param type [Symbol]
20
+ # normalization type
21
+ # @param val [Object]
22
+ # source value
23
+ # @return [Object]
24
+ # the normalized value
25
+ def normalize(type, val)
26
+ if respond_to?(type, true)
27
+ send(type, val)
28
+ else
29
+ raise NormalizerTypeError.new(type)
30
+ end
31
+ end
32
+
33
+ # Set normalization function.
34
+ #
35
+ # @param name [Symbol]
36
+ # normalization type name
37
+ # @yieldparam val [Object]
38
+ # source object
39
+ def set(name, &block)
40
+ singleton_class.instance_eval do
41
+ define_method(name, &block)
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def boolean(val)
48
+ BooleanValue.of(val)
49
+ rescue => e
50
+ raise NormalizerValueError.new(:boolean, val, e.message)
51
+ end
52
+
53
+ def string(val)
54
+ val.to_s
55
+ rescue => e
56
+ raise NormalizerValueError.new(:string, val, e.message)
57
+ end
58
+
59
+ def symbol(val)
60
+ val.to_sym
61
+ rescue => e
62
+ raise NormalizerValueError.new(:symbol, val, e.message)
63
+ end
64
+
65
+ def symbol_downcase(val)
66
+ val.to_s.downcase.to_sym
67
+ rescue => e
68
+ raise NormalizerValueError.new(:symbol_downcase, val, e.message)
69
+ end
70
+
71
+ def symbol_uppercase(val)
72
+ val.to_s.uppercase.to_sym
73
+ rescue => e
74
+ raise NormalizerValueError.new(:symbol_uppercase, val, e.message)
75
+ end
76
+
77
+ def integer(val)
78
+ val.to_i
79
+ rescue => e
80
+ raise NormalizerValueError.new(:integer, val, e.message)
81
+ end
82
+
83
+ def positive_integer(val)
84
+ n = integer(val)
85
+ if n > 0
86
+ n
87
+ else
88
+ raise NormalizerValueError.new(:positive_integer, val, "It should be a positive integer.")
89
+ end
90
+ end
91
+
92
+ def float(val)
93
+ val.to_f
94
+ rescue => e
95
+ raise NormalizerValueError.new(:float, val, e.message)
96
+ end
97
+
98
+ def path(val)
99
+ if val.is_a?(Pathname)
100
+ val
101
+ else
102
+ Pathname.new(val)
103
+ end
104
+ rescue => e
105
+ raise NormalizerValueError.new(:path, val, e.message)
106
+ end
107
+
108
+ def date(val)
109
+ if val.is_a?(Date)
110
+ val
111
+ else
112
+ Date.iso8601(val)
113
+ end
114
+ rescue => e
115
+ raise NormalizerValueError.new(:date, val, e.message)
116
+ end
117
+
118
+ def uri(val)
119
+ URI.parse(val)
120
+ rescue => e
121
+ raise NormalizerValueError.new(:uri, val, e.message)
122
+ end
123
+ end
124
+ end
125
+
126
+ module BooleanValue
127
+ class << self
128
+ # Return boolean value of the object.
129
+ #
130
+ # @param val [Object]
131
+ # target object
132
+ # @return [Boolean]
133
+ # boolean of the object
134
+ def of(val)
135
+ case val
136
+ when NilClass
137
+ false
138
+ when TrueClass
139
+ true
140
+ when FalseClass
141
+ false
142
+ when String
143
+ of_string(val)
144
+ when Number
145
+ of_number(val)
146
+ else
147
+ raise ArgumentError.new(val)
148
+ end
149
+ end
150
+
151
+ private
152
+
153
+ # Return boolean value of the string. Ignoring letter cases, "true",
154
+ # "t", "yes", and "y" are true, and "false", "f", "no", and "n" are
155
+ # false. Ohterwise raise `ArgumentError`.
156
+ #
157
+ # @param val [String]
158
+ # the string
159
+ # @return [Boolean]
160
+ # boolean of the string
161
+ def of_string(val)
162
+ case val.downcase
163
+ when "true", "t", "yes", "y"
164
+ true
165
+ when "false", "f", "no", "n"
166
+ false
167
+ else
168
+ raise ArgumentError.new(val)
169
+ end
170
+ end
171
+
172
+ # Return boolean value of the number. Return true if the number is
173
+ # greater than 0.
174
+ #
175
+ # @param val [Numeric]
176
+ # the number
177
+ # @return [Boolean]
178
+ # boolean of the number
179
+ def of_number(val)
180
+ val > 0
181
+ end
182
+ end
183
+ end
184
+ end
@@ -0,0 +1,152 @@
1
+ module Rootage
2
+ # Option is an option item for PIONE command.
3
+ class Option < Item
4
+ # `type` is a normalization type. The string user specified is normalized
5
+ # by this type.
6
+ member :type
7
+
8
+ # `range` is candidates of option values.
9
+ member :range
10
+
11
+ # `short` is an short option name. This requires first hyphen character
12
+ # and successful one character. For example, `"-q"` is a short option.
13
+ member :short
14
+
15
+ # `long` is a long option name. This requires first hyphen characters and
16
+ # any characters. For example, `"--quiet"` is a long option.
17
+ member :long
18
+
19
+ # `arg` is an option's argument. The notation of this follows
20
+ # "optarg.rb". For example, `"ARG"` is requisite argument, and `"[ARG]"`
21
+ # is optional argument.
22
+ member :arg
23
+
24
+ # Default value that the option is specified. This value is normalized by
25
+ # option type.
26
+ member :default
27
+
28
+ # Initial model value. This value is normalized by option type.
29
+ member :init
30
+
31
+ # This validates option's value. Validation is done after application of
32
+ # post process.
33
+ member :validator
34
+
35
+ # This flag is true, the option is requisite.
36
+ member :requisite
37
+
38
+ def define_validator(&b)
39
+ self.validator = b
40
+ end
41
+
42
+ # Setup an OptionParser's option by this item.
43
+ #
44
+ # @param opt [OptionParser]
45
+ # option parser
46
+ # @param cmd [Command::PlainCommand]
47
+ # command object
48
+ # @return [void]
49
+ def setup(opt, cmd)
50
+ # build OptionParser#on arguments
51
+ args = [short_for_optparse, long_for_optparse, desc].compact
52
+ # call #on with the argument
53
+ opt.on(*args) {|val| specify(cmd, val)}
54
+ end
55
+
56
+ private
57
+
58
+ # Return short option string for optparse.rb.
59
+ def short_for_optparse
60
+ (arg and long.nil?) ? ("%s %s" % [short, arg]) : short
61
+ end
62
+
63
+ # Return log option string for optparse.rb.
64
+ def long_for_optparse
65
+ (arg and not(long.nil?)) ? ("%s %s" % [long, arg]) : long
66
+ end
67
+
68
+ # Specify the option value.
69
+ #
70
+ # @param cmd [Command::PlainCommand]
71
+ # command object
72
+ # @param val [Object]
73
+ # option value
74
+ # @return [void]
75
+ def specify(cmd, val)
76
+ # set default value
77
+ if val.nil? and not(self.default.nil?)
78
+ val = self.default
79
+ end
80
+
81
+ # normalization
82
+ _val = Normalizer.normalize(type, val)
83
+
84
+ if range.nil? or range.include?(_val)
85
+ if processes.empty?
86
+ cmd.model.specify(key, _val)
87
+ else
88
+ execute(cmd, _val)
89
+ end
90
+ else
91
+ arg = {value: _val, range: range}
92
+ raise OptionError.new(cmd, '"%{value}" is out of %{range}' % arg)
93
+ end
94
+ end
95
+ end
96
+
97
+ # OptionCollection is a collection interface for options.
98
+ module OptionCollection
99
+ include CollectionInterface
100
+ set_item_class Option
101
+ end
102
+
103
+ # `OptionDefinition` is a class that holds command's option definitions.
104
+ class OptionDefinition < Sequence
105
+ include CollectionInterface
106
+ set_item_class Option
107
+
108
+ private
109
+
110
+ # Parse the command options.
111
+ #
112
+ # @param cmd [Command]
113
+ # command object
114
+ # @return [void]
115
+ def execute_main(cmd)
116
+ # append help option
117
+ _list = list.clone
118
+ _list << HelpOption.help
119
+
120
+ _list.each do |item|
121
+ # merge init values
122
+ if item.init
123
+ cmd.model[item.name] = Normalizer.normalize(item.type, item.init)
124
+ end
125
+ # set process context class
126
+ if item.process_context_class.nil?
127
+ item.process_context_class = get_process_context_class(cmd)
128
+ end
129
+ end
130
+
131
+ # parse options
132
+ OptionParser.new do |opt|
133
+ _list.each {|item| item.setup(opt, cmd)}
134
+ end.send(cmd.has_subcommands? ? :order! : :parse!, cmd.argv)
135
+
136
+ # check option's validness
137
+ check(cmd)
138
+ rescue OptionParser::ParseError, NormalizerValueError => e
139
+ raise OptionError.new(cmd, e.message)
140
+ end
141
+
142
+ # Check validness of the command options.
143
+ def check(cmd)
144
+ # check requisite options
145
+ list.each do |item|
146
+ if item.requisite and not(cmd.model[item.key])
147
+ raise OptionError.new(cmd, 'option "%s" is requisite' % [item.long])
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,39 @@
1
+
2
+ Scenario Result
3
+ ===============
4
+
5
+ Scenario : <%= scenario.is_a?(Rootage::Scenario) ? scenario.class : scenario %>
6
+ Arguments : <%= args %>
7
+ Result : <%= success? ? "scenario succeeded" : "scenario failed" %>
8
+ Time : <%= Time.now %>
9
+ <% if not(success?) %>
10
+
11
+ Error Message
12
+ -------------
13
+
14
+ <%= exception.message %>
15
+
16
+
17
+ Backtrance
18
+ ----------
19
+
20
+ <% exception.backtrace.each do |line| %>
21
+ <%= line %>
22
+
23
+ <% end %>
24
+ <% end %>
25
+ <% if stdout.string.size > 0 %>
26
+
27
+ Stdout
28
+ ------
29
+
30
+ <%= stdout.string %>
31
+ <% end %>
32
+ <% if stderr.string.size > 0 %>
33
+
34
+ Stderr
35
+ ------
36
+
37
+ <%= stderr.string %>
38
+ <% end %>
39
+
@@ -0,0 +1,362 @@
1
+ module Rootage
2
+ # ScenarioDefinition is an information table for Scenario class.
3
+ class ScenarioDefinition
4
+ # Create a new information table.
5
+ def initialize(table=Hash.new)
6
+ @table = table
7
+ self[:name] ||= "anonymous"
8
+ end
9
+
10
+ # define forwarding messages
11
+ forward :@table, :has_key?
12
+
13
+ # Define a scenario information that associates key and value.
14
+ #
15
+ # @param key [Symbol]
16
+ # key name
17
+ # @param val [Object]
18
+ # item's value
19
+ # @param args_block [Proc]
20
+ # value's arguments
21
+ # @return [void]
22
+ def define(key, val=nil, &block)
23
+ if val.nil? and not(block_given?)
24
+ raise ArgumentError.new("Cannot define %{key} with no data." % {key: key})
25
+ end
26
+ @table[key] = {value: val, block: block}
27
+ end
28
+
29
+ # Return the item value of the key.
30
+ #
31
+ # @param key [Object]
32
+ # the key
33
+ # @return [Object]
34
+ # the value
35
+ def value_of(key)
36
+ if @table.has_key?(key)
37
+ @table[key][:value]
38
+ end
39
+ end
40
+ alias :[] :value_of
41
+
42
+ # Return the item block of the key.
43
+ #
44
+ # @param key [Object]
45
+ # the key
46
+ # @return [Object]
47
+ # the block
48
+ def block_of(key)
49
+ if @table.has_key?(key)
50
+ @table[key][:block]
51
+ end
52
+ end
53
+
54
+ def []=(key, val)
55
+ define(key, val)
56
+ end
57
+
58
+ def scenario_name
59
+ self[:name]
60
+ end
61
+
62
+ def desc
63
+ self[:desc]
64
+ end
65
+
66
+ def clone
67
+ self.class.new(@table.clone)
68
+ end
69
+ end
70
+
71
+ # ScenarioInterface provides basic methods for Scenario.
72
+ module ScenarioInterface
73
+ def define(*args)
74
+ @info.define(*args)
75
+ end
76
+
77
+ def scenario_name
78
+ @info.scenario_name
79
+ end
80
+
81
+ def desc
82
+ @info.desc
83
+ end
84
+
85
+ # Return a process context class.
86
+ #
87
+ # @return [Class]
88
+ # process context class
89
+ def process_context_class
90
+ @info[:process_context_class]
91
+ end
92
+
93
+ # Return a phase class used for this scnario.
94
+ def phase_class
95
+ @info[:phase_class]
96
+ end
97
+
98
+ # Define a phase.
99
+ #
100
+ # @raise [ArgumentError]
101
+ # if there is same name phase already
102
+ # @param name [String]
103
+ # phase name
104
+ # @yieldparam phase [Phase]
105
+ # phase object
106
+ # @return [void]
107
+ def define_phase(name, &block)
108
+ if @__phase__.has_key?(name)
109
+ raise ArgumentError.new('Phase "%{name}" has been defined already.' % {name: name})
110
+ end
111
+
112
+ @__phase__[name] = phase_class.new(name)
113
+
114
+ if block_given?
115
+ block.call(@__phase__[name])
116
+ end
117
+ end
118
+
119
+ # Find a phase object by the name.
120
+ #
121
+ # @param name [Symbol]
122
+ # the phase name
123
+ # @return [Command::Phase]
124
+ # a phase object
125
+ def phase(name, &block)
126
+ unless @__phase__.has_key?(name)
127
+ raise ArgumentError.new('Unknown phase name: "%s"' % name)
128
+ end
129
+
130
+ if block_given?
131
+ block.call(@__phase__[name])
132
+ end
133
+
134
+ @__phase__[name]
135
+ end
136
+ end
137
+
138
+ # Scenario is a class that controls flows and handles sequencial actions.
139
+ class Scenario
140
+ # import basic methods for object
141
+ include ScenarioInterface
142
+
143
+ # initial settings
144
+ @requirements = Array.new
145
+ @info = ScenarioDefinition.new
146
+ @__phase__ = Hash.new
147
+
148
+ class << self
149
+ # import basic methods for class
150
+ include ScenarioInterface
151
+
152
+ attr_accessor :requirements
153
+ attr_accessor :info
154
+ attr_accessor :__phase__
155
+
156
+ def inherited(subclass)
157
+ # subclass inherits superclass's requirements
158
+ subclass.requirements = @requirements.clone
159
+
160
+ # subclass inherits superclass's info
161
+ subclass.info = @info.clone
162
+
163
+ # subclass inherits superclass's phases
164
+ subclass.__phase__ = Hash.new
165
+ @__phase__.each do |key, val|
166
+ subclass.__phase__[key] = val.copy
167
+ end
168
+ end
169
+
170
+ def scenario_name
171
+ @info[:name]
172
+ end
173
+
174
+ # Make a new scenario class.
175
+ #
176
+ # @yieldparam block [Class]
177
+ # a new scenario class
178
+ def make(&block)
179
+ klass = Class.new(self)
180
+ klass.instance_exec(&block)
181
+ return klass
182
+ end
183
+
184
+ # Define a requirement of library. This is same as Kernel.require, but
185
+ # loads it when the scenario runs.
186
+ #
187
+ # @param path [String]
188
+ # path that is required when the scenario runs
189
+ # @return [void]
190
+ def require(path)
191
+ @requirements << path
192
+ end
193
+
194
+ # Run the scenario with the arguments.
195
+ #
196
+ # @param args [Array<Object>]
197
+ # scenario arguments
198
+ def run(*args)
199
+ self.new(*args).run
200
+ end
201
+
202
+ # Register the action to the phase.
203
+ #
204
+ # @param phase_name [Symbol]
205
+ # phase name
206
+ # @param action [Symbol or Action]
207
+ # action name or item
208
+ def define_action(phase_name, action, &b)
209
+ # setup action item
210
+ case action
211
+ when Symbol
212
+ item = Action.new(name: action)
213
+ when Action
214
+ item = action.copy
215
+ else
216
+ raise ArgumentError.new("%p is invalid action" % action)
217
+ end
218
+
219
+ # action customization
220
+ b.call(item) if b
221
+
222
+ # append it to the phase
223
+ @__phase__[phase_name].define(item)
224
+ end
225
+ end
226
+
227
+ # define default phase class
228
+ define(:phase_class, Phase)
229
+
230
+ # define default process context class
231
+ define(:process_context_class, ProcessContext)
232
+
233
+ attr_reader :args
234
+ attr_accessor :model
235
+ attr_accessor :info
236
+ attr_reader :current_phase
237
+ attr_accessor :exit_status
238
+ attr_reader :running_thread
239
+
240
+ forward! :class, :desc
241
+ alias :name :scenario_name
242
+
243
+ # Initialize a scenario object.
244
+ def initialize(*args)
245
+ # hold scenario arguments
246
+ @args = args
247
+
248
+ # copy requirements, info, phases from the class
249
+ @requirements = self.class.requirements.clone
250
+ @info = self.class.instance_variable_get(:@info).clone
251
+ @__phase__ = self.class.instance_variable_get(:@__phase__).clone
252
+
253
+ # setup scenario model
254
+ if self.class.info[:model]
255
+ @model = self.class.info[:model].new
256
+ else
257
+ @model = Model.new
258
+ end
259
+ @model[:scenario_name] = @info.scenario_name
260
+ @model[:scenario_desc] = @info.desc
261
+
262
+ # init
263
+ @current_phase = nil
264
+ @exit_status = true
265
+ @running_thread = nil
266
+ end
267
+
268
+ # Run the lifecycle of command. This fires sequencial actions in each phase.
269
+ #
270
+ # @return [void]
271
+ def run
272
+ # raise an error when running thread exists already
273
+ if @running_thread
274
+ raise ScenarioError.new("Running thread exists already.")
275
+ end
276
+
277
+ @running_thread = Thread.current
278
+ load_requirements
279
+ execute_phases
280
+ @running_thread = nil
281
+ return self
282
+ end
283
+
284
+ # Push the phase action to the scenario.
285
+ #
286
+ # @param object [Action]
287
+ # action
288
+ # @return [void]
289
+ def <<(object)
290
+ if @__phase__.empty?
291
+ phase_name = :default
292
+ define_phase(phase_name)
293
+ else
294
+ phase_name = @__phase__.keys.last
295
+ end
296
+ phase(phase_name) << object
297
+ end
298
+
299
+ # Exit running scenario and return the status.
300
+ #
301
+ # @return [void]
302
+ def exit
303
+ Log.debug('"%{name}" exits with status "%{status}".' % {name: name, status: @exit_status})
304
+
305
+ if respond_to?(:exit_process, true)
306
+ exit_process
307
+ end
308
+ end
309
+
310
+ # Exit the running command and return failure status. Note that this
311
+ # method enters termination phase before it exits.
312
+ def abort(msg_or_exception, pos=caller(1).first)
313
+ # setup abortion message
314
+ msg = msg_or_exception.is_a?(Exception) ? msg_or_exception.message : msg_or_exception
315
+ Log.fatal(msg, pos)
316
+
317
+ # set exit status code
318
+ @exit_status = false
319
+
320
+ # do abort process
321
+ if respond_to?(:abort_process, true)
322
+ abort_process
323
+ end
324
+
325
+ # finally, exit the command
326
+ exit
327
+ end
328
+
329
+ private
330
+
331
+ # Load additional modules.
332
+ #
333
+ # @return [void]
334
+ def load_requirements
335
+ @requirements.each do |requirement|
336
+ Kernel.require(requirement)
337
+ end
338
+ end
339
+
340
+ # Enter the phase.
341
+ #
342
+ # @param phase_name [Symbol]
343
+ # the phase name that is entered
344
+ # @return [void]
345
+ def enter_phase(phase_name)
346
+ Log.debug('"%{cmd}" has entered the phase "%{phase}".' % {cmd: name, phase: phase_name})
347
+ @__phase__[phase_name].execute(self)
348
+ Log.debug('"%{cmd}" has exited the phase "%{phase}".' % {cmd: name, phase: phase_name})
349
+ end
350
+
351
+ # Execute phases.
352
+ #
353
+ # @return [void]
354
+ def execute_phases
355
+ @__phase__.each do |phase_name, phase|
356
+ @current_phase = phase
357
+ enter_phase(phase_name)
358
+ end
359
+ @current_phase = nil
360
+ end
361
+ end
362
+ end