commit: bf8e1294a8935b1edc8a4e6a0054cc302317a5d8 Author: Devan Franchini <twitch153 <AT> gentoo <DOT> org> AuthorDate: Wed Jun 18 20:39:52 2014 +0000 Commit: Devan Franchini <twitch153 <AT> gentoo <DOT> org> CommitDate: Thu Jun 19 03:51:30 2014 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/layman.git;a=commit;h=bf8e1294
Adds layman-overlay-maker tool This tool has been made to assist users in creating their own layman overlay definitions. --- bin/layman-overlay-maker | 239 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 239 insertions(+) diff --git a/bin/layman-overlay-maker b/bin/layman-overlay-maker new file mode 100755 index 0000000..35cf7a6 --- /dev/null +++ b/bin/layman-overlay-maker @@ -0,0 +1,239 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# File: overlay_maker.py +# +# Creates overlay definitions and writes them to an XML file +# +# Copyright: +# (c) 2014 Devan Franchini +# Distributed under the terms of the GNU General Public License v2 +# +# Author(s): +# Devan Franchini <[email protected]> +# + +#=============================================================================== +# +# Dependencies +# +#------------------------------------------------------------------------------- +from __future__ import unicode_literals + +import layman.overlays.overlay as overlay +import xml.etree.ElementTree as ET + +import sys + +from layman.compatibility import fileopen +from layman.config import BareConfig +from layman.utils import indent + +#py3 +if sys.hexversion >= 0x30200f0: + _UNICODE = 'unicode' +else: + _UNICODE = 'UTF-8' + + +def get_input(msg): + ''' + py2 py3 compatibility function + to obtain user input. + + @params msg: message prompt for user + @rtype str: input from user + ''' + try: + value = raw_input(msg) + except NameError: + value = input(msg) + + return value + +def get_ans(msg): + ''' + Handles yes/no input + + @params msg: message prompt for user + @rtype boolean: reflects whether the user answered yes or no. + ''' + ans = get_input(msg).lower() + + while ans not in ('y', 'yes', 'n', 'no'): + ans = get_input('Please respond with [y/n]: ').lower() + + return ans in ('y', 'yes') + +def write_file(tree): + ''' + Writes overlay file to desired location + + @param tree ElementTree.ElementTree object + ''' + print('') + filename = get_input('Desired overlay file name: ') + filepath = get_input('Desired output path: ') + + if not filename.endswith('.xml'): + filename += ".xml" + + if not filepath.endswith('/'): + filepath += "/" + + destination = filepath + filename + + try: + with fileopen(destination, 'w') as xml: + tree.write(xml, encoding=_UNICODE) + + except IOError as e: + print("Writing XML failed: %(error)s" % ({'error': e})) + +def check_overlay_type(ovl_type): + ''' + Validates overlay type. + + @params ovl_type: overlay type to validate. + @rtype None or str (if overlay type is valid). + ''' + supported_types = ['bzr', 'cvs', 'darcs', 'git', 'g-sorcery', + 'mercurial', 'svn', 'tar'] + + if ovl_type.lower() in supported_types: + return ovl_type.lower() + print('Specified type "%(type)s" not valid.' % ({'type': ovl_type})) + print('Supported types include: %(types)s.' % ({'types': ', '.join(supported_types)})) + return None + +def get_sources(overlay, required): + ''' + Prompts user for possible overlay source + information such as type, url, and branch. + + @params overlay: dict of overlay components + @rtype dict + ''' + ovl_type = None + source_amount = int(get_input('How many sources exist for this overlay?: ')) + + while not ovl_type: + ovl_type = check_overlay_type(get_input('Define overlay\'s type: ')) + + sources = [] + overlay['sources'] = [] + for i in range(1, source_amount + 1): + if source_amount > 1: + sources.append(get_input('Define source[%(i)s] URL: ' % ({'i': str(i)}))) + sources.append(ovl_type) + if 'branch' in required: + sources.append('Define source[%(i)s]\'s branch (if applicable): ' % \ + ({'i': str(i)})) + else: + sources.append('') + else: + sources.append(get_input('Define source URL: ')) + sources.append(ovl_type) + if 'branch' in required: + sources.append('Define source branch (if applicable): ') + else: + sources.append('') + + overlay['sources'].append(sources) + + return overlay + +def get_feeds(overlay): + ''' + Prompts user for any overlay RSS feeds + and returns the overlay dict. + + @params overlay: dict of overlay components + @rtype dict + ''' + feed_amount = int(get_input('How many RSS feeds exist for this overlay?: ')) + feeds = [] + + for i in range(1, feed_amounts + 1): + if feed_amounts > 1: + feeds.append(get_input('Define overlay feed[%(i)s]: ' % ({'i': str(i)}))) + else: + feeds.append(get_input('Define overlay feed: ')) + + overlay['feeds'] = feeds + + return overlay + +def update_required(required): + ''' + Prompts user for optional components and updates + the required components accordingly. + + @params required: List of current required overlay components + @rtype list + ''' + possible_components = ['name', 'description', 'homepage', 'owner', 'quality', + 'priority', 'sources', 'branch', 'irc', 'feed'] + + for possible in possible_components: + if possible not in required: + available = get_ans("Include %(comp)s for this overlay? [y/n]: " % ({'comp': possible})) + if available: + required.append(possible) + + return required + +def get_components(): + ''' + Acquires overlay components via user interface. + + @rtype dict + ''' + overlay = {} + + required = ['name', 'description', 'owner', 'type', 'sources'] + + required = update_required(required) + + for component in required: + + if 'feed' in component: + overlay = get_feeds(overlay) + print('') + + elif 'name' in component: + print('') + overlay['name'] = get_input('Define overlay name: ') + + elif 'owner' in component: + print('') + overlay['owner_name'] = get_input('Define owner name: ') + overlay['owner_email'] = get_input('Define owner email: ') + print('') + + elif 'sources' in component: + overlay = get_sources(overlay, required) + + else: + if 'type' not in component and 'branch' not in component: + overlay[component] = get_input('Define %(comp)s: ' % ({'comp': component})) + + return overlay + +def main(): + config = BareConfig() + repo = ET.Element('repositories', version='1.0', encoding=_UNICODE) + + for x in range(1, int(get_input("How many overlays would you like to create?: "))+1): + print('') + print("Overlay #%(x)s: " % ({'x': str(x)})) + print("~~~~~~~~~~~~~ ") + overlay_dict = get_components() + ovl = overlay.Overlay(config=config, ovl_dict=overlay_dict, ignore=1) + repo.append(ovl.to_xml()) + + indent(repo) + tree = ET.ElementTree(repo) + write_file(tree) + +if __name__ == '__main__': + main()
