def test_make_dirs_for_file_with_existing_file(self): path = self.temp_path("a/b/c.txt") fsutil.create_file(path) fsutil.make_dirs_for_file(path) self.assertTrue(fsutil.is_dir(self.temp_path("a/b/"))) self.assertFalse(fsutil.is_dir(path)) self.assertTrue(fsutil.is_file(path))
def test_is_dir(self): path = self.temp_path("a/b/") self.assertFalse(fsutil.is_dir(path)) fsutil.create_dir(path) self.assertTrue(fsutil.is_dir(path)) path = self.temp_path("a/b/c.txt") self.assertFalse(fsutil.is_dir(path)) fsutil.create_file(path) self.assertFalse(fsutil.is_dir(path))
def test_make_dirs_race_condition(self): path = self.temp_path("a/b/c/") for i in range(0, 20): t = threading.Thread(target=fsutil.make_dirs, args=[path], kwargs={}) t.start() t.join() self.assertTrue(fsutil.is_dir(path))
def test_extract_zip_file(self): zip_path = self.temp_path("archive.zip") unzip_path = self.temp_path("unarchive/") f1_path = self.temp_path("a/b/f1.txt") f2_path = self.temp_path("a/b/f2.txt") f3_path = self.temp_path("j/k/f3.txt") f4_path = self.temp_path("j/k/f4.txt") f5_path = self.temp_path("x/y/z/f5.txt") f6_path = self.temp_path("x/y/z/f6.txt") f5_f6_dir = self.temp_path("x") fsutil.create_file(f1_path, content="hello world 1") fsutil.create_file(f2_path, content="hello world 2") fsutil.create_file(f3_path, content="hello world 3") fsutil.create_file(f4_path, content="hello world 4") fsutil.create_file(f5_path, content="hello world 5") fsutil.create_file(f6_path, content="hello world 6") fsutil.create_zip_file( zip_path, [f1_path, f2_path, f3_path, f4_path, f5_f6_dir] ) fsutil.extract_zip_file(zip_path, unzip_path) self.assertTrue(fsutil.is_dir(unzip_path)) self.assertTrue(fsutil.is_file(self.temp_path("unarchive/f1.txt"))) self.assertTrue(fsutil.is_file(self.temp_path("unarchive/f2.txt"))) self.assertTrue(fsutil.is_file(self.temp_path("unarchive/f3.txt"))) self.assertTrue(fsutil.is_file(self.temp_path("unarchive/f4.txt"))) self.assertTrue(fsutil.is_file(self.temp_path("unarchive/y/z/f5.txt"))) self.assertTrue(fsutil.is_file(self.temp_path("unarchive/y/z/f6.txt"))) self.assertTrue(fsutil.is_file(zip_path))
def test_extract_zip_file(self): zip_path = self.temp_path('archive.zip') unzip_path = self.temp_path('unarchive/') f1_path = self.temp_path('a/b/f1.txt') f2_path = self.temp_path('a/b/f2.txt') f3_path = self.temp_path('j/k/f3.txt') f4_path = self.temp_path('j/k/f4.txt') f5_path = self.temp_path('x/y/z/f5.txt') f6_path = self.temp_path('x/y/z/f6.txt') f5_f6_dir = self.temp_path('x') fsutil.create_file(f1_path, content='hello world 1') fsutil.create_file(f2_path, content='hello world 2') fsutil.create_file(f3_path, content='hello world 3') fsutil.create_file(f4_path, content='hello world 4') fsutil.create_file(f5_path, content='hello world 5') fsutil.create_file(f6_path, content='hello world 6') fsutil.create_zip_file(zip_path, [f1_path, f2_path, f3_path, f4_path, f5_f6_dir]) fsutil.extract_zip_file(zip_path, unzip_path) self.assertTrue(fsutil.is_dir(unzip_path)) self.assertTrue(fsutil.is_file(self.temp_path('unarchive/f1.txt'))) self.assertTrue(fsutil.is_file(self.temp_path('unarchive/f2.txt'))) self.assertTrue(fsutil.is_file(self.temp_path('unarchive/f3.txt'))) self.assertTrue(fsutil.is_file(self.temp_path('unarchive/f4.txt'))) self.assertTrue(fsutil.is_file(self.temp_path('unarchive/y/z/f5.txt'))) self.assertTrue(fsutil.is_file(self.temp_path('unarchive/y/z/f6.txt'))) self.assertTrue(fsutil.is_file(zip_path))
def __init__(self, relative, absolute, url): self.url = url self.absolute = absolute self.relative = relative self.name = fsutil.get_filename(absolute) self.is_exist = fsutil.exists(absolute) self.is_file = fsutil.is_file(absolute) self.is_dir = fsutil.is_dir(absolute) self.__content = None
def test_extract_zip_file_with_autodelete(self): zip_path = self.temp_path("archive.zip") unzip_path = self.temp_path("unarchive/") path = self.temp_path("f1.txt") fsutil.create_file(path, content="hello world 1") fsutil.create_zip_file(zip_path, [path]) fsutil.extract_zip_file(zip_path, unzip_path, autodelete=True) self.assertTrue(fsutil.is_dir(unzip_path)) self.assertTrue(fsutil.is_file(self.temp_path("unarchive/f1.txt"))) self.assertFalse(fsutil.is_file(zip_path))
def test_extract_zip_file_with_autodelete(self): zip_path = self.temp_path('archive.zip') unzip_path = self.temp_path('unarchive/') path = self.temp_path('f1.txt') fsutil.create_file(path, content='hello world 1') fsutil.create_zip_file(zip_path, [path]) fsutil.extract_zip_file(zip_path, unzip_path, autodelete=True) self.assertTrue(fsutil.is_dir(unzip_path)) self.assertTrue(fsutil.is_file(self.temp_path('unarchive/f1.txt'))) self.assertFalse(fsutil.is_file(zip_path))
from rest_framework.views import APIView from django.shortcuts import render from django.http import JsonResponse, Http404, HttpResponseBadRequest import fsutil import re from django.templatetags.static import static from django.urls import reverse as original_reverse from django.utils.http import urlencode from django.views.decorators.csrf import ensure_csrf_cookie from django_bloom.settings import BASE_DIR, STATIC_URL, STATICFILES_DIRS import base64 import time static_dir = fsutil.join_path(str(BASE_DIR), STATICFILES_DIRS[0]) base_files_dir = fsutil.join_path(static_dir, 'files') if not fsutil.is_dir(base_files_dir): if fsutil.is_file(base_files_dir): fsutil.delete_file(base_files_dir) fsutil.create_dir(base_files_dir) class Node: def __init__(self, relative, absolute, url): self.url = url self.absolute = absolute self.relative = relative self.name = fsutil.get_filename(absolute) self.is_exist = fsutil.exists(absolute) self.is_file = fsutil.is_file(absolute) self.is_dir = fsutil.is_dir(absolute) self.__content = None
def test_make_dirs_with_existing_dir(self): path = self.temp_path("a/b/c/") fsutil.create_dir(path) fsutil.make_dirs(path) self.assertTrue(fsutil.is_dir(path))
def test_make_dirs(self): path = self.temp_path("a/b/c/") fsutil.make_dirs(path) self.assertTrue(fsutil.is_dir(path))
def test_make_dirs_for_file(self): path = self.temp_path('a/b/c.txt') fsutil.make_dirs_for_file(path) self.assertTrue(fsutil.is_dir(self.temp_path('a/b/'))) self.assertFalse(fsutil.is_dir(path)) self.assertFalse(fsutil.is_file(path))