8000 123 init by warner · Pull Request #138 · python-versioneer/python-versioneer · GitHub
[go: up one dir, main page]

Skip to content
8000

123 init #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/git/install.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import sys # --STRIP DURING BUILD
import os, sys # --STRIP DURING BUILD
def run_command(): pass # --STRIP DURING BUILD

def do_vcs_install(manifest_in, versionfile_source, ipy):
def do_vcs_install(root, manifest_in, versionfile_source, ipy):
"""Git-specific installation logic for Versioneer.

For Git, this means creating/changing .gitattributes to mark _version.py
for export-subst keyword substitution.
"""
# manifest_in and ipy must be relative to root
GITS = ["git"]
if sys.platform == "win32":
GITS = ["git.cmd", "git.exe"]
Expand All @@ -22,8 +23,9 @@ def do_vcs_install(manifest_in 8000 , versionfile_source, ipy):
versioneer_file = "versioneer.py"
files.append(versioneer_file)
present = False
gitattributes = os.path.join(root, ".gitattributes")
try:
f = open(".gitattributes", "r")
f = open(gitattributes, "r")
for line in f.readlines():
if line.strip().startswith(versionfile_source):
if "export-subst" in line.strip().split()[1:]:
Expand All @@ -32,10 +34,10 @@ def do_vcs_install(manifest_in, versionfile_source, ipy):
except EnvironmentError:
pass
if not present:
f = open(".gitattributes", "a+")
f = open(gitattributes, "a+")
f.write("%s export-subst\n" % versionfile_source)
f.close()
files.append(".gitattributes")
run_command(GITS, ["add", "--"] + files)
run_command(GITS, ["add", "--"] + files, cwd=root)


