Skip to content

Commit 26dccd7

Browse files
committed
Streamline setup.py file reading
This is partly a refactoring, reducing logic duplication and slightly decreasing overall length of the code that reads values from files. However, it does make two small behavioral changes: - In addition to the VERSION file for which this was already the case, requirements files and the readme are also taken relative to the directory that contains the setup.py file, rather than relative to the current directory if different. Supporting reading the readme and dependencies realtive to a different directory doesn't seem intended, and could lead to confusion in some cases. - The VERSION file is now read entirely and whitespace stripped, rather than reading just the first line. The VERSION file should always contain exactly one line, and this logic is simpler. This changes code for the reading of metadata only. It does not make any changes to the version stamping code.
1 parent 1b43166 commit 26dccd7

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

setup.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@
22

33
import os
44
import sys
5+
from pathlib import Path
56
from typing import Sequence
67

78
from setuptools import setup, find_packages
89
from setuptools.command.build_py import build_py as _build_py
910
from setuptools.command.sdist import sdist as _sdist
1011

1112

12-
with open(os.path.join(os.path.dirname(__file__), "VERSION"), encoding="utf-8") as ver_file:
13-
VERSION = ver_file.readline().strip()
13+
def _read_content(path: str) -> str:
14+
return (Path(__file__).parent / path).read_text(encoding="utf-8")
1415

15-
with open("requirements.txt", encoding="utf-8") as reqs_file:
16-
requirements = reqs_file.read().splitlines()
1716

18-
with open("test-requirements.txt", encoding="utf-8") as reqs_file:
19-
test_requirements = reqs_file.read().splitlines()
20-
21-
with open("README.md", encoding="utf-8") as rm_file:
22-
long_description = rm_file.read()
17+
version = _read_content("VERSION").strip()
18+
requirements = _read_content("requirements.txt").splitlines()
19+
test_requirements = _read_content("test-requirements.txt").splitlines()
20+
long_description = _read_content("README.md")
2321

2422

2523
class build_py(_build_py):
@@ -50,7 +48,7 @@ def _stamp_version(filename: str) -> None:
5048
with open(filename) as f:
5149
for line in f:
5250
if "__version__ =" in line:
53-
line = line.replace('"git"', "'%s'" % VERSION)
51+
line = line.replace('"git"', "'%s'" % version)
5452
found = True
5553
out.append(line)
5654
except OSError:
@@ -66,7 +64,7 @@ def _stamp_version(filename: str) -> None:
6664
setup(
6765
name="GitPython",
6866
cmdclass={"build_py": build_py, "sdist": sdist},
69-
version=VERSION,
67+
version=version,
7068
description="GitPython is a Python library used to interact with Git repositories",
7169
author="Sebastian Thiel, Michael Trier",
7270
author_email="byronimo@gmail.com, mtrier@gmail.com",

0 commit comments

Comments
 (0)