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:
parent
56c731f34e
commit
3e23f97451
@ -15,13 +15,13 @@
|
||||
|
||||
# Synchronize Gerrit users from Launchpad.
|
||||
|
||||
import os
|
||||
import sys
|
||||
import fcntl
|
||||
import uuid
|
||||
import subprocess
|
||||
import logging
|
||||
import logging.config
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
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
|
||||
import pkg_resources
|
||||
|
||||
import StringIO
|
||||
import ConfigParser
|
||||
import argparse
|
||||
import ConfigParser
|
||||
import MySQLdb
|
||||
import StringIO
|
||||
|
||||
from launchpadlib.launchpad import Launchpad
|
||||
from launchpadlib.uris import LPNET_SERVICE_ROOT
|
||||
@ -57,24 +57,30 @@ except IOError:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('user', help='The gerrit admin user')
|
||||
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('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()
|
||||
|
||||
GERRIT_USER = options.user
|
||||
GERRIT_CONFIG = os.environ.get('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')
|
||||
GERRIT_SSH_KEY = options.ssh_key
|
||||
GERRIT_CACHE_DIR = os.path.expanduser(os.environ.get('GERRIT_CACHE_DIR',
|
||||
'~/.launchpadlib/cache'))
|
||||
GERRIT_CREDENTIALS = os.path.expanduser(os.environ.get('GERRIT_CREDENTIALS',
|
||||
GERRIT_CREDENTIALS = os.path.expanduser(os.environ.get(
|
||||
'GERRIT_CREDENTIALS',
|
||||
'~/.launchpadlib/creds'))
|
||||
GERRIT_BACKUP_PATH = os.environ.get('GERRIT_BACKUP_PATH',
|
||||
'/home/gerrit2/dbupdates')
|
||||
|
||||
|
||||
def setup_logging():
|
||||
if 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)
|
||||
logging.config.fileConfig(fp)
|
||||
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()
|
||||
log = logging.getLogger('gerrit_user_sync')
|
||||
@ -95,18 +102,20 @@ for check_path in (os.path.dirname(GERRIT_CACHE_DIR),
|
||||
log.info('mkdir ' + check_path)
|
||||
os.makedirs(check_path)
|
||||
|
||||
|
||||
def get_broken_config(filename):
|
||||
""" gerrit config ini files are broken and have leading tabs """
|
||||
text = ""
|
||||
with open(filename,"r") as conf:
|
||||
with open(filename, "r") as conf:
|
||||
for line in conf.readlines():
|
||||
text = "%s%s" % (text, line.lstrip())
|
||||
|
||||
fp = StringIO.StringIO(text)
|
||||
c=ConfigParser.ConfigParser()
|
||||
c = ConfigParser.ConfigParser()
|
||||
c.readfp(fp)
|
||||
return c
|
||||
|
||||
|
||||
def get_type(in_type):
|
||||
if in_type == "RSA":
|
||||
return "ssh-rsa"
|
||||
@ -117,8 +126,8 @@ gerrit_config = get_broken_config(GERRIT_CONFIG)
|
||||
secure_config = get_broken_config(GERRIT_SECURE_CONFIG)
|
||||
|
||||
DB_USER = gerrit_config.get("database", "username")
|
||||
DB_PASS = secure_config.get("database","password")
|
||||
DB_DB = gerrit_config.get("database","database")
|
||||
DB_PASS = secure_config.get("database", "password")
|
||||
DB_DB = gerrit_config.get("database", "database")
|
||||
|
||||
db_backup_file = "%s.%s.sql" % (DB_DB, datetime.isoformat(datetime.now()))
|
||||
db_backup_path = os.path.join(GERRIT_BACKUP_PATH, db_backup_file)
|
||||
@ -130,15 +139,16 @@ if retval != 0:
|
||||
sys.exit(retval)
|
||||
|
||||
log.info('Connect to mysql DB')
|
||||
conn = MySQLdb.connect(user = DB_USER, passwd = DB_PASS, db = DB_DB)
|
||||
conn = MySQLdb.connect(user=DB_USER, passwd=DB_PASS, db=DB_DB)
|
||||
cur = conn.cursor()
|
||||
|
||||
log.info('Connecting to launchpad')
|
||||
launchpad = Launchpad.login_with('Gerrit User Sync', LPNET_SERVICE_ROOT,
|
||||
GERRIT_CACHE_DIR,
|
||||
credentials_file = GERRIT_CREDENTIALS)
|
||||
credentials_file=GERRIT_CREDENTIALS)
|
||||
log.info('Connected to launchpad')
|
||||
|
||||
|
||||
def get_sub_teams(team, have_teams):
|
||||
for sub_team in launchpad.people[team].sub_teams:
|
||||
if sub_team.name not in have_teams:
|
||||
@ -151,11 +161,11 @@ log.info('Getting teams')
|
||||
teams_todo = get_sub_teams(options.root_team, [])
|
||||
|
||||
log.info('Listing projects')
|
||||
users={}
|
||||
groups={}
|
||||
groups_in_groups={}
|
||||
group_implies_groups={}
|
||||
group_ids={}
|
||||
users = {}
|
||||
groups = {}
|
||||
groups_in_groups = {}
|
||||
group_implies_groups = {}
|
||||
group_ids = {}
|
||||
projects = subprocess.check_output(['/usr/bin/ssh', '-p', '29418',
|
||||
'-i', GERRIT_SSH_KEY,
|
||||
'-l', GERRIT_USER, 'localhost',
|
||||
@ -163,7 +173,6 @@ projects = subprocess.check_output(['/usr/bin/ssh', '-p', '29418',
|
||||
|
||||
log.info('Examining teams')
|
||||
for team_todo in teams_todo:
|
||||
|
||||
team = launchpad.people[team_todo]
|
||||
groups[team.name] = team.display_name
|
||||
|
||||
@ -177,7 +186,6 @@ for team_todo in teams_todo:
|
||||
groups_in_groups[team.name] = group_in_group
|
||||
|
||||
for detail in team.members_details:
|
||||
|
||||
user = None
|
||||
|
||||
# detail.self_link ==
|
||||
@ -187,7 +195,6 @@ for team_todo in teams_todo:
|
||||
if users.has_key(login):
|
||||
user = users[login]
|
||||
else:
|
||||
|
||||
user = dict(add_groups=[])
|
||||
|
||||
status = detail.status
|
||||
@ -213,7 +220,7 @@ for (group_name, group_display_name) in groups.items():
|
||||
group_name):
|
||||
group_ids[group_name] = cur.fetchall()[0][0]
|
||||
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")
|
||||
group_id = cur.fetchall()[0][0]
|
||||
|
||||
@ -275,13 +282,13 @@ if DEBUG:
|
||||
return group_name
|
||||
|
||||
print "groups in groups"
|
||||
for (k,v) in groups_in_groups.items():
|
||||
for (k, v) in groups_in_groups.items():
|
||||
print k, v
|
||||
|
||||
print "group_imples_groups"
|
||||
for (k, v) in group_implies_groups.items():
|
||||
print get_group_name(k)
|
||||
new_groups=[]
|
||||
new_groups = []
|
||||
for val in v:
|
||||
new_groups.append(get_group_name(val))
|
||||
print "\t", new_groups
|
||||
@ -294,23 +301,29 @@ for (username, user_details) in users.items():
|
||||
if cur.execute("""select account_id from account_external_ids where
|
||||
external_id in (%s)""", ("username:%s" % username)):
|
||||
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:
|
||||
# We need details
|
||||
if not member.is_team:
|
||||
|
||||
openid_consumer = consumer.Consumer(dict(id=randomString(16, '0123456789abcdef')), None)
|
||||
openid_request = openid_consumer.begin("https://launchpad.net/~%s" % member.name)
|
||||
user_details['openid_external_id'] = openid_request.endpoint.getLocalID()
|
||||
openid_consumer = consumer.Consumer(
|
||||
dict(id=randomString(16, '0123456789abcdef')),
|
||||
None)
|
||||
openid_request = openid_consumer.begin(
|
||||
"https://launchpad.net/~%s" % member.name)
|
||||
user_details['openid_external_id'] = \
|
||||
openid_request.endpoint.getLocalID()
|
||||
|
||||
# Handle username change
|
||||
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]
|
||||
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
|
||||
set external_id=%s
|
||||
set external_id = %s
|
||||
where external_id like 'username%%'
|
||||
and account_id = %s""",
|
||||
('username:%s' % username, account_id))
|
||||
@ -323,51 +336,66 @@ for (username, user_details) in users.items():
|
||||
user_details['email'] = email
|
||||
|
||||
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")
|
||||
account_id = cur.fetchall()[0][0]
|
||||
|
||||
cur.execute("""insert into accounts (account_id, full_name, preferred_email) values
|
||||
(%s, %s, %s)""", (account_id, username, user_details['email']))
|
||||
cur.execute("""insert into accounts
|
||||
(account_id, full_name, preferred_email)
|
||||
values (%s, %s, %s)""",
|
||||
(account_id, username, user_details['email']))
|
||||
|
||||
# account_external_ids
|
||||
## external_id
|
||||
if not cur.execute("""select account_id from account_external_ids
|
||||
where account_id = %s and external_id = %s""",
|
||||
(account_id, user_details['openid_external_id'])):
|
||||
if not cur.execute("""select account_id
|
||||
from account_external_ids
|
||||
where account_id = %s
|
||||
and external_id = %s""",
|
||||
(account_id,
|
||||
user_details['openid_external_id'])):
|
||||
cur.execute("""insert into account_external_ids
|
||||
(account_id, email_address, external_id)
|
||||
values (%s, %s, %s)""",
|
||||
(account_id, user_details['email'], user_details['openid_external_id']))
|
||||
if not cur.execute("""select account_id from account_external_ids
|
||||
where account_id = %s and external_id = %s""",
|
||||
(account_id, user_details['email'],
|
||||
user_details['openid_external_id']))
|
||||
if not cur.execute("""select account_id
|
||||
from account_external_ids
|
||||
where account_id = %s
|
||||
and external_id = %s""",
|
||||
(account_id, "username:%s" % username)):
|
||||
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))
|
||||
|
||||
if user_details.get('email', None) is not None:
|
||||
if not cur.execute("""select account_id from account_external_ids
|
||||
where account_id = %s and external_id = %s""",
|
||||
(account_id, "mailto:%s" % user_details['email'])):
|
||||
if not cur.execute("""select account_id
|
||||
from account_external_ids
|
||||
where account_id = %s
|
||||
and external_id = %s""",
|
||||
(account_id, "mailto:%s" %
|
||||
user_details['email'])):
|
||||
cur.execute("""insert into account_external_ids
|
||||
(account_id, email_address, external_id)
|
||||
values (%s, %s, %s)""",
|
||||
(account_id, user_details['email'], "mailto:%s" %
|
||||
(account_id,
|
||||
user_details['email'],
|
||||
"mailto:%s" %
|
||||
user_details['email']))
|
||||
|
||||
if account_id is not None:
|
||||
# account_ssh_keys
|
||||
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']:
|
||||
|
||||
cur.execute("""select ssh_public_key from account_ssh_keys where
|
||||
account_id = %s""", account_id)
|
||||
db_keys = [r[0].strip() for r in cur.fetchall()]
|
||||
if key.strip() not in db_keys:
|
||||
|
||||
cur.execute("""select max(seq)+1 from account_ssh_keys
|
||||
where account_id = %s""", account_id)
|
||||
seq = cur.fetchall()[0][0]
|
||||
@ -388,7 +416,8 @@ for (username, user_details) in users.items():
|
||||
groups_to_rm = {}
|
||||
|
||||
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.append(group_ids[group])
|
||||
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.endswith("-core"):
|
||||
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 not cur.execute("""select account_id
|
||||
from account_project_watches
|
||||
|
Loading…
x
Reference in New Issue
Block a user