# 2007 Trolltech ASA
# License: MIT <http://www.opensource.org/licenses/mit-license.php>
#
+# pylint: disable=invalid-name,missing-docstring,too-many-arguments,broad-except
+# pylint: disable=no-self-use,wrong-import-position,consider-iterating-dictionary
+# pylint: disable=wrong-import-order,unused-import,too-few-public-methods
+# pylint: disable=too-many-lines,ungrouped-imports,fixme,too-many-locals
+# pylint: disable=line-too-long,bad-whitespace,superfluous-parens
+# pylint: disable=too-many-statements,too-many-instance-attributes
+# pylint: disable=too-many-branches,too-many-nested-blocks
+#
import sys
- if sys.hexversion < 0x02040000:
- # The limiter is the subprocess module
- sys.stderr.write("git-p4: requires Python 2.4 or later.\n")
+ if sys.version_info.major < 3 and sys.version_info.minor < 7:
+ sys.stderr.write("git-p4: requires Python 2.7 or later.\n")
sys.exit(1)
import os
import optparse
sys.stderr.write(msg + "\n")
sys.exit(1)
- response = raw_input(prompt_text).strip().lower()
+def prompt(prompt_text):
+ """ Prompt the user to choose one of the choices
+
+ Choices are identified in the prompt_text by square brackets around
+ a single letter option.
+ """
+ choices = set(m.group(1) for m in re.finditer(r"\[(.)\]", prompt_text))
+ while True:
++ response = input(prompt_text).strip().lower()
+ if not response:
+ continue
+ response = response[0]
+ if response in choices:
+ return response
+
+ # We need different encoding/decoding strategies for text data being passed
+ # around in pipes depending on python version
+ if bytes is not str:
+ # For python3, always encode and decode as appropriate
+ def decode_text_stream(s):
+ return s.decode() if isinstance(s, bytes) else s
+ def encode_text_stream(s):
+ return s.encode() if isinstance(s, str) else s
+ else:
+ # For python2.7, pass read strings as-is, but also allow writing unicode
+ def decode_text_stream(s):
+ return s
+ def encode_text_stream(s):
+ return s.encode('utf_8') if isinstance(s, unicode) else s
+
+ def decode_path(path):
+ """Decode a given string (bytes or otherwise) using configured path encoding options
+ """
+ encoding = gitConfig('git-p4.pathEncoding') or 'utf_8'
+ if bytes is not str:
+ return path.decode(encoding, errors='replace') if isinstance(path, bytes) else path
+ else:
+ try:
+ path.decode('ascii')
+ except:
+ path = path.decode(encoding, errors='replace')
+ if verbose:
+ print('Path with non-ASCII characters detected. Used {} to decode: {}'.format(encoding, path))
+ return path
+
def write_pipe(c, stdin):
if verbose:
sys.stderr.write('Writing pipe: %s\n' % str(c))
self.gitStream = self.importProcess.stdin
self.gitError = self.importProcess.stderr
+ if bytes is not str:
+ # Wrap gitStream.write() so that it can be called using `str` arguments
+ def make_encoded_write(write):
+ def encoded_write(s):
+ return write(s.encode() if isinstance(s, str) else s)
+ return encoded_write
+
+ self.gitStream.write = make_encoded_write(self.gitStream.write)
+
def closeStreams(self):
+ if self.gitStream is None:
+ return
self.gitStream.close()
if self.importProcess.wait() != 0:
die("fast-import failed: %s" % self.gitError.read())