diff --git a/OsmApi.pm b/OsmApi.pm index 72b1cc2..2e02577 100644 --- a/OsmApi.pm +++ b/OsmApi.pm @@ -27,21 +27,31 @@ our $auth_token; BEGIN { + my $prefs_filename = home()."/.osmtoolsrc"; + my $prefs_eol; - $prefs = { "dryrun" => 1 }; - my $prefs_eol = 1; + sub read_prefs_file { + $prefs = { "dryrun" => 1 }; + $prefs_eol = 1; - open (PREFS, home()."/.osmtoolsrc") or die "cannot open ". home()."/.osmtoolsrc"; - while() - { - if (/^([^=]*)=(.*)/) + open (PREFS, $prefs_filename) or die "cannot open $prefs_filename"; + while() { - $prefs->{$1} = $2; + if (/^([^=]*)=(.*)/) + { + $prefs->{$1} = $2; + } + $prefs_eol = substr ($_, -1) eq "\n"; } - $prefs_eol = substr ($_, -1) eq "\n"; + close (PREFS); } - close (PREFS); - + + read_prefs_file; + if ($prefs->{local}) { + $prefs_filename = "./.osmtoolsrc"; + read_prefs_file; + } + # override user name and password from environment if given $prefs->{username} = $ENV{OSMTOOLS_USERNAME} if (defined($ENV{OSMTOOLS_USERNAME})); $prefs->{password} = $ENV{OSMTOOLS_PASSWORD} if (defined($ENV{OSMTOOLS_PASSWORD})); @@ -76,13 +86,13 @@ BEGIN foreach my $required("username","password","apiurl") { - die home()."/.osmtoolsrc does not have $required" unless defined($prefs->{$required}); + die "$prefs_filename does not have $required" unless defined($prefs->{$required}); } if (!defined($prefs->{instance})) { $prefs->{instance} = sprintf "%010x", $$ * rand(100000000); - open(PREFS, ">>".home()."/.osmtoolsrc"); + open(PREFS, ">>$prefs_filename"); printf PREFS "\n" unless $prefs_eol; printf PREFS "instance=".$prefs->{instance}; close(PREFS); @@ -113,6 +123,8 @@ BEGIN { $prefs->{'weburl'} = $1; } + + print STDERR "Read config from $prefs_filename\n" if ($prefs->{debug}); } sub login diff --git a/README.md b/README.md index 3d3c763..9faa3b5 100644 --- a/README.md +++ b/README.md @@ -47,17 +47,23 @@ These scripts do not have safety nets. Be sure that you feel confident to fix an Configuration ------------- -You will have to create a file named .osmtoolsrc in your home directory containg your user name, password, and the URL of the OSM server to use. The URL must be complete up to the API version number and the slash afterwards, so: +You will have to create a file named `.osmtoolsrc` in your home directory containing your user name, password, and the URL of the OSM server to use. The URL must be complete up to the API version number and the slash afterwards, so: username=fred password=test apiurl=http://api06.dev.openstreetmap.org/api/0.6/ -If your username or password is not specified in your .osmtoolsrc file, these scripts will look for OSMTOOLS_USERNAME and OSMTOOLS_PASSWORD environment variables. As a last resort, you will be prompted for a user name or password on the command line (requires the Term::ReadKey module). +If your username or password is not specified in your `.osmtoolsrc` file, these scripts will look for `OSMTOOLS_USERNAME` and `OSMTOOLS_PASSWORD` environment variables. As a last resort, you will be prompted for a user name or password on the command line (requires the `Term::ReadKey` module). -By default, all tools will run in "dry run" mode, so no changes will be actually written and all write requests will be considered successful. Add the "dryrun=0" parameter to the file for live action. +By default, all tools will run in "dry run" mode, so no changes will be actually written and all write requests will be considered successful. Add the `dryrun=0` parameter to the file for live action. -By default, "dry run" also enables "debug" so you are shown the requests made. If you want to keep debug mode when setting dryrun=0, explicitly set debug=1. There's also debug_request_headers and debug_request_body to print out details about the HTML messages, and the same for responses. +By default, "dry run" also enables "debug" so you are shown the requests made. If you want to keep debug mode when setting `dryrun=0`, explicitly set `debug=1`. There's also `debug_request_headers` and `debug_request_body` to print out details about the HTML messages, and the same for responses. + +### Local configuration + +There's also an alternative way to configure osmtools by reading `.osmtoolsrc` from a local directory instead of your home directory. This allows you to have different configurations ready which is useful if you want to modify the scripts. You may want one configuration ready for a development server (for example, api06.dev.openstreetmap.org) to test your modifications and another one for a live server (for example, api.openstreetmap.org) to use the modified scripts on real data. + +To enable reading preferences from a local directory, `.osmtoolsrc` in your *home* directory should have `local=1` set. In this case the rest of preferences in the home directory file is ignored and `.osmtoolsrc` in the current local directory is read instead. SCRAPE SCRAPE SCRAPE --------------------