39 changes: 19 additions & 20 deletions src/setupfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,22 @@ def do_vcs_install(): pass # --STRIP DURING BUILD
"""


def do_setup():
def do_setup(stdout=sys.stdout, stderr=sys.stderr):
"""Main VCS-independent setup function for installing Versioneer."""
root = get_root()
try:
cfg = get_config_from_root(root)
except (EnvironmentError, configparser.NoSectionError,
configparser.NoOptionError) as e:
if isinstance(e, (EnvironmentError, configparser.NoSectionError)):
print("Adding sample versioneer config to setup.cfg",
file=sys.stderr)
print(u"Adding sample versioneer config to setup.cfg", file=stderr)
with open(os.path.join(root, "setup.cfg"), "a") as f:
f.write(SAMPLE_CONFIG)
print(CONFIG_ERROR, file=sys.stderr)
print(CONFIG_ERROR, file=stderr)
return 1

print(" creating %s" % cfg.versionfile_source)
with open(cfg.versionfile_source, "w") as f:
print(u" creating %s" % cfg.versionfile_source, file=stdout)
with open(os.path.join(root, cfg.versionfile_source), "w") as f:
LONG = LONG_VERSION_PY[cfg.VCS]
f.write(LONG % {"DOLLAR": "$",
"STYLE": cfg.style,
Expand All @@ -76,22 +75,22 @@ def do_setup():
"VERSIONFILE_SOURCE": cfg.versionfile_source,
})

ipy = os.path.join(os.path.dirname(cfg.versionfile_source),
"__init__.py")
if os.path.exists(ipy):
ipy = os.path.join(os.path.dirname(cfg.versionfile_source), "__init__.py")
ipy_abs = os.path.join(root, ipy)
if os.path.exists(ipy_abs):
try:
with open(ipy, "r") as f:
with open(ipy_abs, "r") as f:
old = f.read()
except EnvironmentError:
old = ""
if INIT_PY_SNIPPET not in old:
print(" appending to %s" % ipy)
with open(ipy, "a") as f:
print(u" appending to %s" % ipy, file=stdout)
with open(ipy_abs, "a") as f:
f.write(INIT_PY_SNIPPET)
else:
print(" %s unmodified" % ipy)
print(u" %s unmodified" % ipy, file=stdout)
else:
print(" %s doesn't exist, ok" % ipy)
print(u" %s doesn't exist, ok" % ipy, file=stdout)
ipy = None

# Make sure both the top-level "versioneer.py" and versionfile_source
Expand All @@ -113,23 +112,23 @@ def do_setup():
# it might give some false negatives. Appending redundant 'include'
# lines is safe, though.
if "versioneer.py" not in simple_includes:
print(" appending 'versioneer.py' to MANIFEST.in")
print(u" appending 'versioneer.py' to MANIFEST.in", file=stdout)
with open(manifest_in, "a") as f:
f.write("include versioneer.py\n")
else:
print(" 'versioneer.py' already in MANIFEST.in")
print(u" 'versioneer.py' already in MANIFEST.in", file=stdout)
if cfg.versionfile_source not in simple_includes:
print(" appending versionfile_source ('%s') to MANIFEST.in" %
cfg.versionfile_source)
print(u" appending versionfile_source ('%s') to MANIFEST.in" %
cfg.versionfile_source, file=stdout)
with open(manifest_in, "a") as f:
f.write("include %s\n" % cfg.versionfile_source)
else:
print(" versionfile_source already in MANIFEST.in")
print(u" versionfile_source already in MANIFEST.in", file=stdout)

# Make VCS-specific changes. For git, this means creating/changing
# .gitattributes to mark _version.py for export-subst keyword
# substitution.
do_vcs_install(manifest_in, cfg.versionfile_source, ipy)
do_vcs_install(root, "MANIFEST.in", cfg.versionfile_source, ipy)
return 0


Expand Down
64 changes: 64 additions & 0 deletions test/test_setupfunc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from __future__ import print_function
import unittest
import os, tempfile, mock, io

from versioneer import do_setup

base = """
[versioneer]
VCS = git
style = pep440
versionfile_source = src/petmail/xyz.py
versionfile_build = petmail/xyz.py
tag_prefix = v
parentdir_prefix = petmail-
"""

class Setup(unittest.TestCase):
def populate(self, root, contents):
with open(os.path.join(root, "setup.cfg"), "w") as f:
f.write(contents)
os.mkdir(os.path.join(root, "src"))
os.mkdir(os.path.join(root, "src", "petmail"))
with open(os.path.join(root, "src", "petmail", "__init__.py"), "w") as f:
f.write("")

def check_contents(self, root, fn, expected):
with open(os.path.join(root, fn), "r") as f:
got = f.read()
self.assertEqual(got, expected)

def test_init_snippet(self):
root = tempfile.mkdtemp()
self.populate(root, base)

stdout, stderr = io.StringIO(), io.StringIO()
with mock.patch("versioneer.get_root", return_value=root):
with mock.patch("versioneer.run_command") as rc:
do_setup(stdout, stderr)
git_add_args, git_add_kwargs = rc.call_args
self.assertEqual(git_add_args[1][:2], ["add", "--"])
self.assertEqual(set(git_add_args[1][2:]),
set(["MANIFEST.in",
"src/petmail/xyz.py",
"src/petmail/__init__.py",
"versioneer.py",
".gitattributes"]))
self.check_contents(root, ".gitattributes",
"src/petmail/xyz.py export-subst\n")
self.check_contents(root, "MANIFEST.in",
"include versioneer.py\n"
"include src/petmail/xyz.py\n")
self.check_contents(root, os.path.join("src", "petmail", "__init__.py"),
"\n"
"from ._version import get_versions\n"
"__version__ = get_versions()['version']\n"
"del get_versions\n"
)
self.assertEqual(stdout.getvalue(),
" creating src/petmail/xyz.py\n"
" appending to src/petmail/__init__.py\n"
" appending 'versioneer.py' to MANIFEST.in\n"
" appending versionfile_source ('src/petmail/xyz.py') to MANIFEST.in\n"
)
self.assertEqual(stderr.getvalue(), "")
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ deps =
discover
flake8-docstrings
pep8
mock

commands =
pip --version
Expand Down
0