[paths] Document and test translate_path

The documentation was inaccurate.

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2016-03-07 15:09:07 +01:00
parent f202d24961
commit 8e20f216f1
3 changed files with 45 additions and 6 deletions

View File

@ -905,13 +905,12 @@ Translate Paths Settings
========================
**translate_paths**
(*list*) -- list of paths to translate; format: [(path,translated_path)]
(*list*) -- list of paths to translate; format: ``[(path, translated_path)]``
.. note::
This feature becomes useful when you need to transform compose location
into e.g. a http repo which is can be passed to koji image-build.
Translation needs to be invoked by a function call in pungi.
os.path.normpath() is applied on both path and translated_path
into e.g. a HTTP repo which is can be passed to ``koji image-build``.
The ``path`` part is normalized via ``os.path.normpath()``.
Example config

View File

@ -25,6 +25,7 @@ import os
from pungi.util import makedirs
def translate_path(compose, path):
"""
@param compose - required for access to config
@ -36,11 +37,14 @@ def translate_path(compose, path):
for prefix, newvalue in mapping:
prefix = os.path.normpath(prefix)
if normpath.startswith(prefix):
# don't call os.path.normpath on result since that would break http:// -> http:/ and so on
return normpath.replace(prefix, newvalue, 1) # replace only 1 occurance
# We can't call os.path.normpath on result since it is not actually
# a path - http:// would get changed to http:/ and so on.
# Only the first occurance should be replaced.
return normpath.replace(prefix, newvalue, 1)
return normpath
class Paths(object):
def __init__(self, compose):
paths_module_name = compose.conf.get("paths_module", None)

36
tests/test_paths.py Normal file
View File

@ -0,0 +1,36 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import mock
import unittest
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from pungi import paths
class TranslatePathTestCase(unittest.TestCase):
def test_does_nothing_without_config(self):
compose = mock.Mock(conf={})
ret = paths.translate_path(compose, '/mnt/koji/compose/rawhide/XYZ')
self.assertEqual(ret, '/mnt/koji/compose/rawhide/XYZ')
def test_translates_prefix(self):
compose = mock.Mock(conf={
'translate_paths': [('/mnt/koji', 'http://example.com')]
})
ret = paths.translate_path(compose, '/mnt/koji/compose/rawhide/XYZ')
self.assertEqual(ret, 'http://example.com/compose/rawhide/XYZ')
def test_does_not_translate_not_matching(self):
compose = mock.Mock(conf={
'translate_paths': [('/mnt/koji', 'http://example.com')]
})
ret = paths.translate_path(compose, '/mnt/fedora_koji/compose/rawhide/XYZ')
self.assertEqual(ret, '/mnt/fedora_koji/compose/rawhide/XYZ')
if __name__ == "__main__":
unittest.main()