Procházet zdrojové kódy

Only look for target disk during Upload (#21804)

ManuelMcLure před 4 roky
rodič
revize
bac72ff0cc
No account linked to committer's email address
1 změnil soubory, kde provedl 85 přidání a 82 odebrání
  1. 85
    82
      Marlin/src/HAL/LPC1768/upload_extra_script.py

+ 85
- 82
Marlin/src/HAL/LPC1768/upload_extra_script.py Zobrazit soubor

20
 		  'or copy the firmware (.pio/build/%s/firmware.bin) manually to the appropriate disk\n' \
20
 		  'or copy the firmware (.pio/build/%s/firmware.bin) manually to the appropriate disk\n' \
21
 		  %(e, env.get('PIOENV')))
21
 		  %(e, env.get('PIOENV')))
22
 
22
 
23
-try:
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':
23
+def before_upload(source, target, env):
24
+	try:
31
 		#
25
 		#
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
26
+		# Find a disk for upload
27
+		#
28
+		upload_disk = 'Disk not found'
29
+		target_file_found = False
30
+		target_drive_found = False
31
+		if current_OS == 'Windows':
32
+			#
33
+			# platformio.ini will accept this for a Windows upload port designation: 'upload_port = L:'
34
+			#   Windows - doesn't care about the disk's name, only cares about the drive letter
35
+			import subprocess,string
36
+			from ctypes import windll
36
 
37
 
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
38
+			# getting list of drives
39
+			# https://stackoverflow.com/questions/827371/is-there-a-way-to-list-all-the-available-drive-letters-in-python
40
+			drives = []
41
+			bitmask = windll.kernel32.GetLogicalDrives()
42
+			for letter in string.ascii_uppercase:
43
+				if bitmask & 1:
44
+					drives.append(letter)
45
+				bitmask >>= 1
45
 
46
 
46
-		for drive in drives:
47
-			final_drive_name = drive + ':\\'
48
-			# print ('disc check: {}'.format(final_drive_name))
49
-			try:
50
-				volume_info = str(subprocess.check_output('cmd /C dir ' + final_drive_name, stderr=subprocess.STDOUT))
51
-			except Exception as e:
52
-				print ('error:{}'.format(e))
53
-				continue
54
-			else:
55
-				if target_drive in volume_info and not target_file_found:  # set upload if not found target file yet
56
-					target_drive_found = True
57
-					upload_disk = final_drive_name
58
-				if target_filename in volume_info:
59
-					if not target_file_found:
47
+			for drive in drives:
48
+				final_drive_name = drive + ':\\'
49
+				# print ('disc check: {}'.format(final_drive_name))
50
+				try:
51
+					volume_info = str(subprocess.check_output('cmd /C dir ' + final_drive_name, stderr=subprocess.STDOUT))
52
+				except Exception as e:
53
+					print ('error:{}'.format(e))
54
+					continue
55
+				else:
56
+					if target_drive in volume_info and not target_file_found:  # set upload if not found target file yet
57
+						target_drive_found = True
60
 						upload_disk = final_drive_name
58
 						upload_disk = final_drive_name
61
-					target_file_found = True
59
+					if target_filename in volume_info:
60
+						if not target_file_found:
61
+							upload_disk = final_drive_name
62
+						target_file_found = True
62
 
63
 
