Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pythonPackages.diagrams: init at 0.17.0 #101533

Closed
wants to merge 2 commits into from

Conversation

addict3d
Copy link
Contributor

Motivation for this change

Add python diagrams library. Generate nice architecture diagrams
with python code, allowing for rapid iteration.

The images are rendered using dot from graphviz. It outputs your choice of [png jpg svg pdf]

Closes #101532

Things done

I generated some diagrams using this library, with both of the following shell expressions. There are many examples on the project's website.

nix-shell -I nixpkgs=./ -p python3 python3Packages.diagrams python3Packages.graphviz
nix-shell -I nixpkgs=./ -E 'with import <nixpkgs> {}; (python3.withPackages (ps: [ps.diagrams ps.graphviz])).env'

The closure size is ~150M.
The library itself has ~20M in images.

@evanjs
Copy link
Member

evanjs commented Oct 24, 2020

Please move the maintainers.list change into a separate commit

@evanjs
Copy link
Member

evanjs commented Oct 24, 2020

Perhaps this could be added to all-packages as well.

e.g.

openrazer-daemon = with python3Packages; toPythonApplication openrazer-daemon;

@addict3d addict3d force-pushed the add-python-diagrams-from-pypi branch from 0ee823b to 847aa7e Compare October 24, 2020 13:52
@addict3d
Copy link
Contributor Author

Perhaps this could be added to all-packages as well.

e.g.

openrazer-daemon = with python3Packages; toPythonApplication openrazer-daemon;

Oh cool. I wasn't aware of that. Added it.

Rebased to resolve the suggestions. I've tried my best to follow the nixpkgs and python-modules conventions and am happy to learn more and make any changes.

Copy link
Member

@mweinelt mweinelt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks pretty good already.

It is customary for python packages to get the checkPhase running. Currently no tests are being found, even though upstream has tests. This is because the PyPi Package doesn't include any.

----------------------------------------------------------------------
Ran 0 tests in 0.001s

Also the default checkPhase might not work for this package. To find out how the test suite is run it is usually helpful to check how their CI is set up.

https://github.com/mingrammer/diagrams/blob/master/.github/workflows/test.yml#L35

Please try and get the tests running.

version = "0.17.0";
disabled = ! isPy3k;

# TODO consider packaging from Github: https://github.com/mingrammer/diagrams
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be easy. Use fetchFromGitHub instead of fetchPypi and set format = "pyproject";. I assume we would then have the tests/ directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent a few hours working on this. I am far from a python expert, but it seems like the GitHub repo is structurally quite different from the pypi tarball. I will upload a new branch in a bit with my (unsuccessful) efforts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can see my (stalled) progress on packaging from GitHub here: #102060

@evanjs also had what is probably a terrible idea for tests, which is to 'cherry-pick' just the tests from GitHub...

@addict3d
Copy link
Contributor Author

addict3d commented Oct 26, 2020 via email

@addict3d
Copy link
Contributor Author

addict3d commented Oct 27, 2020 via email

@addict3d
Copy link
Contributor Author

Here is my attempt to package this from the upstream GitHub instead of the pypi tarball. Currently this produces an empty python module. I'm not (yet) familiar with python packaging to understand what is happening.

https://github.com/addict3d/nixpkgs/tree/add-python-diagrams-from-github

It appears that the library code is generated correctly and the tests all pass, but getting the library working is important.

@addict3d addict3d mentioned this pull request Oct 29, 2020
10 tasks
@addict3d addict3d force-pushed the add-python-diagrams-from-pypi branch from 847aa7e to 5857fa9 Compare October 29, 2020 15:51
@addict3d
Copy link
Contributor Author

I removed the top-level/all-packages entry because it doesn't make sense as a binary.

You need a python environment with this library, and then to produce an image, call python my-diagram-code.py (where your code imports diagrams).

Add python diagrams library. Generate nice architecture diagrams
with python code, allowing for rapid iteration.

The images are rendered using dot from graphviz.

Closes NixOS#101532
@addict3d addict3d force-pushed the add-python-diagrams-from-pypi branch from 5857fa9 to 0fcd1f6 Compare October 29, 2020 15:56
@evanjs
Copy link
Member

evanjs commented Oct 29, 2020

Since the setup.py and tests directories don't seem to both be available from a single source, I was wondering if it's acceptable to pull in both sources.

Not quite sure what else can be done if we can't determine how the GitHub sources can be installed or etc.
Anyway, the following seems to work, but feels awfully hacky to me.

