about summary refs log tree commit diff
path: root/users/Profpatsch/parked
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2024-10-05T12·37+0200
committerProfpatsch <mail@profpatsch.de>2024-10-05T13·49+0000
commit2b5a10a45c9c183aa4593620757beb15f9203fff (patch)
treec69dc91d13119c57d7b1168d5f7db10d10bcc4bf /users/Profpatsch/parked
parentb0ee9cd6947a382067a0506aa79567f4410c6c49 (diff)
chore(users/Profpatsch/reverse-haskell-deps): park r/8779
Change-Id: I3940764f8aba806f97d62b0e1cf8d200aa6346ff
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12582
Reviewed-by: Profpatsch <mail@profpatsch.de>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/Profpatsch/parked')
-rw-r--r--users/Profpatsch/parked/reverse-haskell-deps/README.md3
-rw-r--r--users/Profpatsch/parked/reverse-haskell-deps/ReverseHaskellDeps.hs76
-rw-r--r--users/Profpatsch/parked/reverse-haskell-deps/default.nix.inactive32
-rw-r--r--users/Profpatsch/parked/reverse-haskell-deps/reverse-haskell-deps.cabal16
4 files changed, 127 insertions, 0 deletions
diff --git a/users/Profpatsch/parked/reverse-haskell-deps/README.md b/users/Profpatsch/parked/reverse-haskell-deps/README.md
new file mode 100644
index 000000000000..efc288cae4a9
--- /dev/null
+++ b/users/Profpatsch/parked/reverse-haskell-deps/README.md
@@ -0,0 +1,3 @@
+# reverse-haskell-deps
+
+Parse the HTML at `https://packdeps.haskellers.com/reverse` to get the data about Haskell package reverse dependencies in a structured way (they should just expose that as a json tbh).
diff --git a/users/Profpatsch/parked/reverse-haskell-deps/ReverseHaskellDeps.hs b/users/Profpatsch/parked/reverse-haskell-deps/ReverseHaskellDeps.hs
new file mode 100644
index 000000000000..0e18ce8a6b37
--- /dev/null
+++ b/users/Profpatsch/parked/reverse-haskell-deps/ReverseHaskellDeps.hs
@@ -0,0 +1,76 @@
+{-# LANGUAGE LambdaCase #-}
+{-# LANGUAGE MultiWayIf #-}
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE ScopedTypeVariables #-}
+
+module Main where
+
+import Data.ByteString qualified as ByteString
+import Data.Either
+import Data.List qualified as List
+import Data.Maybe
+import Data.Text (Text)
+import Data.Text qualified as Text
+import Data.Text.Encoding qualified
+import MyPrelude
+import Numeric.Natural
+import Text.HTML.TagSoup qualified as Tag
+import Text.Nicify
+import Text.Read qualified as Read
+
+parseNat :: Text -> Maybe Natural
+parseNat = Read.readMaybe . textToString
+
+printNice :: Show a => a -> IO ()
+printNice = putStrLn . nicify . show
+
+type Tag = Tag.Tag Text
+
+main = do
+  reverseHtml <- readStdinUtf8
+  printNice $ List.sortOn snd $ packagesAndReverseDeps reverseHtml
+  where
+    readStdinUtf8 = bytesToTextUtf8Lenient <$> ByteString.getContents
+
+-- | reads the table provided by https://packdeps.haskellers.com/reverse
+-- figuring out all sections (starting with the link to the package name),
+-- then figuring out the name of the package and the first column,
+-- which is the number of reverse dependencies of the package
+packagesAndReverseDeps :: Text -> [(Text, Natural)]
+packagesAndReverseDeps reverseHtml = do
+  let tags = Tag.parseTags reverseHtml
+  let sections = Tag.partitions (isJust . reverseLink) tags
+  let sectionName [] = "<unknown section>"
+      sectionName (sect : _) = sect & reverseLink & fromMaybe "<unknown section>"
+  let sectionNames = map sectionName sections
+  mapMaybe
+    ( \(name :: Text, sect) -> do
+        reverseDeps <- firstNaturalNumber sect
+        pure (sectionPackageName name sect, reverseDeps) :: Maybe (Text, Natural)
+    )
+    $ zip sectionNames sections
+  where
+    reverseLink = \case
+      Tag.TagOpen "a" attrs -> findMaybe attrReverseLink attrs
+      _ -> Nothing
+
+    attrReverseLink = \case
+      ("href", lnk) ->
+        if
+            | "packdeps.haskellers.com/reverse/" `Text.isInfixOf` lnk -> Just lnk
+            | otherwise -> Nothing
+      _ -> Nothing
+
+    sectionPackageName :: Text -> [Tag] -> Text
+    sectionPackageName sectionName = \case
+      (_ : Tag.TagText name : _) -> name
+      (_ : el : _) -> sectionName
+      xs -> sectionName
+
+    firstNaturalNumber :: [Tag] -> Maybe Natural
+    firstNaturalNumber =
+      findMaybe
+        ( \case
+            Tag.TagText t -> parseNat t
+            _ -> Nothing
+        )
diff --git a/users/Profpatsch/parked/reverse-haskell-deps/default.nix.inactive b/users/Profpatsch/parked/reverse-haskell-deps/default.nix.inactive
new file mode 100644
index 000000000000..b0a44420d793
--- /dev/null
+++ b/users/Profpatsch/parked/reverse-haskell-deps/default.nix.inactive
@@ -0,0 +1,32 @@
+{ depot, pkgs, ... }:
+
+# Parses https://packdeps.haskellers.com/reverse
+# and outputs the amount of reverse dependencies of each hackage package.
+
+let
+
+  rev = depot.nix.writeExecline "reverse-haskell-deps" { } [
+    "pipeline"
+    [
+      "${pkgs.curl}/bin/curl"
+      "-L"
+      "https://packdeps.haskellers.com/reverse"
+    ]
+    rev-hs
+
+  ];
+
+  rev-hs = pkgs.writers.writeHaskell "revers-haskell-deps-hs"
+    {
+      libraries = [
+        depot.users.Profpatsch.my-prelude
+        pkgs.haskellPackages.nicify-lib
+        pkgs.haskellPackages.tagsoup
+      ];
+      ghcArgs = [ "-threaded" ];
+    }
+    ./ReverseHaskellDeps.hs;
+
+
+in
+rev
diff --git a/users/Profpatsch/parked/reverse-haskell-deps/reverse-haskell-deps.cabal b/users/Profpatsch/parked/reverse-haskell-deps/reverse-haskell-deps.cabal
new file mode 100644
index 000000000000..4792f52adf25
--- /dev/null
+++ b/users/Profpatsch/parked/reverse-haskell-deps/reverse-haskell-deps.cabal
@@ -0,0 +1,16 @@
+cabal-version:      3.0
+name:               reverse-haskell-deps
+version:            0.1.0.0
+author:             Profpatsch
+maintainer:         mail@profpatsch.de
+
+library
+    exposed-modules:          ReverseHaskellDeps.hs
+
+    build-depends:
+        base >=4.15 && <5,
+        my-prelude,
+        tagsoup,
+        nicify-lib
+
+    default-language: Haskell2010