Exemple #1
0
    def _prep_for_copytree(self, dest):
        """Handles logic, for finalizing target destination, making parent folders
        and checking for clashes, common to _clonetree and _movetree"""
        source = utils.remove_trailing_slash(self)
        dest_is_dir = dest.isdir()
        should_rename = True
        target_dest = dest

        if dest_is_dir or utils.has_trailing_slash(dest):
            target_dest = dest / (source.name if source.resource else
                                  source.virtual_project)
            if target_dest.isdir():
                raise stor_exceptions.TargetExistsError(
                    'Destination path ({}) already exists, will not cause '
                    'duplicate folders to exist. Remove the original first'.
                    format(target_dest))
            should_rename = False

        if not source.resource:
            target_dest.makedirs_p()
        elif not dest_is_dir and target_dest.parent.resource:  # don't call makedirs_p on project
            target_dest.parent.makedirs_p()

        moved_folder_path = target_dest.parent / source.name
        return target_dest, should_rename, moved_folder_path
Exemple #2
0
def get_path(pth, mode=None):
    """
    Convert string to a Path type.

    The string ``-`` is a special string depending on mode.
    With mode 'r', it represents stdin and a temporary file is created and returned.
    """
    service = _obs_relpath_service(pth)
    if not service:
        return Path(pth)

    relprefix = service + ':'

    pwd = Path(_get_pwd(service=service))
    if pwd == pwd.drive:
        raise ValueError(
            'No current directory specified for relative path \'%s\'' % pth)

    pwd = utils.remove_trailing_slash(pwd)
    path_part = pth[len(relprefix):]
    split_parts = path_part.split('/')
    rel_part = split_parts[0]

    prefix = pwd
    depth = 1
    if rel_part == '..':
        # remove trailing slash otherwise we won't find the right parent
        prefix = utils.remove_trailing_slash(prefix)
        while len(split_parts) > depth and split_parts[depth] == '..':
            depth += 1
        if len(pwd[len(pwd.drive):].split('/')) > depth:
            for i in range(0, depth):
                prefix = prefix.parent
        else:
            raise ValueError(
                'Relative path \'%s\' is invalid for current directory \'%s\''
                % (pth, pwd))
    elif rel_part != '.':
        return prefix / path_part
    return prefix / path_part.split(rel_part, depth)[depth].lstrip('/')
Exemple #3
0
def _env_chdir(pth):
    """Sets the new current working directory."""
    parser = _get_env()
    if utils.is_obs_path(pth):
        service = Path(pth).drive.rstrip(':/')
    else:
        raise ValueError('%s is an invalid path' % pth)
    if pth != Path(pth).drive:
        if not Path(pth).isdir():
            raise ValueError('%s is not a directory' % pth)
        pth = utils.remove_trailing_slash(pth)
    parser.set('env', service, pth)
    with open(ENV_FILE, 'w') as outfile:
        parser.write(outfile)
Exemple #4
0
 def test_remove_trailing_slash_multiple(self):
     self.assertEquals(utils.remove_trailing_slash('many/slashes//'),
                       'many/slashes')
Exemple #5
0
 def test_remove_trailing_slash_wo_slash(self):
     self.assertEquals(utils.remove_trailing_slash('no/slash'), 'no/slash')
Exemple #6
0
 def test_remove_trailing_slash_none(self):
     self.assertIsNone(utils.remove_trailing_slash(None))