Skip to content

Commit

Permalink
[makeotf] Fix makeRelativePath
Browse files Browse the repository at this point in the history
Return the original path when it can't be made relative because the paths are in different drives/mounts

Fixes #602
  • Loading branch information
miguelsousa committed Sep 11, 2018
1 parent e0a318c commit a39b3ad
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
19 changes: 12 additions & 7 deletions python/afdko/makeotf.py
Expand Up @@ -25,7 +25,7 @@
"""

__version__ = """\
makeotf.py v2.5.0 Aug 28 2018
makeotf.py v2.5.1 Sep 11 2018
"""

__methods__ = """
Expand Down Expand Up @@ -2283,18 +2283,23 @@ def makeRelativePath(curDir, targetPath):
if targetPath is None:
return

targetPath = os.path.abspath(targetPath)
curDir = os.path.abspath(curDir)
targetPath = os.path.relpath(targetPath, curDir)
return targetPath
abs_targetPath = os.path.abspath(targetPath)
abs_curDir = os.path.abspath(curDir)
try:
return os.path.relpath(abs_targetPath, abs_curDir)
except ValueError:
# the paths are on different drives/mounts
return targetPath


def makeRelativePaths(makeOTFParams):
"""
Change file paths to be relative to fontDir,
if possible, else to absolute paths.
"""
inputFilePath = getattr(makeOTFParams, kFileOptPrefix + kInputFont)
fontDir = os.path.dirname(os.path.abspath(inputFilePath))

# Change file paths to be relative to fontDir,
# if possible, else to absolute paths.
inputFilePath = makeRelativePath(fontDir, inputFilePath)
setattr(makeOTFParams, kFileOptPrefix + kInputFont, inputFilePath)

Expand Down
25 changes: 24 additions & 1 deletion tests/makeotf_test.py
Expand Up @@ -14,7 +14,8 @@

from afdko.makeotf import (
checkIfVertInFeature, getOptions, MakeOTFParams, getSourceGOADBData,
readOptionFile, writeOptionsFile, kMOTFOptions, kOptionNotSeen)
readOptionFile, writeOptionsFile, kMOTFOptions, kOptionNotSeen,
makeRelativePath)

from .runner import main as runner
from .differ import main as differ
Expand Down Expand Up @@ -345,6 +346,28 @@ def test_readOptionFile_filenotfound():
assert readOptionFile(proj_path, params, 0) == (True, 0)


@pytest.mark.parametrize('cur_dir, target_path, result', [
('/folder/folder2', '/folder/folder2/font.pfa', 'font.pfa'),
('/folder/folder2', '/folder/font.pfa', '../font.pfa'),
('/folder', '/folder/font.pfa', 'font.pfa'),
('/folder', '/font.pfa', '../font.pfa'),
('/folder', None, None),
])
def test_makeRelativePath(cur_dir, target_path, result):
if result:
result = os.path.normpath(result)
assert makeRelativePath(cur_dir, target_path) == result


@pytest.mark.skipif(sys.platform != 'win32', reason="windows-only")
@pytest.mark.parametrize('cur_dir, target_path, result', [
('C:\\folder', 'C:\\folder\\font.pfa', 'font.pfa'),
('F:\\folder', 'C:\\folder\\font.pfa', 'C:\\folder\\font.pfa'),
])
def test_makeRelativePath_win_only(cur_dir, target_path, result):
assert makeRelativePath(cur_dir, target_path) == result


@pytest.mark.parametrize('args, input_filename, ttx_filename', [
(['r'], T1PFA_NAME, 't1pfa-cmap.ttx'),
(['r', 'cs', '_1'], T1PFA_NAME, 't1pfa-cmap_cs1.ttx'),
Expand Down

0 comments on commit a39b3ad

Please sign in to comment.