Add the parse_netrc function

The existing `read_netrc` function can only be used from repository rules.
This makes it harder to e.g. test code that builds on top of `read_netrc`.
The new function is pure and can be easily tested.

Closes #12681.

PiperOrigin-RevId: 351586361
diff --git a/tools/build_defs/repo/utils.bzl b/tools/build_defs/repo/utils.bzl
index 533f086..5a1147e 100644
--- a/tools/build_defs/repo/utils.bzl
+++ b/tools/build_defs/repo/utils.bzl
@@ -213,6 +213,19 @@
       about them
     """
     contents = ctx.read(filename)
+    return parse_netrc(contents, filename)
+
+def parse_netrc(contents, filename = None):
+    """Utility function to parse at least a basic .netrc file.
+
+    Args:
+      contents: input for the parser.
+      filename: filename to use in error messages, if any.
+
+    Returns:
+      dict mapping a machine names to a dict with the information provided
+      about them
+    """
 
     # Parse the file. This is mainly a token-based update of a simple state
     # machine, but we need to keep the line structure to correctly determine
@@ -283,6 +296,8 @@
                     currentmachinename = ""
                     currentmachine = {}
                 else:
+                    if filename == None:
+                        filename = "a .netrc file"
                     fail("Unexpected token '%s' while reading %s" %
                          (token, filename))
     if not currentmachinename == None: