def plusdir(self): newfolder=askdirectory() listtoimport=files.listfiles(newfolder) for songimported in listtoimport: if songimported in self.current.songlist: listtoimport.remove(songimported) if(len(listtoimport)>0): self.mustsave=True self.current.add(listtoimport) self.updatemusicdata() print self.current.musicdata self.update()
def files_tests(): from files import (readall, writeall, exists, copy, move, sep, run, isemptydir, listchildren, makedir, getname, listfiles, recursedirs, recursefiles, delete, runWithoutWaitUnicode) import tempfile import os import shutil tmpdir = tempfile.gettempdir() + sep + 'pytest' tmpdirsl = tmpdir + sep if not os.path.exists(tmpdir): os.mkdir(tmpdir) def cleardirectoryfiles(d): shutil.rmtree(d) os.mkdir(d) assertTrue(isemptydir(d)) # test copy_overwrite, source not exist cleardirectoryfiles(tmpdir) assertException(lambda: copy(tmpdirsl + 'src.txt', tmpdirsl + 'srccopy.txt', True), IOError) # test copy_overwrite, simple copy cleardirectoryfiles(tmpdir) writeall(tmpdirsl + 'src.txt', 'src') copy(tmpdirsl + 'src.txt', tmpdirsl + 'srccopy.txt', True) assertEq('src', readall(tmpdirsl + 'srccopy.txt')) assertTrue(exists(tmpdirsl + 'src.txt')) # test copy_overwrite, overwrite cleardirectoryfiles(tmpdir) writeall(tmpdirsl + 'src.txt', 'src') assertEq('src', readall(tmpdirsl + 'src.txt')) writeall(tmpdirsl + 'dest.txt', 'dest') assertEq('dest', readall(tmpdirsl + 'dest.txt')) copy(tmpdirsl + 'src.txt', tmpdirsl + 'dest.txt', True) assertEq('src', readall(tmpdirsl + 'dest.txt')) assertTrue(exists(tmpdirsl + 'src.txt')) # test copy_nooverwrite, source not exist cleardirectoryfiles(tmpdir) assertException(lambda: copy(tmpdirsl + 'src.txt', tmpdirsl + 'srccopy.txt', False), IOError) # test copy_nooverwrite, simple copy cleardirectoryfiles(tmpdir) writeall(tmpdirsl + 'src.txt', 'src') copy(tmpdirsl + 'src.txt', tmpdirsl + 'srccopy.txt', False) assertEq('src', readall(tmpdirsl + 'srccopy.txt')) # test copy_nooverwrite, simple overwrite should fail cleardirectoryfiles(tmpdir) writeall(tmpdirsl + 'src.txt', 'src') assertEq('src', readall(tmpdirsl + 'src.txt')) writeall(tmpdirsl + 'dest.txt', 'dest') assertEq('dest', readall(tmpdirsl + 'dest.txt')) if sys.platform == 'win32': assertException(lambda: copy(tmpdirsl + 'src.txt', tmpdirsl + 'dest.txt', False), IOError, 'CopyFileW failed') else: assertException(lambda: copy(tmpdirsl + 'src.txt', tmpdirsl + 'dest.txt', False), OSError, 'File exists') assertEq('dest', readall(tmpdirsl + 'dest.txt')) # test move_overwrite, source not exist cleardirectoryfiles(tmpdir) assertException(lambda: move(tmpdirsl + 'src.txt', tmpdirsl + 'srcmove.txt', True), IOError) assertTrue(not exists(tmpdirsl + 'src.txt')) # test move_overwrite, simple move cleardirectoryfiles(tmpdir) writeall(tmpdirsl + 'src.txt', 'src') move(tmpdirsl + 'src.txt', tmpdirsl + 'srcmove.txt', True) assertEq('src', readall(tmpdirsl + 'srcmove.txt')) assertTrue(not exists(tmpdirsl + 'src.txt')) # test move_overwrite, overwrite cleardirectoryfiles(tmpdir) writeall(tmpdirsl + 'src.txt', 'src') assertEq('src', readall(tmpdirsl + 'src.txt')) writeall(tmpdirsl + 'dest.txt', 'dest') assertEq('dest', readall(tmpdirsl + 'dest.txt')) move(tmpdirsl + 'src.txt', tmpdirsl + 'dest.txt', True) assertEq('src', readall(tmpdirsl + 'dest.txt')) assertTrue(not exists(tmpdirsl + 'src.txt')) # test move_nooverwrite, source not exist cleardirectoryfiles(tmpdir) assertException(lambda: move(tmpdirsl + 'src.txt', tmpdirsl + 'srcmove.txt', False), IOError) assertTrue(not exists(tmpdirsl + 'src.txt')) # test move_nooverwrite, simple move cleardirectoryfiles(tmpdir) writeall(tmpdirsl + 'src.txt', 'src') move(tmpdirsl + 'src.txt', tmpdirsl + 'srcmove.txt', False) assertEq('src', readall(tmpdirsl + 'srcmove.txt')) assertTrue(not exists(tmpdirsl + 'src.txt')) # test move_nooverwrite, simple overwrite should fail cleardirectoryfiles(tmpdir) writeall(tmpdirsl + 'src.txt', 'src') assertEq('src', readall(tmpdirsl + 'src.txt')) writeall(tmpdirsl + 'dest.txt', 'dest') assertEq('dest', readall(tmpdirsl + 'dest.txt')) if sys.platform == 'win32': assertException(lambda: move(tmpdirsl + 'src.txt', tmpdirsl + 'dest.txt', False), IOError, 'MoveFileExW failed') else: assertException(lambda: move(tmpdirsl + 'src.txt', tmpdirsl + 'dest.txt', False), OSError, 'File exists') assertEq('dest', readall(tmpdirsl + 'dest.txt')) assertTrue(exists(tmpdirsl + 'src.txt')) # test _checkNamedParameters assertException(lambda: list(listchildren(tmpdir, True)), ValueError, 'please name parameters') # tmpdir has files, dirs # tmpdir/s1 has no files, dirs # tmpdir/s1/ss1 has files, no dirs # tmpdir/s1/ss2 has no files, dirs cleardirectoryfiles(tmpdir) dirstomake = [tmpdir, tmpdirsl + 's1', tmpdirsl + 's1' + sep + 'ss1', tmpdirsl + 's1' + sep + 'ss2', tmpdirsl + 's2'] filestomake = [tmpdirsl + 'P1.PNG', tmpdirsl + 'a1.txt', tmpdirsl + 'a2png', tmpdirsl + 's1' + sep + 'ss1' + sep + 'file.txt', tmpdirsl + 's2' + sep + 'other.txt'] for dir in dirstomake: if dir != tmpdir: makedir(dir) for file in filestomake: writeall(file, 'content') # test listchildren expected = ['P1.PNG', 'a1.txt', 'a2png', 's1', 's2'] assertEq([(tmpdirsl + s, s) for s in expected], sorted(list(listchildren(tmpdir)))) assertEq(expected, sorted(list(listchildren(tmpdir, filenamesOnly=True)))) assertEq(['P1.PNG', 'a1.txt'], sorted(list(listchildren(tmpdir, filenamesOnly=True, allowedexts=['png', 'txt'])))) # test listfiles expected = ['P1.PNG', 'a1.txt', 'a2png'] assertEq([(tmpdirsl + s, s) for s in expected], sorted(list(listfiles(tmpdir)))) assertEq(expected, sorted(list(listfiles(tmpdir, filenamesOnly=True)))) assertEq(['P1.PNG', 'a1.txt'], sorted(list(listfiles(tmpdir, filenamesOnly=True, allowedexts=['png', 'txt'])))) # test recursefiles assertEq([(s, getname(s)) for s in filestomake], sorted(list(recursefiles(tmpdir)))) assertEq([getname(s) for s in filestomake], sorted(list(recursefiles(tmpdir, filenamesOnly=True)))) assertEq(['a1.txt', 'file.txt', 'other.txt'], sorted(list(recursefiles(tmpdir, filenamesOnly=True, allowedexts=['txt'])))) assertEq(['a1.txt', 'file.txt', 'other.txt'], sorted(list(recursefiles(tmpdir, filenamesOnly=True, allowedexts=['txt'], fnFilterDirs=lambda d: True)))) assertEq(['a1.txt'], sorted(list(recursefiles(tmpdir, filenamesOnly=True, allowedexts=['txt'], fnFilterDirs=lambda d: False)))) assertEq(['a1.txt', 'other.txt'], sorted(list(recursefiles(tmpdir, filenamesOnly=True, allowedexts=['txt'], fnFilterDirs=lambda dir: getname(dir) != 's1')))) assertEq(['a1.txt', 'file.txt'], sorted(list(recursefiles(tmpdir, filenamesOnly=True, allowedexts=['txt'], fnFilterDirs=lambda dir: getname(dir) != 's2')))) # test recursedirs assertEq(sorted([(s, getname(s)) for s in dirstomake]), sorted(list(recursedirs(tmpdir)))) assertEq(sorted([getname(s) for s in dirstomake]), sorted(list(recursedirs(tmpdir, filenamesOnly=True)))) assertEq(['pytest', 's2'], sorted(list(recursedirs(tmpdir, filenamesOnly=True, fnFilterDirs=lambda dir: getname(dir) != 's1')))) # test run process, simple batch script if sys.platform == 'win32': cleardirectoryfiles(tmpdir) writeall(tmpdirsl + 'src.txt', 'src') writeall(tmpdirsl + 'script.bat', 'copy "%ssrc.txt" "%sdest.txt"'%(tmpdirsl, tmpdirsl)) assertTrue(not exists(tmpdirsl + 'dest.txt')) returncode, stdout, stderr = run([tmpdirsl + 'script.bat']) assertEq(0, returncode) assertTrue(exists(tmpdirsl + 'dest.txt')) # specify no capture and run again delete(tmpdirsl + 'dest.txt') returncode, stdout, stderr = run([tmpdirsl + 'script.bat'], captureoutput=False) assertEq(0, returncode) assertTrue(exists(tmpdirsl + 'dest.txt')) # run process with unicode characters delete(tmpdirsl + 'dest.txt') unicodepath = tmpdirsl + u'scr#1pt#2.bat'.replace('#1', u'\u012B').replace('#2', u'\u013C') writeall(unicodepath, 'copy "%ssrc.txt" "%sdest.txt"'%(tmpdirsl, tmpdirsl)) try: import time runWithoutWaitUnicode([unicodepath]) time.sleep(0.5) assertTrue(exists(tmpdirsl + 'dest.txt')) finally: delete(unicodepath) # test run process, batch script returns failure cleardirectoryfiles(tmpdir) writeall(tmpdirsl + 'script.bat', '\nexit /b 123') returncode, stdout, stderr = run([tmpdirsl + 'script.bat'], throwOnFailure=False) assertEq(123, returncode) # specify no capture and run again returncode, stdout, stderr = run([tmpdirsl + 'script.bat'], throwOnFailure=False, captureoutput=False) assertEq(123, returncode) # except exception assertException(lambda: run([tmpdirsl + 'script.bat']), RuntimeError, 'retcode is not 0') # specify no capture, except exception assertException(lambda: run([tmpdirsl + 'script.bat'], captureoutput=False), RuntimeError, 'retcode is not 0') # test run process, get stdout writeall(tmpdirsl + 'script.bat', '\n@echo off\necho testecho') returncode, stdout, stderr = run([tmpdirsl + 'script.bat']) assertEq(0, returncode) assertEq('testecho', stdout) assertEq('', stderr) # test run process, get stderr writeall(tmpdirsl + 'script.bat', '\n@echo off\necho testechoerr 1>&2') returncode, stdout, stderr = run([tmpdirsl + 'script.bat']) assertEq(0, returncode) assertEq('', stdout) assertEq('testechoerr', stderr) # test run process, get both. (this deadlocks if done naively, but it looks like subprocess correctly uses 2 threads.) writeall(tmpdirsl + 'script.bat', '\n@echo off\necho testecho\necho testechoerr 1>&2') returncode, stdout, stderr = run([tmpdirsl + 'script.bat']) assertEq(0, returncode) assertEq('testecho', stdout) assertEq('testechoerr', stderr) # test run process, send argument without spaces writeall(tmpdirsl + 'script.bat', '\n@echo off\necho %1') returncode, stdout, stderr = run([tmpdirsl + 'script.bat', 'testarg']) assertEq(0, returncode) assertEq('testarg', stdout) # test run process, send argument with spaces (subprocess will quote the args) writeall(tmpdirsl + 'script.bat', '\n@echo off\necho %1') returncode, stdout, stderr = run([tmpdirsl + 'script.bat', 'test arg']) assertEq(0, returncode) assertEq('"test arg"', stdout) # test run process, run without shell cleardirectoryfiles(tmpdir) writeall(tmpdirsl + 'src.txt', 'src') # won't work without the shell: assertException(lambda: run(['copy', tmpdirsl + 'src.txt', tmpdirsl + 'dest.txt']), OSError) assertTrue(not exists(tmpdirsl + 'dest.txt')) # will work with the shell returncode, stdout, stderr = run(['copy', tmpdirsl + 'src.txt', tmpdirsl + 'dest.txt'], shell=True) assertEq(0, returncode) assertTrue(exists(tmpdirsl + 'dest.txt'))