2016-04-30 13:41:09 -04:00
|
|
|
import sys
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2016-11-06 02:01:48 -05:00
|
|
|
# import uos as os
|
|
|
|
import os
|
|
|
|
import machine
|
2016-04-30 13:41:09 -04:00
|
|
|
|
2016-11-06 02:01:48 -05:00
|
|
|
RC = "./boot.py"
|
|
|
|
CONFIG = "./webrepl_cfg.py"
|
2016-04-30 13:41:09 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2016-11-06 02:01:48 -05:00
|
|
|
def input_choice(prompt, choices):
|
2016-04-30 13:41:09 -04:00
|
|
|
while 1:
|
2016-11-06 02:01:48 -05:00
|
|
|
resp = input(prompt)
|
|
|
|
if resp in choices:
|
|
|
|
return resp
|
2016-04-30 13:41:09 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2016-11-06 02:01:48 -05:00
|
|
|
def getpass(prompt):
|
|
|
|
return input(prompt)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2016-11-06 02:01:48 -05:00
|
|
|
def input_pass():
|
2016-04-30 13:41:09 -04:00
|
|
|
while 1:
|
2017-10-08 14:28:32 -04:00
|
|
|
passwd1 = getpass("New password (4-9 chars): ")
|
|
|
|
if len(passwd1) < 4 or len(passwd1) > 9:
|
|
|
|
print("Invalid password length")
|
2016-05-16 18:12:21 -04:00
|
|
|
continue
|
2016-11-06 02:01:48 -05:00
|
|
|
passwd2 = getpass("Confirm password: ")
|
2016-04-30 13:41:09 -04:00
|
|
|
if passwd1 == passwd2:
|
2016-11-06 02:01:48 -05:00
|
|
|
return passwd1
|
|
|
|
print("Passwords do not match")
|
2016-04-30 13:41:09 -04:00
|
|
|
|
|
|
|
|
2016-11-06 02:01:48 -05:00
|
|
|
def exists(fname):
|
|
|
|
try:
|
|
|
|
with open(fname):
|
|
|
|
pass
|
|
|
|
return True
|
|
|
|
except OSError:
|
|
|
|
return False
|
2016-04-30 13:41:09 -04:00
|
|
|
|
2016-11-06 02:01:48 -05:00
|
|
|
|
|
|
|
def get_daemon_status():
|
|
|
|
with open(RC) as f:
|
|
|
|
for l in f:
|
|
|
|
if "webrepl" in l:
|
|
|
|
if l.startswith("#"):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def change_daemon(action):
|
|
|
|
LINES = ("import webrepl", "webrepl.start()")
|
|
|
|
with open(RC) as old_f, open(RC + ".tmp", "w") as new_f:
|
2017-11-29 18:54:33 -05:00
|
|
|
found = False
|
2016-11-06 02:01:48 -05:00
|
|
|
for l in old_f:
|
|
|
|
for patt in LINES:
|
|
|
|
if patt in l:
|
2017-11-29 18:54:33 -05:00
|
|
|
found = True
|
2016-11-06 02:01:48 -05:00
|
|
|
if action and l.startswith("#"):
|
|
|
|
l = l[1:]
|
|
|
|
elif not action and not l.startswith("#"):
|
|
|
|
l = "#" + l
|
|
|
|
new_f.write(l)
|
2017-11-29 18:54:33 -05:00
|
|
|
if not found:
|
|
|
|
new_f.write("import webrepl\nwebrepl.start()\n")
|
2016-11-06 02:01:48 -05:00
|
|
|
# FatFs rename() is not POSIX compliant, will raise OSError if
|
|
|
|
# dest file exists.
|
|
|
|
os.remove(RC)
|
|
|
|
os.rename(RC + ".tmp", RC)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
status = get_daemon_status()
|
|
|
|
|
|
|
|
print("WebREPL daemon auto-start status:", "enabled" if status else "disabled")
|
|
|
|
print("\nWould you like to (E)nable or (D)isable it running on boot?")
|
|
|
|
print("(Empty line to quit)")
|
|
|
|
resp = input("> ").upper()
|
|
|
|
|
|
|
|
if resp == "E":
|
|
|
|
if exists(CONFIG):
|
|
|
|
resp2 = input_choice(
|
|
|
|
"Would you like to change WebREPL password? (y/n) ", ("y", "n", "")
|
2020-02-26 23:36:53 -05:00
|
|
|
)
|
2016-11-06 02:01:48 -05:00
|
|
|
else:
|
|
|
|
print("To enable WebREPL, you must set password for it")
|
|
|
|
resp2 = "y"
|
|
|
|
|
|
|
|
if resp2 == "y":
|
|
|
|
passwd = input_pass()
|
|
|
|
with open(CONFIG, "w") as f:
|
|
|
|
f.write("PASS = %r\n" % passwd)
|
|
|
|
|
|
|
|
if resp not in ("D", "E") or (resp == "D" and not status) or (resp == "E" and status):
|
|
|
|
print("No further action required")
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
change_daemon(resp == "E")
|
|
|
|
|
|
|
|
print("Changes will be activated after reboot")
|
|
|
|
resp = input_choice("Would you like to reboot now? (y/n) ", ("y", "n", ""))
|
|
|
|
if resp == "y":
|
|
|
|
machine.reset()
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2016-11-06 02:01:48 -05:00
|
|
|
main()
|