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

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