| 1 |
""" |
| 2 |
Responsibilities |
| 3 |
|
| 4 |
Path building for entities into a session should be delegated to fs.PathServices |
| 5 |
Session's creation, activation and management at start of fs |
| 6 |
Versions capture are handled at start of module fs |
| 7 |
Utility functions to load - save at the end of fs |
| 8 |
|
| 9 |
See directory layout in manual. |
| 10 |
""" |
| 11 |
import sys |
| 12 |
import json |
| 13 |
import os |
| 14 |
import pickle |
| 15 |
import shutil |
| 16 |
import subprocess |
| 17 |
|
| 18 |
import mpexceptions |
| 19 |
|
| 20 |
def get_media_player_path(): |
| 21 |
here = os.path.basename(os.path.abspath(__file__)) |
| 22 |
path = os.path.join("..", "..", "examples") |
| 23 |
mp_path = os.path.abspath(path) |
| 24 |
return mp_path |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
def activation_set_to(session): |
| 30 |
""" |
| 31 |
exceptions: |
| 32 |
mpexceptions.ExceptionUndefinedSamplesDir |
| 33 |
mpexceptions.ExceptionNoSessionWithThatName |
| 34 |
mpexceptions.ExceptionSamplesDirDoesNotExist |
| 35 |
""" |
| 36 |
samples_dir = get_current_samples_dir() |
| 37 |
activation_file = get_activation_filename(samples_dir) |
| 38 |
if session is None: |
| 39 |
if os.path.isfile(activation_file): |
| 40 |
os.remove(activation_file) |
| 41 |
else: |
| 42 |
json_save(session, activation_file) |
| 43 |
|
| 44 |
|
| 45 |
def get_current_samples_dir(): |
| 46 |
if "pyglet_mp_samples_dir" not in os.environ: |
| 47 |
raise mpexceptions.ExceptionUndefinedSamplesDir() |
| 48 |
path = os.environ["pyglet_mp_samples_dir"] |
| 49 |
if not os.path.isdir(path): |
| 50 |
raise mpexceptions.ExceptionSamplesDirDoesNotExist(path) |
| 51 |
return path |
| 52 |
|
| 53 |
|
| 54 |
def get_path_info_for_active_session(): |
| 55 |
samples_dir = get_current_samples_dir() |
| 56 |
activation_file = get_activation_filename(samples_dir) |
| 57 |
try: |
| 58 |
session = json_load(activation_file) |
| 59 |
except FileNotFoundError: |
| 60 |
raise mpexceptions.ExceptionNoSessionIsActive() |
| 61 |
pathserv = PathServices(samples_dir, session) |
| 62 |
return pathserv |
| 63 |
|
| 64 |
|
| 65 |
def get_path_info_for_session(session): |
| 66 |
""" |
| 67 |
samples_dir comes from env var pyglet_mp_samples_dir |
| 68 |
""" |
| 69 |
samples_dir = get_current_samples_dir() |
| 70 |
pathserv = PathServices(samples_dir, session) |
| 71 |
conf_file = pathserv.configuration_filename() |
| 72 |
if os.path.isdir(pathserv.session_dir) and os.path.isfile(conf_file): |
| 73 |
return pathserv |
| 74 |
raise mpexceptions.ExceptionNoSessionWithThatName() |
| 75 |
|
| 76 |
|
| 77 |
def get_sessions(samples_dir): |
| 78 |
candidates = [s for s in os.listdir(samples_dir) |
| 79 |
if os.path.isdir(os.path.join(samples_dir, s))] |
| 80 |
sessions = [] |
| 81 |
for name in candidates: |
| 82 |
pathserv = PathServices(samples_dir, name) |
| 83 |
if os.path.isfile(pathserv.configuration_filename()): |
| 84 |
sessions.append(name) |
| 85 |
return sessions |
| 86 |
|
| 87 |
|
| 88 |
def get_activation_filename(samples_dir): |
| 89 |
return os.path.join(samples_dir, "activation.json") |
| 90 |
|
| 91 |
|
| 92 |
def new_session(session, playlist_file=None): |
| 93 |
"""creates a new session and sets as the active session""" |
| 94 |
samples_dir = get_current_samples_dir() |
| 95 |
new_session_for_samples_dir(session, samples_dir, playlist_file) |
| 96 |
activation_set_to(session) |
| 97 |
|
| 98 |
|
| 99 |
def new_session_for_samples_dir(session, samples_dir, playlist_file=None): |
| 100 |
"""creates a new session and returns it's pathserv, session is not activated |
| 101 |
|
| 102 |
Does not use env vars. |
| 103 |
""" |
| 104 |
if not os.path.isdir(samples_dir): |
| 105 |
raise mpexceptions.ExceptionSamplesDirDoesNotExist(samples_dir) |
| 106 |
pathserv = PathServices(samples_dir, session) |
| 107 |
if os.path.exists(pathserv.session_dir): |
| 108 |
raise mpexceptions.ExceptionSessionExistWithSameName() |
| 109 |
conf = default_initial_configuration() |
| 110 |
|
| 111 |
if playlist_file is not None: |
| 112 |
if not os.path.isfile(playlist_file): |
| 113 |
raise mpexceptions.ExceptionPlaylistFileDoNotExists() |
| 114 |
conf["has_playlist"] = True |
| 115 |
samples = sane_samples_from_playlist(pathserv, playlist_file) |
| 116 |
else: |
| 117 |
samples = [sample for sample, fname in pathserv.playlist_generator_all()] |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
for path in pathserv.dirs_to_create(): |
| 122 |
os.mkdir(path) |
| 123 |
|
| 124 |
dump_pyglet_info(pathserv) |
| 125 |
copy_samples_version_to_dbg(pathserv) |
| 126 |
save_session_playlist(pathserv, samples) |
| 127 |
json_save(conf, pathserv.configuration_filename()) |
| 128 |
dump_hg_changeset(pathserv) |
| 129 |
return pathserv |
| 130 |
|
| 131 |
|
| 132 |
def dump_pyglet_info(pathserv): |
| 133 |
import pyglet |
| 134 |
import pyglet.info |
| 135 |
filename = pathserv.special_raw_filename("pyglet_info") |
| 136 |
old = sys.stdout |
| 137 |
try: |
| 138 |
with open(filename, "w", encoding="utf-8") as f: |
| 139 |
sys.stdout = f |
| 140 |
pyglet.info.dump() |
| 141 |
except Exception as ex: |
| 142 |
import traceback |
| 143 |
traceback.print_exc() |
| 144 |
finally: |
| 145 |
sys.stdout = old |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
def dump_hg_changeset(pathserv): |
| 150 |
fname = pathserv.special_raw_filename("pyglet_hg_revision") |
| 151 |
|
| 152 |
shell = (sys.platform == "win32") |
| 153 |
with open(fname, "w", encoding="utf8") as outfile: |
| 154 |
subprocess.call(["hg", "parents", "--encoding", "utf8"], |
| 155 |
shell=shell, |
| 156 |
stdout=outfile, |
| 157 |
timeout=5.0) |
| 158 |
|
| 159 |
|
| 160 |
def copy_samples_version_to_dbg(pathserv): |
| 161 |
src = pathserv.samples_version_filename() |
| 162 |
dst = pathserv.special_raw_filename("samples_version") |
| 163 |
shutil.copyfile(src, dst) |
| 164 |
|
| 165 |
|
| 166 |
def sane_samples_from_playlist(pathserv, playlist_file): |
| 167 |
|
| 168 |
samples = [] |
| 169 |
rejected = [] |
| 170 |
for sample, filename in pathserv.playlist_generator_from_file(playlist_file): |
| 171 |
ext = os.path.splitext(sample)[1] |
| 172 |
if (ext in {".dbg", ".htm", ".html", ".json", |
| 173 |
".log", ".pkl", ".py", ".txt"} or |
| 174 |
not os.path.isfile(filename)): |
| 175 |
rejected.append(sample) |
| 176 |
else: |
| 177 |
samples.append(sample) |
| 178 |
if len(rejected) > 0: |
| 179 |
raise mpexceptions.ExceptionBadSampleInPlaylist(rejected) |
| 180 |
return samples |
| 181 |
|
| 182 |
|
| 183 |
def save_session_playlist(pathserv, samples): |
| 184 |
outfile = pathserv.special_raw_filename("session_playlist") |
| 185 |
text = "\n".join(sorted(samples)) |
| 186 |
txt_save(text, outfile) |
| 187 |
|
| 188 |
|
| 189 |
def load_session_playlist(pathserv): |
| 190 |
infile = pathserv.special_raw_filename("session_playlist") |
| 191 |
text = txt_load(infile) |
| 192 |
lines = text.split("\n") |
| 193 |
samples = [s.strip() for s in lines] |
| 194 |
return samples |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
def default_initial_configuration(): |
| 201 |
conf = { |
| 202 |
"config_vs": "1.0", |
| 203 |
"protect_raw_data": False, |
| 204 |
"protect_reports": False, |
| 205 |
"dev_debug": False, |
| 206 |
"has_playlist": False, |
| 207 |
} |
| 208 |
return conf |
| 209 |
|
| 210 |
|
| 211 |
def update_active_configuration(modify): |
| 212 |
"""updates the active session configuration file with the key: values in modify""" |
| 213 |
pathserv = get_path_info_for_active_session() |
| 214 |
update_configuration(pathserv, modify) |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
def update_configuration(pathserv, modify): |
| 220 |
"""The session associated with pathserv gets its configuration updated |
| 221 |
with the key: values in modify""" |
| 222 |
conf = json_load(pathserv.configuration_filename()) |
| 223 |
for k in modify: |
| 224 |
assert k in conf |
| 225 |
conf.update(modify) |
| 226 |
json_save(conf, pathserv.configuration_filename()) |
| 227 |
|
| 228 |
|
| 229 |
def get_session_configuration(pathserv): |
| 230 |
conf = json_load(pathserv.configuration_filename()) |
| 231 |
return conf |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
class PathServices: |
| 238 |
def __init__(self, samples_dir, session): |
| 239 |
self.samples_dir = samples_dir |
| 240 |
self.session = session |
| 241 |
self.session_dir = os.path.join(samples_dir, session) |
| 242 |
|
| 243 |
self.dbg_dir = os.path.join(self.session_dir, "dbg") |
| 244 |
self.rpt_dir = os.path.join(self.session_dir, "reports") |
| 245 |
|
| 246 |
def dirs_to_create(self): |
| 247 |
return [self.session_dir, self.dbg_dir, self.rpt_dir] |
| 248 |
|
| 249 |
def sample(self, filename): |
| 250 |
"""returns sample name from filename |
| 251 |
|
| 252 |
dev note: no checks performed, maybe it should check |
| 253 |
- filename point into the sample_dir directory |
| 254 |
- filename exists |
| 255 |
""" |
| 256 |
return os.path.basename(filename) |
| 257 |
|
| 258 |
def filename(self, sample): |
| 259 |
"""Returns the filename for the sample |
| 260 |
|
| 261 |
dev note: no checks performed, maybe it should check |
| 262 |
- filename exists |
| 263 |
""" |
| 264 |
return os.path.join(self.samples_dir, sample) |
| 265 |
|
| 266 |
def dbg_filename(self, sample): |
| 267 |
"""Returns filename to store media_player's internal state recording for sample""" |
| 268 |
return os.path.join(self.dbg_dir, sample + ".dbg") |
| 269 |
|
| 270 |
def sample_from_dbg_filename(self, filename): |
| 271 |
return os.path.splitext(filename)[0] |
| 272 |
|
| 273 |
def samples_version_filename(self): |
| 274 |
return os.path.join(self.samples_dir, "_version.txt") |
| 275 |
|
| 276 |
def report_filename(self, sample, report_name, txt=True): |
| 277 |
"""Returns filename to store a 'per sample' report for sample""" |
| 278 |
if txt: |
| 279 |
s = os.path.join(self.rpt_dir, "%s.%s.txt" % (sample, report_name)) |
| 280 |
else: |
| 281 |
s = os.path.join(self.rpt_dir, "%s.%s" % (sample, report_name)) |
| 282 |
return s |
| 283 |
|
| 284 |
def special_report_filename(self, report_name): |
| 285 |
"""Returns filename to store a report that don't depend on a single sample""" |
| 286 |
table = { |
| 287 |
|
| 288 |
"summary": "00_summary.txt", |
| 289 |
"pyglet_info": "00_pyglet_info.txt", |
| 290 |
"pyglet_hg_revision": "04_pyglet_hg_revision.txt" |
| 291 |
} |
| 292 |
return os.path.join(self.rpt_dir, table[report_name]) |
| 293 |
|
| 294 |
def special_raw_filename(self, shortname): |
| 295 |
table = { |
| 296 |
"session_playlist": "_session_playlist.txt", |
| 297 |
"crashes_light": "_crashes_light.pkl", |
| 298 |
"pyglet_info": "_pyglet_info.txt", |
| 299 |
"samples_version": "_samples_version.txt", |
| 300 |
"pyglet_hg_revision": "_pyglet_hg_revision.txt" |
| 301 |
} |
| 302 |
return os.path.join(self.dbg_dir, table[shortname]) |
| 303 |
|
| 304 |
def configuration_filename(self): |
| 305 |
return os.path.join(self.session_dir, "configuration.json") |
| 306 |
|
| 307 |
def playlist_generator(self, playlist_text): |
| 308 |
lines = playlist_text.split("\n") |
| 309 |
for line in lines: |
| 310 |
sample = line.strip() |
| 311 |
if sample == "" or sample.startswith("#"): |
| 312 |
continue |
| 313 |
yield (sample, self.filename(sample)) |
| 314 |
|
| 315 |
def playlist_generator_from_file(self, playlist_file): |
| 316 |
with open(playlist_file, "r", encoding="utf-8") as f: |
| 317 |
text = f.read() |
| 318 |
gen = self.playlist_generator(text) |
| 319 |
yield from gen |
| 320 |
|
| 321 |
def playlist_generator_all(self): |
| 322 |
for name in os.listdir(self.samples_dir): |
| 323 |
filename = os.path.join(self.samples_dir, name) |
| 324 |
ext = os.path.splitext(name)[1] |
| 325 |
if (ext in {".dbg", ".htm", ".html", ".json", |
| 326 |
".log", ".pkl", ".py", ".txt"} or |
| 327 |
os.path.isdir(filename)): |
| 328 |
continue |
| 329 |
yield (name, filename) |
| 330 |
|
| 331 |
def playlist_generator_from_samples_iterable(self, samples): |
| 332 |
for sample in samples: |
| 333 |
yield (sample, self.filename(sample)) |
| 334 |
|
| 335 |
def playlist_generator_from_fixed_text(self): |
| 336 |
text = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\nz" |
| 337 |
gen = self.playlist_generator(text) |
| 338 |
yield from gen |
| 339 |
|
| 340 |
def session_playlist_generator(self): |
| 341 |
playlist_file = self.special_raw_filename("session_playlist") |
| 342 |
gen = self.playlist_generator_from_file(playlist_file) |
| 343 |
yield from gen |
| 344 |
|
| 345 |
def session_exists(self): |
| 346 |
filename = self.configuration_filename() |
| 347 |
return os.path.isdir(self.session_dir) and os.path.isfile(filename) |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
def json_save(obj, filename): |
| 354 |
with open(filename, "w", encoding="utf-8") as f: |
| 355 |
json.dump(obj, f) |
| 356 |
|
| 357 |
|
| 358 |
def json_load(filename): |
| 359 |
with open(filename, "r", encoding="utf-8") as f: |
| 360 |
obj = json.load(f) |
| 361 |
return obj |
| 362 |
|
| 363 |
|
| 364 |
def pickle_save(obj, filename): |
| 365 |
with open(filename, "wb") as f: |
| 366 |
pickle.dump(obj, f) |
| 367 |
|
| 368 |
|
| 369 |
def pickle_load(filename): |
| 370 |
with open(filename, "rb") as f: |
| 371 |
obj = pickle.load(f) |
| 372 |
return obj |
| 373 |
|
| 374 |
|
| 375 |
def txt_load(filename): |
| 376 |
with open(filename, "r", encoding="utf-8") as f: |
| 377 |
text = f.read() |
| 378 |
return text |
| 379 |
|
| 380 |
|
| 381 |
def txt_save(text, filename): |
| 382 |
with open(filename, "w", encoding="utf-8") as f: |
| 383 |
f.write(text) |
| 384 |
|
| 385 |
|
| 386 |
def test_composition(): |
| 387 |
text = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\nz" |
| 388 |
|
| 389 |
args = "aaa", "bbb" |
| 390 |
a = PathServices(*args) |
| 391 |
for u, v in zip(a.playlist_generator(text), a.playlist_generator_from_fixed_text()): |
| 392 |
print(u, v) |
| 393 |
|
| 394 |
|