Ejemplo n.º 1
0
 def post(self, p):
     if not files.exists(p):
         abort(404, message=('File not found: %s' % p))
     o = json.load(request.stream)
     if not (isinstance(o, dict) and 'command' in o):
         abort(400)
     cmd = o['command']
     try:
         if cmd == 'move':
             if not 'to' in o:
                 abort(400)
             files.move_file(p, o['to'])
         elif cmd == 'copy':
             if not 'to' in o:
                 abort(400)
             files.copy_file(p, o['to'])
         elif cmd == 'mkdir':
             name = o['name']
             if '/' in name:
                 abort(400, message='Invalid filename.')
             if not files.is_directory(p):
                 abort(400, message='Not a directory.')
             if not 'name' in o:
                 abort(400)
             return files.mkdir(p, name)
         else:
             abort(400, message=('Invalid command: %s' % cmd))
     except OSError as e:
         abort(500, message=('File system error: ' + e.strerror))
     except IOError as e:
         abort(500, message=('File system error: ' + e.strerror))
     return '', 204
Ejemplo n.º 2
0
 def delete(self, p):
     if not files.exists(p):
         abort(404, message=('File not found: %s' % p))
     try:
         files.delete(p)
     except OSError as e:
         abort(501, 'File system error: ' + e.strerror)
Ejemplo n.º 3
0
def file_ui(p):
    path = '/'+p
    if not files.exists(path):
        abort(404)
    if files.is_directory(path):
        return file_listing(p)
    return send_file(files.absolute(path))
Ejemplo n.º 4
0
 def get(self, p):
     if not files.exists(p):
         abort(404, message=('File not found: %s' % p))
     if files.is_directory(p):
         return [format_file_info(f) for f in files.directory_listing(p)]
     else:
         return send_file(files.absolute(p))
Ejemplo n.º 5
0
def file_ui(p):
    path = '/' + p
    if not files.exists(path):
        abort(404)
    if files.is_directory(path):
        return file_listing(p)
    return send_file(files.absolute(path))
Ejemplo n.º 6
0
def softDeleteFile(s):
    import os
    trashdir = os.path.expanduser('~') + u'/local/less_important/trash'
    if not files.exists(trashdir):
        trashdir = ur'C:\data\local\less_important\trash'
        if not files.exists(trashdir):
            raise Exception('please edit softDeleteFile() in common_ui.py ' +
                'and specify a directory for removed files.')
            
    # as a prefix, the first 2 chars of the parent directory
    prefix = files.getname(files.getparent(s))[0:2] + '_'
    newname = trashdir + files.sep + prefix + files.split(s)[1] + getRandomString()
    if files.exists(newname):
        raise Exception('already exists ' + newname +
            '. is this directory full of files, or was the random seed reused?')
    files.move(s, newname, False)
    return newname
Ejemplo n.º 7
0
def navigation_text(filename):
    return ''
    if not exists(filename): return ''
    links = [i for i in navigation_links(filename) if i!='' ]
    if links==[]:    return ''
    return 'See also '+', '.join(links)
Ejemplo n.º 8
0
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'))
Ejemplo n.º 9
0
                    origin_node = nodes[origin],
                    dest_node = nodes[dest],
                    length = info['length'],
                    max_speed = info['max_speed'],
                    initial_amount = info['amount'],
                    max_density = ps['max_density'])


# special initial conditions can be specified in a file named
# init.py contained in the simulation folder
initials = {}
for origin in connections:
    initials[origin] = {}
    for dest in connections[origin]:
        initials[origin][dest] = None
if files.exists('init.py'):
    execfile(files.path_to('init.py'))
for origin in connections:
    for dest in initials[origin]:
        func = initials[origin][dest]
        if func:
            c = connections[origin][dest]
            c.dens[:,0] = func(c.x)

nodes = nodes.values()
conns = []
for origin in connections:
    conns += connections[origin].values()

# run the simulation
iterations_num = lw.t.size - 1