def test_prompt_when_exceeded_repeats(self): self.repeatsRemaining = 2 def mock_raw_input(message): self.repeatsRemaining -= 1 return None self.assertIsNone(User.prompt("input", repeat=self.repeatsRemaining, raw_input=mock_raw_input))
def test_prompt_when_exceeded_repeats(self): self.repeatsRemaining = 2 def mock_raw_input(message): self.repeatsRemaining -= 1 return None self.assertEqual(User.prompt("input", repeat=self.repeatsRemaining, raw_input=mock_raw_input), None)
def test_prompt_repeat(self): self.repeatsRemaining = 2 def mock_raw_input(message): self.repeatsRemaining -= 1 if not self.repeatsRemaining: return UserTest.example_user_response return None self.assertEqual(User.prompt("input", repeat=self.repeatsRemaining, raw_input=mock_raw_input), UserTest.example_user_response)
def test_prompt_when_exceeded_repeats(self): self.repeats_remaining = 2 def mock_raw_input(_): self.repeats_remaining -= 1 return None self.assertIsNone( User.prompt('input', repeat=self.repeats_remaining, input_func=mock_raw_input))
def test_prompt_repeat(self): self.repeats_remaining = 2 def mock_raw_input(_): self.repeats_remaining -= 1 if not self.repeats_remaining: return 'example user response' return None self.assertEqual( User.prompt('input', repeat=self.repeats_remaining, input_func=mock_raw_input), 'example user response')
def prompt_for_bug_title_and_comment(self): bug_title = User.prompt("Bug title: ") print "Bug comment (hit ^D on blank line to end):" lines = sys.stdin.readlines() try: sys.stdin.seek(0, os.SEEK_END) except IOError: # Cygwin raises an Illegal Seek (errno 29) exception when the above # seek() call is made. Ignoring it seems to cause no harm. # FIXME: Figure out a way to get avoid the exception in the first # place. pass comment_text = "".join(lines) return (bug_title, comment_text)
def commit_with_message(self, message, username=None): if self.dryrun: # Return a string which looks like a commit so that things which parse this output will succeed. return "Dry run, no commit.\nCommitted revision 0." svn_commit_args = ["svn", "commit"] if not username and not self.has_authorization_for_realm(): username = User.prompt("%s login: "******"You need to specify the username on %s to perform the commit as." % self.svn_server_host) if username: svn_commit_args.extend(["--username", username]) svn_commit_args.extend(["-m", message]) # FIXME: Should this use cwd=self.checkout_root? return run_command(svn_commit_args, error_handler=commit_error_handler)
def commit_with_message(self, message, username=None, git_commit=None, squash=None): # squash and git-commit are not used by SVN. if self.dryrun: # Return a string which looks like a commit so that things which parse this output will succeed. return "Dry run, no commit.\nCommitted revision 0." svn_commit_args = ["svn", "commit"] if not username and not self.has_authorization_for_realm(): username = User.prompt("%s login: "******"You need to specify the username on %s to perform the commit as." % self.svn_server_host) if username: svn_commit_args.extend(["--username", username]) svn_commit_args.extend(["-m", message]) # FIXME: Should this use cwd=self.checkout_root? return run_command(svn_commit_args, error_handler=commit_error_handler)
def read_credentials(self): username, password = self._credentials_from_environment() # FIXME: We don't currently support pulling the username from one # source and the password from a separate source. if not username or not password: username, password = self._credentials_from_git() if not username or not password: username, password = self._credentials_from_keychain(username) if username and not password and self._keyring: password = self._keyring.get_password(self.host, username) if not username: username = User.prompt("%s login: "******"%s password for %s: " % (self.host, username)) self._offer_to_store_credentials_in_keyring(username, password) return (username, password)
class Credentials(object): def __init__(self, host, git_prefix=None, executive=None, cwd=os.getcwd()): self.host = host self.git_prefix = "%s." % git_prefix if git_prefix else "" self.executive = executive or Executive() self.cwd = cwd def _credentials_from_git(self): return [ Git.read_git_config(self.git_prefix + "username"), Git.read_git_config(self.git_prefix + "password") ] def _keychain_value_with_label(self, label, source_text): match = re.search("%s\"(?P<value>.+)\"" % label, source_text, re.MULTILINE) if match: return match.group('value') def _is_mac_os_x(self): return platform.mac_ver()[0] def _parse_security_tool_output(self, security_output): username = self._keychain_value_with_label("^\s*\"acct\"<blob>=", security_output) password = self._keychain_value_with_label("^password: "******"/usr/bin/security", "find-internet-password", "-g", "-s", self.host, ] if username: security_command += ["-a", username] log("Reading Keychain for %s account and password. " "Click \"Allow\" to continue..." % self.host) try: return self.executive.run_command(security_command) except ScriptError: # Failed to either find a keychain entry or somekind of OS-related # error occured (for instance, couldn't find the /usr/sbin/security # command). log("Could not find a keychain entry for %s." % self.host) return None def _credentials_from_keychain(self, username=None): if not self._is_mac_os_x(): return [username, None] security_output = self._run_security_tool(username) if security_output: return self._parse_security_tool_output(security_output) else: return [None, None] def read_credentials(self): username = None password = None try: if Git.in_working_directory(self.cwd): (username, password) = self._credentials_from_git() except OSError, e: # Catch and ignore OSError exceptions such as "no such file # or directory" (OSError errno 2), which imply that the Git # command cannot be found/is not installed. pass if not username or not password: (username, password) = self._credentials_from_keychain(username) if not username: username = User.prompt("%s login: "******"%s password for %s: " % (self.host, username)) return [username, password]