comparison mercurial/util.py @ 5798:86f5d8f608b7

fetch: hide authentication details
author Bryan O'Sullivan <bos@serpentine.com>
date Fri, 04 Jan 2008 11:58:27 -0800
parents 0145f9afb0e7
children d852151fb8d4
comparison
equal deleted inserted replaced
5797:7b7f03c7dfa5 5798:86f5d8f608b7
1705 1705
1706 def uirepr(s): 1706 def uirepr(s):
1707 # Avoid double backslash in Windows path repr() 1707 # Avoid double backslash in Windows path repr()
1708 return repr(s).replace('\\\\', '\\') 1708 return repr(s).replace('\\\\', '\\')
1709 1709
1710 def hidepassword(url): 1710 def hidepassword(url, user=True, password=True):
1711 '''replaces the password in the url string by three asterisks (***) 1711 '''hide user credential in a url string'''
1712 1712 scheme, netloc, path, params, query, fragment = urlparse.urlparse(url)
1713 >>> hidepassword('http://www.example.com/some/path#fragment') 1713 netloc = re.sub('([^:]*):([^@]*)@(.*)', r'\1:***@\3', netloc)
1714 'http://www.example.com/some/path#fragment' 1714 return urlparse.urlunparse((scheme, netloc, path, params, query, fragment))
1715 >>> hidepassword('http://me@www.example.com/some/path#fragment') 1715
1716 'http://me@www.example.com/some/path#fragment' 1716 def removeauth(url):
1717 >>> hidepassword('http://me:simplepw@www.example.com/path#frag') 1717 '''remove all authentication information from a url string'''
1718 'http://me:***@www.example.com/path#frag' 1718 scheme, netloc, path, params, query, fragment = urlparse.urlparse(url)
1719 >>> hidepassword('http://me:complex:pw@www.example.com/path#frag') 1719 netloc = netloc[netloc.find('@')+1:]
1720 'http://me:***@www.example.com/path#frag' 1720 return urlparse.urlunparse((scheme, netloc, path, params, query, fragment))
1721 >>> hidepassword('/path/to/repo')
1722 '/path/to/repo'
1723 >>> hidepassword('relative/path/to/repo')
1724 'relative/path/to/repo'
1725 >>> hidepassword('c:\\\\path\\\\to\\\\repo')
1726 'c:\\\\path\\\\to\\\\repo'
1727 >>> hidepassword('c:/path/to/repo')
1728 'c:/path/to/repo'
1729 >>> hidepassword('bundle://path/to/bundle')
1730 'bundle://path/to/bundle'
1731 '''
1732 url_parts = list(urlparse.urlparse(url))
1733 host_with_pw_pattern = re.compile('^([^:]*):([^@]*)@(.*)$')
1734 if host_with_pw_pattern.match(url_parts[1]):
1735 url_parts[1] = re.sub(host_with_pw_pattern, r'\1:***@\3',
1736 url_parts[1])
1737 return urlparse.urlunparse(url_parts)
1738