wire-framework 0.1.4.26 → 0.1.5

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.
data/lib/app/repo/svn.rb CHANGED
@@ -1,185 +1,211 @@
1
- require_relative '../repo'
1
+ ##
2
+ # Copyright 2017 Bryan T. Meyers
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ##
16
+
2
17
  require 'nori'
3
18
  require 'fileutils'
19
+ require_relative '../repo'
4
20
 
5
21
  # Force Nori to convert tag names to Symbols
6
22
  $nori = Nori.new :convert_tags_to => lambda { |tag| tag.snakecase.to_sym }
7
23
 
8
24
  module Repo
9
- # Repo::SVN is a connector for svnserve
10
- # @author Bryan T. Meyers
11
- module SVN
12
- extend Wire::App
13
- extend Wire::Resource
14
- extend Repo
15
-
16
- # Make a new SVN repo
17
- # @param [String] path the path to the repositories
18
- # @param [String] repo the new repo name
19
- # @return [Integer] status code
20
- def self.do_create_file(path, repo)
21
- result = 200
22
- `svnadmin create #{path}/#{repo}`
23
- if $?.exitstatus != 0
24
- return 500
25
- end
26
-
27
- if $?.exitstatus != 0
28
- 500
29
- else
30
- result
31
- end
32
- end
33
-
34
- # Read a single file
35
- # @param [String] rev the revision number to access
36
- # @param [String] web the subdirectory for web content
37
- # @param [String] path the path to the repositories
38
- # @param [String] repo the new repo name
39
- # @param [String] id the relative path to the file
40
- # @return [String] the file
41
- def self.do_read_file(rev, web, path, repo, id)
42
- options = "--username #{$environment[:repos_user]} --password #{$environment[:repos_password]}"
43
- if rev.nil?
44
- rev = 'HEAD'
45
- end
46
- if web.nil?
47
- body = `svn cat #{options} -r #{rev} 'svn://localhost/#{repo}/#{id}'`
48
- else
49
- body = `svn cat #{options} -r #{rev} 'svn://localhost/#{repo}/#{web}/#{id}'`
50
- end
51
-
52
- if $?.success?
53
- body
54
- else
55
- 500
56
- end
57
- end
58
-
59
- # Read a directory listing
60
- # @param [String] web the subdirectory for web content
61
- # @param [String] path the path to the repositories
62
- # @param [String] repo the new repo name
63
- # @param [String] id the relative path to the file
64
- # @return [Array] the directory listing
65
- def self.do_read_listing(web, path, repo, id = nil)
66
- options = "--username #{$environment[:repos_user]} --password #{$environment[:repos_password]}"
67
- if web.nil?
68
- if id.nil?
69
- list = `svn list #{options} --xml 'svn://localhost/#{repo}'`
70
- else
71
- list = `svn list #{options} --xml 'svn://localhost/#{repo}/#{id}'`
72
- end
73
- else
74
- if id.nil?
75
- list = `svn list #{options} --xml 'svn://localhost/#{repo}/#{web}'`
76
- else
77
- list = `svn list #{options} --xml 'svn://localhost/#{repo}/#{web}/#{id}'`
78
- end
79
- end
80
- unless $?.exitstatus == 0
81
- return 404
82
- end
83
- list = $nori.parse(list)
84
- list[:lists][:list][:entry]
85
- end
86
-
87
- # Read Metadata for a single file
88
- # @param [String] rev the revision number to access
89
- # @param [String] web the subdirectory for web content
90
- # @param [String] path the path to the repositories
91
- # @param [String] repo the new repo name
92
- # @param [String] id the relative path to the file
93
- # @return [Hash] the metadata
94
- def self.do_read_info(rev, web, path, repo, id)
95
- options = "--username #{$environment[:repos_user]} --password #{$environment[:repos_password]}"
96
- if rev.nil?
97
- rev = 'HEAD'
98
- end
99
- if web.nil?
100
- info = `svn info #{options} -r #{rev} --xml 'svn://localhost/#{repo}/#{id}'`
101
- else
102
- info = `svn info #{options} -r #{rev} --xml 'svn://localhost/#{repo}/#{web}/#{id}'`
103
- end
104
-
105
- unless $?.exitstatus == 0
106
- return 404
107
- end
108
- info = $nori.parse(info)
109
- info[:info][:entry]
110
- end
111
-
112
- # Get a file's MIME type
113
- # @param [String] rev the revision number to access
114
- # @param [String] web the subdirectory for web content
115
- # @param [String] path the path to the repositories
116
- # @param [String] repo the new repo name
117
- # @param [String] id the relative path to the file
118
- # @return [String] the MIME type
119
- def self.do_read_mime(rev, web, path, repo, id)
120
- options = "--username #{$environment[:repos_user]} --password #{$environment[:repos_password]}"
121
- if rev.nil?
122
- rev = 'HEAD'
123
- end
124
- if web.nil?
125
- mime = `svn propget #{options} -r #{rev} --xml svn:mime-type 'svn://localhost/#{repo}/#{id}'`
126
- else
127
- mime = `svn propget #{options} -r #{rev} --xml svn:mime-type 'svn://localhost/#{repo}/#{web}/#{id}'`
128
- end
129
-
130
- unless $?.success?
131
- return 500
132
- end
133
- mime = $nori.parse(mime)
134
- if mime[:properties].nil?
135
- 'application/octet-stream'
136
- else
137
- mime[:properties][:target][:property]
138
- end
139
- end
140
-
141
- # Update a single file
142
- # @param [String] web the subdirectory for web content
143
- # @param [String] path the path to the repositories
144
- # @param [String] repo the new repo name
145
- # @param [String] id the relative path to the file
146
- # @param [String] content the updated file
147
- # @param [String] message the commit message
148
- # @param [String] mime the mime-type to set
149
- # @param [String] user the Author of this change
150
- # @return [Integer] status code
151
- def self.do_update_file(web, path, repo, id, content, message, mime, user)
152
- options = "--username #{$environment[:repos_user]} --password #{$environment[:repos_password]}"
153
- status = 500
154
- `svn checkout #{options} svn://localhost/#{repo} /tmp/svn/#{repo}`
155
- if $?.exitstatus == 0
156
-
157
- if web.nil?
158
- file_path = "/tmp/svn/#{repo}/#{id}"
159
- else
160
- file_path = "/tmp/svn/#{repo}/#{web}/#{id}"
161
- id = id.split('/')
162
- id.pop
163
- id = id.join('/')
164
- dir_path = "/tmp/svn/#{repo}/#{web}/#{id}"
165
- unless Dir.exist? dir_path
166
- FileUtils.mkdir_p( dir_path )
167
- end
168
- end
169
-
170
- file = File.open(file_path, 'w+')
171
- file.syswrite(content)
172
- file.close
173
- `svn add --force /tmp/svn/#{repo}/*`
174
- `svn propset svn:mime-type "#{mime}" #{file_path}`
175
- `svn commit #{options} -m "#{message}" /tmp/svn/#{repo}`
176
- if $?.exitstatus == 0
177
- status = 200
178
- end
179
- `svn propset #{options} --revprop -r HEAD svn:author '#{user}' /tmp/svn/#{repo}`
180
- end
181
- `rm -R /tmp/svn/#{repo}`
182
- status
183
- end
184
- end
25
+ # Repo::SVN is a connector for svnserve
26
+ # @author Bryan T. Meyers
27
+ module SVN
28
+ extend Repo
29
+
30
+ # Make a new SVN repo
31
+ # @param [String] path the path to the repositories
32
+ # @param [String] repo the new repo name
33
+ # @return [Integer] status code
34
+ def self.do_create_file(path, repo)
35
+ result = 200
36
+ `svnadmin create #{path}/#{repo}`
37
+ if $?.exitstatus != 0
38
+ return 500
39
+ end
40
+
41
+ if $?.exitstatus != 0
42
+ 500
43
+ else
44
+ result
45
+ end
46
+ end
47
+
48
+ # Read a single file
49
+ # @param [String] rev the revision number to access
50
+ # @param [String] web the subdirectory for web content
51
+ # @param [String] path the path to the repositories
52
+ # @param [String] repo the new repo name
53
+ # @param [String] id the relative path to the file
54
+ # @return [String] the file
55
+ def self.do_read_file(rev, web, path, repo, id)
56
+ options = "--username #{$environment[:repos_user]} --password #{$environment[:repos_password]}"
57
+ if rev.nil?
58
+ rev = 'HEAD'
59
+ end
60
+ if web.nil?
61
+ body = `svn cat #{options} -r #{rev} 'svn://localhost/#{repo}/#{id}'`
62
+ else
63
+ body = `svn cat #{options} -r #{rev} 'svn://localhost/#{repo}/#{web}/#{id}'`
64
+ end
65
+
66
+ if $?.success?
67
+ body
68
+ else
69
+ 500
70
+ end
71
+ end
72
+
73
+ # Read a directory listing
74
+ # @param [String] web the subdirectory for web content
75
+ # @param [String] path the path to the repositories
76
+ # @param [String] repo the new repo name
77
+ # @param [String] id the relative path to the file
78
+ # @return [Array] the directory listing
79
+ def self.do_read_listing(web, path, repo, id = nil)
80
+ options = "--username #{$environment[:repos_user]} --password #{$environment[:repos_password]}"
81
+ if web.nil?
82
+ if id.nil?
83
+ list = `svn list #{options} --xml 'svn://localhost/#{repo}'`
84
+ else
85
+ list = `svn list #{options} --xml 'svn://localhost/#{repo}/#{id}'`
86
+ end
87
+ else
88
+ if id.nil?
89
+ list = `svn list #{options} --xml 'svn://localhost/#{repo}/#{web}'`
90
+ else
91
+ list = `svn list #{options} --xml 'svn://localhost/#{repo}/#{web}/#{id}'`
92
+ end
93
+ end
94
+ unless $?.exitstatus == 0
95
+ return 404
96
+ end
97
+ list = $nori.parse(list)
98
+ list[:lists][:list][:entry]
99
+ end
100
+
101
+ # Read Metadata for a single file
102
+ # @param [String] rev the revision number to access
103
+ # @param [String] web the subdirectory for web content
104
+ # @param [String] path the path to the repositories
105
+ # @param [String] repo the new repo name
106
+ # @param [String] id the relative path to the file
107
+ # @return [Hash] the metadata
108
+ def self.do_read_info(rev, web, path, repo, id)
109
+ options = "--username #{$environment[:repos_user]} --password #{$environment[:repos_password]}"
110
+ if rev.nil?
111
+ rev = 'HEAD'
112
+ end
113
+ if web.nil?
114
+ info = `svn info #{options} -r #{rev} --xml 'svn://localhost/#{repo}/#{id}'`
115
+ else
116
+ info = `svn info #{options} -r #{rev} --xml 'svn://localhost/#{repo}/#{web}/#{id}'`
117
+ end
118
+
119
+ unless $?.exitstatus == 0
120
+ return 404
121
+ end
122
+ info = $nori.parse(info)
123
+ info[:info][:entry]
124
+ end
125
+
126
+ # Get a file's MIME type
127
+ # @param [String] rev the revision number to access
128
+ # @param [String] web the subdirectory for web content
129
+ # @param [String] path the path to the repositories
130
+ # @param [String] repo the new repo name
131
+ # @param [String] id the relative path to the file
132
+ # @return [String] the MIME type
133
+ def self.do_read_mime(rev, web, path, repo, id)
134
+ options = "--username #{$environment[:repos_user]} --password #{$environment[:repos_password]}"
135
+ if rev.nil?
136
+ rev = 'HEAD'
137
+ end
138
+ if web.nil?
139
+ mime = `svn propget #{options} -r #{rev} --xml svn:mime-type 'svn://localhost/#{repo}/#{id}'`
140
+ else
141
+ mime = `svn propget #{options} -r #{rev} --xml svn:mime-type 'svn://localhost/#{repo}/#{web}/#{id}'`
142
+ end
143
+
144
+ unless $?.success?
145
+ return 500
146
+ end
147
+ mime = $nori.parse(mime)
148
+ if mime[:properties].nil?
149
+ 'application/octet-stream'
150
+ else
151
+ mime[:properties][:target][:property]
152
+ end
153
+ end
154
+
155
+ # Update a single file
156
+ # @param [String] web the subdirectory for web content
157
+ # @param [String] path the path to the repositories
158
+ # @param [String] repo the new repo name
159
+ # @param [String] id the relative path to the file
160
+ # @param [String] content the updated file
161
+ # @param [String] message the commit message
162
+ # @param [String] mime the mime-type to set
163
+ # @param [String] user the Author of this change
164
+ # @return [Integer] status code
165
+ def self.do_update_file(web, path, repo, id, content, message, mime, user)
166
+ options = "--username #{$environment[:repos_user]} --password #{$environment[:repos_password]}"
167
+ status = 500
168
+ id = id.split('/')
169
+ id.pop
170
+ id = id.join('/')
171
+ if web.nil?
172
+ repo_path = "/tmp/svn/#{repo}/#{id}"
173
+ else
174
+ repo_path = "/tmp/svn/#{repo}/#{web}/#{id}"
175
+ end
176
+ unless Dir.exist? repo_path
177
+ FileUtils.mkdir_p(repo_path)
178
+ end
179
+
180
+ `svn checkout #{options} 'svn://localhost/#{repo}' '/tmp/svn/#{repo}'`
181
+ id = CGI.unescape(id)
182
+ if $?.exitstatus == 0
183
+ id = id.split('/')
184
+ id.pop
185
+ id = id.join('/')
186
+ if web.nil?
187
+ file_path = "/tmp/svn/#{repo}/#{id}"
188
+ else
189
+ file_path = "/tmp/svn/#{repo}/#{web}/#{id}"
190
+ end
191
+
192
+ unless Dir.exist? file_path
193
+ FileUtils.mkdir_p(file_path)
194
+ end
195
+
196
+ file = File.open(file_path, 'w+')
197
+ file.syswrite(content)
198
+ file.close
199
+ `svn add --force "/tmp/svn/#{repo}/*"`
200
+ `svn propset svn:mime-type "#{mime}" "#{file_path}"`
201
+ `svn commit #{options} -m "#{message}" "/tmp/svn/#{repo}"`
202
+ if $?.exitstatus == 0
203
+ status = 200
204
+ end
205
+ `svn propset #{options} --revprop -r HEAD svn:author '#{user}' "/tmp/svn/#{repo}"`
206
+ end
207
+ `rm -R '/tmp/svn/#{repo}'`
208
+ status
209
+ end
210
+ end
185
211
  end
