sync: Support manual authentication to the manifest server

Add two new command line options, -u/--manifest-server-username and
-p/--manifest-server-password, which can be used to specify a username
and password to authenticate to the manifest server when using the
-s/--smart-sync or -t/--smart-tag option.

If -u and -p are not specified when using the -s or -t option, use
authentication credentials from the .netrc file (if there are any).

Authentication credentials from -u/-p or .netrc are not used if the
manifest server specified in the manifest file already includes
credentials.

Change-Id: I6cf9540d28f6cef64c5694e8928cfe367a71d28d
This commit is contained in:
David Pursehouse 2012-09-14 10:31:42 +09:00 committed by Gerrit Code Review
parent e00aa6b923
commit cf76b1bcec

View File

@ -83,6 +83,18 @@ build as specified by the manifest-server element in the current
manifest. The -t/--smart-tag option is similar and allows you to manifest. The -t/--smart-tag option is similar and allows you to
specify a custom tag/label. specify a custom tag/label.
The -u/--manifest-server-username and -p/--manifest-server-password
options can be used to specify a username and password to authenticate
with the manifest server when using the -s or -t option.
If -u and -p are not specified when using the -s or -t option, '%prog'
will attempt to read authentication credentials for the manifest server
from the user's .netrc file.
'%prog' will not use authentication credentials from -u/-p or .netrc
if the manifest server specified in the manifest file already includes
credentials.
The -f/--force-broken option can be used to proceed with syncing The -f/--force-broken option can be used to proceed with syncing
other projects if a project sync fails. other projects if a project sync fails.
@ -159,6 +171,12 @@ later is required to fix a server side protocol bug.
p.add_option('-t', '--smart-tag', p.add_option('-t', '--smart-tag',
dest='smart_tag', action='store', dest='smart_tag', action='store',
help='smart sync using manifest from a known tag') help='smart sync using manifest from a known tag')
p.add_option('-u', '--manifest-server-username', action='store',
dest='manifest_server_username',
help='username to authenticate with the manifest server')
p.add_option('-p', '--manifest-server-password', action='store',
dest='manifest_server_password',
help='password to authenticate with the manifest server')
g = p.add_option_group('repo Version options') g = p.add_option_group('repo Version options')
g.add_option('--no-repo-verify', g.add_option('--no-repo-verify',
@ -358,6 +376,14 @@ uncommitted changes are present' % project.relpath
if opt.manifest_name and opt.smart_tag: if opt.manifest_name and opt.smart_tag:
print >>sys.stderr, 'error: cannot combine -m and -t' print >>sys.stderr, 'error: cannot combine -m and -t'
sys.exit(1) sys.exit(1)
if opt.manifest_server_username or opt.manifest_server_password:
if not (opt.smart_sync or opt.smart_tag):
print >>sys.stderr, 'error: -u and -p may only be combined with ' \
'-s or -t'
sys.exit(1)
if None in [opt.manifest_server_username, opt.manifest_server_password]:
print >>sys.stderr, 'error: both -u and -p must be given'
sys.exit(1)
if opt.manifest_name: if opt.manifest_name:
self.manifest.Override(opt.manifest_name) self.manifest.Override(opt.manifest_name)
@ -369,29 +395,36 @@ uncommitted changes are present' % project.relpath
sys.exit(1) sys.exit(1)
manifest_server = self.manifest.manifest_server manifest_server = self.manifest.manifest_server
if not '@' in manifest_server: if not '@' in manifest_server:
try: username = None
info = netrc.netrc() password = None
except IOError: if opt.manifest_server_username and opt.manifest_server_password:
print >>sys.stderr, '.netrc file does not exist or could not be opened' username = opt.manifest_server_username
password = opt.manifest_server_password
else: else:
try: try:
parse_result = urlparse.urlparse(manifest_server) info = netrc.netrc()
if parse_result.hostname: except IOError:
username, _account, password = \ print >>sys.stderr, '.netrc file does not exist or could not be opened'
info.authenticators(parse_result.hostname)
except TypeError:
# TypeError is raised when the given hostname is not present
# in the .netrc file.
print >>sys.stderr, 'No credentials found for %s in .netrc' % \
parse_result.hostname
except netrc.NetrcParseError as e:
print >>sys.stderr, 'Error parsing .netrc file: %s' % e
else: else:
if (username and password): try:
manifest_server = manifest_server.replace('://', '://%s:%s@' % parse_result = urlparse.urlparse(manifest_server)
(username, password), if parse_result.hostname:
1) username, _account, password = \
info.authenticators(parse_result.hostname)
except TypeError:
# TypeError is raised when the given hostname is not present
# in the .netrc file.
print >>sys.stderr, 'No credentials found for %s in .netrc' % \
parse_result.hostname
except netrc.NetrcParseError, e:
print >>sys.stderr, 'Error parsing .netrc file: %s' % e
if (username and password):
manifest_server = manifest_server.replace('://', '://%s:%s@' %
(username, password),
1)
try: try:
server = xmlrpclib.Server(manifest_server) server = xmlrpclib.Server(manifest_server)