Skip to content

Instantly share code, notes, and snippets.

@3v1n0
Last active February 15, 2017 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3v1n0/479ad142eccdd17ad7d0445762dea755 to your computer and use it in GitHub Desktop.
Save 3v1n0/479ad142eccdd17ad7d0445762dea755 to your computer and use it in GitHub Desktop.
snapcraft.cfg generator
#!/usr/bin/python3
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Generates .snapcraft/snapcraft.cfg credentials to upload to the store
#
# Copyright (C) 2017 Marco Trevisan <marco@ubuntu.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
import logging
import os
from snapcraft import storeapi
from snapcraft.internal import load_config
from snapcraft._store import _login
from snapcraft.config import LOCAL_CONFIG_FILENAME
logger = logging.getLogger(__name__)
DEFAULT_CHANNELS = ['edge']
def generate_credentials(packages, channels):
store = storeapi.StoreClient()
if not _login(store, packages=packages, channels=channels, save=False):
raise Exception(
'Cannot continue without logging in successfully.')
os.makedirs(os.path.dirname(LOCAL_CONFIG_FILENAME), exist_ok=True)
with open(LOCAL_CONFIG_FILENAME, 'w') as fd:
store.conf.parser.write(fd)
fd.flush()
parser = argparse.ArgumentParser()
parser.add_argument('--debug', help='Enable debugging', action="store_true")
parser.add_argument('--snap-name', help='The name of the snap')
parser.add_argument(
'--series', default=storeapi.constants.DEFAULT_SERIES,
help='series where to release to')
parser.add_argument(
'--channels', default=','.join(DEFAULT_CHANNELS),
help='Comma-separated list of channels')
args = parser.parse_args()
if args.debug:
logger.setLevel(logging.DEBUG)
series = args.series
snap_name = args.snap_name
if not snap_name:
project_config = load_config()
snap_name = project_config.data['name']
packages = [{'name': snap_name, 'series': series}]
channels = args.channels.split(',')
logging.info(
'Generating credentials to push and release "{}" snaps '
'to "{}" channels in series {}'.format(snap_name, ", ".join(channels), series))
logging.warn(
'The generated "{}" would allow *ANYONE* who can read that file to push '
' to "{}" channels, so please handle it with care and and ensure it\'s '
'properly protected'.format((LOCAL_CONFIG_FILENAME), ", ".join(channels)))
generate_credentials(packages, channels)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment