Advertisement
Arg0s1080

instalacion-v-0-2.py

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