Python lxml.etree.parse() Examples

The following are 30 code examples for showing how to use lxml.etree.parse(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module lxml.etree , or try the search function .

Example 1
Project: yang-explorer   Author: CiscoDevNet   File: collection.py    License: Apache License 2.0 6 votes vote down vote up
def list():
        """ get list of all collection entries """

        cols_elem = ET.Element('collections')
        for col in Col.objects.all():
            path = os.path.join('data', 'collections', col.name)
            if not os.path.exists(path):
                logging.error('Collection has inconstancy : %s !!' % col.name)
                continue
            files = glob.glob(os.path.join(path, '*'))
            for _file in files:
                payload = ET.parse(_file)
                for child in payload.getroot():
                    if child.tag == 'metadata':
                        cols_elem.append(child)
        return cols_elem 
Example 2
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 6 votes vote down vote up
def project_status_requests(self, request_type, filter_function=None):
        requests = []
        for status in self.project_status(None, status=False):
            for request in status.findall(f'{request_type}_requests/request'):
                updated_at = dateutil.parser.parse(request.get('updated'), ignoretz=True)
                updated_delta = datetime.utcnow() - updated_at
                if updated_delta.total_seconds() < 0 * 60:
                    # Allow for dashboard to update caches by not considering
                    # requests whose state has changed in the last 5 minutes.
                    continue

                if filter_function and not filter_function(request, updated_delta):
                    continue

                requests.append(str(request.get('id')))

        return requests 
Example 3
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 6 votes vote down vote up
def ensure_staging_archs(self, project):
        meta = ET.parse(http_GET(self.project_meta_url(project)))
        repository = meta.find('repository[@name="{}"]'.format(self.cmain_repo))

        changed = False
        for arch in self.cstaging_archs:
            if not repository.xpath('./arch[text()="{}"]'.format(arch)):
                elm = ET.SubElement(repository, 'arch')
                elm.text = arch
                changed = True

        if not changed:
            return

        meta = ET.tostring(meta)
        http_PUT(self.project_meta_url(project), data=meta) 
Example 4
Project: openSUSE-release-tools   Author: openSUSE   File: core.py    License: GNU General Public License v2.0 6 votes vote down vote up
def package_source_hash(apiurl, project, package, revision=None):
    query = {}
    if revision:
        query['rev'] = revision

    # Will not catch packages that previous had a link, but no longer do.
    if package_source_link_copy(apiurl, project, package):
        query['expand'] = 1

    try:
        url = makeurl(apiurl, ['source', project, package], query)
        root = ETL.parse(http_GET(url)).getroot()
    except HTTPError as e:
        if e.code == 400 or e.code == 404:
            # 400: revision not found, 404: package not found.
            return None

        raise e

    if revision and root.find('error') is not None:
        # OBS returns XML error instead of HTTP 404 if revision not found.
        return None

    from osclib.util import sha1_short
    return sha1_short(root.xpath('entry[@name!="_link"]/@md5')) 
Example 5
Project: openSUSE-release-tools   Author: openSUSE   File: request_finder.py    License: GNU General Public License v2.0 6 votes vote down vote up
def find_via_stagingapi(self, pkgs):
        """
        Search for all various mutations and return list of SR#s. Use
        and instance of StagingAPI to direct the search, this makes
        sure that the SR# are inside a staging project.
        :param pkgs: mesh of argumets to search for

        This function is only called for its side effect.
        """

        url = self.api.makeurl(['staging', self.api.project, 'staging_projects'], { 'requests': 1 })
        status = ET.parse(self.api.retried_GET(url)).getroot()

        for p in pkgs:
            found = False
            for staging in status.findall('staging_project'):
                for request in staging.findall('staged_requests/request'):
                    if request.get('package') == p or request.get('id') == p:
                        self.srs[int(request.get('id'))] = {'staging': staging.get('name')}
                        found = True
                        break
            if not found:
                raise oscerr.WrongArgs('No SR# found for: {}'.format(p)) 
Example 6
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 6 votes vote down vote up
def project_status_requests(self, request_type, filter_function=None):
        requests = []
        for status in self.project_status(None, status=False):
            for request in status.findall(f'{request_type}_requests/request'):
                updated_at = dateutil.parser.parse(request.get('updated'), ignoretz=True)
                updated_delta = datetime.utcnow() - updated_at
                if updated_delta.total_seconds() < 0 * 60:
                    # Allow for dashboard to update caches by not considering
                    # requests whose state has changed in the last 5 minutes.
                    continue

                if filter_function and not filter_function(request, updated_delta):
                    continue

                requests.append(str(request.get('id')))

        return requests 
Example 7
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 6 votes vote down vote up
def ensure_staging_archs(self, project):
        meta = ET.parse(http_GET(self.project_meta_url(project)))
        repository = meta.find('repository[@name="{}"]'.format(self.cmain_repo))

        changed = False
        for arch in self.cstaging_archs:
            if not repository.xpath('./arch[text()="{}"]'.format(arch)):
                elm = ET.SubElement(repository, 'arch')
                elm.text = arch
                changed = True

        if not changed:
            return

        meta = ET.tostring(meta)
        http_PUT(self.project_meta_url(project), data=meta) 
Example 8
Project: Traffic_sign_detection_YOLO   Author: AmeyaWagh   File: YOLOtest.py    License: MIT License 5 votes vote down vote up
def getlabel(self,xmlPath):
        tree = ET.parse(xmlPath)
        root = tree.getroot()
        print(root[6][0].text) 
Example 9
Project: razzy-spinner   Author: rafasashi   File: mte.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, file_path):
        tree = etree.parse(file_path)
        self.__root = xpath(tree.getroot(), './tei:text/tei:body', self.ns)[0] 
