|
29 | 29 | package_conf_dir = os.path.join(topdir, 'package.conf.d')
|
30 | 30 | else:
|
31 | 31 | sys.exit("could not find package.conf.d directory at {}".format(topdir))
|
| 32 | + repo_root = os.getcwd() |
32 | 33 | else:
|
33 | 34 | sys.exit("Usage: pkgdb_to_bzl.py <REPO_NAME> <TOPDIR>")
|
34 | 35 |
|
| 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 | + |
35 | 43 | def path_to_label(path, pkgroot):
|
36 | 44 | """Substitute one pkgroot for another relative one to obtain a label."""
|
37 | 45 | 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('\\', '/') |
39 | 53 |
|
40 | 54 | topdir_relative_path = path.replace(pkgroot, "$topdir")
|
41 | 55 | if topdir_relative_path.find("$topdir") != -1:
|
@@ -114,31 +128,35 @@ def hs_library_pattern(name, mode = "static", profiling = False):
|
114 | 128 | haddock_html = None
|
115 | 129 |
|
116 | 130 | if pkg.haddock_html:
|
117 |
| - haddock_html = path_to_label(pkg.haddock_html, pkgroot) |
118 | 131 | # We check if the file exists because cabal will unconditionally
|
119 | 132 | # 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)) |
123 | 140 |
|
124 | 141 | # If there is many interfaces, we give them a number
|
125 | 142 | interface_id = 0
|
126 | 143 | haddock_interfaces = []
|
127 | 144 | for interface_path in pkg.haddock_interfaces:
|
128 |
| - interface = path_to_label(interface_path, pkgroot) |
| 145 | + resolved_path = resolve(interface_path, pkgroot).replace('\\', '/') |
129 | 146 |
|
130 | 147 | # We check if the file exists because cabal will unconditionally
|
131 | 148 | # 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) |
134 | 152 |
|
135 | 153 | if not interface:
|
136 | 154 | interface = os.path.join(
|
137 | 155 | "haddock",
|
138 | 156 | "interfaces",
|
139 | 157 | pkg.name + "_" + str(interface_id) + ".haddock",
|
140 | 158 | )
|
141 |
| - output.append("#SYMLINK: {} {}".format(interface_path, interface)) |
| 159 | + output.append("#SYMLINK: {} {}".format(resolved_path, interface)) |
142 | 160 | interface_id += 1
|
143 | 161 | haddock_interfaces.append(interface)
|
144 | 162 |
|
|
0 commit comments