Permalink
Newer
Older
100644 215 lines (186 sloc) 7.61 KB
Sep 16, 2013 @bahniks some minor design changes
1 #! python3
Sep 2, 2013 @bahniks initial commit
2 """
3 Carousel Maze Manager - a program for analysis of data from behavioral neuroscience tasks
Feb 18, 2014 @bahniks updated graphs, fixed some bugs
4 Copyright 2013, 2014 Štěpán Bahník
Sep 2, 2013 @bahniks initial commit
5
6 This file is part of Carousel Maze Manager.
7
8 Carousel Maze Manager is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Carousel Maze Manager is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Carousel Maze Manager. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22 from tkinter import *
23 from tkinter import messagebox
24 from urllib.request import urlopen
25 import zipfile
26 import os
27 import shutil
28 import sys
29 import imp
30
31
32 def returnSiteContent(link):
33 "return text obtained from web site"
34 site = urlopen(link)
35 text = site.read()
36 site.close()
37 text = str(text)[2:-1]
38 return text
39
40
41 def checkNewVersion(version):
42 "checks whether there is a new version available"
43 newVersion = returnSiteContent("http://www.cmmanagerweb.appspot.com/version").split(".")
44 try:
45 from optionget import optionGet
46 from version import version as currentVersion
Aug 5, 2014 @bahniks corrected automatic updating
47 versionSeen = optionGet("DontShowVersion", currentVersion(), "list", True)
Sep 2, 2013 @bahniks initial commit
48 except Exception:
49 versionSeen = version
50 for i in range(3):
51 if int(newVersion[i]) > int(versionSeen[i]):
52 code = returnSiteContent("http://www.cmmanagerweb.appspot.com/code")
53 curl = "https://raw.github.com/bahniks/CM_Manager_{}_{}_{}/master/Stuff/code.txt"
54 curl = urlopen(curl.format(*newVersion))
55 ghcode = curl.read().strip()
56 if int(code) != int(ghcode):
57 return
58 message = "New version of Carousel Maze Manager ({}.{}.{}) is available.\n".format(
59 *newVersion) + "Do you want to download and install the new version?"
60 root = makeRoot()
61 answ = messagebox.askyesnocancel(message = message, icon = "question",
62 title = "New version available", default = "yes",
63 detail = "Select 'No' to not be asked again.")
64 if answ is None:
65 pass
66 elif answ:
67 root.config(cursor = "wait")
68 try:
69 download(newVersion)
70 if "version" in sys.modules:
71 imp.reload(sys.modules["version"])
72 except Exception as e:
73 messagebox.showinfo(title = "Error", icon = "error", detail = e,
74 message = "Some problem with updating occured.")
75 finally:
76 root.config(cursor = "")
77 else:
78 try:
Aug 5, 2014 @bahniks corrected automatic updating
79 print(newVersion)
Sep 2, 2013 @bahniks initial commit
80 from optionwrite import optionWrite
Aug 5, 2014 @bahniks corrected automatic updating
81 optionWrite("DontShowVersion", newVersion, True)
Sep 2, 2013 @bahniks initial commit
82 except Exception:
83 pass
84 root.destroy()
85 break
Aug 5, 2014 @bahniks corrected automatic updating
86 elif int(newVersion[i]) < int(versionSeen[i]):
87 break
88
Sep 2, 2013 @bahniks initial commit
89
90 def download(version):
91 "downloads the version from github"
92 # initializing
93 stuffname = os.path.join(os.getcwd(), "Stuff")
94 if not os.path.exists(stuffname):
95 os.mkdir(stuffname)
96 url = "https://github.com/bahniks/CM_Manager_{}_{}_{}/archive/master.zip".format(*version)
97 filename = os.path.join(stuffname, "CMM.zip")
98
99 # downloading
100 with open(filename, mode = "wb") as outfile:
101 with urlopen(url) as infile:
102 for line in infile:
103 outfile.write(line)
104
105 # extracting
106 if zipfile.is_zipfile(filename):
107 extractedFile = zipfile.ZipFile(filename, mode = "r")
108 commonname = os.path.commonprefix(extractedFile.namelist())
109 for file in extractedFile.namelist():
110 if not os.path.basename(file).startswith("."):
111 extractedFile.extract(file, path = stuffname)
112 extractedFile.close()
113 os.remove(filename)
114 unzipped = os.path.join(stuffname, commonname)
115
116 # updating help files and modules
117 for directory in ["Help", "Modules"]:
118 dirname = os.path.join(stuffname, directory)
119 shutil.rmtree(dirname, ignore_errors = True)
120 shutil.move(os.path.join(unzipped, "Stuff", directory), stuffname)
121
122 # updating parameters
123 if not os.path.exists(os.path.join(stuffname, "Parameters")):
124 os.mkdir(os.path.join(stuffname, "Parameters"))
125 for file in os.listdir(os.path.join(unzipped, "Stuff", "Parameters")):
126 old = os.path.join(stuffname, "Parameters", file)
127 new = os.path.join(unzipped, "Stuff", "Parameters", file)
128 if not os.path.exists(old):
129 os.rename(new, old)
130 elif file == "template.py":
131 os.remove(old)
132 os.rename(new, old)
133
134 # updating data formats - implemented for the future, not yet used
135 if os.path.exists(os.path.join(unzipped, "Stuff", "Data formats")):
136 if not os.path.exists(os.path.join(stuffname, "Data formats")):
137 os.mkdir(os.path.join(stuffname, "Data formats"))
138 for file in os.listdir(os.path.join(unzipped, "Stuff", "Data formats")):
139 old = os.path.join(stuffname, "Data formats", file)
140 new = os.path.join(unzipped, "Stuff", "Data formats", file)
141 if not os.path.exists(old):
142 os.rename(new, old)
143 elif file == "template.py":
144 os.remove(old)
145 os.rename(new, old)
146
147 # updating files in Stuff directory
148 for file in os.listdir(os.path.join(unzipped, "Stuff")):
149 new = os.path.join(unzipped, "Stuff", file)
150 if os.path.isfile(new) and file != "code.txt":
151 old = os.path.join(stuffname, file)
152 if os.path.exists(old):
153 os.remove(old)
154 os.rename(new, old)
155
156 # removing the extracted file
157 shutil.rmtree(unzipped, ignore_errors = True)
158
159
160 def makeRoot():
161 "makes root window for messagebox"
162 root = Tk()
163 root.withdraw()
164 return root
165
166
167 def main():
168 "starts CMM"
169 modules = os.path.join(os.getcwd(), "Stuff", "Modules")
170
171 # developer?
172 try:
173 if os.path.exists(modules):
174 sys.path.append(modules)
175 from optionget import optionGet
Sep 4, 2013 @bahniks options, saving files, time
176 if optionGet("Developer", False, 'bool', True):
Sep 2, 2013 @bahniks initial commit
177 from starter import main as start
178 start()
179 return
180 except Exception:
181 pass
182
183 # new version?
184 try:
185 if os.path.exists(modules):
186 if modules not in sys.path:
187 sys.path.append(modules)
188 from version import version
189 version = version()
190 else:
191 version = [0, 3, 4]
192 except Exception:
193 version = [0, 3, 4]
194 try:
195 checkNewVersion(version)
196 except Exception:
197 pass
198
199 # starting
200 try:
201 if modules not in sys.path:
202 sys.path.append(modules)
203 from starter import main as start
204 except Exception as e:
205 root = makeRoot()
206 messagebox.showinfo(title = "Error", icon = "error", detail = e,
207 message = "Unable to start CMM! Try again.")
208 root.destroy()
209 else:
210 start()
211
212
213
214 if __name__ == "__main__": main()