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
maint
Junio C Hamano 2013-02-04 10:25:34 -08:00
commit 3e515b0d40
6 changed files with 69 additions and 29 deletions

View File

@ -14,7 +14,7 @@ if sys.hexversion < 0x02040000:


def getrevlimit(): def getrevlimit():
var = 'SVNRMAX' var = 'SVNRMAX'
if os.environ.has_key(var): if var in os.environ:
return os.environ[var] return os.environ[var]
return None return None


@ -44,7 +44,7 @@ def writedump(url, lower, upper):


if __name__ == "__main__": if __name__ == "__main__":
if not (len(sys.argv) in (3, 4, 5)): 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) sys.exit(1)
if not sys.argv[1] == 'dump': raise NotImplementedError('only "dump" is suppported.') if not sys.argv[1] == 'dump': raise NotImplementedError('only "dump" is suppported.')
url = sys.argv[2] url = sys.argv[2]

View File

@ -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.importer import GitImporter
from git_remote_helpers.git.non_local import NonLocalGit from git_remote_helpers.git.non_local import NonLocalGit


if sys.hexversion < 0x01050200: if sys.hexversion < 0x02000000:
# os.makedirs() is the limiter # string.encode() is the limiter
sys.stderr.write("git-remote-testgit: requires Python 1.5.2 or later.\n") sys.stderr.write("git-remote-testgit: requires Python 2.0 or later.\n")
sys.exit(1) 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): def get_repo(alias, url):
"""Returns a git repository object initialized for usage. """Returns a git repository object initialized for usage.
""" """
@ -45,7 +61,7 @@ def get_repo(alias, url):
repo.get_head() repo.get_head()


hasher = _digest() hasher = _digest()
hasher.update(repo.path) hasher.update(encode_filepath(repo.path))
repo.hash = hasher.hexdigest() repo.hash = hasher.hexdigest()


repo.get_base_path = lambda base: os.path.join( repo.get_base_path = lambda base: os.path.join(
@ -87,9 +103,9 @@ def do_capabilities(repo, args):
"""Prints the supported capabilities. """Prints the supported capabilities.
""" """


print "import" print("import")
print "export" print("export")
print "refspec refs/heads/*:%s*" % repo.prefix print("refspec refs/heads/*:%s*" % repo.prefix)


dirname = repo.get_base_path(repo.gitdir) dirname = repo.get_base_path(repo.gitdir)


@ -98,11 +114,11 @@ def do_capabilities(repo, args):


path = os.path.join(dirname, 'git.marks') path = os.path.join(dirname, 'git.marks')


print "*export-marks %s" % path print("*export-marks %s" % path)
if os.path.exists(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): def do_list(repo, args):
@ -115,16 +131,16 @@ def do_list(repo, args):


for ref in repo.revs: for ref in repo.revs:
debug("? refs/heads/%s", ref) debug("? refs/heads/%s", ref)
print "? refs/heads/%s" % ref print("? refs/heads/%s" % ref)


if repo.head: if repo.head:
debug("@refs/heads/%s HEAD" % repo.head) debug("@refs/heads/%s HEAD" % repo.head)
print "@refs/heads/%s HEAD" % repo.head print("@refs/heads/%s HEAD" % repo.head)
else: else:
debug("@refs/heads/master HEAD") 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): def update_local_repo(repo):
@ -154,7 +170,7 @@ def do_import(repo, args):
refs = [ref] refs = [ref]


while True: while True:
line = sys.stdin.readline() line = sys.stdin.readline().decode()
if line == '\n': if line == '\n':
break break
if not line.startswith('import '): if not line.startswith('import '):
@ -164,7 +180,7 @@ def do_import(repo, args):
ref = line[7:].strip() ref = line[7:].strip()
refs.append(ref) refs.append(ref)


print "feature done" print("feature done")


if os.environ.get("GIT_REMOTE_TESTGIT_FAILURE"): if os.environ.get("GIT_REMOTE_TESTGIT_FAILURE"):
die('Told to fail') die('Told to fail')
@ -172,7 +188,7 @@ def do_import(repo, args):
repo = update_local_repo(repo) repo = update_local_repo(repo)
repo.exporter.export_repo(repo.gitdir, refs) repo.exporter.export_repo(repo.gitdir, refs)


print "done" print("done")




def do_export(repo, args): def do_export(repo, args):
@ -192,8 +208,8 @@ def do_export(repo, args):
repo.non_local.push(repo.gitdir) repo.non_local.push(repo.gitdir)


for ref in changed: for ref in changed:
print "ok %s" % ref print("ok %s" % ref)
print print('')




COMMANDS = { COMMANDS = {
@ -225,7 +241,7 @@ def read_one_line(repo):


line = sys.stdin.readline() line = sys.stdin.readline()


cmdline = line cmdline = line.decode()


if not cmdline: if not cmdline:
warn("Unexpected EOF") warn("Unexpected EOF")
@ -277,7 +293,11 @@ def main(args):


more = True 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): while (more):
more = read_one_line(repo) more = read_one_line(repo)



View File

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

View File

@ -23,10 +23,16 @@ endif


PYLIBDIR=$(shell $(PYTHON_PATH) -c \ PYLIBDIR=$(shell $(PYTHON_PATH) -c \
"import sys; \ "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) 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) install: $(pysetupfile)
$(PYTHON_PATH) $(pysetupfile) install --prefix $(DESTDIR_SQ)$(prefix) $(PYTHON_PATH) $(pysetupfile) install --prefix $(DESTDIR_SQ)$(prefix)
@ -36,4 +42,4 @@ instlibdir: $(pysetupfile)


clean: clean:
$(QUIET)$(PYTHON_PATH) $(pysetupfile) $(QUIETSETUP) clean -a $(QUIET)$(PYTHON_PATH) $(pysetupfile) $(QUIETSETUP) clean -a
$(RM) *.pyo *.pyc $(RM) *.pyo *.pyc GIT-PYTHON-VERSION

View File

@ -18,13 +18,16 @@ class GitImporter(object):


def get_refs(self, gitdir): def get_refs(self, gitdir):
"""Returns a dictionary with refs. """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"] 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 = {} refs = {}
for line in lines: for line in lines:
value, name = line.split(' ') value, name = line.split(' '.encode('ascii'))
name = name.strip('commit\t') name = name.strip('commit\t'.encode('ascii'))
refs[name] = value refs[name] = value
return refs return refs



View File

@ -4,6 +4,15 @@


from distutils.core import setup 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( setup(
name = 'git_remote_helpers', name = 'git_remote_helpers',
version = '0.1.0', version = '0.1.0',
@ -14,4 +23,5 @@ setup(
url = 'http://www.git-scm.com/', url = 'http://www.git-scm.com/',
package_dir = {'git_remote_helpers': ''}, package_dir = {'git_remote_helpers': ''},
packages = ['git_remote_helpers', 'git_remote_helpers.git'], packages = ['git_remote_helpers', 'git_remote_helpers.git'],
cmdclass = {'build_py': build_py},
) )