commit:     b275450533245b5097b5f6c08a84df999ed2c989
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Sun Apr 17 08:06:28 2016 +0000
Commit:     Brian Dolbec <dolsen <AT> gentoo <DOT> org>
CommitDate: Mon Apr 25 15:03:37 2016 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=b2754505

repoman: Use XML Schema for metadata.xml validation

 pym/repoman/_xml.py                              | 16 +++++-----
 pym/repoman/metadata.py                          | 39 ++++++++++++------------
 pym/repoman/modules/scan/metadata/__init__.py    |  2 +-
 pym/repoman/modules/scan/metadata/pkgmetadata.py |  6 ++--
 pym/repoman/scanner.py                           |  8 ++---
 5 files changed, 36 insertions(+), 35 deletions(-)

diff --git a/pym/repoman/_xml.py b/pym/repoman/_xml.py
index d55dda5..33a536a 100644
--- a/pym/repoman/_xml.py
+++ b/pym/repoman/_xml.py
@@ -12,7 +12,7 @@ from portage import os
 from portage.output import red
 from portage.process import find_binary
 
-from repoman.metadata import fetch_metadata_dtd
+from repoman.metadata import fetch_metadata_xsd
 from repoman._subprocess import repoman_getstatusoutput
 
 
@@ -53,12 +53,12 @@ class 
_MetadataTreeBuilder(xml.etree.ElementTree.TreeBuilder):
 
 class XmlLint(object):
 
-       def __init__(self, options, repoman_settings, metadata_dtd=None):
-               self.metadata_dtd = (metadata_dtd or
-                       os.path.join(repoman_settings["DISTDIR"], 
'metadata.dtd'))
+       def __init__(self, options, repoman_settings, metadata_xsd=None):
+               self.metadata_xsd = (metadata_xsd or
+                       os.path.join(repoman_settings["DISTDIR"], 
'metadata.xsd'))
                self.options = options
                self.repoman_settings = repoman_settings
-               self._is_capable = metadata_dtd is not None
+               self._is_capable = metadata_xsd is not None
                self.binary = None
                self._check_capable()
 
