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 <qwan@redhat.com>
This commit is contained in:
Qixiang Wan 2017-03-24 15:20:55 -06:00
parent d1763fca7e
commit 0f508e2228
13 changed files with 54 additions and 71 deletions

View File

@ -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

View File

@ -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)

View File

@ -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")

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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(

View File

@ -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()

View File

@ -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()