Merge branch 'jk/remote-helpers-in-python-3'

Prepare remote-helper test written in Python to be run with Python3.

* jk/remote-helpers-in-python-3:
  git_remote_helpers: remove GIT-PYTHON-VERSION upon "clean"
  git-remote-testpy: fix path hashing on Python 3
  git-remote-testpy: call print as a function
  git-remote-testpy: don't do unbuffered text I/O
  git-remote-testpy: hash bytes explicitly
  svn-fe: allow svnrdump_sim.py to run with Python 3
  git_remote_helpers: use 2to3 if building with Python 3
  git_remote_helpers: force rebuild if python version changes
  git_remote_helpers: fix input when running under Python 3
  git_remote_helpers: allow building with Python 3
This commit is contained in:
Junio C Hamano 2013-02-04 10:25:34 -08:00
Родитель 9aea11dbc1 ae6037bc71
Коммит 3e515b0d40
6 изменённых файлов: 69 добавлений и 29 удалений

Просмотреть файл

@ -14,7 +14,7 @@ if sys.hexversion < 0x02040000:
def getrevlimit():
var = 'SVNRMAX'
if os.environ.has_key(var):
if var in os.environ:
return os.environ[var]
return None
@ -44,7 +44,7 @@ def writedump(url, lower, upper):
if __name__ == "__main__":
if not (len(sys.argv) in (3, 4, 5)):
print "usage: %s dump URL -rLOWER:UPPER"
print("usage: %s dump URL -rLOWER:UPPER")
sys.exit(1)
if not sys.argv[1] == 'dump': raise NotImplementedError('only "dump" is suppported.')
url = sys.argv[2]

Просмотреть файл

