1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
#!/usr/bin/python3
# {{{ Imports
import sys
import os.path
from urllib.request import urlopen
import argparse
from xml.etree.cElementTree import fromstring as xmlParser
import traceback
# }}}
# {{{ Constants
_NAME="WeeChatScriptsHelper"
_DESC="Weechat Scripts Packaging Helper"
_VERSION="1.0"
REMOTE_LIST="https://weechat.org/files/plugins.xml"
MIN_VERSION="0.3.0"
REWRITE_LICENSES = {
'Apache2.0': 'Apache-2',
'Artistic2.0': 'Artistic-2',
'Beerware': 'Beerware',
'BSD-2c': 'BSD-2c',
'BSD-3c': 'BSD-3c',
'GPL2': 'GPL-2',
'GPL3': 'GPL-3',
'ISC': 'ISC',
'MIT': 'MIT',
'Public_domain': 'public-domain',
'WTFPL': 'WTFPL',
}
SUPPORTED_LICENSES = REWRITE_LICENSES.values()
# }}}
# {{{ Logger
class Logger(object):
_COLOR_INFO = '92'
_COLOR_WARN = '93'
_COLOR_DEBUG = '94'
_COLOR_ERROR = '91'
def __init__(self, verbose=False, debug=False):
self.is_verbose = verbose
self.is_debug = debug
self.errors = 0
self.out = sys.stdout
def _prefix(self, msgtype):
prefix = '[%s]' % (msgtype)
if self.out.isatty():
prefix = '[\033[0;%sm%s\033[0;m]' % (getattr(self, '_COLOR_' + msgtype.upper()), msgtype)
return prefix
def use_stderr(self):
self.out = sys.stderr
def info(self, msg):
if self.is_verbose:
self.out.write('%s %s\n' % (self._prefix('info'), msg))
def warn(self, msg):
if self.is_verbose:
self.out.write('%s %s\n' % (self._prefix('warn'), msg))
def debug(self, msg):
if self.is_debug:
self.out.write('%s %s\n' % (self._prefix('debug'), msg))
def error(self, msg):
self.errors += 1
self.out.write('%s %s\n' % (self._prefix('error'), msg))
# }}}
# {{{ WeechatScripts
class WeechatScripts(object):
def __init__(self, options, logger=None):
self._logger = logger if logger is not None else Logger()
self._options = options
def get_xmldom(self):
xmldom = None
try:
xmlstr = urlopen(REMOTE_LIST).read()
except:
xmlstr = None
self._logger.error("Unable to retrieve '%s'" % (REMOTE_LIST))
if xmlstr:
try:
xmldom = xmlParser(xmlstr)
except:
self._logger.error("Unable to parse XML")
xmldom = None
return xmldom
def get_license(self, license):
if license in REWRITE_LICENSES:
license = REWRITE_LICENSES[license]
return license
def dfsg_license(self, license):
return license in SUPPORTED_LICENSES
def get_datas(self):
scripts = {}
licenses = []
xmldom = self.get_xmldom()
if xmldom:
for el in xmldom.findall('plugin'):
max_vers = el.find('max_weechat').text
if max_vers is not None and max_vers < MIN_VERSION:
continue
lang = el.find('language').text
filename = el.find('url').text.split('/')[-1]
author = "%s <%s>" % (el.find('author').text, el.find('mail').text)
tags = el.find('tags').text.split(',')
if lang == 'python' and 'py3' not in tags:
self._logger.warn("Python script '%s' is not compatible with Python3"
% (os.path.basename(filename)))
continue
date = ''
date_add = int(el.find('added').text.split('-')[0])
if el.find('updated').text == None:
date = "%d" % (date_add)
else:
date_upd = int(el.find('updated').text.split('-')[0])
if date_upd > date_add:
date = "%d-%d" % (date_add, date_upd)
else:
date = "%d" % (date_add)
license = self.get_license(el.find('license').text)
dfsg_license = self.dfsg_license(license)
if not dfsg_license:
self._logger.warn("Script '%s' has a not DFSG compliant lisense: %s"
% (os.path.basename(filename), license))
continue
if not license in licenses:
licenses.append(license)
if not lang in scripts:
scripts[lang] = []
scripts[lang].append({
'language': el.find('language').text,
'url': el.find('url').text,
'file': filename,
'version': el.find('version').text,
'license': license,
'author': author,
'date': date,
})
return scripts, licenses
def short_license(self, license):
content = ''
f = "%s/licenses/%s" % (os.path.dirname(__file__), license)
if os.path.isfile(f):
content = open(f).read()
else:
self._logger.error("Unable to find short license '%s' in %s" %
(license, f))
return content
def copyright_header(self):
content = ''
f = "%s/copyright.in" % (os.path.dirname(__file__))
if os.path.isfile(f):
content = open(f).read()
return content
def copyright(self):
self._logger.use_stderr()
scripts, licenses = self.get_datas()
sys.stdout.write("%s\n" % self.copyright_header())
if scripts != {}:
for l in sorted(scripts.keys()):
for script in scripts[l]:
sys.stdout.write("Files: %s/%s\n" % (l.lower(), script['file']))
sys.stdout.write("Copyright: %s, %s\n" % (script['date'], script['author']))
sys.stdout.write("License: %s\n\n" % (script['license']))
if licenses != []:
for license in sorted(licenses):
sys.stdout.write("License: %s\n" % (license))
sys.stdout.write("%s\n" % (self.short_license(license)))
def wget(self, url, path):
ret = False
content = None
try:
content = urlopen(url).read()
except Exception as e:
self._logger.error("Unable to retrieve '%s': %s" % (url, e))
content = None
if content:
try:
f = open(path, 'w')
f.write(content.decode('utf-8'))
f.close()
ret = True
except Exception as e:
self._logger.error("Unable to write file '%s': %s" % (path, e))
return ret
def download(self):
scriptlist, _ = self.get_datas()
if scriptlist != {}:
for l in scriptlist:
for script in scriptlist[l]:
path = "%s/%s" % (script['language'], script['file'])
if not os.path.exists(script['language']):
os.mkdir(script['language'])
if self.wget(script['url'], path):
self._logger.info("Success while saving '%s' %s (%s)"
% (path, script['version'], script['license']))
else:
self._logger.error("Failure while saving '%s'" % (path))
# }}}
# {{{ main
def main():
parser = argparse.ArgumentParser(prog=_NAME, description=_DESC)
parser.add_argument('--version', action='version', version='%(prog)s ' + _VERSION )
parser.add_argument(
'-v', '--verbose', dest='verbose',
default=False, action='store_true',
help='Be verbose during process (default: no)')
parser.add_argument(
'-d', '--debug', dest='debug',
default=False, action='store_true',
help='Print additional debug informations: traceback, etc. (default: no)')
parser.add_argument(
'-a', '--action', dest='action',
choices=('download', 'copyright'), default=None,
help='Action to realize')
options = parser.parse_args()
logger = Logger(verbose=options.verbose or options.debug, debug=options.debug)
try:
ws = WeechatScripts(options=options, logger=logger)
getattr(ws, options.action)()
except Exception as e:
if not options.debug:
logger.error('Exception raised (use --debug option to get more info): %s' % (str(e)))
else:
logger.error('Exception raised: %s' % (e))
traceback.print_exc(file=sys.stderr)
sys.exit(-1)
else:
if logger.errors > 0:
sys.exit(-3)
# }}}
# {{{ __main__
if __name__ == "__main__":
main()
# }}}
# vim: foldmethod=marker foldlevel=0 foldenable
|