63
-	elif current_OS == 'Linux':
64
-		#
65
-		# platformio.ini will accept this for a Linux upload port designation: 'upload_port = /media/media_name/drive'
66
-		#
67
-		drives = os.listdir(os.path.join(os.sep, 'media', getpass.getuser()))
68
-		if target_drive in drives:  # If target drive is found, use it.
69
-			target_drive_found = True
70
-			upload_disk = os.path.join(os.sep, 'media', getpass.getuser(), target_drive) + os.sep
71
-		else:
64
+		elif current_OS == 'Linux':
65
+			#
66
+			# platformio.ini will accept this for a Linux upload port designation: 'upload_port = /media/media_name/drive'
67
+			#
68
+			drives = os.listdir(os.path.join(os.sep, 'media', getpass.getuser()))
69
+			if target_drive in drives:  # If target drive is found, use it.
70
+				target_drive_found = True
71
+				upload_disk = os.path.join(os.sep, 'media', getpass.getuser(), target_drive) + os.sep
72
+			else:
73
+				for drive in drives:
74
+					try:
75
+						files = os.listdir(os.path.join(os.sep, 'media', getpass.getuser(), drive))
76
+					except:
77
+						continue
78
+					else:
79
+						if target_filename in files:
80
+							upload_disk = os.path.join(os.sep, 'media', getpass.getuser(), drive) + os.sep
81
+							target_file_found = True
82
+							break
83
+			#
84
+			# set upload_port to drive if found
85
+			#
86
+
87
+			if target_file_found or target_drive_found:
88
+				env.Replace(
89
+					UPLOAD_FLAGS="-P$UPLOAD_PORT"
90
+				)
91
+
92
+		elif current_OS == 'Darwin':  # MAC
93
+			#
94
+			# platformio.ini will accept this for a OSX upload port designation: 'upload_port = /media/media_name/drive'
95
+			#
96
+			drives = os.listdir('/Volumes')  # human readable names
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 = '/Volumes/' + target_drive + '/'
72
 			for drive in drives:
100
 			for drive in drives:
73
 				try:
101
 				try:
74
-					files = os.listdir(os.path.join(os.sep, 'media', getpass.getuser(), drive))
102
+					filenames = os.listdir('/Volumes/' + drive + '/')   # will get an error if the drive is protected
75
 				except:
103
 				except:
76
 					continue
104
 					continue
77
 				else:
105
 				else:
78
-					if target_filename in files:
79
-						upload_disk = os.path.join(os.sep, 'media', getpass.getuser(), drive) + os.sep
106
+					if target_filename in filenames:
107
+						if not target_file_found:
108
+							upload_disk = '/Volumes/' + drive + '/'
80
 						target_file_found = True
109
 						target_file_found = True
81
-						break
82
-		#
83
-		# set upload_port to drive if found
84
-		#
85
 
110
 
86
-		if target_file_found or target_drive_found:
87
-			env.Replace(
88
-				UPLOAD_FLAGS="-P$UPLOAD_PORT"
89
-			)
90
-
91
-	elif current_OS == 'Darwin':  # MAC
92
 		#
111
 		#
93
-		# platformio.ini will accept this for a OSX upload port designation: 'upload_port = /media/media_name/drive'
112
+		# Set upload_port to drive if found
94
 		#
113
 		#
95
-		drives = os.listdir('/Volumes')  # human readable names
96
-		if target_drive in drives and not target_file_found:  # set upload if not found target file yet
97
-			target_drive_found = True
98
-			upload_disk = '/Volumes/' + target_drive + '/'
99
-		for drive in drives:
100
-			try:
101
-				filenames = os.listdir('/Volumes/' + drive + '/')   # will get an error if the drive is protected
102
-			except:
103
-				continue
104
-			else:
105
-				if target_filename in filenames:
106
-					if not target_file_found:
107
-						upload_disk = '/Volumes/' + drive + '/'
108
-					target_file_found = True
114
+		if target_file_found or target_drive_found:
115
+			env.Replace(UPLOAD_PORT=upload_disk)
116
+			print('\nUpload disk: ', upload_disk, '\n')
117
+		else:
118
+			print_error('Autodetect Error')
109
 
119
 
110
-	#
111
-	# Set upload_port to drive if found
112
-	#
113
-	if target_file_found or target_drive_found:
114
-		env.Replace(UPLOAD_PORT=upload_disk)
115
-		print('\nUpload disk: ', upload_disk, '\n')
116
-	else:
117
-		print_error('Autodetect Error')
120
+	except Exception as e:
121
+		print_error(str(e))
118
 
122
 
119
-except Exception as e:
120
-	print_error(str(e))
123
+env.AddPreAction("upload", before_upload)

Loading…
Zrušit
Uložit