Script conf_create_in_py
[hide private]
[frames] | no frames]

Source Code for Script script-conf_create_in_py

 1  #!/usr/bin/env python 
 2  # -*- python -*- 
 3  # 
 4  # Git Version: @git@ 
 5   
 6  #----------------------------------------------------------------------- 
 7  # XALT: A tool that tracks users jobs and environments on a cluster. 
 8  # Copyright (C) 2013-2014 University of Texas at Austin 
 9  # Copyright (C) 2013-2014 University of Tennessee 
10  #  
11  # This library is free software; you can redistribute it and/or modify 
12  # it under the terms of the GNU Lesser General Public License as 
13  # published by the Free Software Foundation; either version 2.1 of  
14  # the License, or (at your option) any later version.  
15  # 
16  # This library is distributed in the hope that it will be useful, 
17  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
18  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
19  # Lesser  General Public License for more details.  
20  # 
21  # You should have received a copy of the GNU Lesser General Public 
22  # License along with this library; if not, write to the Free 
23  # Software Foundation, Inc., 59 Temple Place, Suite 330, 
24  # Boston, MA 02111-1307 USA 
25  #----------------------------------------------------------------------- 
26   
27  from __future__ import print_function 
28  import os, sys, re, ConfigParser, getpass, base64 
29   
30  dirNm, execName = os.path.split(sys.argv[0]) 
31  sys.path.insert(1,os.path.abspath(os.path.join(dirNm, "../libexec"))) 
32  sys.path.insert(1,os.path.realpath(os.path.join(dirNm, "../site"))) 
33   
34  import argparse 
35   
36 -class CmdLineOptions(object):
37 - def __init__(self):
38 pass
39
40 - def execute(self):
41 parser = argparse.ArgumentParser() 42 parser.add_argument("--dbhost", dest='dbhost', action="store", help="db host") 43 parser.add_argument("--dbuser", dest='dbuser', action="store", help="db user") 44 parser.add_argument("--passwd", dest='passwd', action="store", help="password") 45 parser.add_argument("--dbname", dest='dbname', action="store", help="name of db") 46 47 args = parser.parse_args() 48 49 return args
50 -class CreateConf(object):
51 - def __init__(self, args):
52 self.__host = args.dbhost 53 self.__user = args.dbuser 54 self.__passwd = args.passwd 55 self.__db = args.dbname
56
57 - def __readFromUser(self):
58 if (not self.__host): self.__host = raw_input("Database host: ") 59 if (not self.__user): self.__user = raw_input("Database user: ") 60 if (not self.__passwd): self.__passwd = getpass.getpass("Database pass: ") 61 if (not self.__db): self.__db = raw_input("Database name: ")
62
63 - def __writeConfig(self):
64 config=ConfigParser.ConfigParser() 65 config.add_section("MYSQL") 66 config.set("MYSQL","HOST",self.__host) 67 config.set("MYSQL","USER",self.__user) 68 config.set("MYSQL","PASSWD",base64.b64encode(self.__passwd)) 69 config.set("MYSQL","DB",self.__db) 70 71 fn = self.__db + "_db.conf" 72 73 f = open(fn,"w") 74 config.write(f) 75 f.close()
76
77 - def create(self):
78 79 self.__readFromUser() 80 self.__writeConfig()
81 82 83
84 -def main():
85 args = CmdLineOptions().execute() 86 createConf = CreateConf(args) 87 createConf.create()
88 89 90 if ( __name__ == '__main__'): main() 91