Skip to content

Commit

Permalink
Fix issue 291: os.makedirs
Browse files Browse the repository at this point in the history
  • Loading branch information
bbayles authored and mauricioabreu committed Aug 11, 2022
1 parent 50963a8 commit a189d8b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
8 changes: 2 additions & 6 deletions m3u8/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,8 @@ def dump(self, filename):

def _create_sub_directories(self, filename):
basename = os.path.dirname(filename)
try:
if basename:
os.makedirs(basename)
except OSError as error:
if error.errno != errno.EEXIST:
raise
if basename:
os.makedirs(basename, exist_ok=True)


class Segment(BasePathMixin):
Expand Down
27 changes: 12 additions & 15 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,21 +612,18 @@ def test_dump_should_create_sub_directories(tmpdir):

assert_file_content(filename, expected)

def test_dump_should_raise_if_create_sub_directories_fails(tmpdir, monkeypatch):
def raiseOSError(*args):
raise OSError

monkeypatch.setattr(os, "makedirs", raiseOSError)

raised = False
try:
obj = m3u8.M3U8(playlists.SIMPLE_PLAYLIST)
obj.dump(str(tmpdir.join('subdir1', 'playlist.m3u8')))
except OSError as e:
raised = True
finally:
assert raised

def test_dump_should_raise_if_create_sub_directories_fails(tmpdir):
# The first subdirectory is read-only
subdir_1 = os.path.join(tmpdir, 'subdir1')
os.mkdir(subdir_1, mode=0o400)

# The file is to be stored in a second subdirectory that's underneath the first
subdir_2 = os.path.join(subdir_1, 'subdir2')
file_name = os.path.join(subdir_2, 'playlist.m3u8')

# When we try to write it, we'll be prevented from creating the second subdirectory
with pytest.raises(OSError):
m3u8.M3U8(playlists.SIMPLE_PLAYLIST).dump(file_name)

def test_dump_should_work_for_variant_streams():
obj = m3u8.M3U8(playlists.VARIANT_PLAYLIST)
Expand Down

0 comments on commit a189d8b

Please sign in to comment.