# Path: oslo_utils/fileutils.py
# _DEFAULT_MODE = stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO
# def ensure_tree(path, mode=_DEFAULT_MODE):
# def delete_if_exists(path, remove=os.unlink):
# def remove_path_on_error(path, remove=delete_if_exists):
# def write_to_tempfile(content, path=None, suffix='', prefix='tmp'):
# def compute_file_checksum(path, read_chunksize=65536, algorithm='sha256'):
# def last_bytes(path, num):
# def is_json(file_path):
# def is_yaml(file_path):


import errno
import hashlib
import json
import os
import shutil
import stat
import tempfile
import time
import uuid
import yaml
from unittest import mock
from oslotest import base as test_base
from oslo_utils import fileutils

class DeleteIfExists(test_base.BaseTestCase):
    def test_file_present(self):
        tmpfile = tempfile.mktemp()

        open(tmpfile, 'w')
        fileutils.delete_if_exists(tmpfile)
        self.assertFalse(os.path.exists(tmpfile))

    def test_file_absent(self):
        tmpfile = tempfile.mktemp()

        fileutils.delete_if_exists(tmpfile)
        self.assertFalse(os.path.exists(tmpfile))

    def test_dir_present(self):
        tmpdir = tempfile.mktemp()
        os.mkdir(tmpdir)

        fileutils.delete_if_exists(tmpdir, remove=os.rmdir)
        self.assertFalse(os.path.exists(tmpdir))

    def test_file_error(self):
        def errm(path):
            raise OSError(errno.EINVAL, '')

        tmpfile = tempfile.mktemp()

        open(tmpfile, 'w')
        self.assertRaises(OSError, fileutils.delete_if_exists, tmpfile, errm)

# Path: oslo_utils/fileutils.py
# _DEFAULT_MODE = stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO
# def ensure_tree(path, mode=_DEFAULT_MODE):
# def delete_if_exists(path, remove=os.unlink):
# def remove_path_on_error(path, remove=delete_if_exists):
# def write_to_tempfile(content, path=None, suffix='', prefix='tmp'):
# def compute_file_checksum(path, read_chunksize=65536, algorithm='sha256'):
# def last_bytes(path, num):
# def is_json(file_path):
# def is_yaml(file_path):


# Path: oslo_utils/tests/test_fileutils.py
import errno
import hashlib
import json
import os
import shutil
import stat
import tempfile
import time
import uuid
import yaml
from unittest import mock
from oslotest import base as test_base
from oslo_utils import fileutils


class DeleteIfExists(test_base.BaseTestCase):
    def test_file_present(self):
        tmpfile = tempfile.mktemp()

        open(tmpfile, 'w')
        fileutils.delete_if_exists(tmpfile)
        self.assertFalse(os.path.exists(tmpfile))

    def test_file_absent(self):
        tmpfile = tempfile.mktemp()

        fileutils.delete_if_exists(tmpfile)
        self.assertFalse(os.path.exists(tmpfile))

    def test_dir_present(self):
        tmpdir = tempfile.mktemp()
        os.mkdir(tmpdir)

        fileutils.delete_if_exists(tmpdir, remove=os.rmdir)
        self.assertFalse(os.path.exists(tmpdir))

    def test_file_error(self):
        def errm(path):
            raise OSError(errno.EINVAL, '')

        tmpfile = tempfile.mktemp()

        open(tmpfile, 'w')
        self.assertRaises(OSError, fileutils.delete_if_exists, tmpfile, errm)

        os.unlink(tmpfile)