Beispiel #1
0
    def create_file(self, relative_path: str) -> None:
        full_path = self.full_file_path(relative_path)
        if utf8_path_exists(full_path):
            raise OSError(
                "Can not create project file at {} as it already exists".format(
                    full_path
                )
            )

        utf8_makedirs(utf8_dirname(full_path), exist_ok=True)

        with open(full_path, "a"):
            pass
Beispiel #2
0
    def move_file(self, current_relative_path: str, new_relative_path: str) -> None:
        current_path = self.full_file_path(current_relative_path)
        new_path = self.full_file_path(new_relative_path)

        if utf8_isdir(new_path):
            # path moving to is a directory so actually move inside the path
            filename = utf8_basename(current_path)
            new_path = utf8_path_join(new_path, filename)

        if utf8_path_exists(new_path):
            raise OSError(
                "Can not move {} to {} as target file exists.".format(
                    current_relative_path, new_relative_path
                )
            )

        utf8_makedirs(utf8_dirname(new_path), exist_ok=True)
        utf8_rename(current_path, new_path)
Beispiel #3
0
 def pull(self, only_file_sources: bool = False) -> None:
     """Perform the pull of the project files."""
     event = ProjectEvent.objects.create(
         event_type=ProjectEventType.SOURCE_PULL.name,
         project=self.project,
         user=logged_in_or_none(self.request.user),
         level=ProjectEventLevel.INFORMATIONAL.value,
     )
     utf8_makedirs(self.project_directory, exist_ok=True)
     try:
         self.pull_directory(only_file_sources=only_file_sources)
         event.success = True
     except Exception as e:
         event.message = str(e)
         event.level = ProjectEventLevel.ERROR.value
         event.success = False
         raise
     finally:
         event.finished = timezone.now()
         event.save()
Beispiel #4
0
 def create_directory(self, relative_path: str) -> None:
     full_path = self.full_file_path(relative_path)
     utf8_makedirs(full_path, exist_ok=True)
Beispiel #5
0
    def pull_directory(self,
                       sub_directory: typing.Optional[str] = None,
                       only_file_sources: bool = True) -> None:
        """
        Pull one 'virtual' directory to disk.

        Will create directories and files in `sub_directory`, then recurse into directories to repeat the pull.
        """
        if self.current_storage is None:
            self.current_storage = self.project_directory_size

        dir_list = list_project_virtual_directory(self.project, sub_directory,
                                                  self.authentication,
                                                  only_file_sources)

        working_directory = sub_directory or ""
        fs_working_directory = utf8_path_join(self.project_directory,
                                              working_directory)

        for entry in dir_list:
            output_path = utf8_path_join(fs_working_directory, entry.name)

            if entry.type in (
                    DirectoryEntryType.DIRECTORY,
                    DirectoryEntryType.LINKED_SOURCE,
            ):
                if utf8_path_exists(
                        output_path) and not utf8_isdir(output_path):
                    utf8_unlink(output_path)  # remove path if is a file

                utf8_makedirs(output_path, exist_ok=True)
            else:
                scf = make_source_content_facade(self.request.user, entry.path,
                                                 entry.source, self.project)

                if self.storage_limit != -1:
                    if isinstance(entry.source, DiskSource):
                        source_size = 0
                    else:
                        source_size = scf.get_size()

                    if source_size + self.current_storage > self.storage_limit:
                        raise StorageLimitExceededException()

                    self.current_storage += source_size

                if utf8_path_exists(output_path) and utf8_isdir(output_path):
                    shutil.rmtree(to_utf8(
                        output_path))  # remove path if it is a directory

                with open(to_utf8(output_path), "wb") as f:
                    shutil.copyfileobj(BytesIO(scf.get_binary_content()), f)

                if scf.error_exists:
                    for message in scf.message_iterator():
                        messages.add_message(self.request, message.level,
                                             message.message)
                    return

        directory_entries = filter(
            lambda e: e.type in
            (DirectoryEntryType.DIRECTORY, DirectoryEntryType.LINKED_SOURCE),
            dir_list,
        )

        for directory in directory_entries:
            self.pull_directory(
                utf8_path_join(working_directory, directory.name))