Gentoo Archives: gentoo-commits

From: Devan Franchini <twitch153@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/layman:gsoc2014 commit in: bin/
Date: Fri, 27 Jun 2014 04:08:04
Message-Id: 1403149890.bf8e1294a8935b1edc8a4e6a0054cc302317a5d8.twitch153@gentoo
1 commit: bf8e1294a8935b1edc8a4e6a0054cc302317a5d8
2 Author: Devan Franchini <twitch153 <AT> gentoo <DOT> org>
3 AuthorDate: Wed Jun 18 20:39:52 2014 +0000
4 Commit: Devan Franchini <twitch153 <AT> gentoo <DOT> org>
5 CommitDate: Thu Jun 19 03:51:30 2014 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/layman.git;a=commit;h=bf8e1294
7
8 Adds layman-overlay-maker tool
9
10 This tool has been made to assist users in creating their own
11 layman overlay definitions.
12
13 ---
14 bin/layman-overlay-maker | 239 +++++++++++++++++++++++++++++++++++++++++++++++
15 1 file changed, 239 insertions(+)
16
17 diff --git a/bin/layman-overlay-maker b/bin/layman-overlay-maker
18 new file mode 100755
19 index 0000000..35cf7a6
20 --- /dev/null
21 +++ b/bin/layman-overlay-maker
22 @@ -0,0 +1,239 @@
23 +#!/usr/bin/python
24 +# -*- coding: utf-8 -*-
25 +# File: overlay_maker.py
26 +#
27 +# Creates overlay definitions and writes them to an XML file
28 +#
29 +# Copyright:
30 +# (c) 2014 Devan Franchini
31 +# Distributed under the terms of the GNU General Public License v2
32 +#
33 +# Author(s):
34 +# Devan Franchini <twitch153@g.o>
35 +#
36 +
37 +#===============================================================================
38 +#
39 +# Dependencies
40 +#
41 +#-------------------------------------------------------------------------------
42 +from __future__ import unicode_literals
43 +
44 +import layman.overlays.overlay as overlay
45 +import xml.etree.ElementTree as ET
46 +
47 +import sys
48 +
49 +from layman.compatibility import fileopen
50 +from layman.config import BareConfig
51 +from layman.utils import indent
52 +
53 +#py3
54 +if sys.hexversion >= 0x30200f0:
55 + _UNICODE = 'unicode'
56 +else:
57 + _UNICODE = 'UTF-8'
58 +
59 +
60 +def get_input(msg):
61 + '''
62 + py2 py3 compatibility function
63 + to obtain user input.
64 +
65 + @params msg: message prompt for user
66 + @rtype str: input from user
67 + '''
68 + try:
69 + value = raw_input(msg)
70 + except NameError:
71 + value = input(msg)
72 +
73 + return value
74 +
75 +def get_ans(msg):
76 + '''
77 + Handles yes/no input
78 +
79 + @params msg: message prompt for user
80 + @rtype boolean: reflects whether the user answered yes or no.
81 + '''
82 + ans = get_input(msg).lower()
83 +
84 + while ans not in ('y', 'yes', 'n', 'no'):
85 + ans = get_input('Please respond with [y/n]: ').lower()
86 +
87 + return ans in ('y', 'yes')
88 +
89 +def write_file(tree):
90 + '''
91 + Writes overlay file to desired location
92 +
93 + @param tree ElementTree.ElementTree object
94 + '''
95 + print('')
96 + filename = get_input('Desired overlay file name: ')
97 + filepath = get_input('Desired output path: ')
98 +
99 + if not filename.endswith('.xml'):
100 + filename += ".xml"
101 +
102 + if not filepath.endswith('/'):
103 + filepath += "/"
104 +
105 + destination = filepath + filename
106 +
107 + try:
108 + with fileopen(destination, 'w') as xml:
109 + tree.write(xml, encoding=_UNICODE)
110 +
111 + except IOError as e:
112 + print("Writing XML failed: %(error)s" % ({'error': e}))
113 +
114 +def check_overlay_type(ovl_type):
115 + '''
116 + Validates overlay type.
117 +
118 + @params ovl_type: overlay type to validate.
119 + @rtype None or str (if overlay type is valid).
120 + '''
121 + supported_types = ['bzr', 'cvs', 'darcs', 'git', 'g-sorcery',
122 + 'mercurial', 'svn', 'tar']
123 +
124 + if ovl_type.lower() in supported_types:
125 + return ovl_type.lower()
126 + print('Specified type "%(type)s" not valid.' % ({'type': ovl_type}))
127 + print('Supported types include: %(types)s.' % ({'types': ', '.join(supported_types)}))
128 + return None
129 +
130 +def get_sources(overlay, required):
131 + '''
132 + Prompts user for possible overlay source
133 + information such as type, url, and branch.
134 +
135 + @params overlay: dict of overlay components
136 + @rtype dict
137 + '''
138 + ovl_type = None
139 + source_amount = int(get_input('How many sources exist for this overlay?: '))
140 +
141 + while not ovl_type:
142 + ovl_type = check_overlay_type(get_input('Define overlay\'s type: '))
143 +
144 + sources = []
145 + overlay['sources'] = []
146 + for i in range(1, source_amount + 1):
147 + if source_amount > 1:
148 + sources.append(get_input('Define source[%(i)s] URL: ' % ({'i': str(i)})))
149 + sources.append(ovl_type)
150 + if 'branch' in required:
151 + sources.append('Define source[%(i)s]\'s branch (if applicable): ' % \
152 + ({'i': str(i)}))
153 + else:
154 + sources.append('')
155 + else:
156 + sources.append(get_input('Define source URL: '))
157 + sources.append(ovl_type)
158 + if 'branch' in required:
159 + sources.append('Define source branch (if applicable): ')
160 + else:
161 + sources.append('')
162 +
163 + overlay['sources'].append(sources)
164 +
165 + return overlay
166 +
167 +def get_feeds(overlay):
168 + '''
169 + Prompts user for any overlay RSS feeds
170 + and returns the overlay dict.
171 +
172 + @params overlay: dict of overlay components
173 + @rtype dict
174 + '''
175 + feed_amount = int(get_input('How many RSS feeds exist for this overlay?: '))
176 + feeds = []
177 +
178 + for i in range(1, feed_amounts + 1):
179 + if feed_amounts > 1:
180 + feeds.append(get_input('Define overlay feed[%(i)s]: ' % ({'i': str(i)})))
181 + else:
182 + feeds.append(get_input('Define overlay feed: '))
183 +
184 + overlay['feeds'] = feeds
185 +
186 + return overlay
187 +
188 +def update_required(required):
189 + '''
190 + Prompts user for optional components and updates
191 + the required components accordingly.
192 +
193 + @params required: List of current required overlay components
194 + @rtype list
195 + '''
196 + possible_components = ['name', 'description', 'homepage', 'owner', 'quality',
197 + 'priority', 'sources', 'branch', 'irc', 'feed']
198 +
199 + for possible in possible_components:
200 + if possible not in required:
201 + available = get_ans("Include %(comp)s for this overlay? [y/n]: " % ({'comp': possible}))
202 + if available:
203 + required.append(possible)
204 +
205 + return required
206 +
207 +def get_components():
208 + '''
209 + Acquires overlay components via user interface.
210 +
211 + @rtype dict
212 + '''
213 + overlay = {}
214 +
215 + required = ['name', 'description', 'owner', 'type', 'sources']
216 +
217 + required = update_required(required)
218 +
219 + for component in required:
220 +
221 + if 'feed' in component:
222 + overlay = get_feeds(overlay)
223 + print('')
224 +
225 + elif 'name' in component:
226 + print('')
227 + overlay['name'] = get_input('Define overlay name: ')
228 +
229 + elif 'owner' in component:
230 + print('')
231 + overlay['owner_name'] = get_input('Define owner name: ')
232 + overlay['owner_email'] = get_input('Define owner email: ')
233 + print('')
234 +
235 + elif 'sources' in component:
236 + overlay = get_sources(overlay, required)
237 +
238 + else:
239 + if 'type' not in component and 'branch' not in component:
240 + overlay[component] = get_input('Define %(comp)s: ' % ({'comp': component}))
241 +
242 + return overlay
243 +
244 +def main():
245 + config = BareConfig()
246 + repo = ET.Element('repositories', version='1.0', encoding=_UNICODE)
247 +
248 + for x in range(1, int(get_input("How many overlays would you like to create?: "))+1):
249 + print('')
250 + print("Overlay #%(x)s: " % ({'x': str(x)}))
251 + print("~~~~~~~~~~~~~ ")
252 + overlay_dict = get_components()
253 + ovl = overlay.Overlay(config=config, ovl_dict=overlay_dict, ignore=1)
254 + repo.append(ovl.to_xml())
255 +
256 + indent(repo)
257 + tree = ET.ElementTree(repo)
258 + write_file(tree)
259 +
260 +if __name__ == '__main__':
261 + main()