@ -31,11 +31,27 @@ from git_remote_helpers.git.exporter import GitExporter
from git_remote_helpers.git.importer import GitImporter
from git_remote_helpers.git.non_local import NonLocalGit
if sys.hexversion < 0x01050200:
# os.makedirs() is the limiter
sys.stderr.write("git-remote-testgit: requires Python 1.5.2 or later.\n")
if sys.hexversion < 0x02000000:
# string.encode() is the limiter
sys.stderr.write("git-remote-testgit: requires Python 2.0 or later.\n")
sys.exit(1)
def encode_filepath(path):
"""Encodes a Unicode file path to a byte string.
On Python 2 this is a no-op; on Python 3 we encode the string as
suggested by [1] which allows an exact round-trip from the command line
to the filesystem.
[1] http://docs.python.org/3/c-api/unicode.html#file-system-encoding
"""
if sys.hexversion < 0x03000000:
return path
return path.encode(sys.getfilesystemencoding(), 'surrogateescape')
def get_repo(alias, url):
"""Returns a git repository object initialized for usage.
"""
@ -45,7 +61,7 @@ def get_repo(alias, url):
repo.get_head()
hasher = _digest()
hasher.update(repo.path)
hasher.update(encode_filepath(repo.path))
repo.hash = hasher.hexdigest()
repo.get_base_path = lambda base: os.path.join(
@ -87,9 +103,9 @@ def do_capabilities(repo, args):
"""Prints the supported capabilities.
"""
print "import"
print "export"
print "refspec refs/heads/*:%s*" % repo.prefix
print("import")
print("export")
print("refspec refs/heads/*:%s*" % repo.prefix)
dirname = repo.get_base_path(repo.gitdir)
@ -98,11 +114,11 @@ def do_capabilities(repo, args):
path = os.path.join(dirname, 'git.marks')
print "*export-marks %s" % path
print("*export-marks %s" % path)
if os.path.exists(path):
print "*import-marks %s" % path
print("*import-marks %s" % path)
print # end capabilities
print('') # end capabilities
def do_list(repo, args):
@ -115,16 +131,16 @@ def do_list(repo, args):
for ref in repo.revs:
debug("? refs/heads/%s", ref)
print "? refs/heads/%s" % ref
print("? refs/heads/%s" % ref)
if repo.head:
debug("@refs/heads/%s HEAD" % repo.head)
print "@refs/heads/%s HEAD" % repo.head
print("@refs/heads/%s HEAD" % repo.head)
else:
debug("@refs/heads/master HEAD")
print "@refs/heads/master HEAD"
print("@refs/heads/master HEAD")
print # end list
print('') # end list
def update_local_repo(repo):
@ -154,7 +170,7 @@ def do_import(repo, args):
refs = [ref]
while True:
line = sys.stdin.readline()
line = sys.stdin.readline().decode()
if line == '\n':
break
if not line.startswith('import '):
@ -164,7 +180,7 @@ def do_import(repo, args):
ref = line[7:].strip()
refs.append(ref)
print "feature done"
print("feature done")
if os.environ.get("GIT_REMOTE_TESTGIT_FAILURE"):
die('Told to fail')
@ -172,7 +188,7 @@ def do_import(repo, args):
repo = update_local_repo(repo)
repo.exporter.export_repo(repo.gitdir, refs)
print "done"
print("done")
def do_export(repo, args):
@ -192,8 +208,8 @@ def do_export(repo, args):
repo.non_local.push(repo.gitdir)
for ref in changed:
print "ok %s" % ref
print
print("ok %s" % ref)
print('')
COMMANDS = {
@ -225,7 +241,7 @@ def read_one_line(repo):
line = sys.stdin.readline()
cmdline = line
cmdline = line.decode()
if not cmdline:
warn("Unexpected EOF")
@ -277,7 +293,11 @@ def main(args):
more = True
sys.stdin = os.fdopen(sys.stdin.fileno(), 'r', 0)
# Use binary mode since Python 3 does not permit unbuffered I/O in text
# mode. Unbuffered I/O is required to avoid data that should be going
# to git-fast-import after an "export" command getting caught in our
# stdin buffer instead.
sys.stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0)
while (more):
more = read_one_line(repo)

1
git_remote_helpers/.gitignore поставляемый
Просмотреть файл

@ -1,2 +1,3 @@
/GIT-PYTHON-VERSION
/build
/dist

Просмотреть файл

@ -23,10 +23,16 @@ endif
PYLIBDIR=$(shell $(PYTHON_PATH) -c \
"import sys; \
print 'lib/python%i.%i/site-packages' % sys.version_info[:2]")
print('lib/python%i.%i/site-packages' % sys.version_info[:2])")
py_version=$(shell $(PYTHON_PATH) -c \
'import sys; print("%i.%i" % sys.version_info[:2])')
all: $(pysetupfile)
$(QUIET)$(PYTHON_PATH) $(pysetupfile) $(QUIETSETUP) build
$(QUIET)test "$$(cat GIT-PYTHON-VERSION 2>/dev/null)" = "$(py_version)" || \
flags=--force; \
$(PYTHON_PATH) $(pysetupfile) $(QUIETSETUP) build $$flags
$(QUIET)echo "$(py_version)" >GIT-PYTHON-VERSION
install: $(pysetupfile)
$(PYTHON_PATH) $(pysetupfile) install --prefix $(DESTDIR_SQ)$(prefix)
@ -36,4 +42,4 @@ instlibdir: $(pysetupfile)
clean:
$(QUIET)$(PYTHON_PATH) $(pysetupfile) $(QUIETSETUP) clean -a
$(RM) *.pyo *.pyc
$(RM) *.pyo *.pyc GIT-PYTHON-VERSION

Просмотреть файл

@ -18,13 +18,16 @@ class GitImporter(object):
def get_refs(self, gitdir):
"""Returns a dictionary with refs.
Note that the keys in the returned dictionary are byte strings as
read from git.
"""
args = ["git", "--git-dir=" + gitdir, "for-each-ref", "refs/heads"]
lines = check_output(args).strip().split('\n')
lines = check_output(args).strip().split('\n'.encode('ascii'))
refs = {}
for line in lines:
value, name = line.split(' ')
name = name.strip('commit\t')
value, name = line.split(' '.encode('ascii'))
name = name.strip('commit\t'.encode('ascii'))
refs[name] = value
return refs

Просмотреть файл

@ -4,6 +4,15 @@
from distutils.core import setup
# If building under Python3 we need to run 2to3 on the code, do this by
# trying to import distutils' 2to3 builder, which is only available in
# Python3.
try:
from distutils.command.build_py import build_py_2to3 as build_py
except ImportError:
# 2.x
from distutils.command.build_py import build_py
setup(
name = 'git_remote_helpers',
version = '0.1.0',
@ -14,4 +23,5 @@ setup(
url = 'http://www.git-scm.com/',
package_dir = {'git_remote_helpers': ''},
packages = ['git_remote_helpers', 'git_remote_helpers.git'],
cmdclass = {'build_py': build_py},
)