From 0f508e2228a32fb71b156be4d948b76849ec085d Mon Sep 17 00:00:00 2001 From: Qixiang Wan Date: Mar 29 2017 02:12:32 +0000 Subject: move translate_path from paths.py to util.py So translate_path can be used in util.py, or it will result in circular import error. Signed-off-by: Qixiang Wan --- diff --git a/doc/configuration.rst b/doc/configuration.rst index d4df326..8868178 100644 --- a/doc/configuration.rst +++ b/doc/configuration.rst @@ -1405,7 +1405,7 @@ Example usage ------------- :: - >>> from pungi.paths import translate_paths + >>> from pungi.util import translate_paths >>> print translate_paths(compose_object_with_mapping, "/mnt/a/c/somefile") http://b/dir/c/somefile diff --git a/pungi/notifier.py b/pungi/notifier.py index a7d4c36..3e56d21 100644 --- a/pungi/notifier.py +++ b/pungi/notifier.py @@ -15,7 +15,7 @@ import json import threading -import pungi.paths +import pungi.util from kobo import shortcuts @@ -39,7 +39,7 @@ class PungiNotifier(object): data.setdefault('compose_id', self.compose.compose_id) # Publish where in the world this compose will end up living - location = pungi.paths.translate_path( + location = pungi.util.translate_path( self.compose, self.compose.paths.compose.topdir()) data.setdefault('location', location) diff --git a/pungi/paths.py b/pungi/paths.py index 0919ac5..f00f22c 100644 --- a/pungi/paths.py +++ b/pungi/paths.py @@ -25,27 +25,6 @@ import os from pungi.util import makedirs -def translate_path(compose, path): - """ - @param compose - required for access to config - @param path - """ - normpath = os.path.normpath(path) - mapping = compose.conf["translate_paths"] - - for prefix, newvalue in mapping: - prefix = os.path.normpath(prefix) - # Strip trailing slashes: the prefix has them stripped by `normpath`. - newvalue = newvalue.rstrip('/') - if normpath.startswith(prefix): - # 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") diff --git a/pungi/phases/image_build.py b/pungi/phases/image_build.py index 1e870ae..3fe1e35 100644 --- a/pungi/phases/image_build.py +++ b/pungi/phases/image_build.py @@ -6,9 +6,9 @@ import time from kobo import shortcuts from pungi.util import get_variant_data, makedirs, get_mtime, get_file_size, failable +from pungi.util import translate_path from pungi.phases import base from pungi.linker import Linker -from pungi.paths import translate_path from pungi.wrappers.kojiwrapper import KojiWrapper from kobo.threads import ThreadPool, WorkerThread from productmd.images import Image diff --git a/pungi/phases/live_images.py b/pungi/phases/live_images.py index c771990..7b56182 100644 --- a/pungi/phases/live_images.py +++ b/pungi/phases/live_images.py @@ -28,7 +28,7 @@ from pungi.wrappers.kojiwrapper import KojiWrapper from pungi.wrappers import iso from pungi.phases import base from pungi.util import get_arch_variant_data, makedirs, get_mtime, get_file_size, failable -from pungi.paths import translate_path +from pungi.util import translate_path # HACK: define cmp in python3 diff --git a/pungi/phases/livemedia_phase.py b/pungi/phases/livemedia_phase.py index 7970c1f..3cb63eb 100644 --- a/pungi/phases/livemedia_phase.py +++ b/pungi/phases/livemedia_phase.py @@ -5,9 +5,9 @@ import time from kobo import shortcuts from pungi.util import get_variant_data, makedirs, get_mtime, get_file_size, failable +from pungi.util import translate_path from pungi.phases.base import ConfigGuardedPhase, ImageConfigMixin, PhaseLoggerMixin from pungi.linker import Linker -from pungi.paths import translate_path from pungi.wrappers.kojiwrapper import KojiWrapper from kobo.threads import ThreadPool, WorkerThread from productmd.images import Image diff --git a/pungi/phases/osbs.py b/pungi/phases/osbs.py index 974500b..b6769cc 100644 --- a/pungi/phases/osbs.py +++ b/pungi/phases/osbs.py @@ -8,7 +8,6 @@ from kobo import shortcuts from .base import ConfigGuardedPhase, PhaseLoggerMixin from .. import util from ..wrappers import kojiwrapper -from ..paths import translate_path class OSBSPhase(PhaseLoggerMixin, ConfigGuardedPhase): @@ -141,10 +140,10 @@ class OSBSThread(WorkerThread): with open(repo_file, 'w') as f: f.write('[%s]\n' % compose.compose_id) f.write('name=Compose %s (RPMs)\n' % compose.compose_id) - f.write('baseurl=%s\n' % translate_path(compose, os_tree)) + f.write('baseurl=%s\n' % util.translate_path(compose, os_tree)) f.write('enabled=1\n') f.write('gpgcheck=%s\n' % gpgcheck) if gpgcheck: f.write('gpgkey=%s\n' % gpgkey) - return translate_path(compose, repo_file) + return util.translate_path(compose, repo_file) diff --git a/pungi/phases/ostree.py b/pungi/phases/ostree.py index 71fca4f..9b268ad 100644 --- a/pungi/phases/ostree.py +++ b/pungi/phases/ostree.py @@ -8,7 +8,7 @@ from kobo.threads import ThreadPool, WorkerThread from .base import ConfigGuardedPhase from .. import util from ..ostree.utils import get_ref_from_treefile, get_commitid_from_commitid_file -from ..paths import translate_path +from ..util import translate_path from ..wrappers import kojiwrapper, scm diff --git a/pungi/phases/ostree_installer.py b/pungi/phases/ostree_installer.py index 0008a1d..508d2f5 100644 --- a/pungi/phases/ostree_installer.py +++ b/pungi/phases/ostree_installer.py @@ -9,8 +9,7 @@ from kobo import shortcuts from .base import ConfigGuardedPhase, PhaseLoggerMixin from .. import util -from ..paths import translate_path -from ..util import get_volid +from ..util import get_volid, translate_path from ..wrappers import kojiwrapper, iso, lorax, scm diff --git a/pungi/util.py b/pungi/util.py index 665eb73..0fbed14 100644 --- a/pungi/util.py +++ b/pungi/util.py @@ -635,3 +635,24 @@ def run_unmount_cmd(cmd, max_retries=10, path=None, logger=None): logger.debug('`%s` command not available for debugging', ' '.join(c)) raise RuntimeError('Failed to run %r: Device or resource busy.' % cmd) + + +def translate_path(compose, path): + """ + @param compose - required for access to config + @param path + """ + normpath = os.path.normpath(path) + mapping = compose.conf["translate_paths"] + + for prefix, newvalue in mapping: + prefix = os.path.normpath(prefix) + # Strip trailing slashes: the prefix has them stripped by `normpath`. + newvalue = newvalue.rstrip('/') + if normpath.startswith(prefix): + # 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 diff --git a/tests/test_notifier.py b/tests/test_notifier.py index 8077e1c..282189d 100644 --- a/tests/test_notifier.py +++ b/tests/test_notifier.py @@ -13,7 +13,7 @@ from pungi.notifier import PungiNotifier class TestNotifier(unittest.TestCase): - @mock.patch('pungi.paths.translate_path') + @mock.patch('pungi.util.translate_path') @mock.patch('kobo.shortcuts.run') def test_invokes_script(self, run, translate_path): compose = mock.Mock( @@ -73,7 +73,7 @@ class TestNotifier(unittest.TestCase): n.send('cmd', foo='bar', baz='quux') self.assertFalse(run.called) - @mock.patch('pungi.paths.translate_path') + @mock.patch('pungi.util.translate_path') @mock.patch('kobo.shortcuts.run') def test_logs_warning_on_failure(self, run, translate_path): compose = mock.Mock( diff --git a/tests/test_paths.py b/tests/test_paths.py deleted file mode 100644 index e2ed8e9..0000000 --- a/tests/test_paths.py +++ /dev/null @@ -1,36 +0,0 @@ -#!/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={'translate_paths': []}) - 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() diff --git a/tests/test_util.py b/tests/test_util.py index 9af308a..cb5b26a 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -519,5 +519,26 @@ class TestUnmountCmd(unittest.TestCase): 'lsof +D /path', 1, 'lsof output')]) +class TranslatePathTestCase(unittest.TestCase): + def test_does_nothing_without_config(self): + compose = mock.Mock(conf={'translate_paths': []}) + ret = util.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 = util.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 = util.translate_path(compose, '/mnt/fedora_koji/compose/rawhide/XYZ') + self.assertEqual(ret, '/mnt/fedora_koji/compose/rawhide/XYZ') + + if __name__ == "__main__": unittest.main()