Exemple #1
0
def mkdir(dirname):
    import stat
    fs = FileSystem()
    if dirname in fs.superblocks or dirname in ('.', '..'):
        raise IOError('File exists')
    if '/' in dirname:
        raise ValueError('"/" is not permitted in directory name')
    fs.add_superblock(dirname, stat.S_IFDIR)
Exemple #2
0
def mkdir(dirname):
    import stat
    fs = FileSystem()
    if dirname in fs.metadata or dirname in ('.', '..'):
        raise IOError('File exists')
    if '/' in dirname:
        raise ValueError('"/" is not permitted in directory name')
    fs.add_item(dirname, stat.S_IFDIR)
Exemple #3
0
def stat(path):
    fs = FileSystem()
    path = fs.get_absolute_of(path)
    try:
        file_ = fs.metadata[path]
    except KeyError:
        raise OSError('No such file "%s"' % path)

    return stat_result(file_['mode'], file_['size'])
Exemple #4
0
def test_filesystem_file_write():
    with open('pykern.test.fs', 'r+') as disk:
        fs = FileSystem(disk)
        with fs.open_file('file.txt', 'w') as f:
            f.write('1234')

    with open('pykern.test.fs', 'r') as f:
        data = f.read()
    assert len(data) > 0

    assert data[1024*1024:] == '1234'

    metadata = OrderedDict(bson.loads(data)['metadata'])
    assert metadata['/file.txt']['size'] == 4
Exemple #5
0
def test_filesystem_file_write():
    with open('pykern.test.fs', 'r+') as disk:
        fs = FileSystem(disk)
        with fs.open_file('file.txt', 'w') as f:
            f.write('1234')

    with open('pykern.test.fs', 'r') as f:
        data = f.read()
    assert len(data) > 0

    assert data[1024 * 1024:] == '1234'

    superblocks = OrderedDict(bson.loads(data)['superblocks'])
    print superblocks
    assert superblocks['/file.txt']['size'] == 4
Exemple #6
0
def listdir(path):
    fs = FileSystem()
    if not os.path.isdir(path):
        raise OSError('Not a directory: "%s"' % path)
    absolute_target_path = fs.get_absolute_of('%s' % path)
    if not absolute_target_path.endswith('/'):
        absolute_target_path += '/'
    result = [
        path[len(absolute_target_path):]
        for path in FileSystem().metadata
        if path.startswith(absolute_target_path) and
        '/' not in path.split(absolute_target_path, 1)[1] and
        path != absolute_target_path
    ]
    return result
Exemple #7
0
def isdir(path):
    try:
        FileSystem().get_superblock(path, mode=FileSystem.DIRECTORY_MODE)
    except OSError:
        return False
    else:
        return True
Exemple #8
0
 def put_file(self, src, dst='/', fs_file_name=None):
     with open(src, 'rb') as rf:
         filename = '.'.join(os.path.split(src)[1].split('.')[:-1])
         filename = os.path.join(dst, filename)
         with open(fs_file_name, 'r+') as disk:
             with FileSystem(disk).open_file(filename, 'w') as vf:
                 self._copy(rf, vf)
Exemple #9
0
def listdir(p):
    if not path.isdir(p):
        raise OSError('Not a directory: "%s"' % p)
    absolute_target_path = path.abspath('%s' % p)
    if not absolute_target_path.endswith('/'):
        absolute_target_path += '/'
    result = [
        p[len(absolute_target_path):] for p in FileSystem().superblocks
        if p.startswith(absolute_target_path) and '/' not in p.split(
            absolute_target_path, 1)[1] and p != absolute_target_path
    ]
    return result
Exemple #10
0
    def install(self, disk_file_name, force=False):
        if os.path.exists(disk_file_name):
            if not force:
                raise ValueError('Already installed')

        with open(disk_file_name, 'wb+') as disk:
            disk.write('\x00'*1024*1024)
            disk.seek(0, 0)
            disk.write(bson.dumps(dict(metadata=[])))
            disk.seek(0, 0)

            fs = FileSystem(disk)

            fs.add_item('/', fs.DIRECTORY_MODE)

            bin_dir = os.path.join(os.path.split(os.path.realpath(__file__))[0], 'bin')
            bins = glob.glob(os.path.join(bin_dir, '*.py'))
            fs.add_item('/bin', fs.DIRECTORY_MODE)

            for filepath in bins:
                self.put_file(filepath, '/bin', disk_file_name)
