Skip to content

Commit 2c88271

Browse files
committed
Handle ${pkgroot} results in relative paths outside of repository
1 parent 612c9de commit 2c88271

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

Diff for: haskell/private/pkgdb_to_bzl.py

+27-9
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,27 @@
2929
package_conf_dir = os.path.join(topdir, 'package.conf.d')
3030
else:
3131
sys.exit("could not find package.conf.d directory at {}".format(topdir))
32+
repo_root = os.getcwd()
3233
else:
3334
sys.exit("Usage: pkgdb_to_bzl.py <REPO_NAME> <TOPDIR>")
3435

36+
def resolve(path, pkgroot):
37+
"""Resolve references to ${pkgroot} with the given value"""
38+
if path.find("${pkgroot}") != -1:
39+
return path.strip("\"").replace("${pkgroot}", pkgroot)
40+
else:
41+
return path
42+
3543
def path_to_label(path, pkgroot):
3644
"""Substitute one pkgroot for another relative one to obtain a label."""
3745
if path.find("${pkgroot}") != -1:
38-
return os.path.normpath(path.strip("\"").replace("${pkgroot}", topdir)).replace('\\', '/')
46+
# determine if the given path is inside the repository root
47+
# if it is not, return None to signal it needs to be symlinked into the
48+
# repository
49+
real_path = os.path.realpath(resolve(path, pkgroot))
50+
relative_path = os.path.relpath(real_path, start=repo_root)
51+
52+
return None if relative_path.startswith('..') else relative_path.replace('\\', '/')
3953

4054
topdir_relative_path = path.replace(pkgroot, "$topdir")
4155
if topdir_relative_path.find("$topdir") != -1:
@@ -114,31 +128,35 @@ def hs_library_pattern(name, mode = "static", profiling = False):
114128
haddock_html = None
115129

116130
if pkg.haddock_html:
117-
haddock_html = path_to_label(pkg.haddock_html, pkgroot)
118131
# We check if the file exists because cabal will unconditionally
119132
# generate the database entry even if no haddock was generated.
120-
if not haddock_html and os.path.exists(pkg.haddock_html):
121-
haddock_html = os.path.join("haddock", "html", pkg.name)
122-
output.append("#SYMLINK: {} {}".format(pkg.haddock_html, haddock_html))
133+
resolved_haddock_html = resolve(pkg.haddock_html, pkgroot)
134+
135+
if os.path.exists(resolved_haddock_html):
136+
haddock_html = path_to_label(pkg.haddock_html, pkgroot)
137+
if not haddock_html:
138+
haddock_html = os.path.join("haddock", "html", pkg.name)
139+
output.append("#SYMLINK: {} {}".format(pkg.haddock_html, haddock_html))
123140

124141
# If there is many interfaces, we give them a number
125142
interface_id = 0
126143
haddock_interfaces = []
127144
for interface_path in pkg.haddock_interfaces:
128-
interface = path_to_label(interface_path, pkgroot)
145+
resolved_path = resolve(interface_path, pkgroot).replace('\\', '/')
129146

130147
# We check if the file exists because cabal will unconditionally
131148
# generate the database entry even if no haddock was generated.
132-
if not os.path.exists(interface or interface_path):
133-
continue
149+
if not os.path.exists(resolved_path): continue
150+
151+
interface = path_to_label(interface_path, pkgroot)
134152

135153
if not interface:
136154
interface = os.path.join(
137155
"haddock",
138156
"interfaces",
139157
pkg.name + "_" + str(interface_id) + ".haddock",
140158
)
141-
output.append("#SYMLINK: {} {}".format(interface_path, interface))
159+
output.append("#SYMLINK: {} {}".format(resolved_path, interface))
142160
interface_id += 1
143161
haddock_interfaces.append(interface)
144162

0 commit comments

Comments
 (0)