data/lib/closet.rb CHANGED
@@ -1,106 +1,118 @@
1
+ ##
2
+ # Copyright 2017 Bryan T. Meyers
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ##
16
+
1
17
  require 'rack'
2
- require 'awesome_print'
3
18
  require_relative 'app'
4
19
  require_relative 'closet/auth'
20
+ require_relative 'closet/config'
5
21
  require_relative 'closet/context'
6
- require_relative 'closet/resource'
7
22
  require_relative 'closet/renderer'
8
23
 
9
24
 
10
25
  module Wire
11
- # A Closet is a configured Wire Environment
12
- # @author Bryan T. Meyers
13
- class Closet
14
- include Wire::App
15
- include Wire::Auth
16
- include Wire::Renderer
17
- include Wire::Resource
26
+ # A Closet is a configured Wire Environment
27
+ # @author Bryan T. Meyers
28
+ class Closet
29
+ include Wire::Auth
18
30
 
19
- # Create an empty Closet
20
- # @return [Wire::Closet] the new closet
21
- def initialize
22
- $apps = {}
23
- $editors = {}
24
- $renderers = {}
25
- $templates = {}
26
- end
31
+ # Create an empty Closet
32
+ # @return [Wire::Closet] the new closet
33
+ def initialize
34
+ $wire_apps = {}
35
+ $wire_editors = {}
36
+ $wire_renderers = {}
37
+ $wire_templates = {}
38
+ end
27
39
 
28
- # Route a Request to the correct Wire::App
29
- # @param [Wire::Context] context the context of this Request
30
- # @return [Response] a Rack Response triplet, or status code
31
- def route(context)
32
- actions = actions_allowed(context)
33
- if actions.include? context.action
34
- context.type.invoke(actions, context)
35
- else
36
- 401
37
- end
38
- end
40
+ # Route a Request to the correct Wire::App
41
+ # @param [Wire::Context] context the context of this Request
42
+ # @return [Response] a Rack Response triplet, or status code
43
+ def route(context)
44
+ actions = actions_allowed(context)
45
+ if actions.include? context.action
46
+ context.config['type'].invoke(actions, context)
47
+ else
48
+ 401
49
+ end
50
+ end
39
51
 
40
- # Fulfill the current request
41
- # @param [Hash] env the Rack environment
42
- # @return [Response] a Rack Response triplet, or status code
43
- def call(env)
44
- begin
45
- context = Wire::Context.new(env)
46
- response = route(context)
47
- rescue Exception => e
48
- $stderr.puts e.message
49
- $stderr.puts e.backtrace
50
- response = [500, {}, e.message]
51
- end
52
- if response.is_a? Array
53
- if response[2]
54
- unless response[2].is_a? Array
55
- response[2] = [response[2]]
56
- end
57
- end
58
- else
59
- if response.is_a? Integer
60
- response = [response, {}, []]
61
- else
62
- response = [200, {}, [response]]
63
- end
64
- end
65
- response
66
- end
52
+ # Fulfill the current request
53
+ # @param [Hash] env the Rack environment
54
+ # @return [Response] a Rack Response triplet, or status code
55
+ def call(env)
56
+ begin
57
+ context = Wire::Context.new(env)
58
+ response = route(context)
59
+ rescue Exception => e
60
+ $stderr.puts e.message
61
+ $stderr.puts e.backtrace
62
+ response = [500, {}, e.message]
63
+ end
64
+ if response.is_a? Array
65
+ if response[2]
66
+ unless response[2].is_a? Array
67
+ response[2] = [response[2]]
68
+ end
69
+ end
70
+ else
71
+ if response.is_a? Integer
72
+ response = [response, {}, []]
73
+ else
74
+ response = [200, {}, [response]]
75
+ end
76
+ end
77
+ response
78
+ end
67
79
 
68
- # A factory method for configuring a Closet
69
- # @param [Proc] block the configuration routine
70
- # @return [Wire::Closet] the configured Closet
71
- def self.build(&block)
72
- closet = Wire::Closet.new
73
- if ENV['RACK_ENV'].eql? 'development'
74
- $stderr.puts 'Starting Up Wire...'
75
- $stderr.puts 'Starting Apps...'
76
- end
77
- Docile.dsl_eval(closet, &block)
78
- if ENV['RACK_ENV'].eql? 'development'
79
- closet.info
80
- end
81
- closet
82
- end
80
+ # A factory method for configuring a Closet
81
+ # @param [Proc] block the configuration routine
82
+ # @return [Wire::Closet] the configured Closet
83
+ def self.build
84
+ closet = Wire::Closet.new
85
+ if ENV['RACK_ENV'].eql? 'development'
86
+ $stderr.puts 'Starting Up Wire...'
87
+ $stderr.puts 'Starting Apps...'
88
+ end
89
+ Wire::App.read_configs
90
+ Wire::Renderer.read_configs
91
+ if ENV['RACK_ENV'].eql? 'development'
92
+ closet.info
93
+ end
94
+ closet
95
+ end
83
96
 
84
- # Print out a human-readable configuration
85
- # @return [void]
86
- def info
87
- $stderr.puts "Apps:\n"
88
- $apps.each do |app, config|
89
- $stderr.puts "\u{2502}"
90
- $stderr.puts "\u{251c} Name: #{app}"
91
- if config[:auth]
92
- $stderr.puts "\u{2502}\t\u{251c} Auth:"
93
- if config[:auth][:level] == :app
94
- $stderr.puts "\u{2502}\t\u{2502}\t\u{251c} Level:\t#{config[:auth][:level]}"
95
- $stderr.puts "\u{2502}\t\u{2502}\t\u{2514} Handler:\t#{config[:auth][:handler]}"
96
- else
97
- $stderr.puts "\u{2502}\t\u{2502}\t\u{2514} Level:\t#{config[:auth][:level]}"
98
- end
99
- end
100
- if config[:type]
101
- $stderr.puts "\u{2502}\t\u{2514} Type: #{config[:type]}"
102
- end
103
- end
104
- end
105
- end
97
+ # Print out a human-readable configuration
98
+ # @return [void]
99
+ def info
100
+ $stderr.puts "Apps:\n"
101
+ $wire_apps.each do |app, config|
102
+ $stderr.puts "\u{2502}"
103
+ $stderr.puts "\u{251c} Name: #{app}"
104
+ if config['auth_handler']
105
+ $stderr.puts "\u{2502}\t\u{251c} Auth:"
106
+ $stderr.puts "\u{2502}\t\u{2502}\t\u{2514} Handler:\t#{config['auth_handler']}"
107
+ end
108
+ if config['auth_read_only']
109
+ $stderr.puts "\u{2502}\t\u{251c} Auth:"
110
+ $stderr.puts "\u{2502}\t\u{2502}\t\u{2514} Read Only:\t#{config['auth_read_only']}"
111
+ end
112
+ if config['type']
113
+ $stderr.puts "\u{2502}\t\u{2514} Type: #{config['type']}"
114
+ end
115
+ end
116
+ end
117
+ end
106
118
  end