From 02018666f24e94808efa4eb870ab827be6c12804 Mon Sep 17 00:00:00 2001
From: Ahmed Ilyas <ahmed.ilyas@hu-berlin.de>
Date: Fri, 19 Jul 2024 08:06:24 +0000
Subject: [PATCH] Add magic libraries for macos/windows and update docs

---
 docs/howto/develop/setup.md | 36 +++++++++++++++++++++++++-----------
 nomad/parsing/parsers.py    |  4 ++++
 pyproject.toml              |  2 ++
 3 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/docs/howto/develop/setup.md b/docs/howto/develop/setup.md
index 4639c6bb83..6c897cbf1d 100644
--- a/docs/howto/develop/setup.md
+++ b/docs/howto/develop/setup.md
@@ -103,21 +103,35 @@ Make sure you have the most recent version of `pip`:
 pip install --upgrade pip
 ```
 
-### Install missing system libraries (e.g. on MacOS)
+### Install missing system libraries (e.g. on Windows, MacOS)
+Even though the NOMAD infrastructure is written in Python, there are C libraries 
+required by some of our Python dependencies. Specifically, the libmagic library, 
+which allows determining the MIME type of files, and the hdf5 library, which is 
+essential for handling HDF5 files, must be installed on most Unix/Linux systems. 
 
-Even though the NOMAD infrastructure is written in Python, there is a C library
-required by one of our Python dependencies. Libmagic is missing on some systems.
-Libmagic allows to determine the MIME type of files. It should be installed on most
-Unix/Linux systems. It can be installed on MacOS with homebrew:
+The absence of these libraries can lead to issues during installation or runtime.
 
-```shell
-brew install libmagic
+For macOS (using Homebrew, pre-compiled binaries for `libmagic` are included in the `pylibmagic` library):
+
+```bash
+brew install hdf5
+```
+
+For Windows (pre-compiled binaries for `hdf5` are included in the dependencies):
+
+-libmagic: We include python-magic-bin as a dependency for Windows users. 
+If you encounter an error such as NameError: name '_compressions' is not defined, try uninstalling and reinstalling the library:
+
+```bash
+pip uninstall python-magic-bin
+pip install python-magic-bin
 ```
 
-If you are using a Mac with Apple Silicon, we recommend that you use rosetta, homebrew
-for Intel, and install and use an Intel-based Python. The second answer in this
-[Stackoverflow post](https://stackoverflow.com/questions/64882584/how-to-run-the-homebrew-installer-under-rosetta-2-on-m1-macbook){:target="_blank"}
-describes how to use both the Apple and Intel homebrew simultaneously.
+You can confirm that the magic library is correctly installed by running:
+
+```bash
+python -c "import magic"
+```
 
 ### Install NOMAD
 
diff --git a/nomad/parsing/parsers.py b/nomad/parsing/parsers.py
index 8b5aa9836f..8781d9672b 100644
--- a/nomad/parsing/parsers.py
+++ b/nomad/parsing/parsers.py
@@ -38,6 +38,10 @@ from .tabular import TabularDataParser
 try:
     # these packages are not available without parsing extra, which is ok, if the
     # parsers are only initialized to load their metainfo definitions
+    import platform
+
+    if platform.system() == 'Darwin':
+        import pylibmagic
     import magic
     import gzip
     import bz2
diff --git a/pyproject.toml b/pyproject.toml
index 8c4029e04a..58a802dc11 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -68,6 +68,8 @@ parsing = [
     'nomad-openbis',
     'quippy-ase==0.9.14',
     'rdkit==2023.9.5',
+    'pylibmagic; sys_platform == "darwin"',
+    'python-magic-bin; sys_platform == "win32"',
 ]
 infrastructure = [
     'asgiref~=3.5',
-- 
GitLab