Indentation fix

Fix several formatting issues following the guideline of PEP8:
1. List the import statements in alphabetical order.
2. Fix a lot of indentation issues.
3. Adjust the length of several lines.
4. Other PEP8 issues fixed.

Change-Id: I8fefa4db882d4fb72146052852a27b6994fc5d5b
Reviewed-on: https://review.openstack.org/12250
Reviewed-by: Clark Boylan <clark.boylan@gmail.com>
Reviewed-by: James E. Blair <corvus@inaugust.com>
Approved: Monty Taylor <mordred@inaugust.com>
Reviewed-by: Monty Taylor <mordred@inaugust.com>
Tested-by: Jenkins
This commit is contained in:
Ray Chen 2012-08-31 18:00:15 +08:00 committed by Jenkins
parent 56c731f34e
commit 3e23f97451

View File

@ -15,13 +15,13 @@
# Synchronize Gerrit users from Launchpad. # Synchronize Gerrit users from Launchpad.
import os
import sys
import fcntl import fcntl
import uuid
import subprocess
import logging import logging
import logging.config import logging.config
import os
import subprocess
import sys
import uuid
from datetime import datetime from datetime import datetime
@ -30,10 +30,10 @@ from datetime import datetime
# so if we head it off at the pass, we can skip cronspam # so if we head it off at the pass, we can skip cronspam
import pkg_resources import pkg_resources
import StringIO
import ConfigParser
import argparse import argparse
import ConfigParser
import MySQLdb import MySQLdb
import StringIO
from launchpadlib.launchpad import Launchpad from launchpadlib.launchpad import Launchpad
from launchpadlib.uris import LPNET_SERVICE_ROOT from launchpadlib.uris import LPNET_SERVICE_ROOT
@ -57,24 +57,30 @@ except IOError:
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('user', help='The gerrit admin user') parser.add_argument('user', help='The gerrit admin user')
parser.add_argument('ssh_key', help='The gerrit admin SSH key file') parser.add_argument('ssh_key', help='The gerrit admin SSH key file')
parser.add_argument('site', help='The site in use (typically openstack or stackforge)') parser.add_argument('site',
help='The site in use (typically openstack or stackforge)')
parser.add_argument('root_team', help='The root launchpad team to pull from') parser.add_argument('root_team', help='The root launchpad team to pull from')
parser.add_argument('log_config', default=None, help='Path to file containing logging config') parser.add_argument('log_config',
default=None,
help='Path to file containing logging config')
options = parser.parse_args() options = parser.parse_args()
GERRIT_USER = options.user GERRIT_USER = options.user
GERRIT_CONFIG = os.environ.get('GERRIT_CONFIG', GERRIT_CONFIG = os.environ.get('GERRIT_CONFIG',
'/home/gerrit2/review_site/etc/gerrit.config') '/home/gerrit2/review_site/etc/gerrit.config')
GERRIT_SECURE_CONFIG = os.environ.get('GERRIT_SECURE_CONFIG', GERRIT_SECURE_CONFIG = os.environ.get(
'GERRIT_SECURE_CONFIG',
'/home/gerrit2/review_site/etc/secure.config') '/home/gerrit2/review_site/etc/secure.config')
GERRIT_SSH_KEY = options.ssh_key GERRIT_SSH_KEY = options.ssh_key
GERRIT_CACHE_DIR = os.path.expanduser(os.environ.get('GERRIT_CACHE_DIR', GERRIT_CACHE_DIR = os.path.expanduser(os.environ.get('GERRIT_CACHE_DIR',
'~/.launchpadlib/cache')) '~/.launchpadlib/cache'))
GERRIT_CREDENTIALS = os.path.expanduser(os.environ.get('GERRIT_CREDENTIALS', GERRIT_CREDENTIALS = os.path.expanduser(os.environ.get(
'GERRIT_CREDENTIALS',
'~/.launchpadlib/creds')) '~/.launchpadlib/creds'))
GERRIT_BACKUP_PATH = os.environ.get('GERRIT_BACKUP_PATH', GERRIT_BACKUP_PATH = os.environ.get('GERRIT_BACKUP_PATH',
'/home/gerrit2/dbupdates') '/home/gerrit2/dbupdates')
def setup_logging(): def setup_logging():
if options.log_config: if options.log_config:
fp = os.path.expanduser(options.log_config) fp = os.path.expanduser(options.log_config)
@ -82,7 +88,8 @@ def setup_logging():
raise Exception("Unable to read logging config file at %s" % fp) raise Exception("Unable to read logging config file at %s" % fp)
logging.config.fileConfig(fp) logging.config.fileConfig(fp)
else: else:
logging.basicConfig(filename='/home/gerrit2/gerrit_user_sync.log', level=logging.DEBUG) logging.basicConfig(filename='/home/gerrit2/gerrit_user_sync.log',
level=logging.DEBUG)
setup_logging() setup_logging()
log = logging.getLogger('gerrit_user_sync') log = logging.getLogger('gerrit_user_sync')
@ -95,6 +102,7 @@ for check_path in (os.path.dirname(GERRIT_CACHE_DIR),
log.info('mkdir ' + check_path) log.info('mkdir ' + check_path)
os.makedirs(check_path) os.makedirs(check_path)
def get_broken_config(filename): def get_broken_config(filename):
""" gerrit config ini files are broken and have leading tabs """ """ gerrit config ini files are broken and have leading tabs """
text = "" text = ""
@ -107,6 +115,7 @@ def get_broken_config(filename):
c.readfp(fp) c.readfp(fp)
return c return c
def get_type(in_type): def get_type(in_type):
if in_type == "RSA": if in_type == "RSA":
return "ssh-rsa" return "ssh-rsa"
@ -139,6 +148,7 @@ launchpad = Launchpad.login_with('Gerrit User Sync', LPNET_SERVICE_ROOT,
credentials_file=GERRIT_CREDENTIALS) credentials_file=GERRIT_CREDENTIALS)
log.info('Connected to launchpad') log.info('Connected to launchpad')
def get_sub_teams(team, have_teams): def get_sub_teams(team, have_teams):
for sub_team in launchpad.people[team].sub_teams: for sub_team in launchpad.people[team].sub_teams:
if sub_team.name not in have_teams: if sub_team.name not in have_teams:
@ -163,7 +173,6 @@ projects = subprocess.check_output(['/usr/bin/ssh', '-p', '29418',
log.info('Examining teams') log.info('Examining teams')
for team_todo in teams_todo: for team_todo in teams_todo:
team = launchpad.people[team_todo] team = launchpad.people[team_todo]
groups[team.name] = team.display_name groups[team.name] = team.display_name
@ -177,7 +186,6 @@ for team_todo in teams_todo:
groups_in_groups[team.name] = group_in_group groups_in_groups[team.name] = group_in_group
for detail in team.members_details: for detail in team.members_details:
user = None user = None
# detail.self_link == # detail.self_link ==
@ -187,7 +195,6 @@ for team_todo in teams_todo:
if users.has_key(login): if users.has_key(login):
user = users[login] user = users[login]
else: else:
user = dict(add_groups=[]) user = dict(add_groups=[])
status = detail.status status = detail.status
@ -213,7 +220,7 @@ for (group_name, group_display_name) in groups.items():
group_name): group_name):
group_ids[group_name] = cur.fetchall()[0][0] group_ids[group_name] = cur.fetchall()[0][0]
else: else:
cur.execute("""insert into account_group_id (s) values (NULL)"""); cur.execute("""insert into account_group_id (s) values (NULL)""")
cur.execute("select max(s) from account_group_id") cur.execute("select max(s) from account_group_id")
group_id = cur.fetchall()[0][0] group_id = cur.fetchall()[0][0]
@ -294,21 +301,27 @@ for (username, user_details) in users.items():
if cur.execute("""select account_id from account_external_ids where if cur.execute("""select account_id from account_external_ids where
external_id in (%s)""", ("username:%s" % username)): external_id in (%s)""", ("username:%s" % username)):
account_id = cur.fetchall()[0][0] account_id = cur.fetchall()[0][0]
# We have this bad boy - all we need to do is update his group membership # We have this bad boy
# all we need to do is update his group membership
else: else:
# We need details # We need details
if not member.is_team: if not member.is_team:
openid_consumer = consumer.Consumer(dict(id=randomString(16, '0123456789abcdef')), None) openid_consumer = consumer.Consumer(
openid_request = openid_consumer.begin("https://launchpad.net/~%s" % member.name) dict(id=randomString(16, '0123456789abcdef')),
user_details['openid_external_id'] = openid_request.endpoint.getLocalID() None)
openid_request = openid_consumer.begin(
"https://launchpad.net/~%s" % member.name)
user_details['openid_external_id'] = \
openid_request.endpoint.getLocalID()
# Handle username change # Handle username change
if cur.execute("""select account_id from account_external_ids where if cur.execute("""select account_id from account_external_ids where
external_id in (%s)""", user_details['openid_external_id']): external_id in (%s)""",
user_details['openid_external_id']):
account_id = cur.fetchall()[0][0] account_id = cur.fetchall()[0][0]
log.info('Handling username change id %s to %s' % (account_id, username)) log.info('Handling username change id %s to %s' %
(account_id, username))
cur.execute("""update account_external_ids cur.execute("""update account_external_ids
set external_id = %s set external_id = %s
where external_id like 'username%%' where external_id like 'username%%'
@ -323,51 +336,66 @@ for (username, user_details) in users.items():
user_details['email'] = email user_details['email'] = email
log.info('Add %s to Gerrit DB.' % username) log.info('Add %s to Gerrit DB.' % username)
cur.execute("""insert into account_id (s) values (NULL)"""); cur.execute("""insert into account_id (s) values (NULL)""")
cur.execute("select max(s) from account_id") cur.execute("select max(s) from account_id")
account_id = cur.fetchall()[0][0] account_id = cur.fetchall()[0][0]
cur.execute("""insert into accounts (account_id, full_name, preferred_email) values cur.execute("""insert into accounts
(%s, %s, %s)""", (account_id, username, user_details['email'])) (account_id, full_name, preferred_email)
values (%s, %s, %s)""",
(account_id, username, user_details['email']))
# account_external_ids # account_external_ids
## external_id ## external_id
if not cur.execute("""select account_id from account_external_ids if not cur.execute("""select account_id
where account_id = %s and external_id = %s""", from account_external_ids
(account_id, user_details['openid_external_id'])): where account_id = %s
and external_id = %s""",
(account_id,
user_details['openid_external_id'])):
cur.execute("""insert into account_external_ids cur.execute("""insert into account_external_ids
(account_id, email_address, external_id) (account_id, email_address, external_id)
values (%s, %s, %s)""", values (%s, %s, %s)""",
(account_id, user_details['email'], user_details['openid_external_id'])) (account_id, user_details['email'],
if not cur.execute("""select account_id from account_external_ids user_details['openid_external_id']))
where account_id = %s and external_id = %s""", if not cur.execute("""select account_id
from account_external_ids
where account_id = %s
and external_id = %s""",
(account_id, "username:%s" % username)): (account_id, "username:%s" % username)):
cur.execute("""insert into account_external_ids cur.execute("""insert into account_external_ids
(account_id, external_id) values (%s, %s)""", (account_id, external_id)
values (%s, %s)""",
(account_id, "username:%s" % username)) (account_id, "username:%s" % username))
if user_details.get('email', None) is not None: if user_details.get('email', None) is not None:
if not cur.execute("""select account_id from account_external_ids if not cur.execute("""select account_id
where account_id = %s and external_id = %s""", from account_external_ids
(account_id, "mailto:%s" % user_details['email'])): where account_id = %s
and external_id = %s""",
(account_id, "mailto:%s" %
user_details['email'])):
cur.execute("""insert into account_external_ids cur.execute("""insert into account_external_ids
(account_id, email_address, external_id) (account_id, email_address, external_id)
values (%s, %s, %s)""", values (%s, %s, %s)""",
(account_id, user_details['email'], "mailto:%s" % (account_id,
user_details['email'],
"mailto:%s" %
user_details['email'])) user_details['email']))
if account_id is not None: if account_id is not None:
# account_ssh_keys # account_ssh_keys
log.info('Add ssh keys for %s' % username) log.info('Add ssh keys for %s' % username)
user_details['ssh_keys'] = ["%s %s %s" % (get_type(key.keytype), key.keytext, key.comment) for key in member.sshkeys] user_details['ssh_keys'] = ["%s %s %s" %
(get_type(key.keytype),
key.keytext,
key.comment)
for key in member.sshkeys]
for key in user_details['ssh_keys']: for key in user_details['ssh_keys']:
cur.execute("""select ssh_public_key from account_ssh_keys where cur.execute("""select ssh_public_key from account_ssh_keys where
account_id = %s""", account_id) account_id = %s""", account_id)
db_keys = [r[0].strip() for r in cur.fetchall()] db_keys = [r[0].strip() for r in cur.fetchall()]
if key.strip() not in db_keys: if key.strip() not in db_keys:
cur.execute("""select max(seq)+1 from account_ssh_keys cur.execute("""select max(seq)+1 from account_ssh_keys
where account_id = %s""", account_id) where account_id = %s""", account_id)
seq = cur.fetchall()[0][0] seq = cur.fetchall()[0][0]
@ -388,7 +416,8 @@ for (username, user_details) in users.items():
groups_to_rm = {} groups_to_rm = {}
for group in user_details['add_groups']: for group in user_details['add_groups']:
# if you are in the group nova-core, that should also put you in nova # if you are in the group nova-core, that should also put you
# in nova
add_groups = group_implies_groups[group_ids[group]] add_groups = group_implies_groups[group_ids[group]]
add_groups.append(group_ids[group]) add_groups.append(group_ids[group])
for add_group in add_groups: for add_group in add_groups:
@ -417,7 +446,9 @@ for (username, user_details) in users.items():
if os_project_name is not None: if os_project_name is not None:
if os_project_name.endswith("-core"): if os_project_name.endswith("-core"):
os_project_name = os_project_name[:-5] os_project_name = os_project_name[:-5]
os_project_name = "{site}/{project}".format(site=options.site, project=os_project_name) os_project_name = \
"{site}/{project}".format(site=options.site,
project=os_project_name)
if os_project_name in projects: if os_project_name in projects:
if not cur.execute("""select account_id if not cur.execute("""select account_id
from account_project_watches from account_project_watches