| 1 | """ |
| 2 | Usage |
| 3 | |
| 4 | compare.py --reldir=relpath other_session |
| 5 | |
| 6 | Builds a reports comparing the active session with other_session. |
| 7 | |
| 8 | Outputs to samples_dir/relpath/comparison_<session>_<other_session>.txt |
| 9 | """ |
| 10 | import os |
| 11 | import sys |
| 12 | |
| 13 | import extractors |
| 14 | import fs |
| 15 | from pyglet.media import instrumentation as ins |
| 16 | import mpexceptions |
| 17 | import reports |
| 18 | |
| 19 | |
| 20 | def main(relative_out_dir, other_session): |
| 21 | try: |
| 22 | pathserv = fs.get_path_info_for_active_session() |
| 23 | except mpexceptions.ExceptionUndefinedSamplesDir: |
| 24 | print("The env var 'pyglet_mp_samples_dir' is not defined.") |
| 25 | return 1 |
| 26 | except mpexceptions.ExceptionNoSessionIsActive: |
| 27 | print("*** Error, no session active.") |
| 28 | return 1 |
| 29 | |
| 30 | try: |
| 31 | pathserv_other = fs.get_path_info_for_session(other_session) |
| 32 | except mpexceptions.ExceptionNoSessionWithThatName: |
| 33 | print("No session by that name") |
| 34 | return 1 |
| 35 | |
| 36 | outdir = os.path.join(fs.get_current_samples_dir(), relative_out_dir) |
| 37 | if not os.path.isdir(outdir): |
| 38 | os.mkdir(outdir) |
| 39 | |
| 40 | compare_sessions_to_file(pathserv, pathserv_other, outdir) |
| 41 | return 0 |
| 42 | |
| 43 | |
| 44 | def compare_sessions_to_file(pathserv, pathserv_other, outdir): |
| 45 | """compares two sessions, outputs text to file |
| 46 | outdir/comparison_<session>_<other_session>.txt |
| 47 | """ |
| 48 | outfile = os.path.join(outdir, "comparison_%s_%s.txt" % |
| 49 | (pathserv.session, pathserv_other.session)) |
| 50 | text = compare_sessions_to_text(pathserv, pathserv_other) |
| 51 | fs.txt_save(text, outfile) |
| 52 | |
| 53 | |
| 54 | def compare_sessions_to_text(pathserv, pathserv_other): |
| 55 | """compares two sessions, returns text""" |
| 56 | count_bads = ins.CountBads() |
| 57 | comparison = extractors.CollectSessionComparisonData(pathserv, |
| 58 | pathserv_other, |
| 59 | count_bads.count_bads) |
| 60 | text = reports.report_session_comparison(comparison) |
| 61 | return text |
| 62 | |
| 63 | |
| 64 | def sysargs_to_mainargs(): |
| 65 | """builds main args from sys.argv""" |
| 66 | relative_out_dir = None |
| 67 | if len(sys.argv) > 1 and sys.argv[1].startswith("--"): |
| 68 | a = sys.argv.pop(1) |
| 69 | if a.startswith("--help"): |
| 70 | print(__doc__) |
| 71 | sys.exit(1) |
| 72 | elif a.startswith("--reldir="): |
| 73 | relative_out_dir = a[len("--reldir="):] |
| 74 | else: |
| 75 | print("*** Error, Unknown option:", a) |
| 76 | print(__doc__) |
| 77 | sys.exit(1) |
| 78 | |
| 79 | other_session = sys.argv[1] |
| 80 | return relative_out_dir, other_session |
| 81 | |
| 82 | |
| 83 | if __name__ == "__main__": |
| 84 | relative_out_dir, other_session = sysargs_to_mainargs() |
| 85 | main(relative_out_dir, other_session) |