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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #
  2. # upload_extra_script.py
  3. # set the output_port
  4. # if target_filename is found then that drive is used
  5. # else if target_drive is found then that drive is used
  6. #
  7. from __future__ import print_function
  8. import pioutil
  9. if pioutil.is_pio_build():
  10. target_filename = "FIRMWARE.CUR"
  11. target_drive = "REARM"
  12. import platform
  13. current_OS = platform.system()
  14. Import("env")
  15. def print_error(e):
  16. print('\nUnable to find destination disk (%s)\n' \
  17. 'Please select it in platformio.ini using the upload_port keyword ' \
  18. '(https://docs.platformio.org/en/latest/projectconf/section_env_upload.html) ' \
  19. 'or copy the firmware (.pio/build/%s/firmware.bin) manually to the appropriate disk\n' \
  20. %(e, env.get('PIOENV')))
  21. def before_upload(source, target, env):
  22. try:
  23. from pathlib import Path
  24. #
  25. # Find a disk for upload
  26. #
  27. upload_disk = 'Disk not found'
  28. target_file_found = False
  29. target_drive_found = False
  30. if current_OS == 'Windows':
  31. #
  32. # platformio.ini will accept this for a Windows upload port designation: 'upload_port = L:'
  33. # Windows - doesn't care about the disk's name, only cares about the drive letter
  34. import subprocess,string
  35. from ctypes import windll
  36. from pathlib import PureWindowsPath
  37. # getting list of drives
  38. # https://stackoverflow.com/questions/827371/is-there-a-way-to-list-all-the-available-drive-letters-in-python
  39. drives = []
  40. bitmask = windll.kernel32.GetLogicalDrives()
  41. for letter in string.ascii_uppercase:
  42. if bitmask & 1:
  43. drives.append(letter)
  44. bitmask >>= 1
  45. for drive in drives:
  46. final_drive_name = drive + ':'
  47. # print ('disc check: {}'.format(final_drive_name))
  48. try:
  49. volume_info = str(subprocess.check_output('cmd /C dir ' + final_drive_name, stderr=subprocess.STDOUT))
  50. except Exception as e:
  51. print ('error:{}'.format(e))
  52. continue
  53. else:
  54. if target_drive in volume_info and not target_file_found: # set upload if not found target file yet
  55. target_drive_found = True
  56. upload_disk = PureWindowsPath(final_drive_name)
  57. if target_filename in volume_info:
  58. if not target_file_found:
  59. upload_disk = PureWindowsPath(final_drive_name)
  60. target_file_found = True
  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. import getpass
  66. user = getpass.getuser()
  67. mpath = Path('media', user)
  68. drives = [ x for x in mpath.iterdir() if x.is_dir() ]
  69. if target_drive in drives: # If target drive is found, use it.
  70. target_drive_found = True
  71. upload_disk = mpath / target_drive
  72. else:
  73. for drive in drives:
  74. try:
  75. fpath = mpath / drive
  76. filenames = [ x.name for x in fpath.iterdir() if x.is_file() ]
  77. except:
  78. continue
  79. else:
  80. if target_filename in filenames:
  81. upload_disk = mpath / drive
  82. target_file_found = True
  83. break
  84. #
  85. # set upload_port to drive if found
  86. #
  87. if target_file_found or target_drive_found:
  88. env.Replace(
  89. UPLOAD_FLAGS="-P$UPLOAD_PORT"
  90. )
  91. elif current_OS == 'Darwin': # MAC
  92. #
  93. # platformio.ini will accept this for a OSX upload port designation: 'upload_port = /media/media_name/drive'
  94. #
  95. dpath = Path('/Volumes') # human readable names
  96. drives = [ x for x in dpath.iterdir() if x.is_dir() ]
  97. if target_drive in drives and not target_file_found: # set upload if not found target file yet
  98. target_drive_found = True
  99. upload_disk = dpath / target_drive
  100. for drive in drives:
  101. try:
  102. fpath = dpath / drive # will get an error if the drive is protected
  103. filenames = [ x.name for x in fpath.iterdir() if x.is_file() ]
  104. except:
  105. continue
  106. else:
  107. if target_filename in filenames:
  108. upload_disk = dpath / drive
  109. target_file_found = True
  110. break
  111. #
  112. # Set upload_port to drive if found
  113. #
  114. if target_file_found or target_drive_found:
  115. env.Replace(UPLOAD_PORT=str(upload_disk))
  116. print('\nUpload disk: ', upload_disk, '\n')
  117. else:
  118. print_error('Autodetect Error')
  119. except Exception as e:
  120. print_error(str(e))
  121. env.AddPreAction("upload", before_upload)