def post(self, request, absolute_path=''): absolute_path = self.atob(absolute_path) node = Node.parse(relative=absolute_path) if node.is_exist: raise ParseError('Folder already existed.') fsutil.create_dir(node.absolute) return Response(data=True)
def test_is_empty(self): fsutil.create_file(self.temp_path('a/b/c.txt')) fsutil.create_file(self.temp_path('a/b/d.txt'), content='1') fsutil.create_dir(self.temp_path('a/b/e')) self.assertTrue(fsutil.is_empty(self.temp_path('a/b/c.txt'))) self.assertFalse(fsutil.is_empty(self.temp_path('a/b/d.txt'))) self.assertTrue(fsutil.is_empty(self.temp_path('a/b/e'))) self.assertFalse(fsutil.is_empty(self.temp_path('a/b')))
def test_is_empty(self): fsutil.create_file(self.temp_path("a/b/c.txt")) fsutil.create_file(self.temp_path("a/b/d.txt"), content="1") fsutil.create_dir(self.temp_path("a/b/e")) self.assertTrue(fsutil.is_empty(self.temp_path("a/b/c.txt"))) self.assertFalse(fsutil.is_empty(self.temp_path("a/b/d.txt"))) self.assertTrue(fsutil.is_empty(self.temp_path("a/b/e"))) self.assertFalse(fsutil.is_empty(self.temp_path("a/b")))
def test_is_empty_dir(self): path = self.temp_path("a/b/") fsutil.create_dir(path) self.assertTrue(fsutil.is_empty_dir(path)) filepath = self.temp_path("a/b/c.txt") fsutil.create_file(filepath) self.assertTrue(fsutil.is_file(filepath)) self.assertFalse(fsutil.is_empty_dir(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_exists(self): path = self.temp_path("a/b/") self.assertFalse(fsutil.exists(path)) fsutil.create_dir(path) self.assertTrue(fsutil.exists(path)) path = self.temp_path("a/b/c.txt") self.assertFalse(fsutil.exists(path)) fsutil.create_file(path) self.assertTrue(fsutil.exists(path))
def test_list_dirs(self): for i in range(0, 5): fsutil.create_dir(self.temp_path('a/b/c/d-{}'.format(i))) fsutil.create_file(self.temp_path('a/b/c/f-{}'.format(i)), content='{}'.format(i)) dirpaths = fsutil.list_dirs(self.temp_path('a/b/c')) dirnames = [fsutil.split_path(dirpath)[-1] for dirpath in dirpaths] self.assertEqual(len(dirpaths), 5) self.assertEqual(dirnames, ['d-0', 'd-1', 'd-2', 'd-3', 'd-4'])
def test_list_dirs(self): for i in range(0, 5): fsutil.create_dir(self.temp_path("a/b/c/d-{}".format(i))) fsutil.create_file( self.temp_path("a/b/c/f-{}".format(i)), content="{}".format(i) ) dirpaths = fsutil.list_dirs(self.temp_path("a/b/c")) dirnames = [fsutil.split_path(dirpath)[-1] for dirpath in dirpaths] self.assertEqual(len(dirpaths), 5) self.assertEqual(dirnames, ["d-0", "d-1", "d-2", "d-3", "d-4"])
def test_list_files(self): for i in range(0, 5): fsutil.create_dir(self.temp_path('a/b/c/d-{}'.format(i))) fsutil.create_file(self.temp_path('a/b/c/f-{}.txt'.format(i)), content='{}'.format(i)) filepaths = fsutil.list_files(self.temp_path('a/b/c')) filenames = [fsutil.get_filename(filepath) for filepath in filepaths] self.assertEqual(len(filepaths), 5) self.assertEqual( filenames, ['f-0.txt', 'f-1.txt', 'f-2.txt', 'f-3.txt', 'f-4.txt'])
def __write_folder(self, meta, dst): # create folder if it does not exist fsutil.create_dir(dst) # list all its children, try: children = self.__drive.children().list(folderId=meta.get('id')).execute() for child in children.get('items'): child_meta = self.__drive.files().get(fileId=child.get('id')).execute() child_dst = fsutil.join_paths(dst, child_meta.get('title')) # if child is folder, recurs if child_meta.get('mimeType') == self.__FOLDER_MIME: self.__write_folder(child_meta, child_dst) # if child is file, write it to folder else: self.__write_file(child_meta, child_dst) except HttpError, error: StdOut.display( msg=message.get(message.ERROR_REMOTE_OPERATION, file=dst, drive=self.name(), error=error) )
def test_clean_dir_only_dirs(self): path = self.temp_path("a/b/c.txt") fsutil.create_dir(self.temp_path("x/y/z/a")) fsutil.create_dir(self.temp_path("x/y/z/b")) fsutil.create_dir(self.temp_path("x/y/z/c")) fsutil.create_dir(self.temp_path("x/y/z/d")) fsutil.create_dir(self.temp_path("x/y/z/e")) fsutil.create_file(self.temp_path("x/y/z/b/f.txt"), content="hello world") fsutil.create_file(self.temp_path("x/y/z/d/f.txt"), content="hello world") fsutil.clean_dir(self.temp_path("x/y"), dirs=False, files=True) self.assertTrue(fsutil.exists(self.temp_path("x/y/z/a"))) self.assertTrue(fsutil.exists(self.temp_path("x/y/z/b"))) self.assertTrue(fsutil.exists(self.temp_path("x/y/z/c"))) self.assertTrue(fsutil.exists(self.temp_path("x/y/z/d"))) self.assertTrue(fsutil.exists(self.temp_path("x/y/z/e"))) fsutil.clean_dir(self.temp_path("x/y"), dirs=True, files=True) self.assertFalse(fsutil.exists(self.temp_path("x/y/z/a"))) self.assertTrue(fsutil.exists(self.temp_path("x/y/z/b"))) self.assertFalse(fsutil.exists(self.temp_path("x/y/z/c"))) self.assertTrue(fsutil.exists(self.temp_path("x/y/z/d"))) self.assertFalse(fsutil.exists(self.temp_path("x/y/z/e")))
def put(self, request, absolute_path=''): file_obj = request.FILES.get('file', None) if not file_obj: raise ParseError absolute_path = self.atob(absolute_path) node = Node.parse(relative=absolute_path) if not node.is_exist: fsutil.create_dir(node.absolute) file_name = file_obj.name base_file_name, extension = fsutil.split_filename(file_name) absolute_path = fsutil.join_path( absolute_path, base_file_name + '-' + str(int(time.time())) + ('.' + extension if extension else '')) node = Node.parse(relative=absolute_path) if node.is_exist: raise ParseError('File already existed.') with open(node.absolute, 'wb+') as destination: for chunk in file_obj.chunks(): destination.write(chunk) return Response(data=True)
def test_clean_dir_only_dirs(self): path = self.temp_path('a/b/c.txt') fsutil.create_dir(self.temp_path('x/y/z/a')) fsutil.create_dir(self.temp_path('x/y/z/b')) fsutil.create_dir(self.temp_path('x/y/z/c')) fsutil.create_dir(self.temp_path('x/y/z/d')) fsutil.create_dir(self.temp_path('x/y/z/e')) fsutil.create_file(self.temp_path('x/y/z/b/f.txt'), content='hello world') fsutil.create_file(self.temp_path('x/y/z/d/f.txt'), content='hello world') fsutil.clean_dir(self.temp_path('x/y'), dirs=False, files=True) self.assertTrue(fsutil.exists(self.temp_path('x/y/z/a'))) self.assertTrue(fsutil.exists(self.temp_path('x/y/z/b'))) self.assertTrue(fsutil.exists(self.temp_path('x/y/z/c'))) self.assertTrue(fsutil.exists(self.temp_path('x/y/z/d'))) self.assertTrue(fsutil.exists(self.temp_path('x/y/z/e'))) fsutil.clean_dir(self.temp_path('x/y'), dirs=True, files=True) self.assertFalse(fsutil.exists(self.temp_path('x/y/z/a'))) self.assertTrue(fsutil.exists(self.temp_path('x/y/z/b'))) self.assertFalse(fsutil.exists(self.temp_path('x/y/z/c'))) self.assertTrue(fsutil.exists(self.temp_path('x/y/z/d'))) self.assertFalse(fsutil.exists(self.temp_path('x/y/z/e')))
def create_dir(request): path = request.POST['path'] if fsutil.exists(path): return HttpResponseBadRequest fsutil.create_dir(path) return JsonResponse(True, safe=False)
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 @staticmethod def parse(relative=None, absolute=None):
def create_drive(drive_name): fsutil.create_dir(Conf.drives.get(drive_name).get(Conf.DRIVE_HOME))
def create_home(): fsutil.create_dir(Conf.HOME)
def test_make_dirs_for_file_with_existing_file(self): path = self.temp_path("a/b/c.txt") fsutil.create_dir(path) with self.assertRaises(OSError): fsutil.make_dirs_for_file(path)
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_assert_dir(self): path = self.temp_path("a/b/") with self.assertRaises(OSError): fsutil.assert_dir(path) fsutil.create_dir(path) fsutil.assert_dir(path)
def test_assert_exists_with_directory(self): path = self.temp_path("a/b/") with self.assertRaises(OSError): fsutil.assert_exists(path) fsutil.create_dir(path) fsutil.assert_exists(path)
def test_assert_file_with_directory(self): path = self.temp_path("a/b/c.txt") fsutil.create_dir(path) with self.assertRaises(OSError): fsutil.assert_file(path)