Use argparse

This commit is contained in:
James Eagan 2023-03-06 18:48:21 +01:00
parent 831eb193aa
commit bf15188573

View file

@ -13,7 +13,6 @@ import gitlab
import sys import sys
GITLAB_URL='https://gitlab.telecom-paris.fr' GITLAB_URL='https://gitlab.telecom-paris.fr'
PRIVATE_TOKEN=sys.argv[3] if len(sys.argv) > 3 else ''
def readNames(fileName): def readNames(fileName):
result = [] result = []
@ -33,6 +32,7 @@ def readNames(fileName):
return result return result
def lookupUser(email, gitlab): def lookupUser(email, gitlab):
""" Return the first found user; throw if not found """
return gitlab.users.list(search=email)[0] return gitlab.users.list(search=email)[0]
def createProject(name, groupId, gitlab): def createProject(name, groupId, gitlab):
@ -43,16 +43,14 @@ def addProjectMember(project, userId, accessLevel):
return project.members.create({'user_id': userId, return project.members.create({'user_id': userId,
'access_level': accessLevel}) 'access_level': accessLevel})
def makeProjectsInGroupId(groupId, csvFile): def makeProjectsInGroupId(groupId, csvFile, token, options={}):
api = gitlab.Gitlab(GITLAB_URL, PRIVATE_TOKEN) print(groupId, csvFile, options); return
api = gitlab.Gitlab(GITLAB_URL, token)
for (lastName, firstName, email) in readNames(csvFile): for (lastName, firstName, email) in readNames(csvFile):
try: try:
# if email.endswith('@telecom-paris.fr'):
# email = email.split('@')[0] + "@telecom-paristech.fr"
user = lookupUser(email, api) user = lookupUser(email, api)
print("::: Creating project for {} {} <{}>…".format( print("::: Creating project for {} {} <{}> -- {} ({})…".format(
lastName, firstName, email, user.id)) lastName, firstName, email, user.name, user.id))
print("::: Found {} ({})".format(user.name, user.id))
project = createProject('{} {}'.format(lastName, firstName), project = createProject('{} {}'.format(lastName, firstName),
groupId, api) groupId, api)
member = addProjectMember(project, user.id, member = addProjectMember(project, user.id,
@ -64,15 +62,29 @@ def makeProjectsInGroupId(groupId, csvFile):
print >> sys.stderr, "!!! Error creating project for {}: {}".format(email, e) print >> sys.stderr, "!!! Error creating project for {}: {}".format(email, e)
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) != 4: import argparse
print("*** Usage: python {} <csv> <groupId> <token>".format(sys.argv[0]))
print("*** csv: a CSV file exported from Synapses.") # print("*** Usage: python {} <csv> <groupId> <token>".format(sys.argv[0]))
print("*** The first three columns are assumed to be ") # print("*** csv: a CSV file exported from Synapses.")
print("*** last name, first name, email address.") # print("*** The first three columns are assumed to be ")
print("*** groupId: The GitLab id where all the projects will") # print("*** last name, first name, email address.")
print("*** be added.") # print("*** groupId: The GitLab id where all the projects will")
print("*** token: A GitLab API token as created at ") # print("*** be added.")
print("*** {}/-/profile/personal_access_tokens".format(GITLAB_URL)) # print("*** token: A GitLab API token as created at ")
print("*** Be sure to enable API scope.") # print("*** {}/-/profile/personal_access_tokens".format(GITLAB_URL))
sys.exit(1) # print("*** Be sure to enable API scope.")
makeProjectsInGroupId(sys.argv[2], sys.argv[1]) # sys.exit(1)
def parse_args():
parser = argparse.ArgumentParser(description="Create GitLab repositories")
parser.add_argument("csv", help="CSV file as exported from Synapses. The first three columns are assumed to be last name, first name, email address.")
parser.add_argument("groupId", help="The GitLab id where all projects will be added.")
parser.add_argument("-t", "--token", help="GitLab API token as created at {}/-/profile/personal_access_tokens".format(GITLAB_URL))
# TODO: parser.add_argument("--extract-groups", help="Extract groups from the fourth column in the CSV file", action="store_true")
args = parser.parse_args()
return args
args = parse_args()
makeProjectsInGroupId(args.groupId, args.csv, args.token, args)