linecache 0.1 → 0.2
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/ChangeLog +38 -7
- data/NEWS +5 -1
- data/Rakefile +2 -1
- data/VERSION +1 -1
- data/lib/linecache.rb +107 -23
- data/test/short-file +2 -0
- data/test/test-linecache.rb +32 -2
- metadata +3 -2
data/ChangeLog
CHANGED
@@ -1,22 +1,53 @@
|
|
1
|
+
2007-12-10 16:24 rockyb
|
2
|
+
|
3
|
+
* linecache/linecache: release 0.1
|
4
|
+
|
5
|
+
2007-12-09 17:47 rockyb
|
6
|
+
|
7
|
+
* linecache/trunk/lib/linecache.rb: Make Ruby 1.9 compatible.
|
8
|
+
|
9
|
+
2007-12-09 17:17 rockyb
|
10
|
+
|
11
|
+
* linecache/trunk/ChangeLog, linecache/trunk/Rakefile,
|
12
|
+
linecache/trunk/lib/linecache.rb,
|
13
|
+
linecache/trunk/test/test-linecache.rb: Better tests mean more
|
14
|
+
bugs found. "rake" defaults now to "test".
|
15
|
+
|
16
|
+
2007-12-09 14:10 rockyb
|
17
|
+
|
18
|
+
* linecache/trunk/Rakefile: Add VERSION to gem.
|
19
|
+
|
20
|
+
2007-12-09 14:09 rockyb
|
21
|
+
|
22
|
+
* linecache/trunk/test/test-linecache.rb: Change classname to
|
23
|
+
something more appropriate.
|
24
|
+
|
1
25
|
2007-12-09 13:41 rockyb
|
2
26
|
|
3
|
-
*
|
27
|
+
* linecache/trunk, linecache/trunk/Rakefile: . : ignore doc and pkg
|
4
28
|
Rakefile: clean now does clobber_rdoc and clobber_package, i.e.
|
5
29
|
removes
|
6
30
|
the doc and pkg directories.
|
7
31
|
|
8
32
|
2007-12-09 13:15 rockyb
|
9
33
|
|
10
|
-
* ChangeLog, NEWS,
|
11
|
-
|
34
|
+
* linecache/trunk/ChangeLog, linecache/trunk/NEWS,
|
35
|
+
linecache/trunk/README, linecache/trunk/Rakefile,
|
36
|
+
linecache/trunk/lib/linecache.rb,
|
37
|
+
linecache/trunk/test/test-linecache.rb: NEWS, README,
|
38
|
+
linecache.rb: Add $Id$ line
|
12
39
|
test-linecache.rb: make executable
|
13
40
|
Rakefile: Correct documentation
|
14
41
|
|
15
42
|
2007-12-09 12:58 rockyb
|
16
43
|
|
17
|
-
* linecache,
|
18
|
-
|
19
|
-
|
20
|
-
|
44
|
+
* linecache, linecache/trunk, linecache/trunk/AUTHORS,
|
45
|
+
linecache/trunk/COPYING, linecache/trunk/ChangeLog,
|
46
|
+
linecache/trunk/NEWS, linecache/trunk/README,
|
47
|
+
linecache/trunk/Rakefile, linecache/trunk/VERSION,
|
48
|
+
linecache/trunk/lib, linecache/trunk/lib/linecache.rb,
|
49
|
+
linecache/trunk/test, linecache/trunk/test/test-linecache.rb:
|
50
|
+
Initial import of LineCache, a module for reading and caching
|
51
|
+
lines.
|
21
52
|
This time to trunk.
|
22
53
|
|
data/NEWS
CHANGED
@@ -1,5 +1,9 @@
|
|
1
|
+
0.2
|
2
|
+
- Make this work with ruby-debug-base. Add reload-on-change parameters.
|
3
|
+
add checkcache, cache, cached? sha1, and stat methods.
|
4
|
+
|
1
5
|
0.1
|
2
6
|
|
3
7
|
- Initial release of LineCache, a module for reading and caching lines.
|
4
8
|
|
5
|
-
$Id: NEWS
|
9
|
+
$Id: NEWS 25 2008-01-16 01:30:13Z rockyb $
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2
|
data/lib/linecache.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
# $Id: linecache.rb
|
2
|
+
# $Id: linecache.rb 23 2008-01-15 22:42:39Z rockyb $
|
3
3
|
#
|
4
|
-
# Copyright (C) 2007 Rocky Bernstein <rockyb@rubyforge.net>
|
4
|
+
# Copyright (C) 2007, 2008 Rocky Bernstein <rockyb@rubyforge.net>
|
5
5
|
#
|
6
6
|
# This program is free software; you can redistribute it and/or modify
|
7
7
|
# it under the terms of the GNU General Public License as published by
|
@@ -29,9 +29,9 @@
|
|
29
29
|
# == SYNOPSIS
|
30
30
|
#
|
31
31
|
# The LineCache module allows one to get any line from any file,
|
32
|
-
# caching lines of the file on first access to the file. The
|
33
|
-
#
|
34
|
-
#
|
32
|
+
# caching lines of the file on first access to the file. The may be is
|
33
|
+
# useful when a small random sets of lines are read from a single
|
34
|
+
# file, in particular in a debugger to show source lines.
|
35
35
|
#
|
36
36
|
# require 'linecache'
|
37
37
|
# lines = LineCache::getlines('/tmp/myruby.rb')
|
@@ -46,20 +46,29 @@
|
|
46
46
|
# LineCache::clear_file_cache('/tmp/myruby.rb')
|
47
47
|
# LineCache::update_cache # Check for modifications of all cached files.
|
48
48
|
#
|
49
|
-
#
|
49
|
+
# Some parts of the interface is derived from the Python module of the
|
50
|
+
# same name.
|
50
51
|
#
|
51
52
|
|
53
|
+
require 'digest/sha1'
|
54
|
+
|
55
|
+
# Defining SCRIPT_LINES__ causes Ruby to cache the lines of files
|
56
|
+
# it reads. The key the setting of __FILE__ at the time when Ruby does
|
57
|
+
# its read. LineCache keeps a separate copy of the lines elsewhere
|
58
|
+
# and never destroys __SCRIPT_LINES
|
59
|
+
SCRIPT_LINES__ = {} unless defined? SCRIPT_LINES__
|
60
|
+
|
52
61
|
# require "rubygems"
|
53
62
|
# require "ruby-debug" ; Debugger.start
|
54
63
|
|
55
64
|
# = module LineCache
|
56
65
|
# Module caching lines of a file
|
57
66
|
module LineCache
|
58
|
-
LineCacheInfo = Struct.new(:stat, :lines, :fullname)
|
67
|
+
LineCacheInfo = Struct.new(:stat, :lines, :fullname, :sha1)
|
59
68
|
|
60
|
-
# Get line +
|
61
|
-
# a problem. If a file named filename is not found, the
|
62
|
-
# look for it in the $: path array.
|
69
|
+
# Get line +line_number+ from file named +filename+. Return nil if
|
70
|
+
# there was a problem. If a file named filename is not found, the
|
71
|
+
# function will look for it in the $: path array.
|
63
72
|
#
|
64
73
|
# Examples:
|
65
74
|
#
|
@@ -70,10 +79,10 @@ module LineCache
|
|
70
79
|
# lines = LineCache::getlines ('myfile.rb')
|
71
80
|
# end
|
72
81
|
#
|
73
|
-
def getline(filename,
|
74
|
-
lines = getlines(filename)
|
75
|
-
if (1..lines.size) ===
|
76
|
-
return lines[
|
82
|
+
def getline(filename, line_number, reload_on_change=true)
|
83
|
+
lines = getlines(filename, reload_on_change)
|
84
|
+
if (1..lines.size) === line_number
|
85
|
+
return lines[line_number-1]
|
77
86
|
else
|
78
87
|
return nil
|
79
88
|
end
|
@@ -92,11 +101,12 @@ module LineCache
|
|
92
101
|
|
93
102
|
# Read lines of +filename+ and cache the results. However +filename+ was
|
94
103
|
# previously cached use the results from the cache.
|
95
|
-
def getlines(filename)
|
104
|
+
def getlines(filename, reload_on_change=false)
|
105
|
+
checkcache(filename) if reload_on_change
|
96
106
|
if @@file_cache.member?(filename)
|
97
107
|
return @@file_cache[filename].lines
|
98
108
|
else
|
99
|
-
return update_cache(filename)
|
109
|
+
return update_cache(filename, true)
|
100
110
|
end
|
101
111
|
end
|
102
112
|
|
@@ -104,6 +114,9 @@ module LineCache
|
|
104
114
|
|
105
115
|
# Discard cache entries that are out of date. If +filename+ is +nil+
|
106
116
|
# all entries in the file cache +@@file_cache+ are checked.
|
117
|
+
# If we don't have stat information about a file which can happen
|
118
|
+
# if the file was read from __SCRIPT_LINES but no corresponding file
|
119
|
+
# is found, it will be kept.
|
107
120
|
def checkcache(filename=nil)
|
108
121
|
|
109
122
|
if !filename
|
@@ -120,7 +133,8 @@ module LineCache
|
|
120
133
|
if File.exist?(fullname)
|
121
134
|
cache_info = @@file_cache[filename]
|
122
135
|
stat = File.stat(fullname)
|
123
|
-
if
|
136
|
+
if stat &&
|
137
|
+
(cache_info.size != stat.size or cache_info.mtime != stat.mtime)
|
124
138
|
@@file_cache.delete(filename)
|
125
139
|
end
|
126
140
|
else
|
@@ -128,19 +142,77 @@ module LineCache
|
|
128
142
|
end
|
129
143
|
end
|
130
144
|
end
|
131
|
-
|
132
145
|
module_function :checkcache
|
146
|
+
|
147
|
+
# Cache filename if it's not already cached.
|
148
|
+
# Return the expanded filename for it in the cache
|
149
|
+
# or nil if we can't find the file.
|
150
|
+
def cache(filename, reload_on_change=false)
|
151
|
+
if @@file_cache.member?(filename)
|
152
|
+
checkcache(filename) if reload_on_change
|
153
|
+
else
|
154
|
+
return update_cache(filename, true)
|
155
|
+
end
|
156
|
+
if @@file_cache.member?(filename)
|
157
|
+
@@file_cache[filename].fullname
|
158
|
+
else
|
159
|
+
nil
|
160
|
+
end
|
161
|
+
end
|
162
|
+
module_function :cache
|
163
|
+
|
164
|
+
# Return true if filename is cached
|
165
|
+
def cached?(filename)
|
166
|
+
@@file_cache.member?(filename)
|
167
|
+
end
|
168
|
+
module_function :cached?
|
169
|
+
|
170
|
+
# Return SHA1 of filename.
|
171
|
+
def sha1(filename)
|
172
|
+
return nil unless @@file_cache.member?(filename)
|
173
|
+
return @@file_cache[filename].sha1.hexdigest if
|
174
|
+
@@file_cache[filename].sha1
|
175
|
+
sha1 = Digest::SHA1.new
|
176
|
+
@@file_cache[filename].lines.each do |line|
|
177
|
+
sha1 << line
|
178
|
+
end
|
179
|
+
@@file_cache[filename].sha1 = sha1
|
180
|
+
sha1.hexdigest
|
181
|
+
end
|
182
|
+
module_function :sha1
|
133
183
|
|
184
|
+
# Return File.stat in the cache for filename.
|
185
|
+
def stat(filename)
|
186
|
+
return nil unless @@file_cache.member?(filename)
|
187
|
+
@@file_cache[filename].stat
|
188
|
+
end
|
189
|
+
module_function :stat
|
190
|
+
|
134
191
|
# Update a cache entry and return its list of lines. if something's
|
135
192
|
# wrong, discard the cache entry, and return an empty list.
|
136
|
-
|
193
|
+
# If use_script_lines is true, try to get the
|
194
|
+
def update_cache(filename, use_script_lines=false)
|
137
195
|
|
138
196
|
return [] unless filename
|
139
197
|
|
140
198
|
@@file_cache.delete(filename)
|
141
|
-
|
142
199
|
fullname = File.expand_path(filename)
|
143
|
-
|
200
|
+
|
201
|
+
if use_script_lines
|
202
|
+
[filename, fullname].each do |name|
|
203
|
+
if !SCRIPT_LINES__[name].nil? && SCRIPT_LINES__[name] != true
|
204
|
+
begin
|
205
|
+
stat = File.stat(name)
|
206
|
+
rescue
|
207
|
+
stat = nil
|
208
|
+
end
|
209
|
+
lines = SCRIPT_LINES__[name]
|
210
|
+
@@file_cache[filename] = LineCacheInfo.new(stat, lines, fullname, nil)
|
211
|
+
return lines
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
144
216
|
if File.exist?(fullname)
|
145
217
|
stat = File.stat(fullname)
|
146
218
|
else
|
@@ -166,7 +238,7 @@ module LineCache
|
|
166
238
|
return []
|
167
239
|
end
|
168
240
|
@@file_cache[filename] = LineCacheInfo.new(File.stat(fullname), lines,
|
169
|
-
fullname)
|
241
|
+
fullname, nil)
|
170
242
|
return lines
|
171
243
|
end
|
172
244
|
|
@@ -176,11 +248,23 @@ end
|
|
176
248
|
|
177
249
|
# example usage
|
178
250
|
if __FILE__ == $0 or
|
179
|
-
($DEBUG and ['
|
251
|
+
($DEBUG and ['rcov'].include?(File.basename($0)))
|
252
|
+
def yes_no(var)
|
253
|
+
return var ? "" : "not "
|
254
|
+
end
|
255
|
+
|
180
256
|
lines = LineCache::getlines(__FILE__)
|
257
|
+
puts "#{__FILE__} has #{lines.size} lines"
|
181
258
|
line = LineCache::getline(__FILE__, 6)
|
259
|
+
puts "The 6th line is\n#{line}"
|
182
260
|
LineCache::update_cache(__FILE__)
|
183
261
|
LineCache::checkcache(__FILE__)
|
262
|
+
puts("#{__FILE__} is %scached." %
|
263
|
+
yes_no(LineCache::cached?(__FILE__)))
|
264
|
+
LineCache::stat(__FILE__).inspect
|
184
265
|
LineCache::checkcache # Check all files in the cache
|
185
266
|
LineCache::clear_file_cache
|
267
|
+
puts("#{__FILE__} is now %scached." %
|
268
|
+
yes_no(LineCache::cached?(__FILE__)))
|
269
|
+
|
186
270
|
end
|
data/test/short-file
ADDED
data/test/test-linecache.rb
CHANGED
@@ -5,10 +5,14 @@ require "tempfile"
|
|
5
5
|
|
6
6
|
# Test LineCache module
|
7
7
|
class TestLineCache < Test::Unit::TestCase
|
8
|
-
@@
|
9
|
-
|
8
|
+
@@TEST_DIR = File.expand_path(File.dirname(__FILE__))
|
9
|
+
@@TOP_SRC_DIR = File.join(@@TEST_DIR, '..', 'lib')
|
10
10
|
require File.join(@@TOP_SRC_DIR, 'linecache.rb')
|
11
11
|
|
12
|
+
def setup
|
13
|
+
LineCache::clear_file_cache
|
14
|
+
end
|
15
|
+
|
12
16
|
def test_basic
|
13
17
|
fp = File.open(__FILE__, 'r')
|
14
18
|
compare_lines = fp.readlines()
|
@@ -57,4 +61,30 @@ class TestLineCache < Test::Unit::TestCase
|
|
57
61
|
LineCache::update_cache(__FILE__)
|
58
62
|
LineCache::clear_file_cache
|
59
63
|
end
|
64
|
+
|
65
|
+
def test_cached
|
66
|
+
assert_equal(false, LineCache::cached?(__FILE__),
|
67
|
+
"file #{__FILE__} shouldn't be cached - just cleared cache.")
|
68
|
+
line = LineCache::getline(__FILE__, 1)
|
69
|
+
assert line
|
70
|
+
assert_equal(true, LineCache::cached?(__FILE__),
|
71
|
+
"file #{__FILE__} should now be cached")
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_stat
|
75
|
+
assert_equal(nil, LineCache::stat(__FILE__),
|
76
|
+
"stat for #{__FILE__} shouldn't be nil - just cleared cache.")
|
77
|
+
line = LineCache::getline(__FILE__, 1)
|
78
|
+
assert line
|
79
|
+
assert(LineCache::stat(__FILE__),
|
80
|
+
"file #{__FILE__} should now have a stat")
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_sha1
|
84
|
+
test_file = File.join(@@TEST_DIR, 'short-file')
|
85
|
+
LineCache::cache(test_file)
|
86
|
+
assert_equal('d7ae2d65d8815607ddffa005e91a8add77ef4a1e',
|
87
|
+
LineCache::sha1(test_file))
|
88
|
+
end
|
89
|
+
|
60
90
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: linecache
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: "0.
|
7
|
-
date:
|
6
|
+
version: "0.2"
|
7
|
+
date: 2008-01-15 00:00:00 -05:00
|
8
8
|
summary: Read file with caching
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -38,6 +38,7 @@ files:
|
|
38
38
|
- VERSION
|
39
39
|
- lib/linecache.rb
|
40
40
|
- test/test-linecache.rb
|
41
|
+
- test/short-file
|
41
42
|
test_files: []
|
42
43
|
|
43
44
|
rdoc_options: []
|