/
Browser.pm
323 lines (276 loc) · 9.27 KB
/
Browser.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package Bio::Graphics::Browser;
# $Id: Browser.pm,v 1.235 2009-05-05 23:28:17 lstein Exp $
# Globals and utilities for GBrowse and friends
use strict;
use warnings;
use base 'Bio::Graphics::Browser::AuthorizedFeatureFile';
use File::Spec;
use File::Path 'mkpath';
use File::Basename 'dirname','basename';
use Text::ParseWords 'shellwords';
use File::Path 'mkpath';
use Bio::Graphics::Browser::DataSource;
use Bio::Graphics::Browser::Session;
use GBrowse::ConfigData;
use Carp 'croak','carp';
use CGI 'redirect','url';
my %CONFIG_CACHE;
our $VERSION = 1.992;
sub new {
my $class = shift;
my $config_file_path = shift;
# this code caches the config info so that we don't need to
# reparse in persistent (e.g. modperl) environment
my $mtime = (stat($config_file_path))[9];
if (exists $CONFIG_CACHE{$config_file_path}
&& $CONFIG_CACHE{$config_file_path}{mtime} >= $mtime) {
return $CONFIG_CACHE{$config_file_path}{object};
}
my $self = $class->SUPER::new(-file=>$config_file_path,
-safe=>1);
# a little trick here -- force the setting of "config_base" from the config file
# base if not explicitly overridden
unless ($self->setting('general' => 'config_base')) {
my $dir = dirname($config_file_path);
$self->setting('general' => 'config_base',$dir);
}
$CONFIG_CACHE{$config_file_path}{object} = $self;
$CONFIG_CACHE{$config_file_path}{mtime} = $mtime;
return $self;
}
## methods for dealing with paths
sub resolve_path {
my $self = shift;
my $path = shift;
my $path_type = shift; # one of "config" "htdocs" or "url"
return unless $path;
return $path if $path =~ m!^/!; # absolute path
return $path if $path =~ m!\|\s*$!; # a pipe
return $path if $path =~ m!^(http|ftp):!; # an URL
my $method = ${path_type}."_base";
$self->can($method) or croak "path_type must be one of 'config','htdocs', or 'url'";
my $base = $self->$method or return $path;
return File::Spec->catfile($base,$path);
}
sub config_path {
my $self = shift;
my $option = shift;
$self->resolve_path($self->setting(general => $option),'config');
}
sub htdocs_path {
my $self = shift;
my $option = shift;
$self->resolve_path($self->setting(general => $option),'htdocs')
|| "$ENV{DOCUMENT_ROOT}/gbrowse2";
}
sub url_path {
my $self = shift;
my $option = shift;
$self->resolve_path($self->setting(general => $option),'url');
}
sub config_base {$ENV{GBROWSE_CONF}
|| eval {shift->setting(general=>'config_base')}
|| GBrowse::ConfigData->config('conf')
|| '/etc/GBrowse2' }
sub htdocs_base {eval{shift->setting(general=>'htdocs_base')}
|| GBrowse::ConfigData->config('htdocs')
|| '/var/www/gbrowse2' }
sub url_base {eval{shift->setting(general=>'url_base')}
|| basename(GBrowse::ConfigData->config('htdocs'))
|| '/gbrowse2' }
sub tmp_base {eval{shift->setting(general=>'tmp_base')}
|| GBrowse::ConfigData->config('tmp')
|| '/tmp' }
sub db_base {eval{shift->setting(general=>'db_base')}
|| GBrowse::ConfigData->config('databases')
|| '//var/www/gbrowse2/databases' }
# these are url-relative options
sub button_url { shift->url_path('buttons') }
sub balloon_url { shift->url_path('balloons') }
sub js_url { shift->url_path('js') }
sub help_url { shift->url_path('gbrowse_help') }
sub stylesheet_url { shift->url_path('stylesheet') }
sub make_path {
my $self = shift;
my $path = shift;
return unless $path =~ /^(.+)$/;
$path = $1;
mkpath($path,0,0777) unless -d $path;
}
sub tmpdir {
my $self = shift;
my @components = @_;
my $path = File::Spec->catfile($self->tmp_base,@components);
$self->make_path($path) unless -d $path;
return $path;
}
sub user_dir {
my $self = shift;
my @components = @_;
return $self->tmpdir('userdata',@components);
}
sub tmpimage_dir {
my $self = shift;
return $self->tmpdir('images',@_);
}
sub image_url {
my $self = shift;
my $path = File::Spec->catfile($self->url_base,'i');
return $path;
}
sub cache_dir {
my $self = shift;
my $path = File::Spec->catfile($self->tmp_base,'cache',@_);
$self->make_path($path) unless -d $path;
return $path;
}
sub session_locks {
my $self = shift;
my $path = File::Spec->catfile($self->tmp_base,'locks',@_);
$self->make_path($path) unless -d $path;
return $path;
}
# return one of
# 'flock' -- standard flock locking
# 'nfs' -- use File::NFSLock
# 'mysql' -- use mysql advisory locks
sub session_locktype {
my $self = shift;
return $self->setting(general=>'session lock type') || 'default';
}
sub session_dir {
my $self = shift;
my $path = File::Spec->catfile($self->tmp_base,'sessions',@_);
$self->make_path($path) unless -d $path;
return $path;
}
sub slave_dir {
my $self = shift;
my $path = $self->setting(general=>'tmp_slave') || '/tmp/gbrowse_slave';
$self->make_path($path) unless -d $path;
return $path;
}
sub slave_status_path {
my $self = shift;
my $path = File::Spec->catfile($self->tmp_base,'slave_status');
return $path;
}
# these are relative to the config base
sub plugin_path { shift->config_path('plugin_path') }
sub language_path { shift->config_path('language_path') }
sub templates_path { shift->config_path('templates_path') }
sub moby_path { shift->config_path('moby_path') }
sub global_timeout { shift->setting(general=>'global_timeout') || 60 }
sub remember_settings_time { shift->setting(general=>'expire session') || '1M' }
sub cache_time { shift->setting(general=>'expire cache') || '2h' }
sub upload_time { shift->setting(general=>'expire uploads') || '6w' }
sub url_fetch_timeout { shift->setting(general=>'url_fetch_timeout') }
sub url_fetch_max_size { shift->setting(general=>'url_fetch_max_size') }
sub session_driver { shift->setting(general=>'session driver')
|| 'driver:file;serializer:default' }
sub session_args {
my $self = shift;
my %args = shellwords($self->setting(general=>'session args')||'');
return \%args if %args;
return {Directory=>$self->session_dir};
}
## methods for dealing with data sources
sub data_sources {
return sort shift->SUPER::configured_types();
}
sub data_source_description {
my $self = shift;
my $dsn = shift;
return $self->setting($dsn=>'description');
}
sub data_source_show {
my $self = shift;
my $dsn = shift;
return if $self->setting($dsn=>'hide');
return $self->authorized($dsn);
}
sub data_source_path {
my $self = shift;
my $dsn = shift;
$self->resolve_path($self->setting($dsn=>'path'),'config');
}
sub create_data_source {
my $self = shift;
my $dsn = shift;
my $path = $self->data_source_path($dsn) or return;
return Bio::Graphics::Browser::DataSource->new($path,$dsn,$self->data_source_description($dsn),$self);
}
sub default_source {
my $self = shift;
my $source = $self->setting(general => 'default source');
return $source if $self->valid_source($source);
return ($self->data_sources)[0];
}
sub valid_source {
my $self = shift;
my $proposed_source = shift;
return unless exists $self->{config}{$proposed_source};
my $path = $self->data_source_path($proposed_source) or return;
return -e $path || $path =~ /\|\s*$/;
}
sub get_source_from_cgi {
my $self = shift;
my $source = CGI::param('source') || CGI::param('src') || CGI::path_info();
$source =~ s!\#$!!; # get rid of trailing # left by IE
$source =~ s!^/+!!; # get rid of leading & trailing / from path_info()
$source =~ s!/+$!!;
$source;
}
sub update_data_source {
my $self = shift;
my $session = shift;
my $new_source = shift;
my $old_source = $session->source || $self->default_source;
$new_source ||= $self->get_source_from_cgi();
my $source;
if ($self->valid_source($new_source)) {
$session->source($new_source);
$source = $new_source;
} else {
carp "Invalid source $new_source";
my $fallback_source = $self->valid_source($old_source)
? $old_source
: $self->default_source;
$session->source($fallback_source);
$source = $fallback_source;
}
return $source;
}
sub time2sec {
my $self = shift;
my $time = shift;
$time =~ s/\s*#.*$//; # strip comments
my(%mult) = ('s'=>1,
'm'=>60,
'h'=>60*60,
'd'=>60*60*24,
'w'=>60*60*24*7,
'M'=>60*60*24*30,
'y'=>60*60*24*365);
my $offset = $time;
if (!$time || (lc($time) eq 'now')) {
$offset = 0;
} elsif ($time=~/^([+-]?(?:\d+|\d*\.\d*))([smhdwMy])/) {
$offset = ($mult{$2} || 1)*$1;
}
return $offset;
}
## methods for dealing with the session
sub session {
my $self = shift;
my $id = shift;
return Bio::Graphics::Browser::Session->new(driver => $self->session_driver,
id => $id||undef,
args => $self->session_args,
source => $self->default_source,
lockdir => $self->session_locks,
locktype => $self->session_locktype,
expires => $self->remember_settings_time,
);
}
1;