diff options
author | 2020-01-25 23:42:22 +0100 | |
---|---|---|
committer | 2020-01-25 23:42:48 +0100 | |
commit | 11fac4ba6ecd358613f9e8c0a9e799adbbe99a27 (patch) | |
tree | 50f4031296c133b9523f9229d705b060110ca096 | |
parent | Bump version to 0.6.6 (diff) | |
parent | setup.py: Generate entrypoint script (diff) | |
download | metagen-11fac4ba6ecd358613f9e8c0a9e799adbbe99a27.tar.gz metagen-11fac4ba6ecd358613f9e8c0a9e799adbbe99a27.tar.bz2 metagen-11fac4ba6ecd358613f9e8c0a9e799adbbe99a27.zip |
Merge branch 'py3'v0.7.0
Signed-off-by: Sebastian Pipping <sping@gentoo.org>
-rw-r--r-- | docs/README | 2 | ||||
-rwxr-xr-x | metagen/__main__.py (renamed from metagen/main.py) | 45 | ||||
-rw-r--r-- | metagen/meta_unittest.py | 4 | ||||
-rwxr-xr-x | metagen/metagenerator.py | 80 | ||||
-rwxr-xr-x | metagen/test_cli | 2 | ||||
-rw-r--r-- | metagen/version.py | 2 | ||||
-rwxr-xr-x[-rw-r--r--] | setup.py | 89 |
7 files changed, 114 insertions, 110 deletions
diff --git a/docs/README b/docs/README index 3a40768..81bb6d2 100644 --- a/docs/README +++ b/docs/README @@ -7,7 +7,7 @@ metagen is a command line tool that writes a Gentoo metadata.xml file in the current working directory. The metagen package also has a metagenerator class that can be used -from Python to create metadata.xml files easily: +from Python 3 to create metadata.xml files easily: from metagen.metagenerator import MyMetadata diff --git a/metagen/main.py b/metagen/__main__.py index c642b12..0452771 100755 --- a/metagen/main.py +++ b/metagen/__main__.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 """ @@ -16,7 +16,7 @@ import os import sys import tempfile from argparse import ArgumentParser -from commands import getstatusoutput +from subprocess import getstatusoutput from textwrap import dedent from portage import config @@ -39,18 +39,18 @@ def parse_echangelog_variable(name, email): try: e = os.environ["ECHANGELOG_USER"] except KeyError: - print red("!!! Environmental variable ECHANGELOG_USER not set.") - print red("!!! Set ECHANGELOG_USER or use -e and -n") + print(red("!!! Environmental variable ECHANGELOG_USER not set.")) + print(red("!!! Set ECHANGELOG_USER or use -e and -n")) sys.exit(1) try: my_email = e[e.find("<") +1:e.find(">")] except: - print red("!!! ECHANGELOG_USER not set properly") + print(red("!!! ECHANGELOG_USER not set properly")) sys.exit(1) try: my_name = e[0:e.find("<")-1] except: - print red("!!! ECHANGELOG_USER not set properly") + print(red("!!! ECHANGELOG_USER not set properly")) sys.exit(1) if email: email = "%s,%s" % (my_email, email) @@ -109,7 +109,7 @@ def _check_maintainer_type_list(text): _check_maintainer_type_list.__name__ = 'maintainer type' -if __name__ == '__main__': +def main(): parser = ArgumentParser(prog='metagen') parser.add_argument('--version', action='version', version='%(prog)s ' + __version__) @@ -151,43 +151,46 @@ if __name__ == '__main__': if options.desc or options.name: if not options.email and not options.echangelog: - print red("!!! No maintainer's email address specified.") - print red("!!! Options -d and -n are only valid with -e or -m") + print(red("!!! No maintainer's email address specified.")) + print(red("!!! Options -d and -n are only valid with -e or -m")) sys.exit(1) if not options.email and not options.echangelog: - print red("!!! You must specify --echangelog|-m " + - "or maintainer's email address (-e)\n") + print(red("!!! You must specify --echangelog|-m " + + "or maintainer's email address (-e)\n")) sys.exit(1) if (options.email or options.echangelog) and not options.maintainer_type: - print red("!!! No maintainer type specified. Please pass one of the following, in addition:") + print(red("!!! No maintainer type specified. Please pass one of the following, in addition:")) for candidate in _VALID_MAINTAINER_TYPES: - print red("!!! --type %s" % candidate) + print(red("!!! --type %s" % candidate)) sys.exit(1) txt = generate_xml(options) error_status = validate_xml(txt) if error_status < 0: - print red("!!! Error - Invalid XML") - print red("!!! Please report this bug with the options you used and the output:") - print error_status - print txt + print(red("!!! Error - Invalid XML")) + print(red("!!! Please report this bug with the options you used and the output:")) + print(error_status) + print(txt) sys.exit(1) if options.verbose: - print "\n%s" % txt + print("\n%s" % txt) out_file = "./metadata.xml" if options.output: out_file = options.output if not options.no_write and os.path.exists(out_file): if not options.force: - print red("!!! File %s exists." % out_file) - print red("!!! Use -f to force overwrite.") + print(red("!!! File %s exists." % out_file)) + print(red("!!! Use -f to force overwrite.")) sys.exit(1) if not options.no_write: open("%s" % out_file, "w").writelines(txt) - print blue("%s written") % out_file + print(blue("%s written") % out_file) + +if __name__ == '__main__': + main() diff --git a/metagen/meta_unittest.py b/metagen/meta_unittest.py index 73b5072..d54236a 100644 --- a/metagen/meta_unittest.py +++ b/metagen/meta_unittest.py @@ -1,6 +1,6 @@ -#!/usr/bin/python +#!/usr/bin/python3 -from metagenerator import MyMetadata +from .metagenerator import MyMetadata def test1(): diff --git a/metagen/metagenerator.py b/metagen/metagenerator.py index b08839b..e733997 100755 --- a/metagen/metagenerator.py +++ b/metagen/metagenerator.py @@ -1,67 +1,97 @@ -#!/usr/bin/python - +#!/usr/bin/python3 import sys +from xml.dom import minidom -import jaxml +from lxml import etree +from lxml.etree import Element, SubElement from portage.output import red -class MyMetadata(jaxml.XML_document): +class MyMetadata: """Create Gentoo Linux metadata.xml""" + class _Maintainer: + def __init__(self, type_=None, email=None, name=None, description=None): + self.type_ = type_ + self.email = email + self.name = name + self.description = description + def __init__(self): - jaxml.XML_document.__init__(self, "1.0", "UTF-8") - self._indentstring("\t") - self._text('<!DOCTYPE pkgmetadata SYSTEM ' + - '"http://www.gentoo.org/dtd/metadata.dtd">') - self.pkgmetadata() + self._maintainers = [] + self._long_description = None def set_maintainer(self, emails, names, descs, types): """Set maintainer(s)'s email, name, desc""" if len(types) != len(emails): if len(types) != 1: - print red("!!! Nbr maintainer types != nbr emails") + print(red("!!! Nbr maintainer types != nbr emails")) sys.exit(1) types = [types[0] for _ in emails] i = 0 for e in emails: - self._push("maintainer_level") - self.maintainer(type=types[i]).email(e) + maintainer = self._Maintainer(type_=types[i], email=e) if names: if len(names) > len(emails): - print red("!!! Nbr names > nbr emails") + print(red("!!! Nbr names > nbr emails")) sys.exit(1) if i <= len(names) -1: - self.name(names[i]) + maintainer.name = names[i] if descs: if len(descs) > len(emails): - print red("!!! Nbr descs > nbr emails") + print(red("!!! Nbr descs > nbr emails")) sys.exit(1) if i <= len(descs) -1: - self.description(descs[i]) - self._pop("maintainer_level") + maintainer.description = descs[i] i += 1 + self._maintainers.append(maintainer) def set_longdescription(self, longdesc): """Set package's long description.""" - self.longdescription(longdesc) + self._long_description = longdesc + + def __str__(self): + doctype = '<!DOCTYPE pkgmetadata SYSTEM "http://www.gentoo.org/dtd/metadata.dtd">' + root = Element('pkgmetadata') + + for maintainer_data in self._maintainers: + maintainer_element = SubElement(root, 'maintainer') + maintainer_element.set('type', maintainer_data.type_) + if maintainer_data.email: + SubElement(maintainer_element, 'email').text = maintainer_data.email + if maintainer_data.name: + SubElement(maintainer_element, 'name').text = maintainer_data.name + if maintainer_data.description: + SubElement(maintainer_element, 'description').text = maintainer_data.description + + if self._long_description: + long_description = SubElement(root, 'longdescription') + long_description.text = self._long_description + + xml_text = etree.tostring(root, xml_declaration=True, doctype=doctype) + + # Re-write indentation to tabulators + # (for backwards compatibility and smaller diffs with existing files) + reparsed = minidom.parseString(xml_text) + return reparsed.toprettyxml(indent='\t', encoding='UTF-8').decode() + def do_tests(): - import meta_unittest + from metagen import meta_unittest fails = 0 for func in dir(meta_unittest): if func[0:4] == "test": try: - exec "print meta_unittest.%s.__name__ + ':'," % func - exec "print meta_unittest.%s.__doc__" % func - exec "print meta_unittest.%s()" % func + exec("print(meta_unittest.%s.__name__ + ':', end='')" % func) + exec("print(meta_unittest.%s.__doc__)" % func) + exec("print(meta_unittest.%s())" % func) except: fails += 1 - print "Test %s failed:" % func - print sys.exc_type, sys.exc_value - print "%s tests failed." % fails + print("Test %s failed:" % func) + print(sys.exc_info()[0], sys.exc_info()[1]) + print("%s tests failed." % fails) if __name__ == "__main__": do_tests() diff --git a/metagen/test_cli b/metagen/test_cli index 959388d..fb394ca 100755 --- a/metagen/test_cli +++ b/metagen/test_cli @@ -1,6 +1,6 @@ #!/bin/bash metagen() { - PYTHONPATH=. ./metagen/main.py "$@" + PYTHONPATH=. python3 -m metagen "$@" } handle_error() { diff --git a/metagen/version.py b/metagen/version.py index 074d83e..2426eb8 100644 --- a/metagen/version.py +++ b/metagen/version.py @@ -1 +1 @@ -__version__="0.6.6" +__version__="0.7.0" @@ -3,10 +3,10 @@ NAME: setup.py SYNOPSIS: - python setup.py [options] [command] + python3 setup.py [options] [command] DESCRIPTION: - Using distutils "setup", build, install, or make tarball of the package. + Using setuptools "setup", build, install, or make tarball of the package. OPTIONS: See Distutils documentation for details on options and commands. @@ -24,22 +24,17 @@ EXAMPLES: (cp myfile-0.1.tar.gz here) gzip -cd myfile-0.1.tar.gz | tar xvf - cd myfile-0.1 - python setup.py build - python setup.py install - python setup.py sdist + python3 setup.py build + python3 setup.py install + python3 setup.py sdist """ -import os,sys,re,string,getopt,shutil,commands,glob -from distutils.core import setup,Extension +import glob +from setuptools import setup from metagen.version import __version__ -modname='setup' -debug_p=0 - pkgname='metagen' -#version=string.strip(open("VERSION").readline()) version = __version__ -exec_prefix=sys.exec_prefix description = "Metadata.xml Generator for Ebuilds" author = "Rob Cakebread" author_email = "pythonhead@gentoo.org" @@ -50,56 +45,32 @@ packages=['metagen'] package_data={"metagen" : ["test_cli"]} data_files=[("share/doc/%s-%s" % ("metagen", version), glob.glob("docs/*"))] -#===utilities========================== -def debug(ftn,txt): - if debug_p: - sys.stdout.write("%s.%s:%s\n" % (modname,ftn,txt)) - sys.stdout.flush() - -def fatal(ftn,txt): - msg="%s.%s:FATAL:%s\n" % (modname,ftn,txt) - raise SystemExit, msg - -def usage(): - print __doc__ -#============================= def main(): - setup (#---meta-data--- - name = pkgname, - version = version, - description = description, - author = author, - author_email = author_email, - url=url, - license = license, + setup( + name = pkgname, + version = version, + description = description, + author = author, + author_email = author_email, + url=url, + license = license, - #---scripts,modules and packages--- - packages = packages, - data_files = data_files, - ) -#============================== -if __name__ == '__main__': - opts,pargs=getopt.getopt(sys.argv[1:],'hv', - ['help','version','exec-prefix']) - for opt in opts: - if opt[0]=='-h' or opt[0]=='--help': - usage() - sys.exit(0) - elif opt[0]=='-v' or opt[0]=='--version': - print modname+": version="+version - elif opt[0]=='--exec-prefix': - exec_prefix=opt[1] + install_requires = [ + 'lxml', + ], - for arg in pargs: - if arg=='test': - do_test() - sys.exit(0) - elif arg=='doc': - do_doc() - sys.exit(0) - else: - pass + packages = packages, + data_files = data_files, + package_data = package_data, + + entry_points = { + 'console_scripts': [ + "metagen = metagen.__main__:main", + ], + } + ) - main() +if __name__ == '__main__': + main() |