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
@@ -3,6 +3,8 @@ module Pione
3
3
  class DataLocation < BasicLocation
4
4
  location_type :data
5
5
 
6
+ KNOWN_ATTRS = [:need_caching, :real_appendable, :writable]
7
+
6
8
  class << self
7
9
  # @return [String]
8
10
  # location's scheme name
@@ -17,16 +19,27 @@ module Pione
17
19
  SCHEMES[name] = self
18
20
  end
19
21
 
20
- def set_real_appendable(b)
21
- @appendable = b
22
+
23
+ # Define a location's attribute.
24
+ #
25
+ # - need_caching : whether the location needs to be cached or not
26
+ # - real_appendable : whether the location can appendable or not
27
+ # - writable : whether the location is writable or not
28
+ def define(name, val)
29
+ if DataLocation::KNOWN_ATTRS.include?(name)
30
+ (@attr ||= Hash.new)[name] = val
31
+ else
32
+ raise ArgumentError.new(name)
33
+ end
22
34
  end
23
35
 
24
- def real_appendable?
25
- @appendable
36
+ # Return true if the location needs caching.
37
+ def need_caching?
38
+ @attr[:need_caching]
26
39
  end
27
40
 
28
- def set_writable(b)
29
- @writable = b
41
+ def real_appendable?
42
+ @attr[:real_appendable]
30
43
  end
31
44
 
32
45
  def writable?
@@ -34,7 +47,7 @@ module Pione
34
47
  end
35
48
  end
36
49
 
37
- forward! :class, :scheme, :real_appendable?, :writable?
50
+ forward! :class, :scheme, :real_appendable?, :writable?, :need_caching?
38
51
  forward :@uri, :host
39
52
 
40
53
  # @return [URI]
@@ -62,7 +75,7 @@ module Pione
62
75
  if scheme == "local"
63
76
  self
64
77
  else
65
- Location[Temppath.create].tap {|tmp| copy(tmp)}
78
+ Location[Temppath.create].tap {|tmp| copy(tmp) if exist?}
66
79
  end
67
80
  end
68
81
 
@@ -164,7 +177,13 @@ module Pione
164
177
  # data content
165
178
  # @return [void]
166
179
  def append(data)
167
- raise NotImplmentedError
180
+ if real_appendable?
181
+ raise NotImplmentedError
182
+ else
183
+ _local = local
184
+ _local.append(data)
185
+ _local.copy(self)
186
+ end
168
187
  end
169
188
 
170
189
  # Read location data.
@@ -264,22 +283,31 @@ module Pione
264
283
  raise NotImplementedError
265
284
  end
266
285
 
267
- # Return true if data in the location is a file.
286
+ # Return true if data at the location is a file. When there exists no
287
+ # files and no directories, then return false.
268
288
  #
269
289
  # @return [Boolean]
270
- # true if data in the location is a file
290
+ # true if data at the location is a file
271
291
  def file?
272
292
  raise NotImplementedError
273
293
  end
274
294
 
275
- # Return true if data in the location is a directory.
295
+ # Return true if data at the location is a directory. When there exists no
296
+ # files and no direcotries, then return false.
276
297
  #
277
298
  # @return [Boolean]
278
- # true if data in the location is a directory
299
+ # true if data at the location is a directory
279
300
  def directory?
280
301
  raise NotImplementedError
281
302
  end
282
303
 
304
+ # Make the path a directory.
305
+ #
306
+ # @return [void]
307
+ def mkdir
308
+ raise NotImplementedError
309
+ end
310
+
283
311
  # Move to the destination.
284
312
  #
285
313
  # @param dest [BasicLocation]
@@ -4,99 +4,183 @@ module Pione
4
4
  class DropboxLocation < DataLocation
5
5
  set_scheme "dropbox"
6
6
 
7
+ define(:need_caching, true)
8
+ define(:real_appendable, false)
9
+ define(:writable, true)
10
+
7
11
  class << self
8
- attr_reader :session
12
+ attr_reader :client
9
13
 
10
- # Initialize dropbox settings.
14
+ # Return true if Dropbox's access token cache exists.
11
15
  #
12
- # @param tuple_space_server [TupleSpaceServer]
13
- # tuple space server
16
+ # @return [Boolean]
17
+ # true if Dropbox's access token cache exists
18
+ def cached?
19
+ cache = Pathname.new("~/.pione/dropbox_api.cache").expand_path
20
+ return (cache.exist? and cache.read.size > 0)
21
+ end
22
+
23
+ # Setup Dropbox location for CUI client. This method gets Dropbox's
24
+ # access token from cache file or OAuth2.
25
+ #
26
+ # @param tuple_space [TupleSpaceServer]
27
+ # tuple space
14
28
  # @return [void]
