Advertisement
Guest User

main.py

a guest
May 1st, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.77 KB | None | 0 0
  1. # !/usr/bin/python
  2. import binascii
  3. import os
  4. import zipfile
  5. import subprocess
  6. import psycopg2
  7. from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
  8. import io
  9. try:
  10.     from Tkinter import *
  11. except ImportError:
  12.     from tkinter import *
  13.  
  14. try:
  15.     import ttk
  16.  
  17.     py3 = 0
  18. except ImportError:
  19.     import tkinter.ttk as ttk
  20.  
  21.     py3 = 1
  22.  
  23. choosefile = r'%SystemDrive%\\Suport Linx'
  24. tmppath = "C:\\Users\\{}\\AppData\\Local\\Temp\\".format(os.getlogin())
  25.  
  26. def unzip(file):
  27.     with zipfile.ZipFile(file, "r") as zipped_file:
  28.         zipped_file.extractall(tmppath)
  29.  
  30.  
  31. def install(db, pw, host, port, file):
  32.     try:
  33.         conn = psycopg2.connect(user='postgres', database='postgres', password=pw, host=host, port=port)
  34.         conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
  35.         cur = conn.cursor()
  36.         print('Iniciando instalação do banco de dados')
  37.         try:
  38.             cur.execute('create user distro')
  39.             print('Usuario distro criado')
  40.         except Exception as e:
  41.             print(e)
  42.         try:
  43.             cur.execute('create user autosystem')
  44.             print('Usuario autosystem criado')
  45.         except Exception as e:
  46.             print(e)
  47.         try:
  48.             cur.execute(f'CREATE DATABASE {db} ENCODING "SQL_ASCII" TEMPLATE template0')
  49.             print(f'Banco de dados {db} criado')
  50.         except Exception as e:
  51.             print(e)
  52.         unzip(file)
  53.         try:
  54.             print('Iniciando carregamento ...')
  55.             log = 'error.log'
  56.             #filename = 'sqlquery'
  57.             filename = tmppath + "load3.2.3.127-30052017.sql"
  58.             '''command = f'cmd.exe set PGPASSWORD={pw}'
  59.            subprocess.Popen(command, stdin=subprocess.PIPE, stdout=log)
  60.            sqlrun = f'psql.exe -h {host} -p {port} -f {filename}{db} postgres'
  61.            subprocess.Popen(sqlrun, stdin=subprocess.PIPE, stdout=log)'''
  62.             file = open(filename, 'r')
  63.             fileconv = binascii.a2b_uu(file)
  64.             reader = io.BufferedReader(io.BytesIO(fileconv.encode("utf-8")))
  65.             sql = s = " ".join(file.readlines())
  66.             try:
  67.                 conn = psycopg2.connect(user='postgres', database=db, password=pw, host=host, port=port)
  68.                 conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
  69.                 #conn.set_client_encoding('iso8859_15')
  70.                 cur = conn.cursor()
  71.                 try:
  72.                     decoded = sql
  73.                     sqlfinal = decoded.encode('iso8859_15', 'replace')
  74.                     cur.execute(reader)
  75.                 except Exception as msg:
  76.                     print(msg)
  77.             except Exception as e:
  78.                 print(e)
  79.             print('LoadDB carregado com sucesso !')
  80.         except Exception as e:
  81.             print(e)
  82.     except Exception as e:
  83.         print(e)
  84.         print('Verifique os parametros informados e tente novamente.')
  85.  
  86.  
  87. def change_pass_distro(pw, host, port):
  88.     try:
  89.         conn = psycopg2.connect(user='postgres', database='postgres', password=pw, host=host, port=port)
  90.         conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
  91.         cur = conn.cursor()
  92.         try:
  93.             cur.execute(f"alter user distro with password '{pw}'")
  94.             print('Senha usuário distro alterada')
  95.         except Exception as e:
  96.             print(e)
  97.     except Exception as e:
  98.         print(e)
  99.         print('Verifique os parametros informados e tente novamente.')
  100.  
  101.  
  102. def init(top, gui, *args, **kwargs):
  103.     global w, top_level, root
  104.     w = gui
  105.     top_level = top
  106.     root = top
  107.  
  108.  
  109. def destroy_window():
  110.     # Function which closes the window.
  111.     global top_level
  112.     top_level.destroy()
  113.     top_level = None
  114.  
  115.  
  116. if __name__ == '__main__':
  117.     import gui
  118.  
  119.     gui.vp_start_gui()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement