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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 (.pioenvs/' + 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 = 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: ')
  32. # typical result (array of stings): ['C:\\', 'D:\\', 'E:\\', 'F:\\',
  33. # 'G:\\', 'H:\\', 'I:\\', 'J:\\', 'K:\\', 'L:\\', 'M:\\', 'Y:\\', 'Z:\\']
  34. drives = driveStr.split()
  35. upload_disk = 'Disk not found'
  36. target_file_found = False
  37. target_drive_found = False
  38. for drive in drives:
  39. final_drive_name = drive.strip().rstrip('\\') # typical result (string): 'C:'
  40. try:
  41. volume_info = subprocess.check_output('cmd /C dir ' + final_drive_name, stderr=subprocess.STDOUT)
  42. except Exception as e:
  43. continue
  44. else:
  45. if target_drive in volume_info and target_file_found == False: # set upload if not found target file yet
  46. target_drive_found = True
  47. upload_disk = final_drive_name
  48. if target_filename in volume_info:
  49. if target_file_found == False:
  50. upload_disk = final_drive_name
  51. target_file_found = True
  52. #
  53. # set upload_port to drive if found
  54. #
  55. if target_file_found == True or target_drive_found == True:
  56. env.Replace(
  57. UPLOAD_PORT=upload_disk
  58. )
  59. print('upload disk: ', upload_disk)
  60. else:
  61. print_error('Autodetect Error')
  62. elif current_OS == 'Linux':
  63. #
  64. # platformio.ini will accept this for a Linux upload port designation: 'upload_port = /media/media_name/drive'
  65. #
  66. upload_disk = 'Disk not found'
  67. target_file_found = False
  68. target_drive_found = False
  69. medias = os.listdir('/media') #
  70. for media in medias:
  71. drives = os.listdir('/media/' + media) #
  72. if target_drive in drives and target_file_found == False: # set upload if not found target file yet
  73. target_drive_found = True
  74. upload_disk = '/media/' + media + '/' + target_drive + '/'
  75. for drive in drives:
  76. try:
  77. files = os.listdir('/media/' + media + '/' + drive)
  78. except:
  79. continue
  80. else:
  81. if target_filename in files:
  82. if target_file_found == False:
  83. upload_disk = '/media/' + media + '/' + drive + '/'
  84. target_file_found = True
  85. #
  86. # set upload_port to drive if found
  87. #
  88. if target_file_found == True or target_drive_found == True:
  89. env.Replace(
  90. UPLOAD_FLAGS="-P$UPLOAD_PORT",
  91. UPLOAD_PORT=upload_disk
  92. )
  93. print('upload disk: ', upload_disk)
  94. else:
  95. print_error('Autodetect Error')
  96. elif current_OS == 'Darwin': # MAC
  97. #
  98. # platformio.ini will accept this for a OSX upload port designation: 'upload_port = /media/media_name/drive'
  99. #
  100. upload_disk = 'Disk not found'
  101. drives = os.listdir('/Volumes') # human readable names
  102. target_file_found = False
  103. target_drive_found = False
  104. if target_drive in drives and target_file_found == False: # set upload if not found target file yet
  105. target_drive_found = True
  106. upload_disk = '/Volumes/' + target_drive + '/'
  107. for drive in drives:
  108. try:
  109. filenames = os.listdir('/Volumes/' + drive + '/') # will get an error if the drive is protected
  110. except:
  111. continue
  112. else:
  113. if target_filename in filenames:
  114. if target_file_found == False:
  115. upload_disk = '/Volumes/' + drive + '/'
  116. target_file_found = True
  117. #
  118. # set upload_port to drive if found
  119. #
  120. if target_file_found == True or target_drive_found == True:
  121. env.Replace(
  122. UPLOAD_PORT=upload_disk
  123. )
  124. print('\nupload disk: ', upload_disk, '\n')
  125. else:
  126. print_error('Autodetect Error')
  127. except Exception as e:
  128. print_error(str(e))