-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.py
184 lines (161 loc) · 7.07 KB
/
setup.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import shutil, os, sys, platform
from os import path, listdir
from pathlib import Path
from subprocess import Popen
from getpass import getuser
import argparse
def GetOptions():
parser = argparse.ArgumentParser()
parser.add_argument(
"--try_copy_data",
action="store_true",
default=False,
help="Attempt to access \\rds01.umn.edu\ to download entire data directory. If this is official umn lab production, this defaults to True.",
)
parser.add_argument(
"--dbv_db", help="Full path to database that DBViewer will use."
)
return parser.parse_args()
# root directory full path, save it in a file
def SetRootDir():
root_dir = Path(__file__).parent
root_dir = root_dir.resolve()
root_dir_file = root_dir / "resources" / "rootDirectory.txt"
with open(root_dir_file, "w") as f:
f.write(str(root_dir))
return root_dir
def Main():
print("===============================================")
print("== Initial PANGUI official production setup. ==")
print("== This script only needs to be run once. ==")
print("===============================================")
options = GetOptions()
# ===============================================================================
# 1. Determine key paths
# ===============================================================================
username = getuser()
# root dir of this working area
root_dir = SetRootDir()
# umn network data area = authoritative area into which all local data merges
network_top_dir = Path("\\\\rds01.storage.umn.edu\\cse_spa_mu2e")
network_data_dir = network_top_dir / "Data"
network_db = network_data_dir / "database.db"
# local copy of data
local_data_dir = root_dir / "data"
local_db = local_data_dir / "database.db"
# umn lab computers shalt install here
official_lab_production_root = Path(
"C:\\Users\\{0}\\Desktop\\Production".format(username)
)
# merge local data into network vs into dummy database
is_official_lab_production = str(official_lab_production_root) in str(root_dir)
if not is_official_lab_production:
print("... Software development mode detected.")
print(" Will not automerge with the official network database.")
# network database location -- accessed by pangui -- stored in this txt file
merge_destination_db_path_file = root_dir / "resources" / "networkDatabasePath.txt"
# database -- accessed by database viewer -- stored in this txt file
dbv_db_path_file = root_dir / "resources" / "dbvDatabasePath.txt"
# ===============================================================================
# 2. Make local copy of network data
# TODO add exception(s), e.g. not connected to the internet
# ===============================================================================
if is_official_lab_production or options.try_copy_data:
print("... Copying the Data/ dir from the network.")
print(" This can take several minutes so grab a cup of coffee.")
print(" Beginning copy of Data dir...")
try:
shutil.copytree(network_data_dir, local_data_dir)
print("... Done copying Data dir.")
except FileExistsError as e:
print("... Data dir already exists here!")
print(
" If things aren't working, you might need to refresh this directory."
)
else:
print("... Local environment detected.")
print("... Checking if Data directory exists")
if os.path.isdir("data"):
print(" Data directory was found.")
else:
print(" Data directory was not found.")
print(
" If you intend to run pangui, please, download the data "
"directory and add it to this folder."
)
try:
os.mkdir("data")
except OSError as error:
pass
with open("data/__init__.py", "w") as file:
pass
# ============================================================================
# 3. Set locations of local and merge destination databases.
#
# Write them to txt files that databaseManager will read.
#
# For official lab panel production, the destination database IS the
# database.db on the network. For software development, the destination
# database is a dummy.db located in this directory.
# =============================================================================
# Merge destination depends on whether this is official lab panel production or
# software development.
# official --> merge with network
# software --> merge with local dummy
merge_destination_db = str(
network_db
if is_official_lab_production
else path.join(local_data_dir, "dummy.db")
)
with open(merge_destination_db_path_file, "w") as f:
f.write(merge_destination_db)
# location of database that database viewer loads
dbv_db = None
if is_official_lab_production:
dbv_db = str(network_db)
elif options.dbv_db:
dbv_db = options.dbv_db
else:
dbv_db = str(local_data_dir / "database.db")
with open(dbv_db_path_file, "w") as f:
f.write(dbv_db)
# ===============================================================================
# 4. Finally, if this is software development, we need to make the dummy.db
# and setup autoformatter.
# ===============================================================================
if not is_official_lab_production:
if not path.isfile(merge_destination_db):
print(
"... Copying the local database as dummy.db, which we'll set as the automerge destination."
)
print(" Again, this might take a few minutes.")
try:
shutil.copyfile(local_db, merge_destination_db)
except FileNotFoundError:
print("Local database not found. Not making a dummy merge target db.")
print("... Finally, setting up autoformatter.")
system = platform.system()
cmd = 'cd "{0}"; pre-commit install'.format(root_dir)
try:
if system == "Windows":
p = Popen(["powershell.exe", cmd], stdout=sys.stdout)
p.communicate()
elif system == "Darwin" or system == "Linux":
p = Popen(cmd, stdout=sys.stdout, shell=True)
p.communicate()
else:
print(" Unknown operating system.")
print(" Please, contact a GUI developer if you see this message.")
exit()
except:
print(" pre-commit linter hook not setup.")
# ===============================================================================
# 5. Create an empty folder to dump logfiles in.
# ===============================================================================
try:
os.mkdir("logfiles")
except OSError as error:
pass
print("Done!")
if __name__ == "__main__":
Main()