15
- def self.init(tuple_space_server)
16
- tuple_consumer_key = TupleSapce::AttributeTuple.new("dropbox_consumer_key", nil)
17
- tuple_consumer_secret = TupleSpace::AttributeTuple.new("dropbox_consumer_secret", nil)
18
- tuple_access_token_key = TupleSpace::AttributeTuple.new("dropbox_access_token_key", nil)
19
- tuple_access_token_secret = TupleSpace::AttributeTuple.new("dropbox_access_token_secret", nil)
29
+ def setup_for_cui_client(tuple_space)
30
+ return if @client
20
31
 
21
- consumer_key = tuple_space_server.read(tuple_consumer_key, 0).value rescue nil
22
- consumer_secret = tuple_space_server.read(tuple_consumer_secret, 0).value rescue nil
23
- access_token_key = tuple_space_server.read(tuple_access_token_key, 0).value rescue nil
24
- access_token_secret = tuple_space_server.read(tuple_access_token_secret, 0).value rescue nil
32
+ access_token = nil
33
+ cache = Pathname.new("~/.pione/dropbox_api.cache").expand_path
34
+
35
+ if cache.exist?
36
+ # load access token from cache file
37
+ access_token = cache.read
38
+ else
39
+ # get access token by OAtuh2
40
+ setup_consumer_key_and_secret
41
+ flow = auth_by_oauth2flow_no_redirect
42
+ access_token, user_id = get_code_for_cui_client(flow)
43
+
44
+ # cache session
45
+ cache.open("w+") {|c| c.write access_token}
46
+ end
47
+
48
+ # make a client
49
+ @client = DropboxClient.new(access_token)
50
+
51
+ # share access token in tuple space
52
+ share_access_token(tuple_space, access_token)
53
+ end
25
54
 
26
- @session = DropboxSession.new(consumer_key, consumer_secret)
27
- @session.set_access_token(access_token_key, access_token_secret)
55
+ # Enable Dropbox locations. This method get an access token from the
56
+ # tuple space and make a Dropbox client. This assumes to be called from
57
+ # task worker agents.
58
+ #
59
+ # @param tuple_space_server [TupleSpaceServer]
60
+ # tuple space server
61
+ # @return [void]
62
+ def enable(tuple_space)
63
+ tuple = TupleSpace::AttributeTuple.new("dropbox_access_token", nil)
64
+ if tuple_access_token = tuple_space.read!(tuple)
65
+ @client = DropboxClient.new(tuple_access_token.value)
66
+ else
67
+ raise DropboxLocationUnavailable.new("There is no access token.")
68
+ end
28
69
  end
29
70
 
30
71
  def rebuild(path)
31
72
  Location["dropbox:%s" % path]
32
73
  end
33
74
 
34
- # Share dropbox's access token with PIONE agents.
75
+ # Setup Dropbox's consumer key and secret. They are loaded from a YAML
76
+ # file "dropbox_api.yml" at PIONE's home directory.
35
77
  #
36
- # @param tuple_space_server [TupleSpaceServer]
37
- # tuple space server
38
- # @param consumer_key [String]
39
- # consumer key
40
- # @param consumer_secret [String]
41
- # consumer secret
42
78
  # @return [void]
43
- def share_access_token(tuple_space_server, consumer_key, consumer_secret)
44
- access_token = session.get_access_token
45
- [ TupleSpace::AttributeTuple.new("dropbox_consumer_key", consumer_key),
46
- TupleSpace::AttributeTuple.new("dropbox_consumer_secret", consumer_secret),
47
- TupleSpace::AttributeTuple.new("dropbox_access_token_key", access_token.key),
48
- TupleSpace::AttributeTuple.new("dropbox_access_token_secret", access_token.secret)
49
- ].each {|tuple| tuple_space_server.write(tuple) }
79
+ def setup_consumer_key_and_secret
80
+ path = Pathname.new("~/.pione/dropbox_api.yml").expand_path
81
+ if File.exist?(path)
82
+ api = YAML.load(path.read)
83
+ @consumer_key = api["key"]
84
+ @consumer_secret = api["secret"]
85
+ else
86
+ raise DropboxLocationUnavailable.new("There are no consumer key and consumer secret.")
87
+ end
50
88
  end
