Provide Python 3 matplotlib

Once I get around to making numpy/scipy imported for Python 3, I'll
probably upgrade everything to Python 3 and drop Python 2 support from
the codebase, but I don't feel like doing that quite yet.

Change-Id: I164c8fbb3ce922a83b56e46f5cc432004c3a989c
diff --git a/debian/matplotlib.BUILD b/debian/matplotlib.BUILD
index 5f89ba9..af2c4b7 100644
--- a/debian/matplotlib.BUILD
+++ b/debian/matplotlib.BUILD
@@ -1,124 +1,8 @@
-genrule(
-    name = "patch_init",
-    srcs = [
-        "usr/lib/python2.7/dist-packages/matplotlib/__init__.py",
-        "@//debian:matplotlib_patches",
-    ],
-    outs = ["matplotlib/__init__.py"],
-    cmd = " && ".join([
-        "cp $(location usr/lib/python2.7/dist-packages/matplotlib/__init__.py) $@",
-        "readonly PATCH=\"$$(readlink -f $(location @patch))\"",
-        "readonly FILE=\"$$(readlink -f $(location @//debian:matplotlib_patches))\"",
-        "(cd $(@D) && \"$${PATCH}\" -p1 < \"$${FILE}\") > /dev/null",
-    ]),
-    tools = [
-        "@patch",
-    ],
-)
+load("@//debian:matplotlib.bzl", "build_matplotlib")
 
-_src_files = glob(
-    include = ["usr/lib/python2.7/dist-packages/**/*.py"],
-    exclude = [
-        "usr/lib/python2.7/dist-packages/matplotlib/__init__.py",
-    ],
-)
+build_matplotlib("3", "3.5")
 
-_data_files = glob([
-    "usr/share/matplotlib/mpl-data/**",
-    "usr/share/tcltk/**",
-])
-
-_src_copied = ["/".join(f.split("/")[4:]) for f in _src_files]
-
-_builtin_so_files = glob([
-    "usr/lib/python2.7/dist-packages/**/*.x86_64-linux-gnu.so",
-    "usr/lib/python2.7/lib-dynload/*.so",
-])
-
-_system_so_files = glob([
-    "usr/lib/**/*.so*",
-    "lib/x86_64-linux-gnu/**/*.so*",
-])
-
-_builtin_so_copied = ["/".join(f.split("/")[4:]) for f in _builtin_so_files]
-
-_system_so_copied = ["rpathed/" + f for f in _system_so_files]
-
-_builtin_rpaths = [":".join([
-    "\\$$ORIGIN/%s" % rel,
-    "\\$$ORIGIN/%s/rpathed/usr/lib/x86_64-linux-gnu" % rel,
-    "\\$$ORIGIN/%s/rpathed/usr/lib" % rel,
-    "\\$$ORIGIN/%s/rpathed/lib/x86_64-linux-gnu" % rel,
-]) for rel in ["/".join([".." for _ in so.split("/")[1:]]) for so in _builtin_so_copied]]
-
-_system_rpaths = [":".join([
-    "\\$$ORIGIN/%s/rpathed/usr/lib/x86_64-linux-gnu" % rel,
-    "\\$$ORIGIN/%s/rpathed/lib/x86_64-linux-gnu" % rel,
-]) for rel in ["/".join([".." for _ in so.split("/")[1:]]) for so in _system_so_copied]]
-
-genrule(
-    name = "run_patchelf_builtin",
-    srcs = _builtin_so_files,
-    outs = _builtin_so_copied,
-    cmd = "\n".join(
-        [
-            "cp $(location %s) $(location %s)" % (src, dest)
-            for src, dest in zip(_builtin_so_files, _builtin_so_copied)
-        ] +
-        ["$(location @patchelf) --set-rpath %s $(location %s)" % (rpath, so) for rpath, so in zip(_builtin_rpaths, _builtin_so_copied)],
-    ),
-    tools = [
-        "@patchelf",
-    ],
-)
-
-genrule(
-    name = "run_patchelf_system",
-    srcs = _system_so_files,
-    outs = _system_so_copied,
-    cmd = "\n".join(
-        [
-            "cp $(location %s) $(location %s)" % (src, dest)
-            for src, dest in zip(_system_so_files, _system_so_copied)
-        ] +
-        ["$(location @patchelf) --set-rpath %s $(location %s)" % (rpath, so) for rpath, so in zip(_system_rpaths, _system_so_copied)],
-    ),
-    tools = [
-        "@patchelf",
-    ],
-)
-
-genrule(
-    name = "copy_files",
-    srcs = _src_files,
-    outs = _src_copied,
-    cmd = " && ".join(["cp $(location %s) $(location %s)" % (src, dest) for src, dest in zip(
-        _src_files,
-        _src_copied,
-    )]),
-)
-
-genrule(
-    name = "create_rc",
-    outs = ["usr/share/matplotlib/mpl-data/matplotlibrc"],
-    cmd = "\n".join([
-        "cat > $@ << END",
-        # This is necessary to make matplotlib actually plot things to the
-        # screen by default.
-        "backend      : TkAgg",
-        "END",
-    ]),
-)
-
-py_library(
-    name = "matplotlib",
-    srcs = _src_copied + [
-        "matplotlib/__init__.py",
-    ],
-    data = _data_files + _builtin_so_copied + _system_so_copied + [
-        ":usr/share/matplotlib/mpl-data/matplotlibrc",
-    ],
-    imports = ["usr/lib/python2.7/dist-packages"],
-    restricted_to = ["@//tools:k8"],
-    visibility = ["//visibility:public"],
+build_matplotlib(
+    "2.7",
+    copy_shared_files = False,
 )