Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jenkins_job module: fix for python3 #54958

Open
wants to merge 1 commit into
base: devel
from

Conversation

Projects
None yet
2 participants
@mrk-its
Copy link

commented Apr 7, 2019

SUMMARY

Fixes #54519

On python3 the jenkins_job fails to reconfigure the respective job due to a parsing issue within lxml when the configuration file contains an encoding declaration:

  File "/tmp/ansible_4y5pmpro/ansible_module_jenkins_job.py", line 276, in update_job
    if self.has_config_changed():
  File "/tmp/ansible_4y5pmpro/ansible_module_jenkins_job.py", line 237, in has_config_changed
    config_file = self.get_config()
  File "/tmp/ansible_4y5pmpro/ansible_module_jenkins_job.py", line 227, in get_config
    return job_config_to_string(self.config)
  File "/tmp/ansible_4y5pmpro/ansible_module_jenkins_job.py", line 336, in job_config_to_string
    return ET.tostring(ET.fromstring(xml_str))
  File "src/lxml/lxml.etree.pyx", line 3213, in lxml.etree.fromstring (src/lxml/lxml.etree.c:79010)
  File "src/lxml/parser.pxi", line 1843, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:118282)
ValueError: Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.

Additionally, after removing encoding declaration it fails inside python-jenkins with:

  File "/tmp/ansible_x5nesxl6/ansible_module_jenkins_job.py", line 279, in update_job
    self.server.reconfig_job(self.name, self.get_config())
  File "/home/mrk/repos/data-processing/venv36/lib/python3.6/site-packages/jenkins/__init__.py", line 1018, in reconfig_job
    self.jenkins_open(Request(reconfig_url, config_xml.encode('utf-8'),
AttributeError: 'bytes' object has no attribute 'encode'
ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

jenkins_job module

ADDITIONAL INFORMATION

How it works:
Instead of etree.{fromstring,tostring} it uses xml.etree.ElementTree.{fromstring,tostring} - it resolves problem with unicode strings with encoding declaration. Additionally it calls decode('ascii') on result of ElementTree.tostring - because ElementTree.tostring returns binary string encoded by default with 'US ascii' codec and python-jenkins library expects unicode strings.

I've tested this solution both on python2 and python3 on configs with and without encoding declaration and it seems to work in all cases.

Mariusz Kryński
@ansibot

This comment has been minimized.

Copy link
Contributor

commented Apr 7, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.