#!/usr/bin/python #-*- coding: utf-8 -*- ########################################################## # INSTALADOR # ########################################################## # Autor: Juan Miguel Taboada Godoy # # Fecha: Malaga, 17 de diciembre de 2007 # # Descripción: Instalador de Likindoy # # Versión: 2008010700 # # # # Codigo fuente bajo licencia GNU/GPL # # Centrologic (Computational Logistic Center) # # http://www.centrologic.com - info@centrologic.com # ########################################################## # Libraries import curses import imp import time class INSTALLER: # Create the object def __init__(self): # Startup ncurses self.__screen=curses.initscr() # Start colors curses.start_color() curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE) curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK) curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_RED) curses.init_pair(4, curses.COLOR_GREEN, curses.COLOR_BLACK) curses.init_pair(5, curses.COLOR_RED, curses.COLOR_BLACK) # Show keys strikes on screen curses.noecho() # One key and action curses.cbreak() # Percents self.__percent_config=0 self.__percent_process=0 self.__percent = curses.newwin(1, 100, curses.LINES-3, 4) # Destroy the object def close(self): # Get library once more #import curses # End the curses application curses.nocbreak() self.__screen.keypad(0) curses.echo() # Close the window curses.endwin() # Print a line def line(self,window, y, text, color): window.addstr(y,0,text,color) blank="" for i in range(len(text),curses.COLS): blank="%s " % (blank) window.addstr(y,len(text),blank,color) # Print the screen def screen(self): # Get the screen screen=self.__screen # Clear screen.clear() # Head self.line(screen,0,"Likindoy installer", curses.color_pair(1) ) screen.refresh() # Body for i in range(1,curses.LINES-1): self.line(screen,i,"---", curses.color_pair(2)) pass # Tail self.line(screen,curses.LINES-2,"Centrologic Computational Logistic Center - http://www.centrologic.com", curses.color_pair(1) ) screen.refresh() # Percents def percent(self,config=None,process=None): if (config!=None): self.__percent_config=config else: config=self.__percent_config if (process!=None): self.__percent_process=process else: process=self.__percent_process # Config config_line="" value=config*20/100 for i in range(0,20): if (i Host (%s): " % (default_host)) host=win.getstr() if (host==""): host=default_host else: default_host=host win.addstr( 1,0,"Host: ") win.addstr( 1,0,"Host: %s" % (host)) self.percent(config=20) win.addstr( 2,0,"> Port (%s): " % (default_port)) port=win.getstr() if (port==""): port=default_port else: default_port=port win.addstr( 2,0,"Port: ") win.addstr( 2,0,"Port: %s" % (port)) self.percent(config=40) win.addstr( 3,0,"> Socket (%s): " % (default_socket)) socket=win.getstr() if (socket==""): socket=default_socket else: default_socket=socket win.addstr( 3,0,"Socket: ") win.addstr( 3,0,"Socket: %s" % (socket)) self.percent(config=60) win.addstr( 4,0,"> User (%s): " % (default_user)) user=win.getstr() if (user==""): user=default_user else: default_user=user win.addstr( 4,0,"User: ") win.addstr( 4,0,"User: %s" % (user)) curses.noecho() self.percent(config=80) win.addstr( 5,0,"> Password: ") password=win.getstr() win.addstr( 5,0,"Password: ") self.percent(config=100) create_database=False win.addstr(7,0," Is correct the information? (y/N)",curses.A_REVERSE) win.refresh() while 1: c = win.getch() if ((c == ord('y')) or (c == ord('Y'))): win.addstr(7,0, " Creating database... ",curses.A_REVERSE) win.refresh() try: win.addstr(8,0, " Connecting to database... ") win.refresh() if (socket=="auto"): # MySQL default socket BDconnection = MySQLdb.connect(host=host,port=int(port),user=user, passwd=password) self.__BDconnection=BDconnection else: # MySQL defined socket BDconnection = MySQLdb.connect(host=host,port=int(port),user=user, passwd=password,unix_socket=socket) self.percent(process=12.5) # Activing cursor win.addstr(9,0, " Getting cursor... ") win.refresh() BD = BDconnection.cursor() self.percent(process=25) # Create database win.addstr(10,0, " Creating database... ") win.refresh() try: BD.execute("USE likindoy_test") win.addstr(10,0, " Creating database...[already exists] ") win.addstr(11,0, " > I am not going to create table datos! ") win.addstr(12,0, " > I am not goint to create table ids! ") self.percent(process=62.5) except: BD.execute("create database likindoy_test") BD.execute("USE likindoy_test") self.percent(process=37.5) # Create tables table_datos='CREATE TABLE `datos` ( `id` int(11) NOT NULL, `server` timestamp NOT NULL default CURRENT_TIMESTAMP, `fecha` date NOT NULL, `hora` time NOT NULL, `valord` tinyint(1) default NULL, `valora` float default NULL, PRIMARY KEY (`id`,`fecha`,`hora`)) ENGINE=MyISAM;' table_ids='CREATE TABLE `ids` ( `id` int(11) NOT NULL auto_increment, `md5` varchar(32) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `md5` (`md5`)) ENGINE=MyISAM'; win.addstr(11,0, " Creating table datos... ") win.refresh() BD.execute(table_datos) self.percent(process=50) win.addstr(12,0, " Creating table ids... ") win.refresh() BD.execute(table_ids) self.percent(process=62.5) # Create user win.addstr(13,0, " Creating user... ") win.refresh() BD.execute("USE mysql") BD.execute("SELECT User FROM `user` WHERE `User`='likindoy' AND `Host`='localhost'") if (BD.rowcount>0): win.addstr(13,0, " Creating user...[already exists] ") else: BD.execute("CREATE USER 'likindoy'@'localhost' IDENTIFIED BY 'likindoyclave'") self.percent(process=75) # Granting user win.addstr(14,0, " Granting user... ") win.refresh() query_grant="GRANT USAGE ON * . * TO 'likindoy'@'localhost' IDENTIFIED BY 'likindoyclave' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;" BD.execute(query_grant) query_grant="GRANT SELECT , INSERT , UPDATE , DELETE ON `likindoy_test` . * TO 'likindoy'@'localhost';" BD.execute(query_grant) self.percent(process=87.5) # Closing connection win.addstr(15,0, " Clossing connection to database... ") win.refresh() BD.close() BDconnection.close() self.percent(process=100) # Say that everything was fine win.addstr(17,0, " Database created, push a key to finish. ",curses.A_REVERSE) win.refresh() c=win.getch() fine=True except Exception,e: win.addstr( 7,0," ") win.addstr( 8,0," ") win.addstr( 9,0," ") win.addstr(10,0," ") win.addstr(11,0," ") win.addstr(12,0," ") win.addstr(13,0," ") win.addstr(14,0," ") win.addstr(15,0," ") win.addstr(16,0," ") win.addstr(7,0,"ERROR (push a key to try again):",curses.color_pair(3)) win.addstr(8,0,"%s" % (e),curses.color_pair(3)) win.refresh() fine=False c = win.getch() break elif ((c == ord('n')) or (c == ord('N')) or (int(c) == 10)): break # === MAIN === ================================================= if __name__=='__main__': i=INSTALLER() try: i.main() i.close() except: import traceback i.close() traceback.print_exc()