ソースを参照

Windows LPC Upload for non-admins (#20208)

Co-authored-by: Victor Mateus Oliveira <rhapsodyv@gmail.com>
Co-authored-by: Scott Lahteine <github@thinkyhead.com>
Vitaliy 4年前
コミット
19d0c985be
コミッターのメールアドレスに関連付けられたアカウントが存在しません
1個のファイルの変更33行の追加55行の削除
  1. 33
    55
      Marlin/src/HAL/LPC1768/upload_extra_script.py

+ 33
- 55
Marlin/src/HAL/LPC1768/upload_extra_script.py ファイルの表示

@@ -23,63 +23,50 @@ def print_error(e):
23 23
 		  %(e, env.get('PIOENV')))
24 24
 
25 25
 try:
26
+	#
27
+	# Find a disk for upload
28
+	#
29
+	upload_disk = 'Disk not found'
30
+	target_file_found = False
31
+	target_drive_found = False
26 32
 	if current_OS == 'Windows':
27 33
 		#
28 34
 		# platformio.ini will accept this for a Windows upload port designation: 'upload_port = L:'
29 35
 		#   Windows - doesn't care about the disk's name, only cares about the drive letter
30
-		#
31
-
32
-		#
33
-		# get all drives on this computer
34
-		#
35 36
 		import subprocess
36
-		# typical result (string): 'Drives: C:\ D:\ E:\ F:\ G:\ H:\ I:\ J:\ K:\ L:\ M:\ Y:\ Z:\'
37
-		driveStr = str(subprocess.check_output("fsutil fsinfo drives"))
38
-		# typical result (string): 'C:\ D:\ E:\ F:\ G:\ H:\ I:\ J:\ K:\ L:\ M:\ Y:\ Z:\'
39
-		# driveStr = driveStr.strip().lstrip('Drives: ') <- Doesn't work in other Languages as English. In German is "Drives:" = "Laufwerke:"
40
-		FirstFound = driveStr.find(':',0,-1)         # Find the first ":" and
41
-		driveStr = driveStr[FirstFound + 1 : -1]     # truncate to the rest
42
-		# typical result (array of stings): ['C:\\', 'D:\\', 'E:\\', 'F:\\',
43
-		# 'G:\\', 'H:\\', 'I:\\', 'J:\\', 'K:\\', 'L:\\', 'M:\\', 'Y:\\', 'Z:\\']
44
-		drives = driveStr.split()
37
+		from ctypes import windll
38
+		import string
39
+
40
+		# getting list of drives
41
+		# https://stackoverflow.com/questions/827371/is-there-a-way-to-list-all-the-available-drive-letters-in-python
42
+		drives = []
43
+		bitmask = windll.kernel32.GetLogicalDrives()
44
+		for letter in string.ascii_uppercase:
45
+			if bitmask & 1:
46
+				drives.append(letter)
47
+			bitmask >>= 1
45 48
 
46
-		upload_disk = 'Disk not found'
47
-		target_file_found = False
48
-		target_drive_found = False
49 49
 		for drive in drives:
50
-			final_drive_name = drive.strip().rstrip('\\')   # typical result (string): 'C:'
50
+			final_drive_name = drive + ':\\'
51
+			# print ('disc check: {}'.format(final_drive_name))
51 52
 			try:
52 53
 				volume_info = str(subprocess.check_output('cmd /C dir ' + final_drive_name, stderr=subprocess.STDOUT))
53 54
 			except Exception as e:
55
+				print ('error:{}'.format(e))
54 56
 				continue
55 57
 			else:
56
-				if target_drive in volume_info and target_file_found == False:  # set upload if not found target file yet
58
+				if target_drive in volume_info and not target_file_found:  # set upload if not found target file yet
57 59
 					target_drive_found = True
58 60
 					upload_disk = final_drive_name
59 61
 				if target_filename in volume_info:
60
-					if target_file_found == False:
62
+					if not target_file_found:
61 63
 						upload_disk = final_drive_name
62 64
 					target_file_found = True
63 65
 
64
-		#
65
-		# set upload_port to drive if found
66
-		#
67
-
68
-		if target_file_found == True or target_drive_found == True:
69
-			env.Replace(
70
-				UPLOAD_PORT=upload_disk
71
-			)
72
-			print('upload disk: ', upload_disk)
73
-		else:
74
-			print_error('Autodetect Error')
75
-
76 66
 	elif current_OS == 'Linux':
77 67
 		#
78 68
 		# platformio.ini will accept this for a Linux upload port designation: 'upload_port = /media/media_name/drive'
79 69
 		#
80
-		upload_disk = 'Disk not found'
81
-		target_file_found = False
82
-		target_drive_found = False
83 70
 		drives = os.listdir(os.path.join(os.sep, 'media', getpass.getuser()))
84 71
 		if target_drive in drives:  # If target drive is found, use it.
85 72
 			target_drive_found = True
@@ -101,22 +88,15 @@ try:
101 88
 
102 89
 		if target_file_found or target_drive_found:
103 90
 			env.Replace(
104
-				UPLOAD_FLAGS="-P$UPLOAD_PORT",
105
-				UPLOAD_PORT=upload_disk
91
+				UPLOAD_FLAGS="-P$UPLOAD_PORT"
106 92
 			)
107
-			print('upload disk: ', upload_disk)
108
-		else:
109
-			print_error('Autodetect Error')
110 93
 
111 94
 	elif current_OS == 'Darwin':  # MAC
112 95
 		#
113 96
 		# platformio.ini will accept this for a OSX upload port designation: 'upload_port = /media/media_name/drive'
114 97
 		#
115
-		upload_disk = 'Disk not found'
116 98
 		drives = os.listdir('/Volumes')  # human readable names
117
-		target_file_found = False
118
-		target_drive_found = False
119
-		if target_drive in drives and target_file_found == False:  # set upload if not found target file yet
99
+		if target_drive in drives and not target_file_found:  # set upload if not found target file yet
120 100
 			target_drive_found = True
121 101
 			upload_disk = '/Volumes/' + target_drive + '/'
122 102
 		for drive in drives:
@@ -126,20 +106,18 @@ try:
126 106
 				continue
127 107
 			else:
128 108
 				if target_filename in filenames:
129
-					if target_file_found == False:
109
+					if not target_file_found:
130 110
 						upload_disk = '/Volumes/' + drive + '/'
131 111
 					target_file_found = True
132
-		#
133
-		# set upload_port to drive if found
134
-		#
135 112
 
136
-		if target_file_found == True or target_drive_found == True:
137
-			env.Replace(
138
-				UPLOAD_PORT=upload_disk
139
-			)
140
-			print('\nupload disk: ', upload_disk, '\n')
141
-		else:
142
-			print_error('Autodetect Error')
113
+	#
114
+	# Set upload_port to drive if found
115
+	#
116
+	if target_file_found or target_drive_found:
117
+		env.Replace(UPLOAD_PORT=upload_disk)
118
+		print('\nUpload disk: ', upload_disk, '\n')
119
+	else:
120
+		print_error('Autodetect Error')
143 121
 
144 122
 except Exception as e:
145 123
 	print_error(str(e))

読み込み中…
キャンセル
保存