Example 10
Project: yang-explorer   Author: CiscoDevNet   File: views.py    License: Apache License 2.0 5 votes vote down vote up
def get_session_config(username):
    session = ET.Element('session')
    user = ET.Element('username')
    user.text = username
    session.append(user)
    if username != '':
        # Pass global static configuration data
        _global = ET.parse(os.path.join('static', 'global-config.xml')).getroot()
        session.append(_global)
    return session 
Example 11
Project: yang-explorer   Author: CiscoDevNet   File: dygraph.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, filename):
        self.modules = {}
        logging.debug('Parsing %s !!' % filename)
        root = ET.parse(filename).getroot()

        for child in root:
            module = DYModule(child)
            self.modules[module.name] = module

        for name in self.modules:
            module = self.modules[name]
            for imp in module.imports:
                i_module = self.modules.get(imp, None)
                if i_module is None:
                    logging.warning('Dependent modules %s not found in dependency list !!' % imp)
                    continue
                if imp not in i_module.depends:
                    i_module.depends.append(name)

            for inc in module.includes:
                i_module = self.modules.get(inc, None)
                if i_module is None:
                    logging.warning('Included modules %s not found in dependency list !!' % inc)
                    continue
                if inc not in i_module.depends:
                    i_module.depends.append(name) 
Example 12
Project: yang-explorer   Author: CiscoDevNet   File: cxml.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, filename):
        self.filename = filename
        self.modulename = os.path.splitext(os.path.basename(filename))[0]
        if os.path.exists(filename):
            logging.debug('Parsing %s' % filename)
            try:
                self.cxml = ET.parse(filename)
            except:
                self.cxml = None
                logging.error('ET Failed to parse %s' % filename)
        else:
            self.cxml = None
            logging.error('File %s does not exists' % filename) 
Example 13
Project: yang-explorer   Author: CiscoDevNet   File: cxml.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, filename, cxml=None, options={}):
        if cxml:
            self.handle = cxml
        else:
            self.handle = ET.parse(filename)
        self.inc_keys = options.get('include-keys', False)
        self.inc_prefixes = options.get('include-prefixes', [])
        self.inc_default = options.get('include-default', False)
        self.add_root_prefix = options.get('add-root-prefix', False)
        self.current = self.handle.getroot()
        self.prefix = self.current.get('prefix', None)
        self.path = [self.current.get('name')] 
Example 14
Project: openSUSE-release-tools   Author: openSUSE   File: rabbit-repoid.py    License: GNU General Public License v2.0 5 votes vote down vote up
def check_arch(self, project, repository, architecture):
        url = makeurl(self.apiurl, [
                      'build', project, repository, architecture], {'view': 'status'})
        root = ET.parse(http_GET(url)).getroot()
        if root.get('code') == 'finished':
            buildid = root.find('buildid')
            if buildid is not None:
                return buildid.text 
Example 15
Project: openSUSE-release-tools   Author: openSUSE   File: rabbit-openqa.py    License: GNU General Public License v2.0 5 votes vote down vote up
def gather_isos(self, name, repository):
        url = self.api.makeurl(['published', name, repository, 'iso'])
        f = self.api.retried_GET(url)
        root = ET.parse(f).getroot()
        ret = []
        for entry in root.findall('entry'):
            if entry.get('name').endswith('iso'):
                ret.append(self.map_iso(name, entry.get('name')))
        return ret 
