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 getpass
  11. import platform
  12. current_OS = platform.system()
  13. Import("env")
  14. def print_error(e):
  15. print('\nUnable to find destination disk (' + e + ')\n' \
  16. 'Please select it in platformio.ini using the upload_port keyword ' \
  17. '(https://docs.platformio.org/en/latest/projectconf/section_env_upload.html) ' \
  18. 'or copy the firmware (.pio/build/' + env.get('PIOENV') + '/firmware.bin) manually to the appropriate disk\n')
  19. try:
  20. if current_OS == 'Windows':
  21. #
  22. # platformio.ini will accept this for a Windows upload port designation: 'upload_port = L:'
  23. # Windows - doesn't care about the disk's name, only cares about the drive letter
  24. #
  25. #
  26. # get all drives on this computer
  27. #
  28. import subprocess
  29. # typical result (string): 'Drives: C:\ D:\ E:\ F:\ G:\ H:\ I:\ J:\ K:\ L:\ M:\ Y:\ Z:\'
  30. driveStr = str(subprocess.check_output("fsutil fsinfo drives"))
  31. # typical result (string): 'C:\ D:\ E:\ F:\ G:\ H:\ I:\ J:\ K:\ L:\ M:\ Y:\ Z:\'
  32. # driveStr = driveStr.strip().lstrip('Drives: ') <- Doesn't work in other Languages as English. In German is "Drives:" = "Laufwerke:"
  33. FirstFound = driveStr.find(':',0,-1) # Find the first ":" and
  34. driveStr = driveStr[FirstFound + 1 : -1] # truncate to the rest
  35. # typical result (array of stings): ['C:\\', 'D:\\', 'E:\\', 'F:\\',
  36. # 'G:\\', 'H:\\', 'I:\\', 'J:\\', 'K:\\', 'L:\\', 'M:\\', 'Y:\\', 'Z:\\']
  37. drives = driveStr.split()
  38. upload_disk = 'Disk not found'
  39. target_file_found = False
  40. target_drive_found = False
  41. for drive in drives:
  42. final_drive_name = drive.strip().rstrip('\\') # typical result (string): 'C:'
  43. try:
  44. volume_info = str(subprocess.check_output('cmd /C dir ' + final_drive_name, stderr=subprocess.STDOUT))
  45. except Exception as e:
  46. continue
  47. else:
  48. if target_drive in volume_info and target_file_found == False: # set upload if not found target file yet
  49. target_drive_found = True
  50. upload_disk = final_drive_name
  51. if target_filename in volume_info:
  52. if target_file_found == False:
  53. upload_disk = final_drive_name
  54. target_file_found = True
  55. #
  56. # set upload_port to drive if found
  57. #
  58. if target_file_found == True or target_drive_found == True:
  59. env.Replace(
  60. UPLOAD_PORT=upload_disk
  61. )
  62. print('upload disk: ', upload_disk)
  63. else:
  64. print_error('Autodetect Error')
  65. elif current_OS == 'Linux':
  66. #
  67. # platformio.ini will accept this for a Linux upload port designation: 'upload_port = /media/media_name/drive'
  68. #
  69. upload_disk = 'Disk not found'
  70. target_file_found = False
  71. target_drive_found = False
  72. drives = os.listdir(os.path.join(os.sep, 'media', getpass.getuser()))
  73. if target_drive in drives: # If target drive is found, use it.
  74. target_drive_found = True
  75. upload_disk = os.path.join(os.sep, 'media', getpass.getuser(), target_drive) + os.sep
  76. else:
  77. for drive in drives:
  78. try:
  79. files = os.listdir(os.path.join(os.sep, 'media', getpass.getuser(), drive))
  80. except:
  81. continue
  82. else:
  83. if target_filename in files:
  84. upload_disk = os.path.join(os.sep, 'media', getpass.getuser(), drive) + os.sep
  85. target_file_found = True
  86. break
  87. #
  88. # set upload_port to drive if found
  89. #
  90. if target_file_found or target_drive_found:
  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))