51
89
 
52
- # Return true if dropbox session is authorized.
90
+ # Authorize dropbox account by `DropboxOAuth2FlowNoRedirect`.
53
91
  #
54
- # @return [Boolean]
55
- # true if dropbox session is authorized
56
- def ready?
57
- @session.authorized?
92
+ # @return [String]
93
+ # authorize URL
94
+ def auth_by_oauth2flow_no_redirect
95
+ if @consumer_key and @consumer_secret
96
+ return DropboxOAuth2FlowNoRedirect.new(@consumer_key, @consumer_secret)
97
+ else
98
+ raise DropboxLocationUnavailable.new("There are no consumer key and consumer secret.")
99
+ end
58
100
  end
59
101
 
60
- # Set the session.
102
+ # Authorize dropbox account by `DropboxOAuth2Flow`.
61
103
  #
62
- # @param session [String]
63
- # dropbox session
104
+ # @param redirect [String]
105
+ # redirect URL
106
+ # @param session [Hash]
107
+ # session
108
+ # @return [String]
109
+ # authorize URL
110
+ def auth_by_oauth2flow(redirect, session)
111
+ if @consumer_key and @consumer_secret
112
+ return DropboxOAuth2Flow.new(@consumer_key, @consumer_secret, redirect, session, :dropbox_auth_csrf_token)
113
+ else
114
+ raise DropboxLocationUnavailabel.new("There are no consumer key and consumer secret.")
115
+ end
116
+ end
117
+
118
+ # Share dropbox's access token with PIONE agents.
119
+ #
120
+ # @param tuple_space_server [TupleSpaceServer]
121
+ # tuple space server
122
+ # @param access_token [String]
123
+ # access token
64
124
  # @return [void]
65
- def self.set_session(session)
66
- @session = session
125
+ def share_access_token(tuple_space_server, access_token)
126
+ tuple = TupleSpace::AttributeTuple.new("dropbox_access_token", access_token)
127
+ tuple_space_server.write(tuple)
128
+ end
129
+
130
+ # Get code for CUI client.
131
+ #
132
+ # @return [Array]
133
+ # access token and Dropbox user ID
134
+ def get_code_for_cui_client(flow)
135
+ puts '1. Go to: %s' % flow.start
136
+ puts '2. Click "Allow"'
137
+ puts '3. Copy the authorization code'
138
+ print 'Enter the authorization code here: '
139
+ code = STDIN.gets.strip
140
+ flow.finish(code)
67
141
  end
68
142
  end
69
143
 
70
144
  def initialize(uri)
71
145
  super(uri)
72
- @client = DropboxClient.new(self.class.session, "app_folder")
73
146
  end
74
147
 
75
148
  def create(data)
76
- @client.put_file(@path, StringIO.new(data))
149
+ if exist?
150
+ raise ExistAlready.new(self)
151
+ else
152
+ client.put_file(@path.to_s, StringIO.new(data))
153
+ end
77
154
  return self
78
155
  end
79
156
 
80
157
  def read
81
- @client.get_file(@path)
158
+ exist? ? client.get_file(@path.to_s) : (raise NotFound.new(self))
82
159
  end
83
160
 
84
161
  def update(data)
85
- @client.put_file(@path, StringIO.new(data), true)
162
+ client.put_file(@path.to_s, StringIO.new(data), true)
86
163
  return self
87
164
  end
88
165
 
89
166
  def delete
90
- @client.file_delete(@path)
167
+ if exist?
168
+ client.file_delete(@path.to_s)
169
+ end
91
170
  end
92
171
 
93
172
  # dropbox have "modified" time only, therefore ctime is not implemented
94
173
 
95
174
  def mtime
96
- metadata = @client.metadata(@path)
175
+ metadata = client.metadata(@path.to_s)
97
176
  Time.parse(metadata["modified"])
98
177
  end
99
178
 
179
+ def size
180
+ metadata = client.metadata(@path.to_s)
181
+ return metadata["bytes"]
182
+ end
183
+
100
184
  def entries(option={})
101
185
  rel_entries(option).map {|entry| rebuild(@path + entry)}
102
186
  end
@@ -104,10 +188,11 @@ module Pione
104
188
  def rel_entries(option={})
105
189
  list = []
106
190
  raise NotFound.new(self) if not(directory?)
107
- metadata = @client.metadata(@path)
191
+
192
+ metadata = client.metadata(@path.to_s)
108
193
  metadata["contents"].select{|entry| not(entry["is_deleted"])}.each do |entry|
