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