2014-01-29 23:00:52 +08:00
|
|
|
#!/usr/bin/env node
|
2012-11-07 17:59:39 +08:00
|
|
|
|
2014-01-29 23:00:52 +08:00
|
|
|
var path = require('path');
|
|
|
|
var redis = require('redis');
|
|
|
|
var Step = require('step');
|
2012-11-07 17:59:39 +08:00
|
|
|
|
|
|
|
|
2014-01-29 23:00:52 +08:00
|
|
|
function usage(me, exitcode) {
|
|
|
|
console.log("Usage: " + me + " [--env <environment>] <username> [<tablename>|~<token>]");
|
|
|
|
process.exit(exitcode);
|
|
|
|
}
|
2014-01-17 01:51:02 +08:00
|
|
|
|
2014-01-29 23:00:52 +08:00
|
|
|
var node_path = process.argv.shift();
|
|
|
|
var script_path = process.argv.shift();
|
|
|
|
var basedir = path.dirname(script_path);
|
|
|
|
var me = path.basename(script_path);
|
2014-01-17 01:51:02 +08:00
|
|
|
|
2014-01-29 23:00:52 +08:00
|
|
|
var ENV = 'development.js';
|
|
|
|
var username, token;
|
|
|
|
var arg;
|
|
|
|
while ( arg = process.argv.shift() ) {
|
|
|
|
if ( arg == '--env' ) {
|
|
|
|
ENV = process.argv.shift();
|
|
|
|
}
|
|
|
|
else if ( ! username ) {
|
|
|
|
username = arg;
|
|
|
|
}
|
|
|
|
else if ( ! token ) {
|
|
|
|
token = arg;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.warn("Unused parameter " + arg);
|
|
|
|
}
|
|
|
|
}
|
2014-01-17 01:51:02 +08:00
|
|
|
|
2014-01-29 23:00:52 +08:00
|
|
|
if ( ! username ) usage(me, 1);
|
2014-01-17 01:51:02 +08:00
|
|
|
|
2014-01-29 23:00:52 +08:00
|
|
|
console.log("Using environment " + ENV);
|
2012-11-07 17:59:39 +08:00
|
|
|
|
2014-01-29 23:00:52 +08:00
|
|
|
global.environment = require('../config/environments/' + ENV);
|
|
|
|
var serverOptions = require('../lib/cartodb/server_options'); // _after_ setting global.environment
|
2012-11-07 17:59:39 +08:00
|
|
|
|
2014-01-29 23:00:52 +08:00
|
|
|
var client;
|
|
|
|
var dbname;
|
|
|
|
Step(
|
|
|
|
function getClient() {
|
|
|
|
client = redis.createClient(serverOptions.redis.port, serverOptions.redis.host);
|
|
|
|
client.on('connect', this);
|
|
|
|
},
|
|
|
|
function getUserMeta(err) {
|
|
|
|
if ( err ) throw err;
|
|
|
|
client.select(5);
|
|
|
|
client.hgetall('rails:users:' + username, this);
|
|
|
|
},
|
|
|
|
function readDB(err, data) {
|
|
|
|
if ( err ) throw err;
|
|
|
|
if ( ! data )
|
|
|
|
throw new Error('Username ' + username + ' unknown by redis on port '
|
|
|
|
+ serverOptions.redis.port + ' (try CARTODB/script/restore_redis?)');
|
|
|
|
//console.log("Data:"); console.dir(data);
|
|
|
|
dbname = data['database_name'];
|
|
|
|
console.log("Database name for user " + username + ": " + dbname);
|
|
|
|
client.select(0);
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function showTokens(err) {
|
|
|
|
if ( err ) throw err;
|
|
|
|
if ( token ) return null;
|
|
|
|
var next = this;
|
|
|
|
Step(
|
|
|
|
function getTokens() {
|
|
|
|
client.keys('map_style|' + dbname + '|*', this);
|
|
|
|
},
|
|
|
|
function showTokens(err, data) {
|
|
|
|
if (err) throw err;
|
|
|
|
if ( data ) console.log(data.join('\n'));
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function showTokensFinish(err) {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function showStyle(err) {
|
|
|
|
if ( err ) throw err;
|
|
|
|
if ( ! token ) return null;
|
|
|
|
var next = this;
|
|
|
|
Step(
|
|
|
|
function getStyle() {
|
|
|
|
client.get('map_style|' + dbname + '|' + token, this);
|
|
|
|
},
|
|
|
|
function showStyle(err, data) {
|
|
|
|
if ( err ) throw err;
|
|
|
|
if ( ! data ) {
|
|
|
|
throw new Error(token + ': no such map style known by redis on port '
|
|
|
|
+ serverOptions.redis.port);
|
|
|
|
}
|
|
|
|
//console.log("data: " + data);
|
|
|
|
var x=JSON.parse(data);
|
|
|
|
printMapnikStyle(x, this);
|
|
|
|
},
|
|
|
|
function showStyleFinish(err) {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function finish(err) {
|
|
|
|
if ( err ) {
|
|
|
|
console.error(err.message)
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
);
|
2014-01-17 01:51:02 +08:00
|
|
|
|
2014-01-29 23:00:52 +08:00
|
|
|
function printMapnikStyle(x, callback) {
|
|
|
|
console.log('style: ' + x.style);
|
|
|
|
console.log('version: ' + x.version);
|
|
|
|
var grainstore = require(basedir + '/../node_modules/windshaft/node_modules/grainstore/lib/grainstore');
|
|
|
|
var mml_store = new grainstore.MMLStore(serverOptions.redis, serverOptions.grainstore);
|
|
|
|
var builderconfig = {dbname:dbname};
|
|
|
|
if ( token.match(/^~/) ) {
|
|
|
|
builderconfig.token = token.substring(1);
|
|
|
|
} else {
|
|
|
|
builderconfig.table = token;
|
|
|
|
}
|
|
|
|
var mml_builder;
|
|
|
|
Step(
|
|
|
|
function getBuilder() {
|
|
|
|
mml_builder = mml_store.mml_builder(builderconfig, this);
|
|
|
|
},
|
|
|
|
function getXML(err, builder) {
|
|
|
|
if ( err ) throw err;
|
|
|
|
mml_builder.toXML(this);
|
|
|
|
},
|
|
|
|
function showXML(err, xml) {
|
|
|
|
if ( err ) throw err;
|
|
|
|
console.log('- XML - ');
|
|
|
|
console.log(xml);
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function finish(err) {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
);
|
2014-01-18 00:47:37 +08:00
|
|
|
}
|
2014-01-29 23:00:52 +08:00
|
|
|
|