-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
go_back.py
82 lines (69 loc) · 2.74 KB
/
go_back.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
import sys
if sys.version_info.major != 3:
print("This script will only run on python 3")
print("Run using 'python3 go_back.py'")
sys.exit(0)
import os
import subprocess
BASE = '\033['
class TextColors:
RESET = BASE + '39m'
RED = BASE + '31m'
GREEN = BASE + '32m'
YELLOW = BASE + '33m'
class TextStyles:
BRIGHT = BASE + '1m'
NORMAL = BASE + '22m'
print("OctoPrint upgrade to Python 3: go_back.py (v1.1)")
print("This script will move your old installation back (Just in case!)")
print("{}Only use it if you have used the upgrade script and it failed{}".format(TextColors.YELLOW, TextColors.RESET))
print("Warning: There have been reports of this script failing, if it fails for you please report it to me as soon as possible. Thanks!")
try:
go = input("Press {}[enter]{} to continue, or ctrl-c to stop".format(TextColors.GREEN, TextColors.RESET))
except KeyboardInterrupt:
print("\nBye!")
sys.exit(0)
PATH_TO_VENV = None
CONFBASE = None
if os.path.isfile("/etc/octopi_version"):
print("\n{}Detected OctoPi installation{}".format(TextColors.GREEN, TextColors.RESET))
PATH_TO_VENV = "/home/pi/oprint"
STOP_COMMAND = "sudo service octoprint stop"
START_COMMAND = "sudo service octoprint start"
else:
print("\n{}Manual install detected{}".format(TextColors.GREEN, TextColors.RESET))
print("Please provide the path to your virtual environment")
while not PATH_TO_VENV:
path = input("Path: ")
if os.path.isfile("{}/bin/python".format(path)):
print("{}Venv found{}".format(TextColors.GREEN, TextColors.RESET))
PATH_TO_VENV = path
else:
print("{}Invalid venv path, please try again{}".format(TextColors.RED, TextColors.RESET))
print("\nTo revert the install, we need the service stop and start commands for OctoPrint")
STOP_COMMAND = input("Stop command: ")
START_COMMAND = input("Start command: ")
COMMANDS = [
STOP_COMMAND.split(),
['mv', PATH_TO_VENV, '{}FAIL.bak'.format(PATH_TO_VENV)],
['mv', '{}.bak'.format(PATH_TO_VENV), PATH_TO_VENV],
START_COMMAND.split()
]
for command in COMMANDS:
process = subprocess.Popen(
command,
stdout=subprocess.PIPE
)
while True:
output = process.stdout.readline().decode('utf-8')
poll = process.poll()
if output == '' and poll is not None:
break
if process.poll() != 0:
print("{}ERROR: failed to restore backup{}".format(TextColors.RED, TextColors.RESET))
print("Please try manually")
print("Exiting")
sys.exit(0)
print("Successfully reverted to the old install")
print("Before reverting another failed install you should remove the folder {}FAIL.bak".format(PATH_TO_VENV))