示例#1
0
    def handle(self) -> int:
        from poetry.utils.source import source_to_table

        name = self.argument("name")

        sources = AoT([])
        removed = False

        for source in self.poetry.get_sources():
            if source.name == name:
                self.line(f"Removing source with name <c1>{source.name}</c1>.")
                removed = True
                continue
            sources.append(source_to_table(source))

        if not removed:
            self.line_error(
                f"<error>Source with name <c1>{name}</c1> was not found.</error>"
            )
            return 1

        self.poetry.pyproject.poetry_config["source"] = sources
        self.poetry.pyproject.save()

        return 0
示例#2
0
def load_pipfile(p=None):
    # type: (Optional[path.Path]) -> TOMLDocument
    if p is None:
        p = path.Path(ROOT_PIPFILE)

    with open(p, "r") as fp:
        doc = loads(fp.read())
    doc._parsed = True

    sources = []
    have_default = False
    for key in ("source", "sources"):
        try:
            item = doc.item(key)  # type: Union[AoT, Table]
        except KeyError:
            continue

        doc.remove(key)

        if isinstance(item, AoT):
            items = item.body
        else:
            items = item.value

        for source in items:
            if not isinstance(source, Table):
                source = Table(source, Trivia(trail=""), is_aot_element=True)
            container = source.value
            reorder_container(container,
                              pipfile_source_key,
                              key_type=KeyType.Basic)
            if not have_default and is_default(container):
                have_default = True

            sources.append(source)

    if not have_default:
        source = Table(Container(True), Trivia(), True)
        for k, v in DEFAULT_SOURCE:
            source.append(k, v)

        sources.insert(0, source)

    doc.append("source", AoT(sources, parsed=True))
    return doc
示例#3
0
    def handle(self) -> Optional[int]:
        from poetry.factory import Factory
        from poetry.repositories import Pool

        name = self.argument("name")
        url = self.argument("url")
        is_default = self.option("default")
        is_secondary = self.option("secondary")

        if is_default and is_secondary:
            self.line_error(
                "Cannot configure a source as both <c1>default</c1> and <c1>secondary</c1>."
            )
            return 1

        new_source = Source(name=name,
                            url=url,
                            default=is_default,
                            secondary=is_secondary)
        existing_sources = self.poetry.get_sources()

        sources = AoT([])

        for source in existing_sources:
            if source == new_source:
                self.line(
                    f"Source with name <c1>{name}</c1> already exits. Skipping addition."
                )
                return 0
            elif source.default and is_default:
                self.line_error(
                    f"<error>Source with name <c1>{source.name}</c1> is already set to default. "
                    f"Only one default source can be configured at a time.</error>"
                )
                return 1

            if source.name == name:
                self.line(
                    f"Source with name <c1>{name}</c1> already exits. Updating."
                )
                source = new_source
                new_source = None

            sources.append(self.source_to_table(source))

        if new_source is not None:
            self.line(f"Adding source with name <c1>{name}</c1>.")
            sources.append(self.source_to_table(new_source))

        # ensure new source is valid. eg: invalid name etc.
        self.poetry._pool = Pool()
        try:
            Factory.configure_sources(self.poetry, sources, self.poetry.config,
                                      NullIO())
            self.poetry.pool.repository(name)
        except ValueError as e:
            self.line_error(
                f"<error>Failed to validate addition of <c1>{name}</c1>: {e}</error>"
            )
            return 1

        self.poetry.pyproject.poetry_config["source"] = sources
        self.poetry.pyproject.save()

        return 0