Skip to content

Commit

Permalink
new: check if existing pyproject.toml contains a poetry or build-syst…
Browse files Browse the repository at this point in the history
…em section. If not prepend data on init.
  • Loading branch information
finswimmer committed May 21, 2020
1 parent e70ee31 commit 4c741a8
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from typing import Tuple
from typing import Union

import tomlkit

from cleo import option
from tomlkit import inline_table

Expand All @@ -22,7 +24,6 @@


class InitCommand(Command):

name = "init"
description = (
"Creates a basic <comment>pyproject.toml</> file in the current directory."
Expand Down Expand Up @@ -66,9 +67,26 @@ def handle(self):
from poetry.utils.env import SystemEnv
from poetry.core.vcs.git import GitConfig

original_toml = None

if (Path.cwd() / "pyproject.toml").exists():
self.line("<error>A pyproject.toml file already exists.</error>")
return 1
with (Path.cwd() / "pyproject.toml").open() as toml_file:
original_toml = tomlkit.loads(toml_file.read())

try:
if original_toml["tool"]["poetry"]:
self.line(
"<error>A pyproject.toml file with a poetry section already exists.</error>"
)
return 1
except KeyError:
pass

if original_toml.get("build-system"):
self.line(
"<error>A pyproject.toml file with a defined build-system already exists.</error>"
)
return 1

vcs_config = GitConfig()

Expand Down Expand Up @@ -202,6 +220,10 @@ def handle(self):
with (Path.cwd() / "pyproject.toml").open("w", encoding="utf-8") as f:
f.write(content)

if original_toml:
f.write("\n")
f.write(tomlkit.dumps(original_toml))

def _determine_requirements(
self, requires, allow_prereleases=False, source=None
): # type: (List[str], bool) -> List[Dict[str, str]]
Expand Down

0 comments on commit 4c741a8

Please sign in to comment.