@@ -25,26 +25,43 @@ def print_exception(e):
2525 print ('Exception: {}, on line {}' .format (e , sys .exc_info ()[- 1 ].tb_lineno ))
2626
2727
28+ def ftp_directory_exists (ftpobj , directory_name ):
29+ filelist = []
30+ ftpobj .retrlines ('LIST' ,filelist .append )
31+ for f in filelist :
32+ if f .split ()[- 1 ] == directory_name :
33+ return True
34+ return False
35+
36+
2837def transfer_file (args ):
29- with FTP (args .ip , timeout = 10 ) as ftp :
38+ with FTP (args .ip , timeout = 20 ) as ftp :
3039 print ('FTP connection established' )
3140
3241 if '230' in ftp .login (args .user , args .password ):
3342 print ('Login successful' )
3443
35- if '250' in ftp .cwd ('/flash/sys' ):
36- print ("Entered '/flash/sys' directory" )
37-
38- with open (args .file , "rb" ) as fwfile :
39- print ('Firmware image found, initiating transfer...' )
40-
41- if '226' in ftp .storbinary ("STOR " + 'mcuimg.bin' , fwfile , 512 ):
42- print ('File transfer complete' )
43- return True
44+ if '250' in ftp .cwd ('/flash' ):
45+ if not ftp_directory_exists (ftp , 'sys' ):
46+ print ('/flash/sys directory does not exist' )
47+ if not '550' in ftp .mkd ('sys' ):
48+ print ('/flash/sys directory created' )
4449 else :
45- print ('Error: file transfer failed' )
50+ print ('Error: cannot create /flash/sys directory' )
51+ return False
52+ if '250' in ftp .cwd ('sys' ):
53+ print ("Entered '/flash/sys' directory" )
54+ with open (args .file , "rb" ) as fwfile :
55+ print ('Firmware image found, initiating transfer...' )
56+ if '226' in ftp .storbinary ("STOR " + 'mcuimg.bin' , fwfile , 512 ):
57+ print ('File transfer complete' )
58+ return True
59+ else :
60+ print ('Error: file transfer failed' )
61+ else :
62+ print ('Error: cannot enter /flash/sys directory' )
4663 else :
47- print ('Error: cannot enter /flash/sys directory' )
64+ print ('Error: cannot enter /flash directory' )
4865 else :
4966 print ('Error: ftp login failed' )
5067
@@ -97,12 +114,12 @@ def verify_update(args):
97114 firmware_tag = ''
98115
99116 def find_tag (tag ):
100- nonlocal success , firmware_tag
101117 if tag in firmware_tag :
102- success = True
103118 print ("Verification passed" )
119+ return True
104120 else :
105121 print ("Error: verification failed, the git tag doesn't match" )
122+ return False
106123
107124 try :
108125 # Specify a longer time out value here because the board has just been
@@ -114,14 +131,14 @@ def find_tag (tag):
114131 tag_file_path = args .file .rstrip ('mcuimg.bin' ) + 'genhdr/mpversion.h'
115132
116133 if args .tag is not None :
117- find_tag (bytes (args .tag , 'ascii' ))
134+ success = find_tag (bytes (args .tag , 'ascii' ))
118135 else :
119136 with open (tag_file_path ) as tag_file :
120137 for line in tag_file :
121138 bline = bytes (line , 'ascii' )
122139 if b'MICROPY_GIT_HASH' in bline :
123140 bline = bline .lstrip (b'#define MICROPY_GIT_HASH ' ).replace (b'"' , b'' ).replace (b'\r ' , b'' ).replace (b'\n ' , b'' )
124- find_tag (bline )
141+ success = find_tag (bline )
125142 break
126143
127144 except Exception as e :
0 commit comments