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

Python 2.6 support #34

Merged
merged 1 commit into from
Aug 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ env:
matrix:
allow_failures:
- env: TOXENV=cov
- env: TOXENV=py26
install:
- pip install --quiet --use-mirrors tox
script:
Expand Down
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,15 @@ The following package managers are supported:
- pacman (Arch Linux, Manjaro, ...)
- rpm (Fedora, Red Hat, OpenSUSE, ...)

The following Python versions are supported:
The following Python versions are fully supported:

- Python 2.7
- Python 3.3+
- PyPy

Python 2.6 should also work, but you need to manually install argparse from the
`Python Package Index <https://pypi.python.org/pypi/swid_generator/>`__.

Install with pip
----------------

Expand Down
2 changes: 1 addition & 1 deletion swid_generator/generators/swid_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,5 @@ def create_swid_tags(environment, entity_name, regid, full=False, matcher=all_ma
payload_tag = _create_payload_tag(pi)
software_identity.append(payload_tag)

swidtag_flat = ET.tostring(software_identity, encoding='utf-8', method='xml').replace(b'\n', b'')
swidtag_flat = ET.tostring(software_identity, encoding='utf-8').replace(b'\n', b'')
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argument does't exist in 2.6. xml is the default anyways.

yield XML_DECLARATION.encode('utf-8') + swidtag_flat
34 changes: 34 additions & 0 deletions swid_generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
from __future__ import print_function, division, absolute_import, unicode_literals

import sys
import logging
import subprocess

from .argparser import MainArgumentParser
from .environments.environment_registry import EnvironmentRegistry
Expand All @@ -36,15 +38,45 @@
from .exceptions import AutodetectionError, EnvironmentNotInstalledError


def py26_check_output(*popenargs, **kwargs):
"""
This function is an ugly hack to monkey patch the backported `check_output`
method into the subprocess module.

Taken from https://gist.github.com/edufelipe/1027906.

"""
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get('args')
if cmd is None:
cmd = popenargs[0]
error = subprocess.CalledProcessError(retcode, cmd)
error.output = output
raise error
return output


def main():
# Python 2.6 compatibility
if 'check_output' not in dir(subprocess):
# Ugly monkey patching hack ahead
logging.debug('Monkey patching subprocess.check_output')
subprocess.check_output = py26_check_output

# Register environments
environment_registry = EnvironmentRegistry()
environment_registry.register('rpm', RpmEnvironment)
environment_registry.register('dpkg', DpkgEnvironment)
environment_registry.register('pacman', PacmanEnvironment)

# Parse arguments
parser = MainArgumentParser(environment_registry)
options = parser.parse() # without any parameter it takes arguments passed by command line

# Get correct environment
try:
env = environment_registry.get_environment(options.env)
except EnvironmentNotInstalledError:
Expand All @@ -55,6 +87,8 @@ def main():
parser.print_usage()
sys.exit(3)

# Handle commands

if options.command == 'swid':
swid_args = {
'environment': env,
Expand Down
3 changes: 0 additions & 3 deletions tests/fixtures/rpm_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ def rpm_document_strings(rpm_environment):

@pytest.fixture
def rpm_networkmanager_generated(rpm_document_strings):
ET.register_namespace('', 'http://standards.iso.org/iso/19770/-2/2014/schema.xsd')
return get_swid_by_name(rpm_document_strings, 'NetworkManager')
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method does not exist in 2.6. This is just testing code. The tests appear to work without this line.



@pytest.fixture
def rpm_documents_as_xml(rpm_document_strings):
ET.register_namespace('', 'http://standards.iso.org/iso/19770/-2/2014/schema.xsd')
return [ET.fromstring(document) for document in rpm_document_strings]


Expand All @@ -56,7 +54,6 @@ def rpm_networkmanager_template():

### Helper Functions ###
def get_swid_by_name(rpm_document_strings, name):
ET.register_namespace('', 'http://standards.iso.org/iso/19770/-2/2014/schema.xsd')
for document in rpm_document_strings:
if name in document.decode('utf8'):
return ET.fromstring(document)
5 changes: 5 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ commands = py.test \
--basetemp={envtmpdir} \
{posargs}

[testenv:py26]
deps =
{[testenv]deps}
argparse

[testenv:pypy]
basepython = pypy

Expand Down