download_and_install_gccarm.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #! /usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*
  4. import os, urllib, subprocess, sys
  5. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*
  6. import archive_directory
  7. import tool_directory
  8. import dev_platform
  9. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*
  10. # FOR PRINTING IN COLOR
  11. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*
  12. class bcolors:
  13. HEADER = '\033[95m'
  14. BLUE = '\033[94m'
  15. GREEN = '\033[92m'
  16. WARNING = '\033[93m'
  17. FAIL = '\033[91m'
  18. ENDC = '\033[0m'
  19. BOLD = '\033[1m'
  20. UNDERLINE = '\033[4m'
  21. BOLD_BLUE = '\033[1m' + '\033[94m'
  22. BOLD_GREEN = '\033[1m' + '\033[92m'
  23. BOLD_RED = '\033[1m' + '\033[91m'
  24. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*
  25. # DISTRIBUTION GCC
  26. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*
  27. def distributionGCC () :
  28. gcc = "gcc-arm-none-eabi-7-2017-q4-major"
  29. if dev_platform.getPlatform () == "linux32" :
  30. gcc = "gcc-arm-none-eabi-5_4-2016q3"
  31. return gcc
  32. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*
  33. # ARCHIVE URL (from https://developer.arm.com/open-source/gnu-toolchain/gnu-rm)
  34. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*
  35. def compilerArchiveURL () :
  36. baseURL = "https://developer.arm.com/-/media/Files/downloads/gnu-rm/"
  37. PLATFORM = dev_platform.getPlatform ()
  38. distribution = "7-2017q4/gcc-arm-none-eabi-7-2017-q4-major-" + PLATFORM + ".tar.bz2"
  39. if dev_platform.getPlatform () == "linux32" :
  40. distribution = "5_4-2016q3/gcc-arm-none-eabi-5_4-2016q3-20160926-linux.tar.bz2"
  41. return baseURL + distribution
  42. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*
  43. # runCommand
  44. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*
  45. def runCommand (cmd) :
  46. str = "+"
  47. for s in cmd:
  48. str += " " + s
  49. print bcolors.BOLD_BLUE + str + bcolors.ENDC
  50. returncode = subprocess.call (cmd)
  51. if returncode != 0 :
  52. sys.exit (returncode)
  53. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*
  54. # ARCHIVE DOWNLOAD
  55. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*
  56. def downloadReportHook (a,b,fileSize):
  57. # "," at the end of the line is important!
  58. if fileSize < (1 << 10):
  59. sizeString = str (fileSize)
  60. else:
  61. if fileSize < (1 << 20):
  62. sizeString = str (fileSize >> 10) + "Ki"
  63. else:
  64. sizeString = str (fileSize >> 20) + "Mi"
  65. print "% 3.1f%% of %sB\r" % (min(100.0, float(a * b) / fileSize * 100.0), sizeString),
  66. sys.stdout.flush()
  67. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*
  68. def downloadArchive (archiveURL, archivePath):
  69. print ("URL: "+ archiveURL)
  70. print "Downloading..."
  71. urllib.urlretrieve (archiveURL, archivePath, downloadReportHook)
  72. print ""
  73. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*
  74. # Install GCC
  75. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*
  76. def install_gcc (INSTALL_DIR) :
  77. print bcolors.BOLD_GREEN + "Install GCC tools..." + bcolors.ENDC
  78. DISTRIBUTION = distributionGCC ()
  79. #------------------------------------------------------------------ Archive dir
  80. COMPILER_ARCHIVE_DIR = archive_directory.createAndGetArchiveDirectory ()
  81. #------------------------------------------------------------------ Download
  82. if not os.path.exists (COMPILER_ARCHIVE_DIR + "/" + DISTRIBUTION + ".tar.bz2"):
  83. url = compilerArchiveURL ()
  84. runCommand (["rm", "-f", COMPILER_ARCHIVE_DIR + "/" + DISTRIBUTION + ".tar.bz2.downloading"])
  85. downloadArchive (url, COMPILER_ARCHIVE_DIR + "/" + DISTRIBUTION + ".tar.bz2.downloading")
  86. runCommand (["mv",
  87. COMPILER_ARCHIVE_DIR + "/" + DISTRIBUTION + ".tar.bz2.downloading",
  88. COMPILER_ARCHIVE_DIR + "/" + DISTRIBUTION + ".tar.bz2"
  89. ])
  90. #------------------------------------------------------------------ Install
  91. if not os.path.exists (INSTALL_DIR):
  92. os.mkdir (INSTALL_DIR)
  93. if not os.path.exists (INSTALL_DIR + "/" + DISTRIBUTION):
  94. runCommand (["cp", COMPILER_ARCHIVE_DIR + "/" + DISTRIBUTION + ".tar.bz2", INSTALL_DIR + "/" + DISTRIBUTION + ".tar.bz2"])
  95. savedCurrentDir = os.getcwd ()
  96. os.chdir (INSTALL_DIR)
  97. print bcolors.BOLD_BLUE + "+ cd " + INSTALL_DIR + bcolors.ENDC
  98. runCommand (["bunzip2", DISTRIBUTION + ".tar.bz2"])
  99. runCommand (["tar", "xf", DISTRIBUTION + ".tar"])
  100. runCommand (["rm", DISTRIBUTION + ".tar"])
  101. os.chdir (savedCurrentDir)
  102. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*
  103. # GET GCC TOOL DIRECTORY
  104. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*
  105. def installGCCARMandGetToolDirectory () :
  106. #--- Absolute path of tool directory
  107. TOOL_DIRECTORY = tool_directory.toolDirectory () + "/" + distributionGCC ()
  108. #--- Install tool ?
  109. if not os.path.exists (TOOL_DIRECTORY) :
  110. install_gcc (tool_directory.toolDirectory ())
  111. #--- Return tool directory
  112. return TOOL_DIRECTORY
  113. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————*