Commit 9babee5e authored by Leo Gordon's avatar Leo Gordon
Browse files

centralized the generation of unambig_url in URL module

parent cc430bb2
......@@ -73,7 +73,7 @@ sub find_by_url {
if(my $parsed_url = Bio::EnsEMBL::Hive::Utils::URL::parse( $url )) {
my $unambig_url = $parsed_url->{'unambig_url'};
my $unambig_key = Bio::EnsEMBL::Hive::Utils::URL::hash_to_unambig_url( $parsed_url );
my $query_params = $parsed_url->{'query_params'};
my $conn_params = $parsed_url->{'conn_params'};
......@@ -82,17 +82,17 @@ sub find_by_url {
my $hive_pipeline;
if($parsed_url->{'unambig_url'} eq ':///') {
if($unambig_key eq ':///') {
$hive_pipeline = $default_pipeline;
} elsif( not ($hive_pipeline = $class->pipelines_collection->{ $unambig_url }) ) {
} elsif( not ($hive_pipeline = $class->pipelines_collection->{ $unambig_key }) ) {
if($query_params and ($query_params->{'object_type'} eq 'NakedTable') ) { # do not check schema version when performing table dataflow:
$no_sql_schema_version_check = 1;
}
$class->pipelines_collection->{ $unambig_url } = $hive_pipeline = Bio::EnsEMBL::Hive::HivePipeline->new(
$class->pipelines_collection->{ $unambig_key } = $hive_pipeline = Bio::EnsEMBL::Hive::HivePipeline->new(
-url => $parsed_url->{'dbconn_part'},
-disconnect_when_inactive => $disconnect_when_inactive,
-no_sql_schema_version_check=> $no_sql_schema_version_check,
......
......@@ -196,14 +196,15 @@ sub init_pipeline {
unshift @ARGV, (-pipeline_url => $url, -hive_force_init => 1);
lives_ok(sub {
my $expected_url = Bio::EnsEMBL::Hive::Utils::URL::parse($url)->{unambig_url};
ok($expected_url, 'Given URL could be parsed');
my $orig_unambig_url = Bio::EnsEMBL::Hive::Utils::URL::parse($url)->{'unambig_url'};
ok($orig_unambig_url, 'Given URL could be parsed');
my $returned_url = Bio::EnsEMBL::Hive::Scripts::InitPipeline::init_pipeline($file_or_module, $tweaks);
ok($returned_url, 'pipeline initialized on '.$returned_url);
my $got_url = Bio::EnsEMBL::Hive::Utils::URL::parse($returned_url)->{unambig_url};
# $url has the password but may be missing the port number
# parse() returns the port number but is always missing the password
is($got_url, $expected_url, 'pipeline initialized on '.$url);
my $returned_unambig_url = Bio::EnsEMBL::Hive::Utils::URL::parse($returned_url)->{'unambig_url'};
# Both $url and $returned_url MAY contain the password (if applicable for the driver) but can be missing the port number assuming a default
# Both $orig_unambig_url and $returned_unambig_url SHOULD contain the port number (if applicable for the driver) but WILL NOT contain a password
is($returned_unambig_url, $orig_unambig_url, 'pipeline initialized on '.$url);
}, sprintf('init_pipeline("%s", %s)', $file_or_module, stringify($options)));
}
......
......@@ -40,7 +40,7 @@ sub parse {
my $url = shift @_ or return;
my ($old_parse, $new_parse,
$dbconn_part, $driver, $user, $pass, $host, $port, $dbname, $table_name, $tparam_name, $tparam_value, $conn_param_string, $query_part);
$dbconn_part, $url_parts_hash, $table_name, $tparam_name, $tparam_value, $conn_param_string, $query_part);
# In case the whole URL is quoted (should we do this with double-quotes too ?)
if( $url=~/^'(.*)'$/ ) {
......@@ -56,7 +56,7 @@ sub parse {
} else {
if( ($dbconn_part, $driver, $user, $pass, $host, $port, $dbname, $table_name, $tparam_name, $tparam_value, $conn_param_string) =
if( ($dbconn_part, @$url_parts_hash{'driver', 'user', 'pass', 'host', 'port', 'dbname'}, $table_name, $tparam_name, $tparam_value, $conn_param_string) =
$url =~ m{^((\w*)://(?:(\w+)(?:\:([^/\@]*))?\@)?(?:([\w\-\.]+)(?:\:(\d*))?)?/([\w\-\.]*))(?:/(\w+)(?:\?(\w+)=([\w\[\]\{\}]*))?)?((?:;(\w+)=(\w+))*)$} ) {
my ($dummy, %conn_params) = split(/[;=]/, $conn_param_string // '' );
......@@ -89,18 +89,11 @@ sub parse {
if($exception_from_OLD_format) {
warn "\nOLD URL parser thinks you are using the NEW URL syntax for a remote $query_params->{'object_type'}, so skipping it (it may be wrong!)\n";
} else {
my $unambig_port = $port // { 'mysql' => 3306, 'pgsql' => 5432, 'sqlite' => '' }->{$driver//''} // '';
my $unambig_host = ($host//'') eq 'localhost' ? '127.0.0.1' : ($host//'');
my $unambig_url = ($driver//'') .'://'. ($user ? $user.'@' : '') . $unambig_host . ( $unambig_port ? ':'.$unambig_port : '') .'/'. ($dbname//'');
my $unambig_url = hash_to_unambig_url( $url_parts_hash );
$old_parse = {
'dbconn_part' => $dbconn_part,
'driver' => $driver,
'user' => $user,
'pass' => $pass,
'host' => $host,
'port' => $port,
'dbname' => $dbname,
%$url_parts_hash,
'conn_params' => \%conn_params,
'query_params' => $query_params,
'unambig_url' => $unambig_url,
......@@ -108,13 +101,15 @@ sub parse {
}
} # /if OLD format
if( ($dbconn_part, $driver, $user, $pass, $host, $port, $dbname, $query_part, $conn_param_string) =
if( ($dbconn_part, @$url_parts_hash{'driver', 'user', 'pass', 'host', 'port', 'dbname'}, $query_part, $conn_param_string) =
$url =~ m{^((\w+)://(?:(\w+)(?:\:([^/\@]*))?\@)?(?:([\w\-\.]+)(?:\:(\d*))?)?(?:/([/~\w\-\.]*))?)?(?:\?(\w+=[\w\[\]\{\}]*(?:&\w+=[\w\[\]\{\}]*)*))?(;\w+=\w+(?:;\w+=\w+)*)?$} ) {
my ($dummy, %conn_params) = split(/[;=]/, $conn_param_string // '' );
my $query_params = $query_part ? { split(/[&=]/, $query_part ) } : undef;
my $exception_from_NEW_format;
my ($driver, $dbname) = @$url_parts_hash{'driver', 'dbname'};
if(!$query_params and ($driver eq 'mysql' or $driver eq 'pgsql') and $dbname and $dbname=~m{/}) { # a special case of multipart dbpath hints at the OLD format (or none at all)
$query_params = { 'object_type' => 'NakedTable' };
......@@ -148,18 +143,11 @@ sub parse {
if($exception_from_NEW_format) {
warn "\nNEW URL parser thinks you are using the OLD URL syntax for a remote $query_params->{'object_type'}, so skipping it (it may be wrong!)\n";
} else {
my $unambig_port = $port // { 'mysql' => 3306, 'pgsql' => 5432, 'sqlite' => '' }->{$driver//''} // '';
my $unambig_host = ($host//'') eq 'localhost' ? '127.0.0.1' : ($host//'');
my $unambig_url = ($driver//'') .'://'. ($user ? $user.'@' : '') . $unambig_host . ( $unambig_port ? ':'.$unambig_port : '') .'/'. ($dbname//'');
my $unambig_url = hash_to_unambig_url( $url_parts_hash );
$new_parse = {
'dbconn_part' => $dbconn_part,
'driver' => $driver,
'user' => $user,
'pass' => $pass,
'host' => $host,
'port' => $port,
'dbname' => $dbname,
%$url_parts_hash,
'conn_params' => \%conn_params,
'query_params' => $query_params,
'unambig_url' => $unambig_url,
......@@ -188,12 +176,35 @@ sub parse {
}
=head2 hash_to_unambig_url
Arg [1] : a hash describing (at least) db connection parameters
Example : my $unambig_url = hash_to_unambig_url( $url_parts_hash );
Description : Generates a degenerate URL that omits unnecessary parts (password, default port numbers)
: but tries to uniquely represent a connection.
Returntype : a string
=cut
sub hash_to_unambig_url {
my $url_parts_hash = shift @_; # expected to contain the parts from which to build a URL
my $driver = $url_parts_hash->{'driver'} // '';
my $unambig_port = $url_parts_hash->{'port'} // { 'mysql' => 3306, 'pgsql' => 5432, 'sqlite' => '' }->{$driver} // '';
my $unambig_host = ( $url_parts_hash->{'host'} // '' ) eq 'localhost' ? '127.0.0.1' : ( $url_parts_hash->{'host'} // '' );
my $unambig_url = $driver .'://'. ($url_parts_hash->{'user'} ? $url_parts_hash->{'user'}.'@' : '')
. $unambig_host . ( $unambig_port ? ':'.$unambig_port : '') .'/'. ( $url_parts_hash->{'dbname'} // '' );
return $unambig_url;
}
=head2 hash_to_url
Arg [1] : a hash describing a db connection, or accumulator, as generated by parse_url
Example : my $parse = parse_url($url1); my $url2 = hash_to_url($parse);
Description : Generates a "new-style" URL from a hash containing the parse of a URL
: (old or new style). In cases where there a trailing slash is optional, it leaves
: (old or new style). In cases where a trailing slash is optional, it leaves
: off the trailing slash
Returntype : a URL as a string
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment