forked from Just-Some-Bots/MusicBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.py
65 lines (51 loc) · 2.3 KB
/
update.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
import os
import subprocess
import sys
def y_n(q):
while True:
ri = input('{} (y/n): '.format(q))
if ri.lower() in ['yes', 'y']: return True
elif ri.lower() in ['no', 'n']: return False
def main():
print('Starting...')
# Make sure that we're in a Git repository
if not os.path.isdir('.git'):
raise EnvironmentError("This isn't a Git repository.")
# Make sure that we can actually use Git on the command line
# because some people install Git Bash without allowing access to Windows CMD
try:
subprocess.check_call('git --version', shell=True, stdout=subprocess.DEVNULL)
except subprocess.CalledProcessError:
raise EnvironmentError("Couldn't use Git on the CLI. You will need to run 'git pull' yourself.")
print("Passed Git checks...")
# Check that the current working directory is clean
sp = subprocess.check_output('git status --porcelain', shell=True, universal_newlines=True)
if sp:
oshit = y_n('You have modified files that are tracked by Git (e.g the bot\'s source files).\n'
'We can try to reset your folder to a clean version for you. Continue?')
if oshit:
try:
subprocess.check_call('git reset --hard', shell=True)
except subprocess.CalledProcessError:
raise OSError("Could not reset the directory to a clean state.")
else:
print('Okay. Cancelling update process for now.')
return
print("Attempting to update the bot using Git...")
try:
subprocess.check_call('git pull', shell=True)
except subprocess.CalledProcessError:
raise OSError("Could not update the bot. You will need to run 'git pull' yourself.")
print("Attempting to update dependencies...")
try:
subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-U', '-r', 'requirements.txt'], shell=True)
except subprocess.CalledProcessError:
raise OSError("Could not update dependencies. You will need to run '{0} -m pip install -U -r requirements.txt' yourself.".format(sys.executable))
try:
from musicbot.constants import VERSION
print('MusicBot is at version {0}'.format(VERSION))
except Exception:
pass
print("Done!")
if __name__ == '__main__':
main()