Example #1
0
    def _collateral(self, project_uuid: str, project_path: str):
        """Create a project on the filesystem.

        Given a directory it will detect what parts are missing from the
        .orchest directory for the project to be considered initialized,
        e.g. the actual .orchest directory, .gitignore file,
        environments directory, etc. As part of process initialization
        environments are built and kernels refreshed.

        Raises:
            NotADirectoryError:
            FileExistsError:
            NotADirectoryError:
        """
        full_project_path = os.path.join(current_app.config["PROJECTS_DIR"],
                                         project_path)
        # exist_ok=True is there so that this function can be used both
        # when initializing a project that was discovered through the
        # filesystem or initializing a project from scratch.
        os.makedirs(full_project_path, exist_ok=True)

        # This would actually be created as a collateral effect when
        # populating with default environments, do not rely on that.
        expected_internal_dir = os.path.join(full_project_path, ".orchest")
        if os.path.isfile(expected_internal_dir):
            raise NotADirectoryError(
                "The expected internal directory (.orchest) is a file.")
        elif not os.path.isdir(expected_internal_dir):
            os.makedirs(expected_internal_dir, exist_ok=True)

        # Init the .gitignore file if it is not there already.
        expected_git_ignore_file = os.path.join(full_project_path, ".orchest",
                                                ".gitignore")
        if os.path.isdir(expected_git_ignore_file):
            raise FileExistsError(".orchest/.gitignore is a directory")
        elif not os.path.isfile(expected_git_ignore_file):
            with open(expected_git_ignore_file, "w") as ign_file:
                ign_file.write(
                    current_app.config["PROJECT_ORCHEST_GIT_IGNORE_CONTENT"])

        # Initialize with default environments only if the project has
        # no environments directory.
        expected_env_dir = os.path.join(full_project_path, ".orchest",
                                        "environments")
        if os.path.isfile(expected_env_dir):
            raise NotADirectoryError(
                "The expected environments directory (.orchest/environments) "
                "is a file.")
        elif not os.path.isdir(expected_env_dir):
            populate_default_environments(project_uuid)

        # Refresh kernels after change in environments, given that
        # either we added the default environments or the project has
        # environments of its own.
        populate_kernels(current_app, db, project_uuid)

        # Build environments on project creation.
        build_environments_for_project(project_uuid)

        Project.query.filter_by(uuid=project_uuid,
                                path=project_path).update({"status": "READY"})
        db.session.commit()
Example #2
0
    def _collateral(self, project_uuid: str, project_path: str):
        """Create a project on the filesystem.

        Given a directory it will detect what parts are missing from the
        .orchest directory for the project to be considered initialized,
        e.g. the actual .orchest directory, .gitignore file,
        environments directory, etc. As part of process initialization
        environments are built and kernels refreshed.

        Raises:
            NotADirectoryError:
            FileExistsError:
            NotADirectoryError:
        """
        full_project_path = safe_join(current_app.config["PROJECTS_DIR"],
                                      project_path)
        # exist_ok=True is there so that this function can be used both
        # when initializing a project that was discovered through the
        # filesystem or initializing a project from scratch.
        os.makedirs(full_project_path, exist_ok=True)

        # Create top-level `.gitignore` file with sane defaults, in case
        # it not already exists.
        root_gitignore = safe_join(full_project_path, ".gitignore")
        if not os.path.exists(root_gitignore):
            with open(root_gitignore, "w") as f:
                f.write("\n".join(
                    current_app.config["GIT_IGNORE_PROJECT_ROOT"]))

        # This would actually be created as a collateral effect when
        # populating with default environments, do not rely on that.
        expected_internal_dir = safe_join(full_project_path, ".orchest")
        if os.path.isfile(expected_internal_dir):
            raise NotADirectoryError(
                "The expected internal directory (.orchest) is a file.")
        elif not os.path.isdir(expected_internal_dir):
            os.makedirs(expected_internal_dir, exist_ok=True)

        # Init the `.orchest/.gitignore` file if it is not there
        # already. NOTE: This `.gitignore` file is created inside the
        # `.orchest/` directory because an existing project might be
        # added to Orchest and already contain a root-level
        # `.gitignore`, which we don't want to inject ourselves in.
        expected_git_ignore_file = safe_join(full_project_path, ".orchest",
                                             ".gitignore")
        if os.path.isdir(expected_git_ignore_file):
            raise FileExistsError(".orchest/.gitignore is a directory")
        elif not os.path.isfile(expected_git_ignore_file):
            with open(expected_git_ignore_file, "w") as ign_file:
                ign_file.write("\n".join(
                    current_app.config["GIT_IGNORE_PROJECT_HIDDEN_ORCHEST"]))

        # Initialize with default environments only if the project has
        # no environments directory.
        expected_env_dir = safe_join(full_project_path, ".orchest",
                                     "environments")
        if os.path.isfile(expected_env_dir):
            raise NotADirectoryError(
                "The expected environments directory (.orchest/environments) "
                "is a file.")
        elif not os.path.isdir(expected_env_dir):
            populate_default_environments(project_uuid)

        # Initialize .git directory
        expected_git_dir = safe_join(full_project_path, ".git")

        # If no git directory exists initialize git repo
        if not os.path.exists(expected_git_dir):
            p = subprocess.Popen(["git", "init"], cwd=full_project_path)
            p.wait()

        # Refresh kernels after change in environments, given that
        # either we added the default environments or the project has
        # environments of its own.
        populate_kernels(current_app, db, project_uuid)

        resp = requests.post(
            f'http://{current_app.config["ORCHEST_API_ADDRESS"]}/api/projects/',
            json={
                "uuid": project_uuid,
                "name": project_path
            },
        )
        if resp.status_code != 201:
            raise Exception("Orchest-api project creation failed.")

        # Build environments on project creation.
        build_environments_for_project(project_uuid)

        Project.query.filter_by(uuid=project_uuid,
                                path=project_path).update({"status": "READY"})
        db.session.commit()