<commit_before>#!/usr/bin/env node
/**
 * Spaceify Package Manager, 30.4.2014 Spaceify Inc.
 * 
 * @file spm-js
 * 
 * #/usr/bin/spm
 */

var fs = require("fs");
var read = require("read");
var http = require("http");
var qs = require("querystring");
var fibrous = require("fibrous");
var logger = require("./logger");
var Config = require("./config")();
var Const = require("./constants");
var Utility = require("./utility");
var Language = require("./language");
var AppManager = require("./appmanager");
var appManagerRPCClient = require("./websocketrpcclient");
var spmRPCServer = require("./websocketrpcserver");

function SPM()
{
var self = this;

var spmRPCServer = null;
var appManagerRPCClient = null;
var appManager = new AppManager();

var INSTALL = "install";
var REMOVE = "remove";
var START = "start";
var STOP = "stop";
var RESTART = "restart";
var PUBLISH = "publish";
var SOURCE = "source";
var LIST = "list";
var HELP = "help";
var QUICKHELP = "quickhelp";
var AUTHENTICATE = "authenticate";
var CERTIFICATE = "certificate";
var SPACELET = "spacelet";
var SANDBOXED = "sandboxed";
var NATIVE = "native";
var VERBOSE = "verbose";

var commands = INSTALL+"|"+REMOVE+"|"+START+"|"+STOP+"|"+RESTART+"|"+PUBLISH+"|"+SOURCE+"|"+LIST+"|"+HELP;
var oper_regex = new RegExp("^(" + commands + ")$");
var options = AUTHENTICATE+"|"+CERTIFICATE+"|"+SPACELET+"|"+SANDBOXED+"|"+NATIVE+"|"+VERBOSE;
var opts_regex = new RegExp("^(" + options + ")$");



self.start = fibrous( function()
	{
	var command = "";
	var package = "";
	var authenticate = false;
	var certificate = false;
	var type = [];
	var verbose = false;
	var username = "";
	var password = "";

	logger.force();
	logger.force("Spaceify Package Manager v" + Const.SPM_VERSION);

	try {
		// CHECK INPUT (node ..path/spm.js command ...)
		if(process.argv.length < 3)
			process.argv.push(QUICKHELP);//throw Utility.ferror(false, Language.E_WRONG_NUMBER_OF_ARGUMENTS.p("SPM::start()"), {":commands": commands, ":options": options});

		command = process.argv[2].trim();																// command is always the third argument
		if(command.search(oper_regex) == -1 && command != QUICKHELP)
			throw Utility.ferror(false, Language.E_UNKNOW_COMMAND.p("SPM::start()"), {":command": command, ":commands": commands});

		if((command != HELP && command != QUICKHELP && command != LIST) && process.argv.length < 4)		// help and list do not need options or package name
			throw Utility.ferror(false, Language.E_WRONG_NUMBER_OF_ARGUMENTS.p("SPM::start()"), {":commands": commands, ":options": options});

		for(var i=3; i<process.argv.length; i++)														// options are always after $>spm command, ignore if not recognized as an option
			{
			if(process.argv[i] == AUTHENTICATE)
				authenticate = true;
			if(process.argv[i] == CERTIFICATE)
				certificate = true;
			if(process.argv[i] == SPACELET || process.argv[i] == SANDBOXED || process.argv[i] == NATIVE)
				{
				if(type.indexOf(process.argv[i]) == -1)
					type.push(process.argv[i]);
				}
			if(process.argv[i] == VERBOSE)
				verbose = true;
			}

		if(command != HELP && command != LIST)
			package = process.argv[process.argv.length - 1].trim();								// package is the last argument

		// OPTIONS
		if((authenticate && (command == INSTALL || command == SOURCE)) || command == PUBLISH)
			{
			var auth_host = "";
			if(command == INSTALL)
				auth_host = package;
			else if(command == PUBLISH || command == SOURCE)
				auth_host = Config.REGISTRY_HOSTNAME;

			logger.force(Utility.replace(Language.AUTHENTICATION, {":auth_host": auth_host}));
			username = read.sync({ prompt: Language.USERNAME });
			password = read.sync({ prompt: Language.PASSWORD, silent: true });
			}

		// DO THE REQUESTED COMMAND
		logger.force(Utility.ucfirst(command) + " " + package);
		logger.force();

		openSPMServer.sync();																	// Open a WebSocket server for relaying messages from the running AppManager

		connectAppManagerClient.sync();															// Try to open a JSON-RPC connection to the AppManager

		if(command == INSTALL)
			install.sync(package, username, password, certificate);
		else if(command == PUBLISH)
			publish.sync(package, username, password);
		else if(command == SOURCE)
			source.sync(package, username, password);
		else if(command == REMOVE)
			remove.sync(package);
		else if(command == START)
			start.sync(package);
		else if(command == STOP)
			stop.sync(package);
		else if(command == RESTART)
			restart.sync(package);
		else if(command == LIST)
			list.sync(type, verbose);
		else if(command == HELP || command == QUICKHELP)
			help.sync(command == HELP ? true : false);
		}
	catch(err)
		{
		Utility.printErrors(err);
		}
	finally
		{
		closeAppManagerClient();
		closeSPMServer();
		}
	});

var connectAppManagerClient = fibrous( function()
	{
	try {
		appManagerRPCClient = new appManagerRPCClient();
		appManagerRPCClient.sync.connect({hostname: null, port: Config.APPMAN_PORT_WEBSOCKET, isSsl: false, persistent: true, owner: "spm"});
		}
	catch(err)
		{
		appManagerRPCClient = null;
		}
	});

var closeAppManagerClient = function()
	{
	if(appManagerRPCClient)
		appManagerRPCClient.close();
	appManagerRPCClient = null;
	}

var openSPMServer = fibrous( function()
	{
	try {
		spmRPCServer = new spmRPCServer();
		spmRPCServer.exposeRPCMethod("messages", self, self.messages);
		spmRPCServer.connect.sync({hostname: null, port: Config.SPM_PORT_WEBSOCKET, isSsl: false, owner: "spm"});
		}
	catch(err)
		{
		spmRPCServer = null;
		}
	});

var closeSPMServer = function()
	{<commit_msg>spm fix<commit_after>#!/usr/bin/env node
/**
 * Spaceify Package Manager, 30.4.2014 Spaceify Inc.
 * 
 * @file spm-js
 * 
 * #/usr/bin/spm
 */

var fs = require("fs");
var read = require("read");
var http = require("http");
var qs = require("querystring");
var fibrous = require("fibrous");
var logger = require("./logger");
var Config = require("./config")();
var Const = require("./constants");
var Utility = require("./utility");
var Language = require("./language");
var AppManager = require("./appmanager");
var AppManagerRPCClient = require("./websocketrpcclient");
var SpmRPCServer = require("./websocketrpcserver");

function SPM()
{
var self = this;

var spmRPCServer = null;
var appManagerRPCClient = null;
var appManager = new AppManager();

var INSTALL = "install";
var REMOVE = "remove";
var START = "start";
var STOP = "stop";
var RESTART = "restart";
var PUBLISH = "publish";
var SOURCE = "source";
var LIST = "list";
var HELP = "help";
var QUICKHELP = "quickhelp";
var AUTHENTICATE = "authenticate";
var CERTIFICATE = "certificate";
var SPACELET = "spacelet";
var SANDBOXED = "sandboxed";
var NATIVE = "native";
var VERBOSE = "verbose";

var commands = INSTALL+"|"+REMOVE+"|"+START+"|"+STOP+"|"+RESTART+"|"+PUBLISH+"|"+SOURCE+"|"+LIST+"|"+HELP;
var oper_regex = new RegExp("^(" + commands + ")$");
var options = AUTHENTICATE+"|"+CERTIFICATE+"|"+SPACELET+"|"+SANDBOXED+"|"+NATIVE+"|"+VERBOSE;
var opts_regex = new RegExp("^(" + options + ")$");

self.start = fibrous( function()
	{
	var command = "";
	var package = "";
	var authenticate = false;
	var certificate = false;
	var type = [];
	var verbose = false;
	var username = "";
	var password = "";

	logger.force();
	logger.force("Spaceify Package Manager v" + Const.SPM_VERSION);

	try {
		// CHECK INPUT (node ..path/spm.js command ...)
		if(process.argv.length < 3)
			process.argv.push(QUICKHELP);//throw Utility.ferror(false, Language.E_WRONG_NUMBER_OF_ARGUMENTS.p("SPM::start()"), {":commands": commands, ":options": options});

		command = process.argv[2].trim();																// command is always the third argument
		if(command.search(oper_regex) == -1 && command != QUICKHELP)
			throw Utility.ferror(false, Language.E_UNKNOW_COMMAND.p("SPM::start()"), {":command": command, ":commands": commands});

		if((command != HELP && command != QUICKHELP && command != LIST) && process.argv.length < 4)		// help and list do not need options or package name
			throw Utility.ferror(false, Language.E_WRONG_NUMBER_OF_ARGUMENTS.p("SPM::start()"), {":commands": commands, ":options": options});

		for(var i=3; i<process.argv.length; i++)														// options are always after $>spm command, ignore if not recognized as an option
			{
			if(process.argv[i] == AUTHENTICATE)
				authenticate = true;
			if(process.argv[i] == CERTIFICATE)
				certificate = true;
			if(process.argv[i] == SPACELET || process.argv[i] == SANDBOXED || process.argv[i] == NATIVE)
				{
				if(type.indexOf(process.argv[i]) == -1)
					type.push(process.argv[i]);
				}
			if(process.argv[i] == VERBOSE)
				verbose = true;
			}

		if(command != HELP && command != LIST)
			package = process.argv[process.argv.length - 1].trim();								// package is the last argument

		// OPTIONS
		if((authenticate && (command == INSTALL || command == SOURCE)) || command == PUBLISH)
			{
			var auth_host = "";
			if(command == INSTALL)
				auth_host = package;
			else if(command == PUBLISH || command == SOURCE)
				auth_host = Config.REGISTRY_HOSTNAME;

			logger.force(Utility.replace(Language.AUTHENTICATION, {":auth_host": auth_host}));
			username = read.sync({ prompt: Language.USERNAME });
			password = read.sync({ prompt: Language.PASSWORD, silent: true });
			}

		// DO THE REQUESTED COMMAND
		logger.force(Utility.ucfirst(command) + " " + package);
		logger.force();

		openSPMServer.sync();																	// Open a WebSocket server for relaying messages from the running AppManager

		connectAppManagerClient.sync();															// Try to open a JSON-RPC connection to the AppManager

		if(command == INSTALL)
			install.sync(package, username, password, certificate);
		else if(command == PUBLISH)
			publish.sync(package, username, password);
		else if(command == SOURCE)
			source.sync(package, username, password);
		else if(command == REMOVE)
			remove.sync(package);
		else if(command == START)
			start.sync(package);
		else if(command == STOP)
			stop.sync(package);
		else if(command == RESTART)
			restart.sync(package);
		else if(command == LIST)
			list.sync(type, verbose);
		else if(command == HELP || command == QUICKHELP)
			help.sync(command == HELP ? true : false);
		}
	catch(err)
		{
		Utility.printErrors(err);
		}
	finally
		{
		closeAppManagerClient();
		closeSPMServer();
		}
	});

var connectAppManagerClient = fibrous( function()
	{
	try {
		appManagerRPCClient = new AppManagerRPCClient();
		appManagerRPCClient.sync.connect({hostname: null, port: Config.APPMAN_PORT_WEBSOCKET, isSsl: false, persistent: true, owner: "spm"});
		}
	catch(err)
		{
		appManagerRPCClient = null;
		}
	});

var closeAppManagerClient = function()
	{
	if(appManagerRPCClient)
		appManagerRPCClient.close();
	appManagerRPCClient = null;
	}

var openSPMServer = fibrous( function()
	{
	try {
		spmRPCServer = new SpmRPCServer();
		spmRPCServer.exposeRPCMethod("messages", self, self.messages);
		spmRPCServer.connect.sync({hostname: null, port: Config.SPM_PORT_WEBSOCKET, isSsl: false, owner: "spm"});
		}
	catch(err)
		{
		spmRPCServer = null;
		}
	});

var closeSPMServer = function()
	{
{'fraction_non_alphanumeric': 0.16471436294827896, 'fraction_numerical': 0.002522977112993332, 'mean_word_length': 4.157527881040892, 'pattern_counts': {'":': 28, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1586797', 'n_tokens_mistral': 4215, 'n_tokens_neox': 3700, 'n_words': 871}