udev_on_linux.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #! /usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #-----------------------------------------------------------------------------*
  4. import subprocess, sys, os, filecmp
  5. #-----------------------------------------------------------------------------*
  6. # FOR PRINTING IN COLOR *
  7. #-----------------------------------------------------------------------------*
  8. class bcolors:
  9. HEADER = '\033[95m'
  10. BLUE = '\033[94m'
  11. GREEN = '\033[92m'
  12. WARNING = '\033[93m'
  13. FAIL = '\033[91m'
  14. ENDC = '\033[0m'
  15. BOLD = '\033[1m'
  16. UNDERLINE = '\033[4m'
  17. BOLD_BLUE = '\033[1m' + '\033[94m'
  18. BOLD_GREEN = '\033[1m' + '\033[92m'
  19. BOLD_RED = '\033[1m' + '\033[91m'
  20. #-----------------------------------------------------------------------------*
  21. # runCommand *
  22. #-----------------------------------------------------------------------------*
  23. def runCommand (cmd) :
  24. str = "+"
  25. for s in cmd:
  26. str += " " + s
  27. print (bcolors.BOLD_BLUE + str + bcolors.ENDC)
  28. returncode = subprocess.call (cmd)
  29. if returncode != 0 :
  30. sys.exit (returncode)
  31. #-----------------------------------------------------------------------------*
  32. def udevPath ():
  33. return "/etc/udev/rules.d"
  34. #-----------------------------------------------------------------------------*
  35. def udevFileForTeensy ():
  36. return "49-teensy.rules"
  37. #-----------------------------------------------------------------------------*
  38. # MAIN *
  39. #-----------------------------------------------------------------------------*
  40. def installUDEVrulesOnLinux ():
  41. #------------------------------------------------------------------ Machine
  42. (SYSTEM_NAME, MODE_NAME, RELEASE, VERSION, MACHINE) = os.uname ()
  43. if SYSTEM_NAME != "Linux" :
  44. print (bcolors.BOLD_RED + "This script is available only for Linux" + bcolors.ENDC)
  45. sys.exit (1)
  46. #------------------------------------------------------------------ Get script absolute path
  47. scriptDir = os.path.dirname (os.path.abspath (__file__))
  48. #------------------------------------------------------------------ Install udev file for Teensy
  49. if not os.path.exists (udevPath () + "/" + udevFileForTeensy ()) :
  50. runCommand (["sudo", "cp", scriptDir + "/" + udevFileForTeensy (), udevPath () + "/" + udevFileForTeensy ()])
  51. runCommand (["sudo", "udevadm", "trigger"])
  52. elif not filecmp.cmp (scriptDir + "/" + udevFileForTeensy (), udevPath () + "/" + udevFileForTeensy ()) :
  53. runCommand (["sudo", "cp", scriptDir + "/" + udevFileForTeensy (), udevPath () + "/" + udevFileForTeensy ()])
  54. runCommand (["sudo", "udevadm", "trigger"])
  55. #---
  56. #-----------------------------------------------------------------------------*
  57. # MAIN *
  58. #-----------------------------------------------------------------------------*
  59. def uninstallUDEVrulesOnLinux ():
  60. #------------------------------------------------------------------ Machine
  61. (SYSTEM_NAME, MODE_NAME, RELEASE, VERSION, MACHINE) = os.uname ()
  62. if SYSTEM_NAME != "Linux" :
  63. print (bcolors.BOLD_RED + "This script is available only for Linux" + bcolors.ENDC)
  64. sys.exit (1)
  65. #------------------------------------------------------------------ Uninstall
  66. if os.path.exists (udevPath () + "/" + udevFileForTeensy ()) :
  67. runCommand (["sudo", "rm", udevPath () + "/" + udevFileForTeensy ()])
  68. #------------------------------------------------------------------ Uninstall
  69. if os.path.exists (udevPath () + "/" + udevFileForOpenOCD ()) :
  70. runCommand (["sudo", "rm", udevPath () + "/" + udevFileForOpenOCD ()])
  71. #-----------------------------------------------------------------------------*