From 0f8a208a2a410a3efda46ae3a85910482be8e719 Mon Sep 17 00:00:00 2001
From: dh00601 <dh00601@surrey.ac.uk>
Date: Fri, 3 Jun 2022 13:08:02 +0100
Subject: [PATCH] updating the methods for the setup

---
 requirements.txt |  25 +++++++
 setup.py         | 177 ++++++++++++++++++++---------------------------
 2 files changed, 101 insertions(+), 101 deletions(-)
 create mode 100644 requirements.txt

diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 000000000..11f1705f0
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,25 @@
+astropy
+cachetools
+colorama
+compress_pickle
+datasize
+diskcache
+flufl.lock
+h5py
+halo
+humanize
+lib_programname
+matplotlib
+msgpack
+numpy
+pandas
+pathos
+psutil
+pytest
+py_rinterpolate
+seaborn
+setproctitle
+str2bool
+psutil
+simplejson
+strip-ansi
\ No newline at end of file
diff --git a/setup.py b/setup.py
index 2998eb677..4fe827fe9 100644
--- a/setup.py
+++ b/setup.py
@@ -11,9 +11,25 @@ import setuptools
 from distutils.core import setup, Extension
 import distutils.command.build
 
-# TODO: replace the tasks that call binary_c-config with a single function that handles the return status a bit better.
+#
+this_file = os.path.abspath(__file__)
+this_file_dir = os.path.dirname(this_file)
+
+
+#########
+
+
+###
+REQUIRED_BINARY_C_VERSIONS = ["2.1.7", "2.2pre1", "2.2.0", "2.2.1"]
+
 
 
+
+############################################################
+# Defining functionality
+############################################################
+
+# Functions
 def version():
     """
     opens VERSION and returns version number
@@ -22,21 +38,36 @@ def version():
     with open("VERSION") as file:
         return file.read().strip()
 
-
-VERSION_NUMBER = version()
-
-# Functions
 def readme():
-    """Opens readme file and returns content"""
+    """
+    Opens readme file and returns content
+    """
+
     with open("README.md") as file:
         return file.read()
 
-
 def license():
-    """Opens license file and returns the content"""
+    """
+    Opens license file and returns the content
+    """
+
     with open("LICENSE.md") as file:
         return file.read()
 
+def requirements(directory):
+    """
+    Opens requirements.txt and returns content as a list
+    """
+
+    requirements_file = os.path.join(directory, 'requirements.txt')
+
+    # Read out file and construct list
+    requirements_list = []
+    with open(requirements_file) as f:
+        for el in f.readlines():
+            requirements_list.append(el.strip())
+
+    return requirements_list
 
 def check_version(installed_binary_c_version, required_binary_c_versions):
     """Function to check the installed version and compare it to the required version"""
@@ -49,7 +80,6 @@ def check_version(installed_binary_c_version, required_binary_c_versions):
     )
     assert installed_binary_c_version in required_binary_c_versions, message
 
-
 def execute_make():
     """
     Function to execute the makefile.
@@ -74,9 +104,25 @@ def execute_make():
         print(stdout.decode("utf-8"))
         print("Successfully built the libbinary_c_api.so")
 
+def call_binary_c_config(binary_c_dir, command):
+    """
+    Function to call the binary_c config
+    """
 
-###
-REQUIRED_BINARY_C_VERSIONS = ["2.1.7", "2.2pre1", "2.2.0", "2.2.1"]
+
+    binary_c_config = os.path.join(BINARY_C_DIR, "binary_c-config")
+
+    command_result = (
+        subprocess.run([binary_c_config, command], stdout=subprocess.PIPE, check=True)
+        .stdout.decode("utf-8")
+        .split()
+    )
+
+    return command_result
+
+############################################################
+# First level checks
+############################################################
 
 ####
 GSL_DIR = os.getenv("GSL_DIR", None)
@@ -92,59 +138,23 @@ if not BINARY_C_DIR:
     )
     quit(1)
 
-# TODO: write code to know exact parent directory of this file.
-CWD = os.getcwd()
-
 ############################################################
 # Getting information from binary_c
 ############################################################
 
 # binary_c must be installed.
-BINARY_C_CONFIG = os.path.join(BINARY_C_DIR, "binary_c-config")
-
-BINARY_C_VERSION = (
-    subprocess.run([BINARY_C_CONFIG, "version"], stdout=subprocess.PIPE, check=True)
-    .stdout.decode("utf-8")
-    .split()
-)
+BINARY_C_VERSION = call_binary_c_config(BINARY_C_DIR, "version")
 check_version(BINARY_C_VERSION[0], REQUIRED_BINARY_C_VERSIONS)
 
-BINARY_C_INCDIRS = (
-    subprocess.run(
-        [BINARY_C_CONFIG, "incdirs_list"], stdout=subprocess.PIPE, check=True
-    )
-    .stdout.decode("utf-8")
-    .split()
-)
-BINARY_C_LIBDIRS = (
-    subprocess.run(
-        [BINARY_C_CONFIG, "libdirs_list"], stdout=subprocess.PIPE, check=True
-    )
-    .stdout.decode("utf-8")
-    .split()
-)
-BINARY_C_CFLAGS = (
-    subprocess.run([BINARY_C_CONFIG, "cflags"], stdout=subprocess.PIPE, check=True)
-    .stdout.decode("utf-8")
-    .split()
-)
+BINARY_C_INCDIRS = call_binary_c_config(BINARY_C_DIR, "incdirs_list")
+BINARY_C_LIBDIRS = call_binary_c_config(BINARY_C_DIR, "libdirs_list")
+BINARY_C_CFLAGS = call_binary_c_config(BINARY_C_DIR, "cflags")
 # BINARY_C_CFLAGS.remove('-fvisibility=hidden')
-BINARY_C_LIBS = (
-    subprocess.run([BINARY_C_CONFIG, "libs_list"], stdout=subprocess.PIPE, check=True)
-    .stdout.decode("utf-8")
-    .split()
-)
+BINARY_C_LIBS = call_binary_c_config(BINARY_C_DIR, "libs_list")
+BINARY_C_DEFINES = call_binary_c_config(BINARY_C_DIR, "define_macros")
 
 # create list of tuples of defined macros
 BINARY_C_DEFINE_MACROS = []
-DEFINES = (
-    subprocess.run(
-        [BINARY_C_CONFIG, "define_macros"], stdout=subprocess.PIPE, check=True
-    )
-    .stdout.decode("utf-8")
-    .split()
-)
-
 LONE = re.compile("^-D(.+)$")
 PARTNER = re.compile("^-D(.+)=(.+)$")
 for x in DEFINES:
@@ -160,8 +170,9 @@ for x in DEFINES:
 API_h = os.path.join(BINARY_C_DIR, "src", "API", "binary_c_API.h")
 
 ############################################################
-# Setting all directories and LIBRARIES to their final values
+# Determine all directories and libraries
 ############################################################
+
 INCLUDE_DIRS = [
     os.path.join(BINARY_C_DIR, "src"),
     os.path.join(BINARY_C_DIR, "src", "API"),
@@ -176,14 +187,14 @@ LIBRARIES = ["binary_c"] + BINARY_C_LIBS
 LIBRARY_DIRS = [
     os.path.join(BINARY_C_DIR, "src"),
     "./",
-    os.path.join(CWD, "lib/"),
-    os.path.join(CWD, "binarycpython/"),
+    os.path.join(this_file_dir, "lib/"),
+    os.path.join(this_file_dir, "binarycpython/"),
 ] + BINARY_C_LIBDIRS
 
 RUNTIME_LIBRARY_DIRS = [
     os.path.join(BINARY_C_DIR, "src"),
     "./",
-    os.path.join(CWD, "lib/"),
+    os.path.join(this_file_dir, "lib/"),
 ] + BINARY_C_LIBDIRS
 
 # filter out duplicates
@@ -193,21 +204,8 @@ LIBRARIES = list(dict.fromkeys(LIBRARIES))
 LIBRARY_DIRS = list(dict.fromkeys(LIBRARY_DIRS))
 RUNTIME_LIBRARY_DIRS = list(dict.fromkeys(RUNTIME_LIBRARY_DIRS))
 
-#
-# print('\n')
-# print("BINARY_C_CONFIG: ", str(BINARY_C_CONFIG) + "\n")
-# print("incdirs: ", str(INCLUDE_DIRS) + "\n")
-# print("BINARY_C_LIBS: ", str(BINARY_C_LIBS) + "\n")
-# print("LIBRARIES: ", str(LIBRARIES) + "\n")
-# print("LIBRARY_DIRS: ", str(LIBRARY_DIRS) + "\n")
-# print("RUNTIME_LIBRARY_DIRS: ", str(RUNTIME_LIBRARY_DIRS) + "\n")
-# print("BINARY_C_CFLAGS: ", str(BINARY_C_CFLAGS) + "\n")
-# print("API_h: ", str(API_h) + "\n")
-# print("macros: ", str(BINARY_C_DEFINE_MACROS) + "\n")
-# print('\n')
-
 ############################################################
-# Making the extension function
+# Making the python-c binding module
 ############################################################
 
 BINARY_C_PYTHON_API_MODULE = Extension(
@@ -223,8 +221,9 @@ BINARY_C_PYTHON_API_MODULE = Extension(
     language="C",
 )
 headers = ["src/includes/header.h"]
+
 ############################################################
-# Making the extension function
+# Custom build command
 ############################################################
 
 # Override build command
@@ -233,10 +232,13 @@ class CustomBuildCommand(distutils.command.build.build):
         # Run the original build command
         distutils.command.build.build.run(self)
 
+############################################################
+# Main setup function call
+############################################################
 
 setup(
     name="binarycpython",
-    version=VERSION_NUMBER,
+    version=version(),
     description="""This is a python API for binary_c (versions {}) by David Hendriks, Rob Izzard and collaborators. Based on the initial set up by Jeff andrews.""".format(
         ",".join(REQUIRED_BINARY_C_VERSIONS),
         ",".join(REQUIRED_BINARY_C_VERSIONS),
@@ -244,7 +246,6 @@ setup(
     author="David Hendriks",
     author_email="davidhendriks93@gmail.com",
     long_description=readme(),
-    # long_description="hello",
     long_description_content_type="text/markdown",
     url="https://gitlab.eps.surrey.ac.uk/ri0005/binary_c-python",
     license="gpl",
@@ -261,33 +262,7 @@ setup(
         "binarycpython.tests",
         "binarycpython.tests.tests_population_extensions",
     ],
-    install_requires=[ # TODO: can we centralise this?
-        "astropy",
-        "cachetools",
-        "colorama",
-        "compress_pickle",
-        "datasize",
-        "diskcache",
-        "flufl.lock",
-        "h5py",
-        "halo",
-        "humanize",
-        "lib_programname",
-        "matplotlib",
-        "msgpack",
-        "numpy",
-        "pandas",
-        "pathos",
-        "psutil",
-        "pytest",
-        "py_rinterpolate",
-        "seaborn",
-        "setproctitle",
-        "str2bool",
-        "psutil",
-        "simplejson",
-        "strip-ansi",
-    ],
+    install_requires=,
     include_package_data=True,
     ext_modules=[BINARY_C_PYTHON_API_MODULE],  # binary_c must be loaded
     classifiers=[
-- 
GitLab