Example 16
Project: openSUSE-release-tools   Author: openSUSE   File: rabbit-openqa.py    License: GNU General Public License v2.0 5 votes vote down vote up
def gather_buildid(self, name, repository):
        url = self.api.makeurl(['published', name, repository], {'view': 'status'})
        f = self.api.retried_GET(url)
        id = ET.parse(f).getroot().find('buildid')
        if id is not None:
            return id.text 
Example 17
Project: openSUSE-release-tools   Author: openSUSE   File: generate.py    License: GNU General Public License v2.0 5 votes vote down vote up
def build_summary(self, project, repository):
        url = makeurl(self.apiurl, ['build', project, '_result'], { 'repository': repository, 'view': 'summary' })
        try:
            f = http_GET(url)
        except HTTPError as e:
            return { 'building': -1 }
        root = ET.parse(f).getroot()
        failed = 0
        unresolvable = 0
        building = 0
        succeeded = 0
        for result in root.findall('.//statuscount'):
            code = result.get('code')
            count = int(result.get('count'))
            if code == 'excluded' or code == 'disabled':
                continue # ignore
            if code == 'succeeded':
                succeeded += count
                continue
            if code == "failed":
                failed += count
                continue
            if code == "unresolvable":
                unresolvable += count
                continue
            building += count
            #print(code, file=sys.stderr)
        # let's count them as building
        if building > 0:
            building += unresolvable
            unresolvable = 0
        if building + failed + succeeded == 0:
            return {'building': -1}
        return { 'building': 1000 - int(building * 1000 / (building + failed + succeeded)),
                 'failed': failed,
                 'unresolvable': unresolvable } 
Example 18
Project: openSUSE-release-tools   Author: openSUSE   File: repochecks.py    License: GNU General Public License v2.0 5 votes vote down vote up
def mirror(apiurl, project, repository, arch):
    """Call bs_mirrorfull script to mirror packages."""
    directory = os.path.join(CACHEDIR, project, repository, arch)

    if not os.path.exists(directory):
        os.makedirs(directory)

    meta = ETL.parse(http_GET('{}/public/source/{}/_meta'.format(apiurl, project))).getroot()
    repotag = meta.xpath("/project/repository[@name='{}']".format(repository))[0]
    if arch not in repotag.xpath("./arch/text()"):
        # Arch not in this project, skip mirroring
        return directory

    download = repotag.xpath("./download[@arch='{}']".format(arch))
    if download is not None and len(download) > 0:
        if len(download) > 1:
            raise Exception('Multiple download urls unsupported')
        repotype = download[0].get('repotype')
        if repotype != 'rpmmd':
            raise Exception('repotype {} not supported'.format(repotype))
        return mirrorRepomd(directory, download[0].get('url'))

    script = os.path.join(SCRIPT_PATH, '..', 'bs_mirrorfull')
    path = '/'.join((project, repository, arch))
    logger.info('mirroring {}'.format(path))
    url = '{}/public/build/{}'.format(apiurl, path)
    p = subprocess.run(['perl', script, '--nodebug', url, directory])

    if p.returncode:
        raise Exception('failed to mirror {}'.format(path))

    return directory 
Example 19
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 5 votes vote down vote up
def _get_staged_requests(self):
        """
        Get all requests that are already staged
        :return dict of staged requests with their project and srid
        """

        packages_staged = {}
        url = self.makeurl(['staging', self.project, 'staging_projects'], { 'requests': 1 })
        status = ET.parse(self.retried_GET(url)).getroot()
        for prj in status.findall('staging_project'):
            for req in prj.findall('./staged_requests/request'):
                packages_staged[req.get('package')] = {'prj': prj.get('name'), 'rq_id': req.get('id')}

        return packages_staged 
Example 20
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 5 votes vote down vote up
def get_staging_projects(self):
        """
        Get all current running staging projects
        :return list of known staging projects
        """

        result = []
        url = self.makeurl(['staging', self.project, 'staging_projects'])
        status = ET.parse(self.retried_GET(url)).getroot()
        for project in status.findall('staging_project'):
            result.append(project.get('name'))
        return result 
Example 21
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 5 votes vote down vote up
def find_devel_project_from_adi_frozenlinks(self, prj):
        try:
            url = self.makeurl(['source', prj, '_project', '_frozenlinks'], {'meta': '1'})
            root = ET.parse(http_GET(url)).getroot()
        except HTTPError as e:
            if e.code == 404:
                return None
        meta = self.get_prj_pseudometa(prj)
        # the first package's devel project is good enough
        return devel_project_get(self.apiurl, self.project, meta['requests'][0].get('package'))[0] 