109
- list << entry["path"][0, @path.size]
110
- entry_location = rebuild(@path + entry)
194
+ list << entry["path"].sub(@path.to_s, "").sub(/^\//, "")
195
+ entry_location = rebuild(@path + File.basename(entry["path"]))
111
196
  if option[:rec] and entry_location.directory?
112
197
  _list = entry_location.rel_entries(option).map {|subentry| entry + subentry}
113
198
  list = list + _list
@@ -117,25 +202,43 @@ module Pione
117
202
  end
118
203
 
119
204
  def exist?
120
- metadata = @client.metadata(@path)
205
+ metadata = client.metadata(@path.to_s)
121
206
  return not(metadata["is_deleted"])
207
+ rescue DropboxLocationUnavailable
208
+ raise
122
209
  rescue DropboxError
123
210
  return false
124
211
  end
125
212
 
126
213
  def file?
127
- metadata = @client.metadata(@path)
128
- return (not(metadata["is_dir"]) and not(metadata["is_deleted"]))
214
+ begin
215
+ metadata = client.metadata(@path.to_s)
216
+ return (not(metadata["is_dir"]) and not(metadata["is_deleted"]))
217
+ rescue DropboxError
218
+ # when there exists no files and no directories
219
+ false
220
+ end
129
221
  end
130
222
 
131
223
  def directory?
132
- metadata = @client.metadata(@path)
133
- return (metadata["is_dir"] and not(metadata["is_deleted"]))
224
+ begin
225
+ metadata = client.metadata(@path.to_s)
226
+ return (metadata["is_dir"] and not(metadata["is_deleted"]))
227
+ rescue DropboxError
228
+ # when there exists no files and no directories
229
+ false
230
+ end
231
+ end
232
+
233
+ def mkdir
234
+ unless exist?
235
+ client.file_create_folder(@path.to_s)
236
+ end
134
237
  end
135
238
 
136
239
  def move(dest)
137
240
  if dest.scheme == scheme
138
- @client.file_move(@path, dest.path)
241
+ client.file_move(@path.to_s, dest.path)
139
242
  else
140
243
  copy(dest)
141
244
  delete
@@ -143,10 +246,12 @@ module Pione
143
246
  end
144
247
 
145
248
  def copy(dest)
249
+ raise NotFound.new(self) unless exist?
250
+
146
251
  if dest.scheme == scheme
147
- @client.file_copy(@path, dest.path)
252
+ client.file_copy(@path.to_s, dest.path)
148
253
  else
149
- dest.update(read)
254
+ dest.write(read)
150
255
  end
151
256
  end
152
257
 
@@ -161,6 +266,20 @@ module Pione
161
266
  def turn(dest)
162
267
  copy(dest)
163
268
  end
269
+
270
+ private
271
+
272
+ # Check availability of Dropbox's access token.
273
+ #
274
+ # @return [void]
275
+ def client
276
+ if self.class.client.nil?
277
+ # raise an exception when Dropbox client isn't enabled
278
+ raise DropboxLocationUnavailable.new("There is no access token.")
279
+ else
280
+ self.class.client
281
+ end
282
+ end
164
283
  end
165
284
  end
166
285
  end
@@ -3,7 +3,10 @@ module Pione
3
3
  # FTPLocation represents locations on FTP server.
4
4
  class FTPLocation < DataLocation
5
5
  set_scheme "ftp"
6
- set_real_appendable false
6
+
7
+ define(:need_caching, true)
8
+ define(:real_appendable, false)
9
+ define(:writable, true)
7
10
 
8
11
  # for myftp scheme
9
12
  SCHEMES["myftp"] = self
@@ -2,8 +2,10 @@ module Pione
2
2
  module Location
3
3
  class HTTPLocation < DataLocation
4
4
  set_scheme "http"
5
- set_real_appendable false
6
- set_writable false
5
+
6
+ define(:need_caching, true)
7
+ define(:real_appendable, false)
8
+ define(:writable, false)
7
9
 
8
10
  def read
9
11
  http_get {|res| res.body}
@@ -33,7 +35,7 @@ module Pione
33
35
 
34
36
  def copy(dest, option={})
35
37
  # setup options
36
- option[:keep_mtime] ||= true
38
+ option[:keep_mtime] = true if option[:keep_mtime].nil?
37
39
 
38
40
  # copy
39
41
  http_get {|rec| dest.write rec.body}