Windows LPC Upload for non-admins (#20208)

Co-authored-by: Victor Mateus Oliveira <rhapsodyv@gmail.com>
Co-authored-by: Scott Lahteine <github@thinkyhead.com>
This commit is contained in:
Vitaliy 2020-11-20 04:49:12 +05:00 committed by GitHub
parent 4a0fc4d699
commit 19d0c985be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -23,63 +23,50 @@ def print_error(e):
%(e, env.get('PIOENV'))) %(e, env.get('PIOENV')))
try: try:
#
# Find a disk for upload
#
upload_disk = 'Disk not found'
target_file_found = False
target_drive_found = False
if current_OS == 'Windows': if current_OS == 'Windows':
# #
# platformio.ini will accept this for a Windows upload port designation: 'upload_port = L:' # platformio.ini will accept this for a Windows upload port designation: 'upload_port = L:'
# Windows - doesn't care about the disk's name, only cares about the drive letter # Windows - doesn't care about the disk's name, only cares about the drive letter
#
#
# get all drives on this computer
#
import subprocess import subprocess
# typical result (string): 'Drives: C:\ D:\ E:\ F:\ G:\ H:\ I:\ J:\ K:\ L:\ M:\ Y:\ Z:\' from ctypes import windll
driveStr = str(subprocess.check_output("fsutil fsinfo drives")) import string
# typical result (string): 'C:\ D:\ E:\ F:\ G:\ H:\ I:\ J:\ K:\ L:\ M:\ Y:\ Z:\'
# driveStr = driveStr.strip().lstrip('Drives: ') <- Doesn't work in other Languages as English. In German is "Drives:" = "Laufwerke:" # getting list of drives
FirstFound = driveStr.find(':',0,-1) # Find the first ":" and # https://stackoverflow.com/questions/827371/is-there-a-way-to-list-all-the-available-drive-letters-in-python
driveStr = driveStr[FirstFound + 1 : -1] # truncate to the rest drives = []
# typical result (array of stings): ['C:\\', 'D:\\', 'E:\\', 'F:\\', bitmask = windll.kernel32.GetLogicalDrives()
# 'G:\\', 'H:\\', 'I:\\', 'J:\\', 'K:\\', 'L:\\', 'M:\\', 'Y:\\', 'Z:\\'] for letter in string.ascii_uppercase:
drives = driveStr.split() if bitmask & 1:
drives.append(letter)
bitmask >>= 1
upload_disk = 'Disk not found'
target_file_found = False
target_drive_found = False
for drive in drives: for drive in drives:
final_drive_name = drive.strip().rstrip('\\') # typical result (string): 'C:' final_drive_name = drive + ':\\'
# print ('disc check: {}'.format(final_drive_name))
try: try:
volume_info = str(subprocess.check_output('cmd /C dir ' + final_drive_name, stderr=subprocess.STDOUT)) volume_info = str(subprocess.check_output('cmd /C dir ' + final_drive_name, stderr=subprocess.STDOUT))
except Exception as e: except Exception as e:
print ('error:{}'.format(e))
continue continue
else: else:
if target_drive in volume_info and target_file_found == False: # set upload if not found target file yet if target_drive in volume_info and not target_file_found: # set upload if not found target file yet
target_drive_found = True target_drive_found = True
upload_disk = final_drive_name upload_disk = final_drive_name
if target_filename in volume_info: if target_filename in volume_info:
if target_file_found == False: if not target_file_found:
upload_disk = final_drive_name upload_disk = final_drive_name
target_file_found = True target_file_found = True
#
# set upload_port to drive if found
#
if target_file_found == True or target_drive_found == True:
env.Replace(
UPLOAD_PORT=upload_disk
)
print('upload disk: ', upload_disk)
else:
print_error('Autodetect Error')
elif current_OS == 'Linux': elif current_OS == 'Linux':
# #
# platformio.ini will accept this for a Linux upload port designation: 'upload_port = /media/media_name/drive' # platformio.ini will accept this for a Linux upload port designation: 'upload_port = /media/media_name/drive'
# #
upload_disk = 'Disk not found'
target_file_found = False
target_drive_found = False
drives = os.listdir(os.path.join(os.sep, 'media', getpass.getuser())) drives = os.listdir(os.path.join(os.sep, 'media', getpass.getuser()))
if target_drive in drives: # If target drive is found, use it. if target_drive in drives: # If target drive is found, use it.
target_drive_found = True target_drive_found = True
@ -101,22 +88,15 @@ try:
if target_file_found or target_drive_found: if target_file_found or target_drive_found:
env.Replace( env.Replace(
UPLOAD_FLAGS="-P$UPLOAD_PORT", UPLOAD_FLAGS="-P$UPLOAD_PORT"
UPLOAD_PORT=upload_disk
) )
print('upload disk: ', upload_disk)
else:
print_error('Autodetect Error')
elif current_OS == 'Darwin': # MAC elif current_OS == 'Darwin': # MAC
# #
# platformio.ini will accept this for a OSX upload port designation: 'upload_port = /media/media_name/drive' # platformio.ini will accept this for a OSX upload port designation: 'upload_port = /media/media_name/drive'
# #
upload_disk = 'Disk not found'
drives = os.listdir('/Volumes') # human readable names drives = os.listdir('/Volumes') # human readable names
target_file_found = False if target_drive in drives and not target_file_found: # set upload if not found target file yet
target_drive_found = False
if target_drive in drives and target_file_found == False: # set upload if not found target file yet
target_drive_found = True target_drive_found = True
upload_disk = '/Volumes/' + target_drive + '/' upload_disk = '/Volumes/' + target_drive + '/'
for drive in drives: for drive in drives:
@ -126,20 +106,18 @@ try:
continue continue
else: else:
if target_filename in filenames: if target_filename in filenames:
if target_file_found == False: if not target_file_found:
upload_disk = '/Volumes/' + drive + '/' upload_disk = '/Volumes/' + drive + '/'
target_file_found = True target_file_found = True
#
# set upload_port to drive if found
#
if target_file_found == True or target_drive_found == True: #
env.Replace( # Set upload_port to drive if found
UPLOAD_PORT=upload_disk #
) if target_file_found or target_drive_found:
print('\nupload disk: ', upload_disk, '\n') env.Replace(UPLOAD_PORT=upload_disk)
else: print('\nUpload disk: ', upload_disk, '\n')
print_error('Autodetect Error') else:
print_error('Autodetect Error')
except Exception as e: except Exception as e:
print_error(str(e)) print_error(str(e))