Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
galaxyproject
GitHub Repository: galaxyproject/training-material
Path: blob/main/_plugins/gtn/mod.rb
1677 views
1
# frozen_string_literal: true
2
3
OUR_PATH = __dir__
4
# two directories up
5
ROOT_PATH = File.expand_path(File.join(OUR_PATH, '..', '..'))
6
7
module Gtn
8
# Module for obtaining modification times of files.
9
# It walks the git history to record the last time a file was modified.
10
# This is faster than talking to the file system.
11
module ModificationTimes
12
@@TIME_CACHE = nil
13
@@COMMIT_COUNT_CACHE = nil
14
15
def self.init_cache
16
return unless @@TIME_CACHE.nil?
17
18
@@TIME_CACHE = {}
19
@@COMMIT_COUNT_CACHE = Hash.new(0)
20
Jekyll.logger.info '[GTN/Time/Mod] Filling Time Cache'
21
cached_command
22
.split('GTN_GTN:')
23
.map { |x| x.split("\n\n") }
24
.select { |x| x.length > 1 }
25
.each do |date, files|
26
files.split(/\n/).each do |f|
27
@@TIME_CACHE[f] = Time.at(date.to_i) if !@@TIME_CACHE.key? f
28
@@COMMIT_COUNT_CACHE[f] += 1
29
end
30
end
31
end
32
33
def self.discover_caches
34
# Really there should only be one, but maybe someone's been silly so
35
# we'll just take the first one we find.
36
Dir.glob('metadata/git-mod-*.txt').first
37
end
38
39
def self.generate_cache
40
rev = `git rev-list -n 1 main`.strip
41
42
if discover_caches.nil?
43
File.write("metadata/git-mod-#{rev}.txt", command)
44
else
45
prev = discover_caches
46
results = cached_command
47
File.delete(prev)
48
File.write("metadata/git-mod-#{rev}.txt", results)
49
end
50
end
51
52
def self.cached_command
53
return command if discover_caches.nil?
54
55
Jekyll.logger.info '[GTN/Time/Mod] Using cached modification times'
56
57
previous_commit = discover_caches.split('-').last.split('.').first
58
previous = File.read(discover_caches)
59
60
`git log --first-parent --name-only --pretty='GTN_GTN:%ct' #{previous_commit}..` + previous
61
end
62
63
def self.command
64
`git log --first-parent --name-only --pretty='GTN_GTN:%ct'`
65
end
66
67
def self.time_cache
68
@@TIME_CACHE
69
end
70
71
def self.commit_count_cache
72
@@COMMIT_COUNT_CACHE
73
end
74
75
def self.clean_path(f)
76
if f =~ %r{^\./}
77
f[2..]
78
elsif f =~ %r{^/}
79
f.gsub(ROOT_PATH, '')
80
else
81
f
82
end
83
end
84
85
def self.obtain_modification_count(f_unk)
86
f = clean_path(f_unk)
87
init_cache
88
if @@COMMIT_COUNT_CACHE.key? f
89
@@COMMIT_COUNT_CACHE[f]
90
else
91
0
92
end
93
end
94
95
def self.obtain_time(f_unk)
96
f = clean_path(f_unk)
97
init_cache
98
if @@TIME_CACHE.key? f
99
@@TIME_CACHE[f]
100
else
101
begin
102
# Non git file.
103
@@TIME_CACHE[f] = File.mtime(f)
104
Jekyll.logger.warn "[GTN/Time/Mod] No git cached time available for #{f}, defaulting to checkout"
105
@@TIME_CACHE[f]
106
rescue StandardError
107
Time.at(0)
108
end
109
end
110
end
111
end
112
113
# Module for obtaining original publication times of files.
114
# It walks the git history to record the last time a file was modified.
115
# This is faster than talking to the file system.
116
module PublicationTimes
117
@@TIME_CACHE = nil
118
@@RENAMES = nil
119
120
def self.chase_rename(path, depth: 0)
121
if @@RENAMES.nil?
122
self.init_cache
123
end
124
125
if @@RENAMES.key? path
126
# TODO(hexylena)
127
# This happens because it's the wrong datastructure, if there's a loop
128
# in there, it'll just cycle through it endlessly.
129
# This is obviously bad. But it'll do for now because it doesn't affect
130
# any of our core files. We should replace this in the future.
131
# This is why we have the grep_v below, to weed out the problematic files.
132
if depth > 10
133
Jekyll.logger.error "[GTN/Time/Pub] Too many renames for #{path}"
134
path
135
else
136
chase_rename(@@RENAMES[path], depth: depth + 1)
137
end
138
else
139
path
140
end
141
end
142
143
def self.init_cache
144
return unless @@TIME_CACHE.nil?
145
146
@@TIME_CACHE = {}
147
@@RENAMES = {}
148
149
Jekyll.logger.info '[GTN/Time/Pub] Filling Publication Time Cache'
150
cached_command
151
.split('GTN_GTN:')
152
.map { |x| x.split("\n\n") }
153
.select { |x| x.length > 1 }
154
.each do |date, files|
155
files.split("\n").grep_v(/\.(png|json|_ga|jpg)/).each do |f|
156
modification_type, path = f.split("\t")
157
if modification_type == 'A'
158
# Chase the renames.
159
final_filename = chase_rename(path)
160
@@TIME_CACHE[final_filename] = Time.at(date.to_i)
161
elsif modification_type[0] == 'R'
162
_, moved_from, moved_to = f.split("\t")
163
@@RENAMES[moved_from] = moved_to # Point from the 'older' version to the newer.
164
end
165
end
166
end
167
# pp renames
168
end
169
170
def self.discover_caches
171
# Really there should only be one, but maybe someone's been silly so
172
# we'll just take the first one we find.
173
Dir.glob('metadata/git-pub-*.txt').first
174
end
175
176
def self.generate_cache
177
rev = `git rev-list -n 1 main`.strip
178
179
if discover_caches.nil?
180
File.write("metadata/git-pub-#{rev}.txt", command)
181
else
182
prev = discover_caches
183
results = cached_command
184
File.delete(prev)
185
File.write("metadata/git-pub-#{rev}.txt", results)
186
end
187
end
188
189
def self.cached_command
190
return command if discover_caches.nil?
191
192
Jekyll.logger.info '[GTN/Time/Pub] Using cached publication times'
193
194
previous_commit = discover_caches.split('-').last.split('.').first
195
previous = File.read(discover_caches)
196
197
`git log --first-parent --name-status --diff-filter=AR --pretty='GTN_GTN:%ct' #{previous_commit}..` + previous
198
end
199
200
def self.command
201
`git log --first-parent --name-status --diff-filter=AR --pretty='GTN_GTN:%ct' `
202
end
203
204
def self.time_cache
205
@@TIME_CACHE
206
end
207
208
def self.clean_path(f)
209
if f =~ %r{^\./}
210
f[2..]
211
elsif f =~ %r{^/}
212
f.gsub(ROOT_PATH, '')
213
else
214
f
215
end
216
end
217
218
def self.obtain_time(f_unk)
219
f = clean_path(f_unk)
220
init_cache
221
if @@TIME_CACHE.key? f
222
@@TIME_CACHE[f]
223
else
224
begin
225
# Non git file.
226
@@TIME_CACHE[f] = File.mtime(f)
227
Jekyll.logger.warn "[GTN/Time/Pub] No git cached time available for #{f}, defaulting to checkout"
228
@@TIME_CACHE[f]
229
rescue StandardError
230
Time.at(0)
231
end
232
end
233
end
234
end
235
end
236
237
if $PROGRAM_NAME == __FILE__
238
# Gtn::ModificationTimes.init_cache
239
# pp Gtn::ModificationTimes.commit_count_cache
240
241
puts ' Moved to bin/list-recently-modified.rb'
242
# Gtn::PublicationTimes.init_cache
243
# Gtn::PublicationTimes.time_cache.select do |_, v|
244
# # Things in last 6 months
245
# v > Time.now - (6 * 30 * 24 * 60 * 60)
246
# end.map { |k, v| puts "#{v} #{k}" }
247
end
248
249