Advertisement
Arg0s1080

instalacion-v-0-2

Oct 19th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.52 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3.  
  4. ##################################################################################
  5. #             Script de instalación lanzador-flameshot.py v 0.2                  #
  6. ##################################################################################
  7. #                                                                                #
  8. # Ejemplo de utilización 1:                                                      #
  9. #                            Instalar   : python2 instalacion.py                 #
  10. #                            Desinstalar: python2 instalacion.py --desinstalar   #
  11. #                                                                                #
  12. # Ejemplo de utilización 2:               sudo chmod +x instalacion.py           #
  13. #                            Instalar   : ./instalacion.py                       #
  14. #                            Desinstalar: ./python2 instalacion.py --desinstalar #
  15. #                                                                                #
  16. # Se puede mirar, copiar o modificar lo que se desee                             #
  17. #                                                                                #
  18. # Iván Rincón 2017                                                               #
  19. ##################################################################################
  20.  
  21. import os
  22. import shutil
  23. import sys
  24. from ConfigParser import ConfigParser
  25.  
  26. version = "v.0.2"
  27. titulo = "Instalación Lanzador de Flameshot"
  28.  
  29. print ("".ljust(60, "="))
  30. print (titulo + version.rjust(60 - len(titulo) + 1, " "))
  31. print ("".ljust(60, "="))
  32.  
  33.  
  34. def checkPreguntaSiNo(pregunta):
  35.     rpt = raw_input(pregunta).lower()
  36.     if rpt == "s" or rpt == "si" or rpt == "sí" or rpt == "y" or rpt == "yes":
  37.         return "si"
  38.     elif rpt.lower() == "n" or rpt == "no":
  39.         return "no"
  40.     elif rpt == "":
  41.         return ""
  42.     else:
  43.         print ("- Respuesta no válida.")
  44.         return rpt
  45.  
  46. class Instalacion:
  47.  
  48.     def __init__(self, desinstalar=False):
  49.  
  50.         home = os.getenv("HOME")
  51.         rutaScript = home + "/.local/share/lanzador-flameshot/script/"
  52.         rutaLDesktop = home + "/.local/share/applications/lanzador-flameshot.desktop"
  53.         ok = True
  54.  
  55.         try:
  56.             if not desinstalar:
  57.  
  58.                 if not os.path.exists(rutaScript):
  59.                     os.makedirs(rutaScript)
  60.                 if not os.path.exists(rutaScript + "lanzador.py"):
  61.                     shutil.copy(os.getcwd() + "/lanzador-flameshot.py", rutaScript + "lanzador.py")
  62.                     print ("- Script copiado")
  63.                 else:
  64.                     print ("- El script ya existía")
  65.  
  66.                 if not os.path.exists(rutaLDesktop):
  67.  
  68.                     interprete = raw_input("- ¿Qué intérprete utilizar? [/usr/bin/python] : ")
  69.  
  70.                     if interprete == "":
  71.                         interprete = "/usr/bin/python"
  72.  
  73.                     if not os.path.exists(interprete):
  74.                         err = True
  75.                         while err:
  76.                             ine = checkPreguntaSiNo("- " + interprete + " no existe, ¿desea continuar? s / [n] : ")
  77.  
  78.                             if ine == "no" or ine == "":
  79.                                 print ("- Operación abortada. Se procede a la desinstalación")
  80.                                 Instalacion(True)
  81.                                 err = False
  82.                                 ok = False
  83.                                 exit()
  84.  
  85.                             if ine == "si":
  86.                                 err = False
  87.  
  88.                     config = ConfigParser()
  89.                     config.optionxform = lambda option: option  # Lee Notas2
  90.  
  91.                     config.add_section("Desktop Entry")
  92.                     config.set("Desktop Entry", "Encoding", "UTF-8")
  93.                     config.set("Desktop Entry", "Name", "Lanzador Flameshot")
  94.                     config.set("Desktop Entry", "Simple y potente software de captura de pantalla")
  95.                     config.set("Desktop Entry", "Exec", interprete + " " + os.getenv("HOME") +
  96.                                "/.local/share/lanzador-flameshot/script/lanzador.py")
  97.                     config.set("Desktop Entry", "Icon", "/usr/local/share/icons/flameshot.png")
  98.                     config.set("Desktop Entry", "Type", "Application")
  99.                     config.set("Desktop Entry", "Categories", "Graphics;Utility;")
  100.  
  101.                     with open(rutaLDesktop, "wb") as archivoConfig:
  102.                         config.write(archivoConfig)
  103.  
  104.                     print ("- Lanzador creado")
  105.  
  106.                 else:
  107.                     print ("- El lanzador ya existía")
  108.  
  109.             else:
  110.                 trp1 = "- Se eliminó el "
  111.                 trp2 = "- No se encontró el "
  112.                 rutaConfig = os.getenv("HOME") + "/.config/lanzador-flameshot/Lanzador.cfg"
  113.  
  114.                 if os.path.exists(rutaScript + "lanzador.py"):
  115.                     shutil.rmtree(rutaScript.replace("script/", ""))
  116.                     print (trp1 + "script")
  117.                 else:
  118.                     print (trp2 + "script")
  119.  
  120.                 if os.path.exists(rutaLDesktop):
  121.                     os.remove(rutaLDesktop)
  122.                     print (trp1 + "lanzador")
  123.                 else:
  124.                     print (trp2 + "lanzador")
  125.  
  126.                 if os.path.exists(rutaConfig):
  127.                     err = True
  128.  
  129.                     while err:
  130.                         eac = checkPreguntaSiNo("- ¿Eliminar el archivo de configuración? s / [n] : ")
  131.  
  132.                         if eac == "si":
  133.                             shutil.rmtree(rutaConfig.replace("Lanzador.cfg", ""))
  134.                             print ("- Se eliminó el archivo de configuración")
  135.                             err = False
  136.                         elif eac == "no" or eac == "":
  137.                             print ("- No se eliminó el archivo de configuración")
  138.                             err = False
  139.                             pass
  140.  
  141.         except IOError as ex:
  142.             print "Error I/O ({0}) : {1}".format(ex.errno, ex.strerror)
  143.             ok &= False
  144.  
  145.         except Exception:
  146.             print ("- Hubo un error inesperado:")
  147.             for mj in sys.exc_info():
  148.                 print "! " + str(mj)
  149.             ok &= False
  150.  
  151.         finally:
  152.             if desinstalar:
  153.                 cadena = "desinstalación"
  154.             else:
  155.                 cadena = "instalación"
  156.  
  157.             if ok:
  158.                 print ("- La " + cadena + " se finalizó con éxito")
  159.             else:
  160.                 print ("- Se produjo un error durante la " + cadena)
  161.  
  162.             print ("".ljust(60, "="))
  163.  
  164.  
  165. # Final de clase Instalacion
  166.  
  167.  
  168. if __name__ == "__main__":
  169.  
  170.     d = False
  171.  
  172.     if len(sys.argv) == 2 and sys.argv[1] == "--desinstalar":
  173.         d = True
  174.  
  175.     elif len(sys.argv) > 1:
  176.         print ("Argumento no válido : " + sys.argv[1] + "\n"
  177.                "Sintaxis ...........: instalacion.py [--desinstalar]")
  178.         exit()
  179.  
  180.     else:
  181.  
  182.         if not os.path.exists("/usr/local/bin/flameshot"):
  183.             err = True
  184.             while err:
  185.                 res = checkPreguntaSiNo("- Parece que Flameshot no está instalado.\n¿Desea continuar? [s] / n :")
  186.                 if res == "si" or res == "":
  187.                     Instalacion()
  188.                 elif res == "no":
  189.                     print ("- Instalación abortada")
  190.                     print ("".ljust(60, "="))
  191.                     exit()
  192.  
  193.     Instalacion(d)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement