-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncWorld.py
executable file
·88 lines (71 loc) · 2.21 KB
/
SyncWorld.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
83
84
85
86
87
88
from requests import get
from git import Repo
import os
import socket
import subprocess
import time
#git variables
PATH_OF_GIT_REPO = os.getcwd()
COMMIT_MESSAGE = 'updated state'
repo = Repo(PATH_OF_GIT_REPO)
#Just starts a command and returns the output
def run_command(command):
p = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True)
return iter(p.stdout.readline, b'')
#Pulls from git repo because it won't push later
repo.remotes.origin.pull()
#Opens mcServerState.txt and gets the value
f = open("mcServerState.txt", "r")
state = f.readline()
print(state)
try:
#If the value is True it shuts down the script
if state == "True":
print("Server is working")
exit()
f.close()
#Updates the value
f = open("mcServerState.txt", "w")
f.write("True")
f.close()
#Uploads the txt to Git
repo.git.add("mcServerState.txt")
repo.index.commit(COMMIT_MESSAGE)
origin = repo.remote(name='origin')
origin.push()
print("No server is running starting yours")
#Changes the current working directory so the server unpacks in the proper folder
os.chdir("McServer")
print(os.getcwd())
#Here is where the server starts
THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
my_file = os.path.join(THIS_FOLDER, 'server.jar')
for output_line in run_command('java -Xmx1024M -Xms1024M -jar '+ my_file + " nogui"):
print(output_line)
#Changes the current directory to parrent, so to be able to upload to git
os.chdir("../")
#If an error occurs the state will not be stuck on true
except Exception as err:
os.chdir("../")
f = open("mcServerState.txt", "w")
f.write("False")
f.close()
repo.git.add("mcServerState.txt")
repo.index.commit("Server Failed Exception")
origin = repo.remote(name='origin')
origin.push()
print("There was an error")
print(err)
exit()
#Saves the world data and changes the state, also pushes to github
f = open("mcServerState.txt", "w")
f.write("False")
f.close()
repo.git.add(".")
repo.index.commit("World Save")
origin = repo.remote(name='origin')
origin.push()
print("done")