I also wonder if sourcing GitHub and pulling the setup.py from Pypi would be any better 🤷‍♂️

sources from Pypi with tests from GitHub
diff --git a/pkgs/development/python-modules/diagrams/default.nix b/pkgs/development/python-modules/diagrams/default.nix
index 1f05abab32c..bbfa50a5a3c 100644
--- a/pkgs/development/python-modules/diagrams/default.nix
+++ b/pkgs/development/python-modules/diagrams/default.nix
@@ -4,17 +4,28 @@
 , fetchPypi
 , isPy3k
 , graphviz
+, pytestCheckHook
 }:

-buildPythonPackage rec {
+let
   pname = "diagrams";
   version = "0.17.0";
+  gitHubSrc = fetchFromGitHub {
+    owner = "mingrammer";
+    repo = pname;
+    rev = "v${version}";
+    sha256 = "1h5i9xb4fsmy81sznvqycxl712d9k695vznkpx2wm1ki7hvz86s8";
+  };
+in
+buildPythonPackage rec {
   disabled = ! isPy3k;
+  inherit pname version;

   # TODO consider packaging from Github: https://github.com/mingrammer/diagrams
   # note: outscale package is broken, __init__.py is missing
   src = fetchPypi {
     inherit pname version;
+
     sha256 = "17v5nzswva74bawxrvkza8a5d9aqyhb2gkncp3fqv6rszmk62hw6";
   };

@@ -22,6 +33,8 @@ buildPythonPackage rec {
     ./diags_fix_aliases.patch
   ];

+  checkInputs = [ pytestCheckHook ];
+
   # This script is patching setup.py from the pypi tarball, to retain
   # the png images which this library uses to generate diagrams.
   postPatch = ''
@@ -55,6 +68,8 @@ buildPythonPackage rec {
       done
       echo "    ],)"
     } >> setup.py
+
+    cp -r ${gitHubSrc}/tests .
   '';

   buildInputs = [ graphviz ];

@mweinelt
Copy link
Member

mweinelt commented Oct 29, 2020

I thought we could fetch this from GitHub and set format = "pyproject"; so it would build from the pyproject.toml. We don't strictly need a setup.py. But IIRC the following part is missing in pyproject.toml.

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

The author only uses this with poetry to develop, build and publish, so they don't need that explicitly.

The test phase uses the unittest module: https://github.com/mingrammer/diagrams/blob/master/.github/workflows/test.yml#L35

The problems remind me of supakeen/pinnwand#93.

@mweinelt
Copy link
Member

mweinelt commented Oct 29, 2020

Builds after patching the upstream git checkout like this and adding format = "pyproject";:

diff --git a/pyproject.toml b/pyproject.toml
index 8725f84..c134ab8 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -11,8 +11,8 @@ include = ["resources/**/*"]
 
 [tool.poetry.dependencies]
 python = "^3.6"
-graphviz = "^0.13.2"
-jinja2 = "^2.10"
+graphviz = "^0.14.1"
+jinja2 = "^2.11"
 contextvars = { version = "^2.4", python = "~3.6" }
 
 [tool.poetry.dev-dependencies]
@@ -24,3 +24,7 @@ isort = "^4.3"
 
 [tool.black]
 line-length = 120
+
+[build-system]
+requires = ["poetry_core>=1.0.0"]
+build-backend = "poetry.core.masonry.api"

You're also missing jinja2 and both graphviz and jinja2 should be propagatedBuildInputs if you're planning to use this as a library. See https://github.com/mingrammer/diagrams/blob/v0.17.0/pyproject.toml#L12-L16

You'll also need poetry in your nativeBuildInputs.

@martinbaillie
Copy link
Contributor

hey @addict3d are you still planning on taking this package into Nixpkgs? I've been using your branch for a while and would be keen to see it merged

@addict3d
Copy link
Contributor Author

hey @addict3d are you still planning on taking this package into Nixpkgs? I've been using your branch for a while and would be keen to see it merged

oh. err sorry about that. yes I will get back to this and address the comments

@addict3d
Copy link
Contributor Author

I'm closing this in favor of packaging directly from upstream (GitHub) rather than pypi. This way the package will include tests, and it removes one layer of indirection.

See #102060

@addict3d addict3d closed this Apr 10, 2021
@addict3d addict3d deleted the add-python-diagrams-from-pypi branch April 12, 2021 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Packaging request: python diagrams library
6 participants