Example 22
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 5 votes vote down vote up
def source_info(self, project, package, rev=None):
        query = {'view': 'info'}
        if rev is not None:
            query['rev'] = rev
        url = makeurl(self.apiurl, ('source', project, package), query=query)
        try:
            return ET.parse(http_GET(url)).getroot()
        except (HTTPError, URLError):
            return None 
Example 23
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 5 votes vote down vote up
def get_ignored_requests(self):
        ignore = {}
        url = self.makeurl(['staging', self.project, 'excluded_requests'])
        root = ET.parse(self.retried_GET(url)).getroot()
        for entry in root.findall('request'):
            ignore[int(entry.get('id'))] = entry.get('description')
        return ignore 
Example 24
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 5 votes vote down vote up
def job_history_get(self, project, repository, architecture, package=None, limit=20):
        query = {}
        if package:
            query['package'] = package
        if limit != None and int(limit) > 0:
            query['limit'] = int(limit)
        u = makeurl(self.apiurl, ['build', project, repository, architecture, '_jobhistory'], query)
        return ET.parse(http_GET(u)).getroot()

    # Modified from osc.core.print_buildlog() 
Example 25
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 5 votes vote down vote up
def buildlog_get(self, prj, package, repository, arch, offset=0, strip_time=False, last=False):
        # to protect us against control characters
        all_bytes = bytes.maketrans(b'', b'')
        remove_bytes = all_bytes[:8] + all_bytes[14:32]  # accept tabs and newlines

        path = ['build', prj, repository, arch, package, '_log']
        if offset < 0:
            url = makeurl(self.apiurl, path, {'view': 'entry'})
            root = ET.parse(http_GET(url)).getroot()
            size = root.xpath('entry[@name="_log"]/@size')
            if size:
                offset += int(size[0])

        query = {'nostream': '1', 'start': '%s' % offset}
        if last:
            query['last'] = 1
        log = StringIO()
        while True:
            query['start'] = offset
            start_offset = offset
            u = makeurl(self.apiurl, path, query)
            for data in streamfile(u, bufsize="line"):
                offset += len(data)
                if strip_time:
                    data = buildlog_strip_time(data)
                log.write(decode_it(data.translate(all_bytes, remove_bytes)))
            if start_offset == offset:
                break

        return log.getvalue() 
Example 26
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 5 votes vote down vote up
def project_status(self, staging, status=True, requests=True, reload=False):
        opts = {}
        if requests:
            opts['requests'] = 1
        if status:
            opts['status'] = 1
        paths = ['staging', self.project, 'staging_projects']
        if staging:
            paths.append(staging)
        url = self.makeurl(paths, opts)
        return ET.parse(self.retried_GET(url)).getroot() 
Example 27
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 5 votes vote down vote up
def linked_packages(self, package, project=None):
        if not project:
            project = self.project

        url = self.makeurl(['source', project, package], { 'cmd': 'showlinked' })
        f = http_POST(url)
        root = ET.parse(f).getroot()
        result = []
        for package in root.findall('package'):
            result.append({'project': package.get('project'), 'package': package.get('name')})
        return result 
Example 28
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 5 votes vote down vote up
def list_requests_in_prj(self, project):
        where = "@by_project='%s'+and+@state='new'" % project

        url = self.makeurl(['search', 'request', 'id'],
                           "match=state/@name='review'+and+review[%s]" % where)
        f = http_GET(url)
        root = ET.parse(f).getroot()
        list = []
        for rq in root.findall('request'):
            list.append(int(rq.get('id')))

        return list 
Example 29
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 5 votes vote down vote up
def get_flag_in_prj(self, project, flag='build', repository=None, arch=None):
        """Return the flag value in a project."""
        url = self.makeurl(['source', project, '_meta'])
        root = ET.parse(http_GET(url)).getroot()
        section = root.find(flag)
        if section is None:
            # the default for build and publish (is all we care for)
            return 'enabled'
        for status in section:
            is_repository = status.get('repository', None) == repository
            is_arch = status.get('arch', None) == arch
            if is_repository and is_arch:
                return status.tag 
Example 30
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 5 votes vote down vote up
def get_binary_version(self, project, rpm, repository='standard', arch='x86_64'):
        """
        Return the version of a built rpm file
        """
        url = self.makeurl(['build', project, repository, arch, '_repository', "%s?view=fileinfo" % rpm])
        try:
            return ET.parse(http_GET(url)).getroot().find('version').text
        except HTTPError as e:
            if e.code == 404:
                return None
            raise