-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
152 lines (136 loc) · 4.79 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# -*- coding: utf-8 -*-
from __future__ import with_statement
from __future__ import absolute_import
import sys
import os
import argparse
from io import open
from typing import Tuple, Union
import beer_engine
from urllib.request import urlopen
def resource_path(relative_path):
"""Get absolute path to resource, works for dev and for PyInstaller"""
if __mode__ in ["pyinstaller", "local"]:
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
elif __mode__ == "deb":
if os.path.basename(relative_path) == "logo.png":
return "/usr/include/wheelers-wort-works/logo.png"
elif os.path.basename(relative_path) == "commit.txt":
return os.path.expanduser("~/.config/Wheelers-Wort-Works/commit.txt")
return os.path.join(
os.path.expanduser("/usr/include/wheelers-wort-works"), relative_path
)
def get_args():
parser = argparse.ArgumentParser(
description="Arguments", formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"-f",
"--file",
required=False,
action="store",
default=None,
help="The file to open `--file file_name.berf[x]`",
)
parser.add_argument(
"-u",
"--update",
required=False,
action="store_true",
help="Using the current `update.py`, download the latest GitHub files",
)
parser.add_argument(
"-U",
"--coreupdate",
required=False,
action="store_true",
help="Pull `update.py` from GitHub, then download the latest GitHub files",
)
parser.add_argument(
"-l", "--local", required=False, action="store_true", help="Use the local mode"
)
parser.add_argument(
"-d",
"--deb",
required=False,
action="store_true",
help="Use the debian mode (only use on a Debian/Ubuntu system)",
)
return parser.parse_args()
def check_for_update() -> Tuple[bool, bool, str]:
update_available = False
update = False
commit = ""
try:
with urlopen("https://github.com/jimbob88/wheelers-wort-works") as response:
text = response.read().decode("utf-8")
sec = "".join(
text[text.find('<a class="commit-tease-sha mr-1"'):].partition("</a>")[
:2
]
)
commit = sec.split('"')[3].split("/")[-1]
if os.path.isfile(resource_path("commit.txt")):
prev_commit = list(open(resource_path("commit.txt"), "r"))[0]
else:
prev_commit = 0
if prev_commit != commit:
update = True
update_available = True
else:
print("Already the Latest Edition")
except Exception:
update = True
return (update, update_available, commit)
__mode__ = "local"
BASE_URL = "https://raw.githubusercontent.com/jimbob88/wheelers-wort-works/master"
if __name__ == "__main__":
args = get_args()
if args.deb:
__mode__ = "deb"
if args.local:
__mode__ = "local"
update, update_available, commit = check_for_update()
if args.update or args.coreupdate:
commit = commit or list(open(resource_path("commit.txt"), "r"))[0]
if args.update and update:
with open(resource_path("update.py"), "r") as f:
exec(f.read())
update()
with open(resource_path("commit.txt"), "w") as f:
f.write(commit)
if args.coreupdate and update:
with urlopen(
f"{BASE_URL}/update.py"
) as response:
update_text = response.read().decode("utf-8")
exec(update_text)
update()
print(
"Updating {file} from {url}".format(
file="update.py",
url=f"{BASE_URL}/update.py",
)
)
with open(resource_path("update.py"), "w") as f:
f.write(update_text)
with open(resource_path("commit.txt"), "w") as f:
f.write(commit)
if __mode__ == "deb":
exit()
if args.file is None:
beer_engine.__mode__ = __mode__
beer_engine.main(update_available=update_available)
elif os.path.splitext(args.file)[1] in [".berf", ".berfx"]:
beer_engine.__mode__ = __mode__
file = (
os.path.expanduser(args.file)
if os.path.isfile(args.file)
else os.path.join(os.getcwd(), args.file)
)
beer_engine.main(file=file, update_available=update_available)