Skip to content

Instantly share code, notes, and snippets.

@freyes
Created February 20, 2023 19:58
Show Gist options
  • Save freyes/6d5304937d4fdb3431165d00b1fffe56 to your computer and use it in GitHub Desktop.
Save freyes/6d5304937d4fdb3431165d00b1fffe56 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
"""A script to retrieve a Launchpad token."""
import argparse
import os
import sys
from unittest import mock
from launchpadlib.launchpad import Launchpad
parser = argparse.ArgumentParser(
description = 'Retrieve a Launchpad auth token to store in plain text.',
)
parser.add_argument('-o', '--output', dest='output_file', metavar='FILE',
required=True, help='File to store the generated token')
parser.add_argument('-n', '--name', dest='app_name', metavar='APP',
required=True,
help='Application name to present to Launchpad')
args = parser.parse_args()
def no_credential():
print("Can't proceed without Launchpad credential.")
sys.exit()
with mock.patch.dict(os.environ, {'LP_CREDENTIALS_FILE': args.output_file},
clear=True):
launchpad = Launchpad.login_with(args.app_name, 'production',
version='devel',
credential_save_failed=no_credential)
if os.path.isfile(args.output_file):
print('Credentials stored in %s' % args.output_file)
else:
raise RuntimeError('Credentials file not found: %s' % args.output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment