|
23 | 23 | #
|
24 | 24 |
|
25 | 25 | import os
|
26 |
| -import tempfile |
27 | 26 | import unittest
|
28 | 27 |
|
29 | 28 | import opentimelineio as otio
|
30 | 29 |
|
31 | 30 |
|
| 31 | +# handle python2 vs python3 difference |
| 32 | +try: |
| 33 | + from tempfile import TemporaryDirectory # noqa: F401 |
| 34 | + import tempfile |
| 35 | +except ImportError: |
| 36 | + # XXX: python2.7 only |
| 37 | + from backports import tempfile |
| 38 | + |
32 | 39 | # Reference data
|
33 | 40 | SAMPLE_DATA_DIR = os.path.join(os.path.dirname(__file__), "sample_data")
|
34 | 41 | HLS_EXAMPLE_PATH = os.path.join(SAMPLE_DATA_DIR, "v1_prog_index.m3u8")
|
@@ -162,13 +169,15 @@ def test_media_pl_from_mem(self):
|
162 | 169 | track.append(segment1)
|
163 | 170 |
|
164 | 171 | # Write out and validate the playlist
|
165 |
| - media_pl_tmp_path = tempfile.mkstemp(suffix=".m3u8", text=True)[1] |
166 |
| - otio.adapters.write_to_file(t, media_pl_tmp_path) |
167 |
| - |
168 |
| - with open(media_pl_tmp_path) as f: |
169 |
| - pl_string = f.read() |
| 172 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 173 | + media_pl_tmp_path = os.path.join( |
| 174 | + temp_dir, |
| 175 | + "test_media_pl_from_mem.m3u8" |
| 176 | + ) |
| 177 | + otio.adapters.write_to_file(t, media_pl_tmp_path) |
170 | 178 |
|
171 |
| - os.remove(media_pl_tmp_path) |
| 179 | + with open(media_pl_tmp_path) as f: |
| 180 | + pl_string = f.read() |
172 | 181 |
|
173 | 182 | # Compare against the reference value
|
174 | 183 | self.assertEqual(pl_string, MEM_PLAYLIST_REF_VALUE)
|
@@ -243,21 +252,22 @@ def test_media_roundtrip(self):
|
243 | 252 | self._validate_sample_playlist(timeline)
|
244 | 253 |
|
245 | 254 | # Write out and validate both playlists have the same lines
|
246 |
| - media_pl_tmp_path = tempfile.mkstemp(suffix=".m3u8", text=True)[1] |
247 |
| - otio.adapters.write_to_file(timeline, media_pl_tmp_path) |
248 |
| - |
249 |
| - # Read in both playlists |
250 |
| - with open(hls_path) as f: |
251 |
| - reference_lines = f.readlines() |
| 255 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 256 | + media_pl_tmp_path = os.path.join( |
| 257 | + temp_dir, |
| 258 | + "test_media_roundtrip.m3u8" |
| 259 | + ) |
| 260 | + otio.adapters.write_to_file(timeline, media_pl_tmp_path) |
252 | 261 |
|
253 |
| - with open(media_pl_tmp_path) as f: |
254 |
| - adapter_out_lines = f.readlines() |
| 262 | + # Read in both playlists |
| 263 | + with open(hls_path) as f: |
| 264 | + reference_lines = f.readlines() |
255 | 265 |
|
256 |
| - # Using otio as well |
257 |
| - in_timeline = otio.adapters.read_from_file(media_pl_tmp_path) |
| 266 | + with open(media_pl_tmp_path) as f: |
| 267 | + adapter_out_lines = f.readlines() |
258 | 268 |
|
259 |
| - # Remove the temp out file |
260 |
| - os.remove(media_pl_tmp_path) |
| 269 | + # Using otio as well |
| 270 | + in_timeline = otio.adapters.read_from_file(media_pl_tmp_path) |
261 | 271 |
|
262 | 272 | # Strip newline chars
|
263 | 273 | reference_lines = [line.strip('\n') for line in reference_lines]
|
@@ -287,12 +297,15 @@ def test_media_segment_size(self):
|
287 | 297 | timeline_streaming_md['max_segment_duration'] = seg_max_duration
|
288 | 298 |
|
289 | 299 | # Write out the playlist
|
290 |
| - media_pl_tmp_path = tempfile.mkstemp(suffix=".m3u8", text=True)[1] |
291 |
| - otio.adapters.write_to_file(timeline, media_pl_tmp_path) |
| 300 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 301 | + media_pl_tmp_path = os.path.join( |
| 302 | + temp_dir, |
| 303 | + "test_media_segment_size.m3u8" |
| 304 | + ) |
| 305 | + otio.adapters.write_to_file(timeline, media_pl_tmp_path) |
292 | 306 |
|
293 |
| - # Read in the playlist |
294 |
| - in_timeline = otio.adapters.read_from_file(media_pl_tmp_path) |
295 |
| - os.remove(media_pl_tmp_path) |
| 307 | + # Read in the playlist |
| 308 | + in_timeline = otio.adapters.read_from_file(media_pl_tmp_path) |
296 | 309 |
|
297 | 310 | # Pick a duration that segments won't exceed but is less than max
|
298 | 311 | seg_upper_duration = otio.opentime.RationalTime(7, 1)
|
@@ -339,15 +352,18 @@ def test_iframe_segment_size(self):
|
339 | 352 | track_hls_metadata['EXT-X-I-FRAMES-ONLY'] = None
|
340 | 353 |
|
341 | 354 | # Write out the playlist
|
342 |
| - media_pl_tmp_path = tempfile.mkstemp(suffix=".m3u8", text=True)[1] |
343 |
| - otio.adapters.write_to_file(timeline, media_pl_tmp_path) |
| 355 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 356 | + media_pl_tmp_path = os.path.join( |
| 357 | + temp_dir, |
| 358 | + "test_iframe_segment_size.m3u8" |
| 359 | + ) |
| 360 | + otio.adapters.write_to_file(timeline, media_pl_tmp_path) |
344 | 361 |
|
345 |
| - # Read in the playlist |
346 |
| - in_timeline = otio.adapters.read_from_file(media_pl_tmp_path) |
347 |
| - with open(media_pl_tmp_path) as f: |
348 |
| - pl_lines = f.readlines() |
349 |
| - pl_lines = [line.strip('\n') for line in pl_lines] |
350 |
| - os.remove(media_pl_tmp_path) |
| 362 | + # Read in the playlist |
| 363 | + in_timeline = otio.adapters.read_from_file(media_pl_tmp_path) |
| 364 | + with open(media_pl_tmp_path) as f: |
| 365 | + pl_lines = f.readlines() |
| 366 | + pl_lines = [line.strip('\n') for line in pl_lines] |
351 | 367 |
|
352 | 368 | # validate the TARGETDURATION value is correct
|
353 | 369 | self.assertTrue('#EXT-X-TARGETDURATION:6' in pl_lines)
|
@@ -420,16 +436,15 @@ def test_simple_master_pl_from_mem(self):
|
420 | 436 | t.tracks.append(atrack)
|
421 | 437 |
|
422 | 438 | # Write out and validate the playlist
|
423 |
| - media_pl_tmp_path = tempfile.mkstemp( |
424 |
| - suffix="master.m3u8", |
425 |
| - text=True |
426 |
| - )[1] |
427 |
| - otio.adapters.write_to_file(t, media_pl_tmp_path) |
428 |
| - |
429 |
| - with open(media_pl_tmp_path) as f: |
430 |
| - pl_string = f.read() |
| 439 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 440 | + media_pl_tmp_path = os.path.join( |
| 441 | + temp_dir, |
| 442 | + "master.m3u8" |
| 443 | + ) |
| 444 | + otio.adapters.write_to_file(t, media_pl_tmp_path) |
431 | 445 |
|
432 |
| - os.remove(media_pl_tmp_path) |
| 446 | + with open(media_pl_tmp_path) as f: |
| 447 | + pl_string = f.read() |
433 | 448 |
|
434 | 449 | # Drop blank lines before comparing
|
435 | 450 | pl_string = '\n'.join((line for line in pl_string.split('\n') if line))
|
@@ -483,16 +498,15 @@ def test_master_pl_with_iframe_pl_from_mem(self):
|
483 | 498 | t.tracks.append(atrack)
|
484 | 499 |
|
485 | 500 | # Write out and validate the playlist
|
486 |
| - media_pl_tmp_path = tempfile.mkstemp( |
487 |
| - suffix="master.m3u8", |
488 |
| - text=True |
489 |
| - )[1] |
490 |
| - otio.adapters.write_to_file(t, media_pl_tmp_path) |
491 |
| - |
492 |
| - with open(media_pl_tmp_path) as f: |
493 |
| - pl_string = f.read() |
| 501 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 502 | + media_pl_tmp_path = os.path.join( |
| 503 | + temp_dir, |
| 504 | + "master.m3u8" |
| 505 | + ) |
| 506 | + otio.adapters.write_to_file(t, media_pl_tmp_path) |
494 | 507 |
|
495 |
| - os.remove(media_pl_tmp_path) |
| 508 | + with open(media_pl_tmp_path) as f: |
| 509 | + pl_string = f.read() |
496 | 510 |
|
497 | 511 | # Drop blank lines before comparing
|
498 | 512 | pl_string = '\n'.join(line for line in pl_string.split('\n') if line)
|
@@ -568,16 +582,15 @@ def test_master_pl_complex_from_mem(self):
|
568 | 582 | t.tracks.append(atrack)
|
569 | 583 |
|
570 | 584 | # Write out and validate the playlist
|
571 |
| - media_pl_tmp_path = tempfile.mkstemp( |
572 |
| - suffix="master.m3u8", |
573 |
| - text=True |
574 |
| - )[1] |
575 |
| - otio.adapters.write_to_file(t, media_pl_tmp_path) |
576 |
| - |
577 |
| - with open(media_pl_tmp_path) as f: |
578 |
| - pl_string = f.read() |
| 585 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 586 | + media_pl_tmp_path = os.path.join( |
| 587 | + temp_dir, |
| 588 | + "master.m3u8" |
| 589 | + ) |
| 590 | + otio.adapters.write_to_file(t, media_pl_tmp_path) |
579 | 591 |
|
580 |
| - os.remove(media_pl_tmp_path) |
| 592 | + with open(media_pl_tmp_path) as f: |
| 593 | + pl_string = f.read() |
581 | 594 |
|
582 | 595 | # Drop blank lines before comparing
|
583 | 596 | pl_string = '\n'.join(line for line in pl_string.split('\n') if line)
|
@@ -610,16 +623,15 @@ def test_master_playlist_hint_metadata(self):
|
610 | 623 | )
|
611 | 624 |
|
612 | 625 | # Write out and validate the playlist
|
613 |
| - media_pl_tmp_path = tempfile.mkstemp( |
614 |
| - suffix="master.m3u8", |
615 |
| - text=True |
616 |
| - )[1] |
617 |
| - otio.adapters.write_to_file(timeline, media_pl_tmp_path) |
618 |
| - |
619 |
| - with open(media_pl_tmp_path) as f: |
620 |
| - pl_string = f.read() |
| 626 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 627 | + media_pl_tmp_path = os.path.join( |
| 628 | + temp_dir, |
| 629 | + "test_media_pl_from_mem.m3u8" |
| 630 | + ) |
| 631 | + otio.adapters.write_to_file(timeline, media_pl_tmp_path) |
621 | 632 |
|
622 |
| - os.remove(media_pl_tmp_path) |
| 633 | + with open(media_pl_tmp_path) as f: |
| 634 | + pl_string = f.read() |
623 | 635 |
|
624 | 636 | # ensure metadata that wasn't supposed to didn't leak out
|
625 | 637 | for line in pl_string.split('\n'):
|
@@ -696,16 +708,15 @@ def test_explicit_master_pl_from_mem(self):
|
696 | 708 | track.append(segment1)
|
697 | 709 |
|
698 | 710 | # Write out and validate the playlist
|
699 |
| - master_pl_tmp_path = tempfile.mkstemp( |
700 |
| - suffix='master.m3u8', |
701 |
| - text=True |
702 |
| - )[1] |
703 |
| - otio.adapters.write_to_file(t, master_pl_tmp_path) |
704 |
| - |
705 |
| - with open(master_pl_tmp_path) as f: |
706 |
| - pl_string = f.read() |
| 711 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 712 | + master_pl_tmp_path = os.path.join( |
| 713 | + temp_dir, |
| 714 | + "master.m3u8" |
| 715 | + ) |
| 716 | + otio.adapters.write_to_file(t, master_pl_tmp_path) |
707 | 717 |
|
708 |
| - os.remove(master_pl_tmp_path) |
| 718 | + with open(master_pl_tmp_path) as f: |
| 719 | + pl_string = f.read() |
709 | 720 |
|
710 | 721 | # Drop blank lines before comparing
|
711 | 722 | pl_string = '\n'.join((line for line in pl_string.split('\n') if line))
|
|
0 commit comments