My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

upload_extra_script.py 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #
  2. # sets output_port
  3. # if target_filename is found then that drive is used
  4. # else if target_drive is found then that drive is used
  5. #
  6. from __future__ import print_function
  7. target_filename = "FIRMWARE.CUR"
  8. target_drive = "REARM"
  9. import os
  10. import platform
  11. current_OS = platform.system()
  12. Import("env")
  13. def print_error(e):
  14. print('\nUnable to find destination disk (' + e + ')\n' \
  15. 'Please select it in platformio.ini using the upload_port keyword ' \
  16. '(https://docs.platformio.org/en/latest/projectconf/section_env_upload.html) ' \
  17. 'or copy the firmware (.pio/build/' + env.get('PIOENV') + '/firmware.bin) manually to the appropriate disk\n')
  18. try:
  19. if current_OS == 'Windows':
  20. #
  21. # platformio.ini will accept this for a Windows upload port designation: 'upload_port = L:'
  22. # Windows - doesn't care about the disk's name, only cares about the drive letter
  23. #
  24. #
  25. # get all drives on this computer
  26. #
  27. import subprocess
  28. # typical result (string): 'Drives: C:\ D:\ E:\ F:\ G:\ H:\ I:\ J:\ K:\ L:\ M:\ Y:\ Z:\'
  29. driveStr = str(subprocess.check_output("fsutil fsinfo drives"))
  30. # typical result (string): 'C:\ D:\ E:\ F:\ G:\ H:\ I:\ J:\ K:\ L:\ M:\ Y:\ Z:\'
  31. # driveStr = driveStr.strip().lstrip('Drives: ') <- Doesn't work in other Languages as English. In German is "Drives:" = "Laufwerke:"
  32. FirstFound = driveStr.find(':',0,-1) # Find the first ":" and
  33. driveStr = driveStr[FirstFound + 1 : -1] # truncate to the rest
  34. # typical result (array of stings): ['C:\\', 'D:\\', 'E:\\', 'F:\\',
  35. # 'G:\\', 'H:\\', 'I:\\', 'J:\\', 'K:\\', 'L:\\', 'M:\\', 'Y:\\', 'Z:\\']
  36. drives = driveStr.split()
  37. upload_disk = 'Disk not found'
  38. target_file_found = False
  39. target_drive_found = False
  40. for drive in drives:
  41. final_drive_name = drive.strip().rstrip('\\') # typical result (string): 'C:'
  42. try:
  43. volume_info = str(subprocess.check_output('cmd /C dir ' + final_drive_name, stderr=subprocess.STDOUT))
  44. except Exception as e:
  45. continue
  46. else:
  47. if target_drive in volume_info and target_file_found == False: # set upload if not found target file yet
  48. target_drive_found = True
  49. upload_disk = final_drive_name
  50. if target_filename in volume_info:
  51. if target_file_found == False:
  52. upload_disk = final_drive_name
  53. target_file_found = True
  54. #
  55. # set upload_port to drive if found
  56. #
  57. if target_file_found == True or target_drive_found == True:
  58. env.Replace(
  59. UPLOAD_PORT=upload_disk
  60. )
  61. print('upload disk: ', upload_disk)
  62. else:
  63. print_error('Autodetect Error')
  64. elif current_OS == 'Linux':
  65. #
  66. # platformio.ini will accept this for a Linux upload port designation: 'upload_port = /media/media_name/drive'
  67. #
  68. upload_disk = 'Disk not found'
  69. target_file_found = False
  70. target_drive_found = False
  71. medias = os.listdir('/media') #
  72. for media in medias:
  73. drives = os.listdir('/media/' + media) #
  74. if target_drive in drives and target_file_found == False: # set upload if not found target file yet
  75. target_drive_found = True
  76. upload_disk = '/media/' + media + '/' + target_drive + '/'
  77. for drive in drives:
  78. try:
  79. files = os.listdir('/media/' + media + '/' + drive)
  80. except:
  81. continue
  82. else:
  83. if target_filename in files:
  84. if target_file_found == False:
  85. upload_disk = '/media/' + media + '/' + drive + '/'
  86. target_file_found = True
  87. #
  88. # set upload_port to drive if found
  89. #
  90. if target_file_found == True or target_drive_found == True:
  91. env.Replace(
  92. UPLOAD_FLAGS="-P$UPLOAD_PORT",
  93. UPLOAD_PORT=upload_disk
  94. )
  95. print('upload disk: ', upload_disk)
  96. else:
  97. print_error('Autodetect Error')
  98. elif current_OS == 'Darwin': # MAC
  99. #
  100. # platformio.ini will accept this for a OSX upload port designation: 'upload_port = /media/media_name/drive'
  101. #
  102. upload_disk = 'Disk not found'
  103. drives = os.listdir('/Volumes') # human readable names
  104. target_file_found = False
  105. target_drive_found = False
  106. if target_drive in drives and target_file_found == False: # set upload if not found target file yet
  107. target_drive_found = True
  108. upload_disk = '/Volumes/' + target_drive + '/'
  109. for drive in drives:
  110. try:
  111. filenames = os.listdir('/Volumes/' + drive + '/') # will get an error if the drive is protected
  112. except:
  113. continue
  114. else:
  115. if target_filename in filenames:
  116. if target_file_found == False:
  117. upload_disk = '/Volumes/' + drive + '/'
  118. target_file_found = True
  119. #
  120. # set upload_port to drive if found
  121. #
  122. if target_file_found == True or target_drive_found == True:
  123. env.Replace(
  124. UPLOAD_PORT=upload_disk
  125. )
  126. print('\nupload disk: ', upload_disk, '\n')
  127. else:
  128. print_error('Autodetect Error')
  129. except Exception as e:
  130. print_error(str(e))