Created
August 31, 2020 14:05
-
-
Save esuomi/b13fba939e970961a65f054b3938c8bd to your computer and use it in GitHub Desktop.
Run this + poetry update after it in git post-merge hook to sync requirements.txt to working Poetry environment automatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tempfile | |
import re | |
def write_requirements(target_file): | |
with open("requirements.txt", "r") as deps: | |
for depline in deps: | |
m = re.search("^(.+)==(.+)$", depline) | |
target_file.write(m.group(1) + " = \"" + m.group(2) + "\"\n") | |
if __name__ == '__main__': | |
tmp = tempfile.NamedTemporaryFile() | |
match_start = "[tool.poetry.dependencies]\n" | |
match_end = "\n" | |
in_match = False | |
skip = -1 | |
# replace contents of pyproject.toml by "ranking" each line with the skip counter: | |
with open(tmp.name, "w") as temp, open("pyproject.toml", "r") as source: | |
for line in source: | |
if line == match_start: | |
in_match = True | |
skip = 3 | |
if in_match == True and line == match_end: | |
# convert requirements.txt to poetry style dependency definitions | |
write_requirements(temp) | |
in_match = False | |
skip = 0 | |
if skip != 1: | |
temp.write(line) | |
if skip > 1: | |
skip -= 1 | |
with open(tmp.name, "r") as temp, open("pyproject.toml", "w") as target: | |
target.write(temp.read()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment