def normpath(path, trailing=False):
    """Normalize path, eliminating double slashes, etc."""
    comps = path.split('/')
    new_comps = []
    for comp in comps:
        if comp == '':
            continue
        if comp in ('.', '..'):
            raise ValueError('relative paths not allowed')
        new_comps.append(comp)
    new_path = '/'.join(new_comps)
    if trailing is True and path.endswith('/'):
        new_path += '/'
    if path.startswith('/') and new_path != '/':
        return '/' + new_path
    return new_path
