Skip to content

Commit

Permalink
cleanup: Replace makedirs_exist_ok with built-in os.makedirs
Browse files Browse the repository at this point in the history
Python 2.7 compatibility no longer needed.
  • Loading branch information
charmander committed Nov 25, 2024
1 parent c8dabb2 commit b18e0df
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 63 deletions.
25 changes: 0 additions & 25 deletions libweasyl/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,13 @@
File manipulation and detection.
"""

import errno
import os

from sanpera.exception import SanperaError

from libweasyl.constants import Category
from libweasyl.exceptions import InvalidFileFormat, UnknownFileFormat
from libweasyl import images


def makedirs_exist_ok(path):
"""
Ensure a directory and all of its parent directories exist.
This is different from :py:func:`os.makedirs` in that it will not raise an
exception if the directory already exists. Any other exceptions (e.g.
permissions failure; filesystem out of inodes) will still propagate.
This is functionally equivalent to specifying ``os.makedirs(path,
exist_ok=True)`` in python 3.2+, but exists in libweasyl for compatibility
with python 2.7.
Parameters:
path: The directory whose existence must be ensured.
"""
try:
os.makedirs(path)
except OSError as e:
if e.errno != errno.EEXIST:
raise


def fanout(name, fanout):
"""
Generate fanout for a particular name.
Expand Down
4 changes: 2 additions & 2 deletions libweasyl/models/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy.orm import relationship, foreign, remote, joinedload, lazyload, load_only
from sqlalchemy.sql.expression import any_

from libweasyl.files import fanout, makedirs_exist_ok
from libweasyl.files import fanout
from libweasyl.models.meta import Base
from libweasyl.models.users import Profile
from libweasyl.models import tables
Expand Down Expand Up @@ -35,7 +35,7 @@ def fetch_or_create(cls, data, file_type=None, im=None, attributes=()):

# Write our file to disk
real_path = obj.full_file_path
makedirs_exist_ok(os.path.dirname(real_path))
os.makedirs(os.path.dirname(real_path), exist_ok=True)
with open(real_path, 'wb') as outfile:
outfile.write(data)

Expand Down
33 changes: 0 additions & 33 deletions libweasyl/test/test_files.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,10 @@
import errno

import pytest

from libweasyl.constants import Category
from libweasyl.test.common import datadir
from libweasyl import exceptions, files


def test_makedirs_default_behavior(tmpdir):
"""
``makedirs_exist_ok`` creates multiple levels of directories.
"""
d = tmpdir.join('a', 'b', 'c')
assert not d.exists()
files.makedirs_exist_ok(d.strpath)
assert d.exists()


def test_makedirs_with_extant_directories(tmpdir):
"""
``makedirs_exist_ok`` doesn't care if the directories already exist.
"""
d = tmpdir.join('a', 'b', 'c')
d.ensure(dir=True)
files.makedirs_exist_ok(d.strpath)
assert d.exists()


def test_makedirs_reraises_other_errors(tmpdir):
"""
``makedirs_exist_ok`` reraises errors it can't handle, such as EACCES.
"""
tmpdir.chmod(0)
d = tmpdir.join('a', 'b', 'c')
with pytest.raises(OSError) as e:
files.makedirs_exist_ok(d.strpath)
assert e.value.errno == errno.EACCES


def test_file_type_for_category_invalid_category():
"""
``file_type_for_category`` raises an exception on unknown categories.
Expand Down
6 changes: 3 additions & 3 deletions weasyl/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from libweasyl.constants import Category
from libweasyl.exceptions import InvalidFileFormat, UnknownFileFormat
from libweasyl.files import file_type_for_category, makedirs_exist_ok
from libweasyl.files import file_type_for_category
from libweasyl import security
from weasyl.error import WeasylError
import weasyl.define as d
Expand All @@ -20,7 +20,7 @@ def read(filename):

def ensure_file_directory(filename):
dirname = os.path.dirname(filename)
makedirs_exist_ok(dirname)
os.makedirs(dirname, exist_ok=True)


def write(filename, content):
Expand Down Expand Up @@ -75,7 +75,7 @@ def clear_temporary(userid):

def make_character_directory(target):
path = d.get_character_directory(target)
makedirs_exist_ok(path)
os.makedirs(path, exist_ok=True)


def make_resource(userid, target, feature, extension=None):
Expand Down

0 comments on commit b18e0df

Please sign in to comment.