1import sys
2
3# import uos as os
4import os
5import machine
6
7RC = "./boot.py"
8CONFIG = "./webrepl_cfg.py"
9
10
11def input_choice(prompt, choices):
12    while 1:
13        resp = input(prompt)
14        if resp in choices:
15            return resp
16
17
18def getpass(prompt):
19    return input(prompt)
20
21
22def input_pass():
23    while 1:
24        passwd1 = getpass("New password (4-9 chars): ")
25        if len(passwd1) < 4 or len(passwd1) > 9:
26            print("Invalid password length")
27            continue
28        passwd2 = getpass("Confirm password: ")
29        if passwd1 == passwd2:
30            return passwd1
31        print("Passwords do not match")
32
33
34def exists(fname):
35    try:
36        with open(fname):
37            pass
38        return True
39    except OSError:
40        return False
41
42
43def get_daemon_status():
44    with open(RC) as f:
45        for l in f:
46            if "webrepl" in l:
47                if l.startswith("#"):
48                    return False
49                return True
50        return None
51
52
53def change_daemon(action):
54    LINES = ("import webrepl", "webrepl.start()")
55    with open(RC) as old_f, open(RC + ".tmp", "w") as new_f:
56        found = False
57        for l in old_f:
58            for patt in LINES:
59                if patt in l:
60                    found = True
61                    if action and l.startswith("#"):
62                        l = l[1:]
63                    elif not action and not l.startswith("#"):
64                        l = "#" + l
65            new_f.write(l)
66        if not found:
67            new_f.write("import webrepl\nwebrepl.start()\n")
68    # FatFs rename() is not POSIX compliant, will raise OSError if
69    # dest file exists.
70    os.remove(RC)
71    os.rename(RC + ".tmp", RC)
72
73
74def main():
75    status = get_daemon_status()
76
77    print("WebREPL daemon auto-start status:", "enabled" if status else "disabled")
78    print("\nWould you like to (E)nable or (D)isable it running on boot?")
79    print("(Empty line to quit)")
80    resp = input("> ").upper()
81
82    if resp == "E":
83        if exists(CONFIG):
84            resp2 = input_choice(
85                "Would you like to change WebREPL password? (y/n) ", ("y", "n", "")
86            )
87        else:
88            print("To enable WebREPL, you must set password for it")
89            resp2 = "y"
90
91        if resp2 == "y":
92            passwd = input_pass()
93            with open(CONFIG, "w") as f:
94                f.write("PASS = %r\n" % passwd)
95
96    if resp not in ("D", "E") or (resp == "D" and not status) or (resp == "E" and status):
97        print("No further action required")
98        sys.exit()
99
100    change_daemon(resp == "E")
101
102    print("Changes will be activated after reboot")
103    resp = input_choice("Would you like to reboot now? (y/n) ", ("y", "n", ""))
104    if resp == "y":
105        machine.reset()
106
107
108main()
109