@@ -69,7 +69,7 @@ class XmlLint(object):
                if not self.binary:
                        print(red("!!! xmllint not found. Can't check 
metadata.xml.\n"))
                elif not self._is_capable:
-                       if not fetch_metadata_dtd(self.metadata_dtd, 
self.repoman_settings):
+                       if not fetch_metadata_xsd(self.metadata_xsd, 
self.repoman_settings):
                                sys.exit(1)
                        # this can be problematic if xmllint changes their 
output
                        self._is_capable = True
@@ -93,8 +93,8 @@ class XmlLint(object):
                # xmlint can produce garbage output even on success, so only 
dump
                # the ouput when it fails.
                st, out = repoman_getstatusoutput(
-                       self.binary + " --nonet --noout --dtdvalid %s %s" % (
-                               portage._shell_quote(self.metadata_dtd),
+                       self.binary + " --nonet --noout --schema %s %s" % (
+                               portage._shell_quote(self.metadata_xsd),
                                portage._shell_quote(
                                        os.path.join(checkdir, 
"metadata.xml"))))
                if st != os.EX_OK:

diff --git a/pym/repoman/metadata.py b/pym/repoman/metadata.py
index e95ad41..7a514dc 100644
--- a/pym/repoman/metadata.py
+++ b/pym/repoman/metadata.py
@@ -33,8 +33,9 @@ metadata_xml_declaration = '<?xml version="1.0" 
encoding="%s"?>' \
        % (metadata_xml_encoding,)
 metadata_doctype_name = 'pkgmetadata'
 metadata_dtd_uri = 'http://www.gentoo.org/dtd/metadata.dtd'
+metadata_xsd_uri = 'http://www.gentoo.org/xml-schema/metadata.xsd'
 # force refetch if the local copy creation time is older than this
-metadata_dtd_ctime_interval = 60 * 60 * 24 * 7  # 7 days
+metadata_xsd_ctime_interval = 60 * 60 * 24 * 7  # 7 days
 
 
 def parse_metadata_use(xml_tree):
@@ -86,36 +87,36 @@ def parse_metadata_use(xml_tree):
        return uselist
 
 
-def fetch_metadata_dtd(metadata_dtd, repoman_settings):
+def fetch_metadata_xsd(metadata_xsd, repoman_settings):
        """
-       Fetch metadata.dtd if it doesn't exist or the ctime is older than
-       metadata_dtd_ctime_interval.
+       Fetch metadata.xsd if it doesn't exist or the ctime is older than
+       metadata_xsd_ctime_interval.
        @rtype: bool
        @return: True if successful, otherwise False
        """
 
        must_fetch = True
-       metadata_dtd_st = None
+       metadata_xsd_st = None
        current_time = int(time.time())
        try:
-               metadata_dtd_st = os.stat(metadata_dtd)
+               metadata_xsd_st = os.stat(metadata_xsd)
        except EnvironmentError as e:
                if e.errno not in (errno.ENOENT, errno.ESTALE):
                        raise
                del e
        else:
-               # Trigger fetch if metadata.dtd mtime is old or clock is wrong.
-               if abs(current_time - metadata_dtd_st.st_ctime) \
-                       < metadata_dtd_ctime_interval:
+               # Trigger fetch if metadata.xsd mtime is old or clock is wrong.
+               if abs(current_time - metadata_xsd_st.st_ctime) \
+                       < metadata_xsd_ctime_interval:
                        must_fetch = False
 
        if must_fetch:
                print()
                print(
-                       "%s the local copy of metadata.dtd "
+                       "%s the local copy of metadata.xsd "
                        "needs to be refetched, doing that now" % green("***"))
                print()
-               parsed_url = urlparse(metadata_dtd_uri)
+               parsed_url = urlparse(metadata_xsd_uri)
                setting = 'FETCHCOMMAND_' + parsed_url.scheme.upper()
                fcmd = repoman_settings.get(setting)
                if not fcmd:
@@ -125,29 +126,29 @@ def fetch_metadata_dtd(metadata_dtd, repoman_settings):
                                return False
 
                destdir = repoman_settings["DISTDIR"]
-               fd, metadata_dtd_tmp = tempfile.mkstemp(
-                       prefix='metadata.dtd.', dir=destdir)
+               fd, metadata_xsd_tmp = tempfile.mkstemp(
+                       prefix='metadata.xsd.', dir=destdir)
                os.close(fd)
 
                try:
                        if not portage.getbinpkg.file_get(
-                               metadata_dtd_uri, destdir, fcmd=fcmd,
-                               filename=os.path.basename(metadata_dtd_tmp)):
+                               metadata_xsd_uri, destdir, fcmd=fcmd,
+                               filename=os.path.basename(metadata_xsd_tmp)):
                                logging.error(
-                                       "failed to fetch metadata.dtd from 
'%s'" % metadata_dtd_uri)
+                                       "failed to fetch metadata.xsd from 
'%s'" % metadata_xsd_uri)
                                return False
 
                        try:
                                portage.util.apply_secpass_permissions(
-                                       metadata_dtd_tmp,
+                                       metadata_xsd_tmp,
                                        gid=portage.data.portage_gid, 
mode=0o664, mask=0o2)
                        except portage.exception.PortageException:
                                pass
 
-                       shutil.move(metadata_dtd_tmp, metadata_dtd)
+                       shutil.move(metadata_xsd_tmp, metadata_xsd)
                finally:
                        try:
-                               os.unlink(metadata_dtd_tmp)
+                               os.unlink(metadata_xsd_tmp)
                        except OSError:
                                pass
 

diff --git a/pym/repoman/modules/scan/metadata/__init__.py 
b/pym/repoman/modules/scan/metadata/__init__.py
index 98df9cf..1a6c0ed 100644
--- a/pym/repoman/modules/scan/metadata/__init__.py
+++ b/pym/repoman/modules/scan/metadata/__init__.py
@@ -18,7 +18,7 @@ module_spec = {
                        'functions': ['check'],
                        'func_desc': {
                        },
-                       'mod_kwargs': ['repo_settings', 'qatracker', 'options', 
'metadata_dtd',
+                       'mod_kwargs': ['repo_settings', 'qatracker', 'options', 
'metadata_xsd',
                        ],
                        'func_kwargs': {
                                'checkdir': (None, None),

diff --git a/pym/repoman/modules/scan/metadata/pkgmetadata.py 
b/pym/repoman/modules/scan/metadata/pkgmetadata.py
index 9216d77..25907b3 100644
--- a/pym/repoman/modules/scan/metadata/pkgmetadata.py
+++ b/pym/repoman/modules/scan/metadata/pkgmetadata.py
@@ -46,17 +46,17 @@ class PkgMetadata(ScanBase):
                @param repo_settings: settings instance
                @param qatracker: QATracker instance
                @param options: argparse options instance
-               @param metadata_dtd: path of metadata.dtd
+               @param metadata_xsd: path of metadata.xsd
                '''
                super(PkgMetadata, self).__init__(**kwargs)
                repo_settings = kwargs.get('repo_settings')
                self.qatracker = kwargs.get('qatracker')
                self.options = kwargs.get('options')
-               metadata_dtd = kwargs.get('metadata_dtd')
+               metadata_xsd = kwargs.get('metadata_xsd')
                self.repoman_settings = repo_settings.repoman_settings
                self.musedict = {}
                self.xmllint = XmlLint(self.options, self.repoman_settings,
-                       metadata_dtd=metadata_dtd)
+                       metadata_xsd=metadata_xsd)
 
        def check(self, **kwargs):
                '''Performs the checks on the metadata.xml for the package

diff --git a/pym/repoman/scanner.py b/pym/repoman/scanner.py
index 74bb7e3..508fedb 100644
--- a/pym/repoman/scanner.py
+++ b/pym/repoman/scanner.py
@@ -56,11 +56,11 @@ class Scanner(object):
                        portage.util.stack_lists([self.categories], 
incremental=1))
                self.categories = self.repo_settings.repoman_settings.categories
 
-               metadata_dtd = None
+               metadata_xsd = None
                for path in 
reversed(self.repo_settings.repo_config.eclass_db.porttrees):
-                       path = os.path.join(path, 'metadata/dtd/metadata.dtd')
+                       path = os.path.join(path, 
'metadata/xml-schema/metadata.xsd')
                        if os.path.exists(path):
-                               metadata_dtd = path
+                               metadata_xsd = path
                                break
 
                self.portdb = repo_settings.portdb
@@ -189,7 +189,7 @@ class Scanner(object):
                        "qatracker": self.qatracker,
                        "vcs_settings": self.vcs_settings,
                        "options": self.options,
-                       "metadata_dtd": metadata_dtd,
+                       "metadata_xsd": metadata_xsd,
                        "uselist": uselist,
                        "checks": self.checks,
                        "repo_metadata": self.repo_metadata,

Reply via email to