magnoline 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,220 @@
1
+ # Magnoline, Command line tool for the magnolia CMS
2
+ #Copyright (C) 2007 Nicolas Modrzyk
3
+ #
4
+ # This library is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU Lesser General Public
6
+ # License as published by the Free Software Foundation; either
7
+ # version 2.1 of the License, or (at your option) any later version.
8
+ #
9
+ #This library is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # Lesser General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Lesser General Public
15
+ # License along with this library; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
+ #
18
+ # Nicolas Modrzyk hellonico at gmail dot com
19
+ #
20
+ # Things learn in this code:
21
+ # 1. Command line parsing
22
+ # 2. Multipart HTTP POST
23
+
24
+ require 'rubygems'
25
+ require 'cooloptions'
26
+ require 'net/http'
27
+ require 'ping'
28
+ require 'http-access2'
29
+ require 'yaml'
30
+
31
+
32
+ include Net
33
+
34
+ #
35
+ # Simple client to import/export content of a magnolia repository.
36
+ # This is using the import/export servlet accessible in magnolia
37
+ module MagnoliaClient
38
+
39
+ #
40
+ # Parse the arguments to instanciate the proper ImportExport object
41
+ class ImportExportSetUp
42
+
43
+ CMD_IMPORT = "import"
44
+ CMD_EXPORT = "export"
45
+ IMPORT_BEHAVIORS = [:new,:remove,:replace]
46
+
47
+ #
48
+ # this method does the parsing of the arguments. Validates them
49
+ # return the ImportExport object
50
+ def parse(argv)
51
+ options = CoolOptions.parse!('[options]') do |opt|
52
+ opt.on 'action STRING','one of import/export'
53
+ opt.on 'out STRING','path to directory where to put the content of the answer', ""
54
+ opt.on 'repository-path STRING','path on the repository to take into account', "/help"
55
+ opt.on 'workspace STRING',' the repository/workspace to take into account', "website"
56
+ opt.on 'user STRING','user name to authenticate with', 'superuser'
57
+ opt.on 'password STRING','password', 'superuser'
58
+ opt.on 'import-file STRING','path to the xml file to import', ''
59
+ opt.on 'server-url STRING','base url of the magnolia service', 'http://localhost:8080/magnoliaAuthor'
60
+ opt.on 'behavior STRING', 'import behavior. One of new/remove/replace or 0/1/2', "0"
61
+ opt.on 'verbose', 'give some more processing info on the command line', false
62
+ opt.on 'console', 'output to standard output, bypass the out option', false
63
+
64
+ opt.after do |r|
65
+ r.out = File.expand_path(r.out)
66
+ opt.error("Invalid action:"+r.action) unless (r.action == CMD_IMPORT || r.action == CMD_EXPORT)
67
+ host = URI.parse(r.server_url).host
68
+ opt.error("host is not reachable:" + host) unless Ping.pingecho(host,10)
69
+ r.behavior = parse_behavior(r.behavior) if r.action == CMD_IMPORT
70
+
71
+ end
72
+ end
73
+
74
+ return Export.new(options) if options.action == CMD_EXPORT
75
+ return Import.new(options) if options.action == CMD_IMPORT
76
+
77
+ end
78
+
79
+ #
80
+ # Parse the import behavior, from an integer or a string
81
+ # Do this while parsing to get proper error output when needed
82
+ def parse_behavior(value)
83
+ IMPORT_BEHAVIORS.each_with_index { |behavior, index|
84
+ return index if value.to_i == index
85
+ return index if value.to_s == behavior.to_s
86
+ }
87
+ raise "Invalid behavior:#{value}"
88
+ end
89
+ end
90
+
91
+ #
92
+ # Class that does the actual import export
93
+ class ImportExportBase
94
+
95
+ MAGNOLIA_BASE_URL = "/.magnolia/pages/"
96
+ MAGNOLIA_IMPORT_URL = MAGNOLIA_BASE_URL + "import.html"
97
+ MAGNOLIA_EXPORT_URL = MAGNOLIA_BASE_URL + "export.html"
98
+
99
+ #
100
+ # No checking of options, this is done in the parser
101
+ # assign values from the hash to attributes
102
+ def initialize(options)
103
+ @console = options.console
104
+ @action = options.action
105
+ @server_url = options.server_url
106
+ @repository_path = options.repository_path
107
+ @workspace = options.workspace
108
+ @user = options.user
109
+ @password = options.password
110
+ @out = File.expand_path(options.out+'/'+@workspace+@repository_path.gsub('/','.')+'.xml')
111
+
112
+ @verbose = options.verbose
113
+ @options = options
114
+ end
115
+
116
+ #
117
+ # prepare form data. Should return a hash with the needed parameters
118
+ # this has to be implemented in the subclasses
119
+ def prepare_form_data
120
+ raise "Sub classing class need to implement this method"
121
+ end
122
+
123
+ #
124
+ # trying to fix a bug in http-access2 where all the parameters values are loosing their last char
125
+ # can't find a reason for that.
126
+ def pad_data(data)
127
+ data.each { |key,value| begin
128
+ if not key==:mgnlFileImport
129
+ value = value + " ";
130
+ data[key] = value.to_s
131
+ end
132
+ rescue
133
+ end
134
+ }
135
+ end
136
+
137
+ #
138
+ # main method
139
+ # this create the necessary http client to simulate interaction and just post the
140
+ # data collected from subclasses to the form.
141
+ def exec
142
+ client = HTTPAccess2::Client.new
143
+ boundary = Array::new(8){ "%2.2d" % rand(42) }.join('__')
144
+ extheader = {'content-type' => "multipart/form-data; boundary=___#{ boundary }___"}
145
+ client.set_basic_auth(@server_url, @user, @password)
146
+
147
+ form_data = pad_data(prepare_form_data)
148
+
149
+ verbose(form_data) if @verbose
150
+
151
+ res = client.post_content(@full_url, form_data, extheader)
152
+
153
+ if @console
154
+ puts res
155
+ else
156
+ f = File.open(@out,'w')
157
+ f.puts res
158
+ f.close
159
+ puts "Output of action "+@action+" has been written to "+@out if @verbose
160
+ end
161
+ end
162
+
163
+ #
164
+ # Verbose output before action
165
+ def verbose(form_data)
166
+ puts "------------ \tOptions data\t ---------------"
167
+ puts @options.to_yaml
168
+ puts "------------ \tPost data\t\t ---------------"
169
+ puts form_data.to_yaml
170
+ puts "------------ \tEnd data\t\t ---------------"
171
+ end
172
+
173
+ end
174
+
175
+ #
176
+ # Class responsible for import specific code
177
+ class Import < ImportExportBase
178
+
179
+ def initialize(options)
180
+ super(options)
181
+ @action = "importxml "
182
+ @full_url = @server_url + MAGNOLIA_IMPORT_URL
183
+ @import_file = options.import_file
184
+ end
185
+
186
+ def prepare_form_data
187
+ form = {
188
+ :mgnlFileImport=>open(@import_file),
189
+ :mgnlRepository=>@workspace,
190
+ :mgnlPath=>@repository_path,
191
+ :mgnlKeepVersions=>false,
192
+ :mgnlFormat=>false,
193
+ :command=>@action
194
+ }
195
+ end
196
+
197
+ end
198
+
199
+ #
200
+ # Class responsible for export specific code
201
+ class Export < ImportExportBase
202
+
203
+ def initialize(options)
204
+ super(options)
205
+ @action = "exportxml"
206
+ @full_url = @server_url + MAGNOLIA_EXPORT_URL
207
+ end
208
+
209
+ def prepare_form_data
210
+ post_data = {
211
+ :mgnlRepository=>@workspace,
212
+ :mgnlPath=>@repository_path,
213
+ :command=>@action,
214
+ :mgnlKeepVersions=>false,
215
+ :mgnlFormat=>false,
216
+ :ext=>'.xml'
217
+ }
218
+ end
219
+ end
220
+ end
@@ -0,0 +1,39 @@
1
+ # Magnoline, Command line tool for the magnolia CMS
2
+ #Copyright (C) 2007 Nicolas Modrzyk
3
+ #
4
+ # This library is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU Lesser General Public
6
+ # License as published by the Free Software Foundation; either
7
+ # version 2.1 of the License, or (at your option) any later version.
8
+ #
9
+ #This library is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # Lesser General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Lesser General Public
15
+ # License along with this library; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
+ #
18
+ # Nicolas Modrzyk hellonico at gmail dot com
19
+ #
20
+
21
+ require 'magnolia_i_e'
22
+
23
+ #
24
+ # Main method: using --help on the command line gives the example usages
25
+
26
+ # Create command line parser
27
+ ies = MagnoliaClient::ImportExportSetUp.new
28
+
29
+ # parse the parameters
30
+ t1 = Time.new
31
+ ie = ies.parse(ARGV)
32
+ t2 = Time.new
33
+ puts "parse took:"+ (t2 - t1).to_s
34
+
35
+ # execute the command
36
+ t3 = Time.new
37
+ ie.exec
38
+ t4 = Time.new
39
+ puts "exec took:"+ (t4 - t3).to_s
@@ -0,0 +1,12 @@
1
+ require 'test/unit'
2
+
3
+ class ParsingTest < Test::Unit::TestCase
4
+
5
+ #def setup
6
+ #end
7
+
8
+ #def teardown
9
+ #end
10
+
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: magnoline
5
+ version: !ruby/object:Gem::Version
6
+ version: "0.1"
7
+ date: 2007-01-24 00:00:00 +09:00
8
+ summary: A command line interface to the Magnolia CMS
9
+ require_paths:
10
+ - lib
11
+ email: hellonico at gmail.com
12
+ homepage: http://rubyforge.org/projects/magnoline
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: magnoline
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Nicolas Modrzyk
31
+ files:
32
+ - lib/main_client.rb
33
+ - lib/magnolia_i_e.rb
34
+ - lib/http-access2
35
+ - lib/http-access2.rb
36
+ - lib/http-access2/http.rb
37
+ - lib/http-access2/cookie.rb
38
+ - test/testsuite.rb
39
+ - README
40
+ test_files:
41
+ - test/testsuite.rb
42
+ rdoc_options: []
43
+
44
+ extra_rdoc_files:
45
+ - README
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ requirements:
51
+ - cooloptions
52
+ dependencies:
53
+ - !ruby/object:Gem::Dependency
54
+ name: cooloptions
55
+ version_requirement:
56
+ version_requirements: !ruby/object:Gem::Version::Requirement
57
+ requirements:
58
+ - - ">"
59
+ - !ruby/object:Gem::Version
60
+ version: 0.0.0
61
+ version: