gorp 0.28.1 → 0.28.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -47,8 +47,8 @@ module Gorp
47
47
  close = '//#END_HIGHLIGHT'
48
48
  elsif self =~ /->$/
49
49
  # CoffeeScript
50
- start = '//#START_HIGHLIGHT'
51
- close = '//#END_HIGHLIGHT'
50
+ start = '#START_HIGHLIGHT'
51
+ close = '#END_HIGHLIGHT'
52
52
  else
53
53
  # Other, most likely Ruby
54
54
  start = '#START_HIGHLIGHT'
@@ -121,12 +121,12 @@ module Gorp
121
121
 
122
122
  def clear_highlights
123
123
  self.gsub! /^ *(\/\/#)\s*(START|END)_HIGHLIGHT\n/, ''
124
- self.gsub! /^\s*(#|<!--|\/\*)\s*(START|END)_HIGHLIGHT\s*(-->|\*\/)?\n/, ''
124
+ self.gsub! /^ *(#|<!--|\/\*)\s*(START|END)_HIGHLIGHT\s*?(-->|\*\/)?\n/, ''
125
125
  end
126
126
 
127
127
  def clear_all_marks
128
128
  self.gsub! /^ *(\/\/#)\s*(START|END)(_HIGHLIGHT|:\w+)\n/, ''
129
- self.gsub! /^\s*(#|<!--|\/\*|\/\/#)\s*(START|END)(_HIGHLIGHT|:\w+)\s*(-->|\*\/)?\n/, ''
129
+ self.gsub! /^ *(#|<!--|\/\*|\/\/#)\s*(START|END)(_HIGHLIGHT|:\w+)\s*?(-->|\*\/)?\n/, ''
130
130
  end
131
131
 
132
132
  def msub pattern, replacement, option=nil
@@ -161,6 +161,7 @@ module Gorp
161
161
  end
162
162
 
163
163
  def edit filename, tag=nil, &block
164
+ filename = Dir[filename].first || filename if filename.include? '*'
164
165
  $x.pre "edit #{filename.gsub('/',FILE_SEPARATOR)}", :class=>'stdin'
165
166
 
166
167
  stale = File.mtime(filename) rescue Time.now-2
@@ -171,12 +172,13 @@ def edit filename, tag=nil, &block
171
172
  data.extend Gorp::StringEditingFunctions
172
173
  data.instance_exec(data, &block) if block_given?
173
174
 
174
- # ensure that the file timestamp changed
175
- now = Time.now
176
- usec = now.usec/1000000.0
177
- sleep 1-usec if now-usec <= stale
178
- open(filename,'w') {|file| file.write data}
179
- File.utime(stale+2, stale+2, filename) if File.mtime(filename) <= stale
175
+ # write the file, ensuring that the file timestamp changed
176
+ while true
177
+ open(filename,'w') {|file| file.write data}
178
+ $lastmod = File.mtime(filename)
179
+ break if $lastmod > stale and Time.now >= $lastmod
180
+ sleep 0.1
181
+ end
180
182
 
181
183
  rescue Exception => e
182
184
  $x.pre :class => 'traceback' do
@@ -30,9 +30,16 @@ $CODE = File.join($DATA,'code')
30
30
  # work directory
31
31
  if (work=ARGV.find {|arg| arg =~ /--work=(.*)/})
32
32
  ARGV.delete(work)
33
- $WORK = File.join($BASE,$1)
33
+ work = $1
34
34
  else
35
- $WORK = File.join($BASE, ENV['GORP_WORK'] || 'work')
35
+ work = ENV['GORP_WORK'] || 'work'
36
+ end
37
+
38
+ if File.respond_to? :realpath
39
+ $WORK = File.realpath(work, $BASE)
40
+ else
41
+ require 'pathname'
42
+ $WORK = Pathname.new($BASE).join(work).realpath.to_s
36
43
  end
37
44
 
38
45
  # deduce environment based on provided Gemfile
@@ -51,6 +58,7 @@ if gemfile
51
58
  end
52
59
 
53
60
  Dir.chdir(File.dirname(gemfile)) do
61
+ exit unless File.exist? 'Gemfile.lock' or system 'bundle install'
54
62
  require 'bundler/setup'
55
63
  end
56
64
  end
@@ -65,28 +73,6 @@ module Gorp
65
73
  extend Commands
66
74
  $x.pre Time.now.httpdate, :class=>'stdout'
67
75
 
68
- cmd "echo $PATH"
69
- cmd "node -v"
70
-
71
- cmd "#{$ruby} -v"
72
- cmd 'gem -v'
73
- Dir.chdir(File.join($WORK, $rails_app.to_s)) do
74
- if $bundle
75
- cmd 'bundle show'
76
- else
77
- cmd 'gem list'
78
- cmd 'echo $RUBYLIB | sed "s/:/\n/g"'
79
- end
80
- end
81
-
82
- if File.exist? 'Gemfile'
83
- rake 'about'
84
- elsif File.exist? 'script/rails'
85
- cmd 'ruby script/rails application -v'
86
- else
87
- cmd Gorp.which_rails($rails) + ' -v'
88
- end
89
-
90
76
  if $rails != 'rails'
91
77
  Dir.chdir($rails) do
92
78
  log :cmd, 'git log -1'
@@ -102,7 +88,95 @@ module Gorp
102
88
  end
103
89
  end
104
90
  end
105
- rescue
91
+
92
+ if File.exist? 'Gemfile'
93
+ if File.read("#$rails/RAILS_VERSION") =~ /^[34]/
94
+ cmd = 'rake about'
95
+ else
96
+ cmd = 'rails about'
97
+ end
98
+
99
+ log :cmd, cmd
100
+ $x.pre cmd, :class=>'stdin'
101
+ about = `#{cmd}`.sub(/^(Middleware\s+)(.*)/) {
102
+ term,dfn=$1,$2
103
+ term+dfn.gsub(', ', ",\n" + ' ' * term.length)
104
+ }
105
+ about.split("\n").each {|line| $x.pre line, :class => :stdout}
106
+ elsif File.exist? 'script/rails'
107
+ cmd 'ruby script/rails application -v'
108
+ else
109
+ cmd Gorp.which_rails($rails) + ' -v'
110
+ end
111
+
112
+ Dir.chdir(File.join($WORK, $rails_app.to_s)) do
113
+ if $bundle
114
+ cmd 'bundle show'
115
+ else
116
+ cmd 'gem list'
117
+ cmd 'echo $RUBYLIB | sed "s/:/\n/g"'
118
+ end
119
+ end
120
+
121
+ cmd 'gem -v'
122
+ cmd "#{$ruby} -v"
123
+
124
+ if not `which rvm`.empty?
125
+ if ENV['rvm_version']
126
+ cmd "echo $rvm_version", :as => 'rvm -v'
127
+ else
128
+ cmd "rvm -v | grep '\\S'", :as => 'rvm -v'
129
+ end
130
+ elsif not `which rbenv`.empty?
131
+ cmd "rbenv --version"
132
+ end
133
+
134
+ if not `which nodejs`.empty?
135
+ cmd "nodejs -v"
136
+ elsif not `which node`.empty?
137
+ cmd "node -v"
138
+ end
139
+
140
+ if not `which mysql`.empty?
141
+ cmd "mysql --version"
142
+ end
143
+
144
+ log :cmd, 'echo $PATH'
145
+ $x.pre 'echo $PATH', :class=>'stdin'
146
+ ENV['PATH'].split(':').each {|path| $x.pre path, :class => :stdout}
147
+
148
+ if not `which lsb_release`.empty?
149
+ cmd "lsb_release -irc"
150
+ elsif File.exist? '/etc/debian_version'
151
+ cmd 'cat /etc/debian_version'
152
+ end
153
+
154
+ if not `which sw_vers`.empty?
155
+ cmd "sw_vers"
156
+ end
157
+
158
+ if not `which uname`.empty?
159
+ cmd "uname -srm"
160
+ end
161
+
162
+ config = Gorp::Config.get
163
+ if config and not config.empty?
164
+ $x.pre 'Gorp.config.get', :class=>'stdin'
165
+ $x.table do
166
+ config.sort.each do |name, value|
167
+ $x.tr do
168
+ $x.td name
169
+ $x.td value.inspect
170
+ end
171
+ end
172
+ end
173
+ end
174
+ rescue Exception => e
175
+ $x.pre :class => 'traceback' do
176
+ STDERR.puts e.inspect
177
+ $x.text! "#{e.inspect}\n"
178
+ e.backtrace.each {|line| $x.text! " #{line}\n"}
179
+ end
106
180
  end
107
181
 
108
182
  def self.log type, message
@@ -1,7 +1,32 @@
1
1
  require 'net/http'
2
2
  require 'cgi'
3
+ require 'http-cookie'
4
+
5
+ $COOKIEJAR = HTTP::CookieJar.new
6
+ $CookieDebug = Gorp::Config[:cookie_debug, true]
7
+
8
+ def update_cookies(uri, response)
9
+ fields = response.get_fields('Set-Cookie')
10
+ return unless fields
11
+ fields.each {|value| $COOKIEJAR.parse(value, uri)}
12
+
13
+ if $CookieDebug
14
+ $x.ul do
15
+ fields.each do |value|
16
+ $x.li do
17
+ $x.b {$x.em '[cookie]'}
18
+ $x.text! value.to_s
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
3
24
 
4
25
  def snap response, form=nil
26
+ if response.code >= '400'
27
+ $x.p "HTTP Response Code: #{response.code}", :class => 'traceback'
28
+ end
29
+
5
30
  if response.content_type == 'text/plain' or response.content_type =~ /xml/
6
31
  $x.div :class => 'body' do
7
32
  response.body.split("\n").each do |line|
@@ -72,8 +97,18 @@ def snap response, form=nil
72
97
  attrs[:id] = body['id'] if body['id']
73
98
  attrs[:class] += ' ' +body['class'] if body['class']
74
99
  $x.div(attrs) do
100
+ html_voids = %w(area base br col command embed hr img input keygen link
101
+ meta param source)
102
+
75
103
  body.children.each do |child|
76
- $x << child.to_xml unless child.instance_of?(Comment)
104
+ next if child.instance_of?(Comment)
105
+ child.search("//*").each do |element|
106
+ next if html_voids.include? element.name
107
+ if element.children.empty? and element.text.empty?
108
+ element.add_child(Nokogiri::XML::Text.new('', element.document))
109
+ end
110
+ end
111
+ $x << child.to_xml
77
112
  end
78
113
  end
79
114
  $x.div '', :style => "clear: both"
@@ -84,6 +119,7 @@ def get path, options={}
84
119
  end
85
120
 
86
121
  def post path, form, options={}
122
+ $lastmod ||= Time.now
87
123
  log :get, path unless form
88
124
  $x.pre "get #{path}", :class=>'stdin' unless options[:snapget] == false
89
125
 
@@ -99,12 +135,13 @@ def post path, form, options={}
99
135
  accept = 'application/json' if path =~ /\.json$/
100
136
  accept = 'application/xml' if path =~ /\.xml$/
101
137
 
138
+ uri = URI.parse("http://#{host}:#{port}/#{path}")
102
139
  get = Net::HTTP::Get.new(path, 'Accept' => accept)
103
140
  get.basic_auth *options[:auth] if options[:auth]
104
- get['Cookie'] = $COOKIE if $COOKIE
141
+ get['Cookie'] = HTTP::Cookie.cookie_value($COOKIEJAR.cookies(uri))
105
142
  response = http.request(get)
106
143
  snap response, form unless options[:snapget] == false
107
- $COOKIE = response.response['set-cookie'] if response.response['set-cookie']
144
+ update_cookies uri, response
108
145
 
109
146
  if form
110
147
  body = xhtmlparse(response.body).at('//body') rescue nil
@@ -154,34 +191,56 @@ def post path, form, options={}
154
191
 
155
192
  $x.ul do
156
193
  form.each do |name, value|
157
- $x.li "#{name} => #{value}"
194
+ $x.li "#{name} => #{value}" unless $CookieDebug
158
195
  end
159
196
 
160
197
  xform.search('.//input[@type="hidden"]').each do |element|
161
- # $x.li "#{element['name']} => #{element['value']}"
198
+ if $CookieDebug
199
+ $x.li "#{element['name']} => #{element['value']}"
200
+ end
162
201
  form[element['name']] ||= element['value']
163
202
  end
203
+
204
+ if $CookieDebug
205
+ uri = URI.parse("http://#{host}:#{port}/#{path}")
206
+ $COOKIEJAR.cookies(uri).each do |cookie|
207
+ $x.li do
208
+ $x.b {$x.em '[cookie]'}
209
+ $x.text! cookie.to_s
210
+ end
211
+ end
212
+ end
164
213
  end
165
214
 
166
215
  log :post, path
216
+ uri = URI.parse("http://#{host}:#{port}/#{path}")
167
217
  post = Net::HTTP::Post.new(path)
168
- post.form_data = form
169
- post['Cookie'] = $COOKIE
218
+ post.set_form_data form
219
+ post['Content-Type'] = 'application/x-www-form-urlencoded'
220
+ post['Cookie'] = HTTP::Cookie.cookie_value($COOKIEJAR.cookies(uri))
170
221
  response=http.request(post)
171
222
  snap response
223
+ update_cookies uri, response
172
224
  end
173
225
 
174
226
  if response.code == '302'
175
- $COOKIE=response.response['set-cookie'] if response.response['set-cookie']
176
227
  path = response['Location']
228
+ uri = URI.parse("http://#{host}:#{port}/#{path}")
177
229
  $x.pre "get #{path}", :class=>'stdin'
178
230
  get = Net::HTTP::Get.new(path, 'Accept' => accept)
179
- get['Cookie'] = $COOKIE if $COOKIE
231
+ get['Cookie'] = HTTP::Cookie.cookie_value($COOKIEJAR.cookies(uri))
180
232
  response = http.request(get)
181
233
  snap response
182
- $COOKIE=response.response['set-cookie'] if response.response['set-cookie']
234
+ update_cookies uri, response
183
235
  end
184
236
  end
185
237
  rescue Timeout::Error
186
238
  Gorp::Commands.stop_server(false, 9)
239
+ ensure
240
+ while true
241
+ open('tmp/lastmod', 'w') {|file| file.write 'data'}
242
+ break if File.mtime('tmp/lastmod') > $lastmod
243
+ sleep 0.1
244
+ end
245
+ File.unlink('tmp/lastmod')
187
246
  end
@@ -1,7 +1,10 @@
1
1
  body {background-color: #F5F5DC}
2
+ body.awdwr a {text-decoration: none}
3
+ body.awdwr ul {padding-left: 1em}
2
4
  #banner {margin-top: 0}
3
5
  pre {font-weight: bold; margin: 0; padding: 0}
4
- pre.stdin {color: #800080; margin-top: 1em; padding: 0}
6
+ pre.stdin, pre.stdin a {color: #800080; margin-top: 1em; padding: 0}
7
+ pre.stdin a:hover {background-color: #F5F5DC; text-decoration: underline}
5
8
  pre.irb {color: #800080; padding: 0}
6
9
  pre.stdout {color: #000; padding: 0}
7
10
  pre.logger {color: #088; padding: 0}
@@ -10,17 +13,22 @@ pre.stderr {color: #F00; padding: 0}
10
13
  div.body {border-style: solid; border-color: #800080; padding: 0.5em}
11
14
  .issue, .traceback {background:#FDD; border: 4px solid #F00;
12
15
  font-weight: bold; margin-top: 1em; padding: 0.5em}
13
- div.body, .issue, .traceback {
14
- -webkit-border-radius: 0.7em; -moz-border-radius: 0.7em;}
16
+ div.body, .issue, .traceback {border-radius: 0.7em}
15
17
  ul.toc {list-style: none}
16
18
  ul a {text-decoration: none}
17
19
  ul a:hover {text-decoration: underline; color: #000;
18
20
  background-color: #F5F5DC}
19
21
  a.toc h2 {background-color: #981A21; color:#FFF; padding: 6px}
20
22
  ul a:visited {color: #000}
21
- h2 {clear: both}
22
23
  p.note {font-style: italic}
23
24
  p.overview {border-width: 2px; border-color: #000;
24
25
  border-style: solid; border-radius: 4em;
25
- background-color: #CCF; margin: 1.5em 1.5em; padding: 1em 2em;
26
- -webkit-border-radius: 1.5em; -moz-border-radius: 4em;}
26
+ background-color: #CCF; margin: 1.5em 1.5em; padding: 1em 2em;}
27
+ .hidden {display: none}
28
+ .traceback #container {background-color: #FFF}
29
+ .traceback .line {background-color: #FFF}
30
+ .traceback .line.active {background-color: #FCC}
31
+ .traceback .source {background-color: #EEE}
32
+ .traceback pre {background-color: #FFF; display: block; margin: 0.5em 0}
33
+ .traceback pre.line_numbers {background-color: #EEE; text-align: right}
34
+ .traceback h1 {background-color: #F00; color: #FFF; padding: 1em; margin: 0}
@@ -9,7 +9,7 @@ at_exit do
9
9
 
10
10
  $x.declare! :DOCTYPE, :html
11
11
  $x.html :xmlns => 'http://www.w3.org/1999/xhtml' do
12
- $x.header do
12
+ $x.head do
13
13
  $x.title $title
14
14
  $x.meta 'charset'=>'utf-8'
15
15
  $x.style :type => "text/css" do
@@ -18,8 +18,8 @@ at_exit do
18
18
  end
19
19
  end
20
20
  end
21
-
22
- $x.body do
21
+
22
+ $x.body class: 'awdwr' do
23
23
  $x.h1 $title, :id=>'banner'
24
24
  $x.h2 'Table of Contents'
25
25
  $x.ul :class => 'toc'
@@ -42,34 +42,34 @@ at_exit do
42
42
  restart_server
43
43
  end
44
44
  end
45
-
45
+
46
46
  # run steps
47
47
  e = nil
48
48
  begin
49
49
  $sections.each do |section, title, steps|
50
- omit = secinclude($omit, section)
51
- omit ||= (!ranges.empty? and !secinclude(ranges, section))
50
+ omit = secinclude($omit, section)
51
+ omit ||= (!ranges.empty? and !secinclude(ranges, section))
52
52
 
53
- if omit
53
+ if omit
54
54
  $x.a(:class => 'omit', :id => "section-#{section}") do
55
55
  $x.comment! title
56
56
  end
57
57
  else
58
- head section, title
59
- steps.call
58
+ section_head section, title
59
+ steps.call
60
60
  end
61
61
  end
62
62
  rescue Exception => e
63
63
  $x.pre :class => 'traceback' do
64
- STDERR.puts e.inspect
65
- $x.text! "#{e.inspect}\n"
66
- e.backtrace.each {|line| $x.text! " #{line}\n"}
64
+ STDERR.puts e.inspect
65
+ $x.text! "#{e.inspect}\n"
66
+ e.backtrace.each {|line| $x.text! " #{line}\n"}
67
67
  end
68
68
  ensure
69
69
  if e.class != SystemExit
70
70
  # terminate server
71
71
  Gorp::Commands.stop_server
72
-
72
+
73
73
  # optionally save a snapshot
74
74
  if ARGV.include?('save') or ARGV.include? '--save'
75
75
  log :snap, 'save'
@@ -87,7 +87,7 @@ at_exit do
87
87
  $x.ul :class => 'todos'
88
88
  end
89
89
  end
90
-
90
+
91
91
  # output results as HTML, after inserting style and toc information
92
92
  $x.target![/<style.*?>()/,1] = "\n#{$style.target!.strip.gsub(/^/,' '*6)}\n"
93
93
  $x.target!.sub! /<ul class="toc"\/>/,
@@ -102,7 +102,7 @@ at_exit do
102
102
  # run tests
103
103
  if $checker
104
104
  Gorp.log :CHECK, "#{$output}.html"
105
- Dir.chdir $BASE
105
+ Dir.chdir $WORK
106
106
  STDOUT.puts
107
107
  if $checker.respond_to? :call
108
108
  $checker.call