Exemple #11
0
    def install(self, disk_file_name, force=False):
        if os.path.exists(disk_file_name):
            if not force:
                raise ValueError('Already installed')

        with open(disk_file_name, 'wb+') as disk:
            disk.write('\x00'*1024*1024)
            disk.seek(0, 0)
            disk.write(bson.dumps(dict(superblocks=[])))
            disk.seek(0, 0)

            fs = FileSystem(disk)

            fs.add_superblock('/', fs.DIRECTORY_MODE)

            bin_dir = os.path.join(os.path.split(os.path.realpath(__file__))[0], 'bin')
            bins = glob.glob(os.path.join(bin_dir, '*.py'))
            fs.add_superblock('/bin', fs.DIRECTORY_MODE)

            for filepath in bins:
                self.put_file(filepath, '/bin', disk_file_name)
Exemple #12
0
import argparse
from os.path import abspath
from pykern.filesystem import FileSystem

parser = argparse.ArgumentParser()
parser.add_argument('source')
parser.add_argument('dest')

args = parser.parse_args()
fs = FileSystem()

src = fs.get_superblock(args.source)

try:
    dest = fs.get_superblock(args.dest)
except OSError:
    pass
fs.superblocks[abspath(args.dest)] = src.copy()
del fs.superblocks[abspath(args.source)]
fs.save_superblocks()
Exemple #13
0
def remove(p):
    FileSystem().remove_file(p)
Exemple #14
0
def chdir(p):
    fs = FileSystem()
    absolute_path = path.abspath('%s' % p)
    fs.get_superblock(absolute_path, mode=fs.DIRECTORY_MODE)
    fs.current_directory = absolute_path
Exemple #15
0
def chdir(path):
    fs = FileSystem()
    absolute_path = fs.get_absolute_of('%s' % path)
    if not os.path.isdir(absolute_path):
        raise OSError('Not a directory: "%s"' % path)
    fs.current_dir = absolute_path
Exemple #16
0
def rmdir(p):
    FileSystem().remove_directory(p)
Exemple #17
0
def stat(p):
    fs = FileSystem()
    p = path.abspath(p)
    superblock = fs.get_superblock(p)
    return stat_result(superblock['mode'], superblock['size'])
Exemple #18
0
def stat(p):
    fs = FileSystem()
    p = path.abspath(p)
    superblock = fs.get_superblock(p)
    return stat_result(superblock['mode'], superblock['size'])
Exemple #19
0
def chdir(p):
    fs = FileSystem()
    absolute_path = path.abspath('%s' % p)
    fs.get_superblock(absolute_path, mode=fs.DIRECTORY_MODE)
    fs.current_directory = absolute_path
Exemple #20
0
parser.add_argument('-l', action='store_true')
parser.add_argument('-a', action='store_true')
parser.add_argument('directory', nargs='?', default='.')
args = parser.parse_args()


def get_filenames():
    global args
    names = os.listdir(args.directory)
    if args.a:
        names += ['.', '..']
    return sorted(names)


if args.l:
    fs = FileSystem()
    filenames = get_filenames()
    filepaths = [
        os.path.abspath(os.path.join(args.directory, name))
        for name in filenames
    ]
    stats = [(name, os.stat(path)) for name, path in zip(filenames, filepaths)]
    max_size = max([stat.st_size for _, stat in stats])
    if max_size == 0:
        size_buffer = 2
    else:
        size_buffer = str(int(math.log(max_size, 10)) + 1)
    for name, stat in stats:
        mode = 'd' if stat.is_directory() else '-'
        print '%s %{0}d %s'.format(size_buffer) % (mode, stat.st_size, name)
else:
Exemple #21
0
def getcwd():
    return FileSystem().current_directory