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

Optimize Setup.py #260

Open
carlkidcrypto opened this issue Dec 9, 2024 · 2 comments · May be fixed by #267
Open

Optimize Setup.py #260

carlkidcrypto opened this issue Dec 9, 2024 · 2 comments · May be fixed by #267
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@carlkidcrypto
Copy link
Owner

carlkidcrypto commented Dec 9, 2024

There is a need for refactoring some setup.py code into functions.
This will make it easier to maintain.

Below is an example of functions that can be made to move out the Homebrew logic to functions.

def get_homebrew_info():
    """
    Checks if Homebrew is installed and retrieves its version.

    Returns:
      tuple: A tuple containing (Homebrew version) or None if Homebrew is not installed.
    """
    try:
        homebrew_version_output = check_output("brew --version", shell=True).decode()
        match = search(r"Homebrew (\d+\.\d+\.\d+)", homebrew_version_output)
        if match:
            return match.group(1)
        else:
            return None
    except CalledProcessError:
        return None


def get_homebrew_net_snmp_info():
    """
    Retrieves net-snmp and its OpenSSL dependency information from Homebrew.

    Returns:
      tuple: A tuple containing (homebrew_version, net_snmp_version, openssl_version, libdirs, incdirs)
             or None if net-snmp is not installed via Homebrew.
             Or None if Homebrew is not installed.
    """
    
    homebrew_version = get_homebrew_info()
    if not homebrew_version:
        return None

    try:
        brew_output = check_output("brew list net-snmp", shell=True).decode()
        lines = brew_output.splitlines()

        # Extract net-snmp version
        pattern = r"/opt/homebrew/Cellar/net-snmp/(\d+\.\d+\.\d+)/"
        match = search(pattern, lines[0])
        if not match:
            return None
        net_snmp_version = match.group(1)

        # Get include directory
        include_dir = next((l for l in lines if "include/net-snmp" in l), None)
        if not include_dir:
            return None
        incdirs = [include_dir[: include_dir.index("include/net-snmp") + 7]]

        # Get library directory (macOS specific)
        libdirs = []
        if platform.system() == "Darwin":
            lib_dir = next((l for l in lines if "lib/libnetsnmp.dylib" in l), None)
            if not lib_dir:
                return None
            libdirs.append(lib_dir[: lib_dir.index("lib/libnetsnmp.dylib") + 3])

        # Get OpenSSL dependency information
        brew_info_output = check_output("brew info net-snmp", shell=True).decode()
        openssl_version = next(
            (
                line.split()[0]
                for line in brew_info_output.splitlines()
                if "/openssl@3/" in line
            ),
            None,
        )
        if not openssl_version:
            return None

        openssl_info_output = check_output(
            f"brew info {openssl_version}", shell=True
        ).decode()
        openssl_path = openssl_info_output.splitlines()[4].split("(")[0].strip()
        libdirs.append(openssl_path + "/lib")
        incdirs.append(openssl_path + "/include")

        return homebrew_version, net_snmp_version, openssl_version, libdirs, incdirs

    except CalledProcessError:
        return None
@carlkidcrypto carlkidcrypto converted this from a draft issue Dec 9, 2024
@carlkidcrypto carlkidcrypto changed the title Setup.py Optimize Setup.py Dec 9, 2024
@carlkidcrypto carlkidcrypto added enhancement New feature or request good first issue Good for newcomers labels Dec 9, 2024
@ademhilmibozkurt
Copy link

I will work on it. Do you have any specific requirements about process? Which branch should i send pull request after finish?

@carlkidcrypto
Copy link
Owner Author

carlkidcrypto commented Dec 11, 2024

@ademhilmibozkurt branch off main and merge into main.

Requirements

  1. Build on Linux With Homebrew
  2. Build on MacOS With Homebrew
  3. Build on MacOS With No Homebrew - WIP in Build Without HomeBrew #263
  4. Build on Linux with No Homebrew - WIP in Build Without HomeBrew #263

@carlkidcrypto carlkidcrypto moved this to In Progress in V2.0.0 Dec 14, 2024
@carlkidcrypto carlkidcrypto linked a pull request Dec 14, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
Status: In Progress
2 participants