Skip to content

Commit 2ae47a1

Browse files
committed
gateway: Change usage of run_cmd function
1 parent a3ca7d1 commit 2ae47a1

4 files changed

Lines changed: 20 additions & 17 deletions

File tree

activate_boot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ def uboot_boot_change(gw, fw_num):
8080
cmd.append("nvram set flag_try_sys2_failed=0")
8181
cmd.append("nvram set flag_boot_rootfs={}".format(fw_num))
8282
cmd.append("nvram commit")
83-
return gw.run_cmd(';'.join(cmd))
83+
out = gw.run_cmd(';'.join(cmd))
84+
return False if out is None else True
8485

8586

8687
if __name__ == "__main__":

create_backup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def backup_and_download(pid, filename, chunk_size = 0, die_on_error = True):
7777
count = max_blocks
7878
cmd = f"rm -f {fn_remote} ; dd if=/dev/mtd{pid} of={fn_remote} bs={blk_size} skip={skip} count={count}"
7979
ret = gw.run_cmd(cmd, timeout = 25, die_on_error = False)
80-
if not ret:
80+
if ret is None:
8181
print(f'ERROR on execute command: "{cmd}"')
8282
if die_on_error:
8383
sys.exit(1)

gateway.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -894,19 +894,21 @@ def ping(self, verbose = 2, contimeout = None):
894894
return False
895895
return True
896896

897-
def run_cmd(self, cmd, msg = None, timeout = None, die_on_error = True):
898-
ret = True
897+
def run_cmd(self, command, msg = None, timeout = None, die_on_error = True):
898+
error = 0
899899
if self.use_ssh:
900900
ssh = self.get_ssh(self.verbose)
901901
else:
902902
tn = self.get_telnet(self.verbose)
903903
if (msg):
904904
print(msg)
905-
cmdlist = []
906-
if isinstance(cmd, str):
907-
cmdlist.append(cmd)
905+
cmdlist = [ ]
906+
if isinstance(command, str):
907+
cmdlist.append(command)
908908
else:
909-
cmdlist = cmd
909+
cmdlist = command
910+
if not cmdlist:
911+
raise ValueError('Incorrect command list')
910912
for idx, cmd in enumerate(cmdlist):
911913
if self.use_ssh:
912914
channel = ssh.open_session()
@@ -920,7 +922,7 @@ def run_cmd(self, cmd, msg = None, timeout = None, die_on_error = True):
920922
channel.wait_eof()
921923
except ssh2.exceptions.Timeout:
922924
ssh.set_timeout(100)
923-
ret = False
925+
error = -4
924926
if die_on_error:
925927
die("SSH execute command timed out! CMD: \"{}\"".format(cmd))
926928
if timeout is not None:
@@ -931,16 +933,16 @@ def run_cmd(self, cmd, msg = None, timeout = None, die_on_error = True):
931933
except Exception:
932934
pass
933935
#status = channel.get_exit_status()
934-
if not ret:
935-
break
936-
else:
936+
else: # telnet
937937
cmd += '\n'
938938
tn.write(cmd.encode('ascii'))
939939
tn.read_until(tn.prompt, timeout = 4 if timeout is None else timeout)
940+
if error != 0:
941+
break
940942
if not self.use_ssh:
941943
tn.write(b"exit\n")
942-
ret = True
943-
return ret
944+
tn.close()
945+
return True if error == 0 else None
944946

945947
def download(self, fn_remote, fn_local, verbose = 1):
946948
if verbose and self.verbose:
@@ -1008,7 +1010,7 @@ def get_md5_for_remote_file(self, fn_remote, timeout = 8):
10081010
md5_remote_fn = f"/tmp/{fname}.{num}.md5"
10091011
cmd = f'md5sum "{fn_remote}" > "{md5_remote_fn}" 2>&1'
10101012
rc = self.run_cmd(cmd, timeout = timeout)
1011-
if not rc:
1013+
if rc is None:
10121014
return -5
10131015
os.remove(md5_local_fn) if os.path.exists(md5_local_fn) else None
10141016
self.download(md5_remote_fn, md5_local_fn, verbose = 0)

install_fw.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ def flash(self):
10791079
cmd.append("nvram set uart_en=1")
10801080
cmd.append("nvram commit")
10811081
rc = gw.run_cmd(';'.join(cmd), timeout = 8)
1082-
if not rc:
1082+
if rc is None:
10831083
die(f'Cannot change nvram parameters!')
10841084

10851085
if fw_img.cmd:
@@ -1133,7 +1133,7 @@ def flash_data_to_mtd(self, img_name, img: Image, timeout, check = True):
11331133
if not self.img_write:
11341134
return True
11351135
rc = self.gw.run_cmd(img.cmd, timeout = timeout, die_on_error = True)
1136-
if not rc:
1136+
if rc is None:
11371137
print(f' ERROR: cannot flash data to partition "{partname}"')
11381138
return False
11391139
if check:

0 commit comments

Comments
 (0)