Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
avdv committed Aug 4, 2023
1 parent bbecdc0 commit 26d9f5a
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions haskell/private/pkgdb_to_bzl.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
package_conf_dir = os.path.join(topdir, 'package.conf.d')
else:
sys.exit("could not find package.conf.d directory at {}".format(topdir))
repo_root = os.getcwd()
else:
sys.exit("Usage: pkgdb_to_bzl.py <REPO_NAME> <TOPDIR>")

Expand All @@ -42,16 +43,13 @@ def resolve(path, pkgroot):
def path_to_label(path, pkgroot):
"""Substitute one pkgroot for another relative one to obtain a label."""
if path.find("${pkgroot}") != -1:
# determine if the given path is inside the pkgroot
# if it is not, replacing ${pkgroot} with topdir might result in a relative
# path that refers to the parent directory of the repository which is an error
# in that case, return an absolute path which needs to be symlinked to the
# determine if the given path is inside the repository root
# if it is not, return None to signal it needs to be symlinked into the
# repository
real_path = os.path.realpath(resolve(path, pkgroot))
if os.path.commonprefix([real_path, pkgroot]) == pkgroot:
return os.path.normpath(resolve(path, topdir)).replace('\\', '/')
else:
return os.path.normpath(resolve(path, pkgroot)).replace('\\', '/')
relative_path = os.path.relpath(real_path, start=repo_root)

return None if relative_path.startswith('..') else relative_path.replace('\\', '/')

topdir_relative_path = path.replace(pkgroot, "$topdir")
if topdir_relative_path.find("$topdir") != -1:
Expand Down Expand Up @@ -130,17 +128,15 @@ def hs_library_pattern(name, mode = "static", profiling = False):
haddock_html = None

if pkg.haddock_html:
haddock_html = path_to_label(pkg.haddock_html, pkgroot)
# We check if the file exists because cabal will unconditionally
# generate the database entry even if no haddock was generated.
if haddock_html:
if os.path.isabs(haddock_html):
absolute_haddock_html = haddock_html
resolved_haddock_html = resolve(pkg.haddock_html, pkgroot)

if os.path.exists(resolved_haddock_html):
haddock_html = path_to_label(pkg.haddock_html, pkgroot)
if not haddock_html:
haddock_html = os.path.join("haddock", "html", pkg.name)
output.append("#SYMLINK: {} {}".format(absolute_haddock_html, haddock_html))
elif os.path.exists(pkg.haddock_html):
haddock_html = os.path.join("haddock", "html", pkg.name)
output.append("#SYMLINK: {} {}".format(pkg.haddock_html, haddock_html))
output.append("#SYMLINK: {} {}".format(pkg.haddock_html, haddock_html))

# If there is many interfaces, we give them a number
interface_id = 0
Expand All @@ -154,19 +150,15 @@ def hs_library_pattern(name, mode = "static", profiling = False):

interface = path_to_label(interface_path, pkgroot)

if not interface or os.path.isabs(interface):
absolute_interface = interface
interface_link = os.path.join(
if not interface:
interface = os.path.join(
"haddock",
"interfaces",
pkg.name + "_" + str(interface_id) + ".haddock",
)
if interface_link.find('\\') == -1:
interface = interface_link
# skip creating symlink on windows
output.append("#SYMLINK: {} {}".format(absolute_interface or resolved_path, interface))
print("#SYMLINK: {} {}".format(absolute_interface or resolved_path, interface), file=sys.stderr)
interface_id += 1
output.append("#SYMLINK: {} {}".format(resolved_path, interface))
print("#SYMLINK: {} {}".format(resolved_path, interface), file=sys.stderr)
interface_id += 1
else:
print('no symlink:', interface, file=sys.stderr)
haddock_interfaces.append(interface)
Expand Down

0 comments on commit 26d9f5a

Please sign in to comment.