szaszm commented on code in PR #1681:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1681#discussion_r1422556409


##########
bootstrap/system_dependency.py:
##########
@@ -0,0 +1,55 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from __future__ import annotations
+
+from typing import Dict, Set
+
+from minifi_option import MinifiOptions
+from package_manager import PackageManager
+
+
+def _create_system_dependencies(minifi_options: MinifiOptions) -> Dict[str, 
Set[str]]:
+    system_dependencies = {'patch': {'patch'}, 'make': {'make'}}
+    if minifi_options.is_enabled("ENABLE_EXPRESSION_LANGUAGE"):
+        system_dependencies['bison'] = {'bison'}
+        system_dependencies['flex'] = {'flex'}
+    if minifi_options.is_enabled("ENABLE_LIBARCHIVE"):
+        system_dependencies['libarchive'] = {'libarchive'}
+    if minifi_options.is_enabled("ENABLE_PCAP"):
+        system_dependencies['libpcap'] = {'libpcap'}
+    if minifi_options.is_enabled("ENABLE_USB_CAMERA"):
+        system_dependencies['libusb'] = {'libusb'}
+        system_dependencies['libpng'] = {'libpng'}
+    if minifi_options.is_enabled("ENABLE_GPS"):
+        system_dependencies['gpsd'] = {'gpsd'}
+    if minifi_options.is_enabled("ENABLE_COAP"):
+        system_dependencies['automake'] = {'automake'}
+        system_dependencies['autoconf'] = {'autoconf'}
+        system_dependencies['libtool'] = {'libtool'}
+    if minifi_options.is_enabled("ENABLE_LUA_SCRIPTING"):
+        system_dependencies['lua'] = {'lua'}
+    if minifi_options.is_enabled("ENABLE_PYTHON_SCRIPTING"):
+        system_dependencies['python'] = {'python'}
+    if minifi_options.is_enabled("MINIFI_OPENSSL"):
+        system_dependencies['openssl'] = {'perl'}

Review Comment:
   OpenSSL also needs NASM, at least on Windows. Is it handled elsewhere?



##########
bootstrap/py_bootstrap.bat:
##########
@@ -0,0 +1,15 @@
+@echo off
+
+set SCRIPT_DIR=%~dp0
+set VENV_DIR=%SCRIPT_DIR%venv
+
+if exist %VENV_DIR% (
+    call %VENV_DIR%\Scripts\activate
+) else (
+    echo Creating virtualenv
+    python -m venv %VENV_DIR%
+    call %VENV_DIR%\Scripts\activate
+    pip install -r %SCRIPT_DIR%requirements.txt
+)
+
+python %SCRIPT_DIR%main.py

Review Comment:
   I like to use quotes, even when they're not strictly required. Windows cmd 
scripts have a strange syntax, so you usually need to quote the whole argument, 
not just the part containing the variable reference, otherwise the quotes just 
become part of the argument.
   ```suggestion
   set "SCRIPT_DIR=%~dp0"
   set "VENV_DIR=%SCRIPT_DIR%venv"
   
   if exist "%VENV_DIR%" (
       call "%VENV_DIR%\Scripts\activate"
   ) else (
       echo Creating virtualenv
       python -m venv "%VENV_DIR%"
       call "%VENV_DIR%\Scripts\activate"
       pip install -r "%SCRIPT_DIR%requirements.txt"
   )
   
   python "%SCRIPT_DIR%main.py"
   ```



##########
bootstrap/package_manager.py:
##########
@@ -0,0 +1,325 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+import glob
+import os
+import platform
+import subprocess
+import sys
+import re
+from typing import Dict, Set
+
+from distro import distro
+
+
+def _query_yes_no(question: str, no_confirm: bool) -> bool:
+    valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}
+
+    if no_confirm:
+        print("Running {} with noconfirm".format(question))
+        return True
+    while True:
+        print("{} [y/n]".format(question))
+        choice = input().lower()
+        if choice in valid:
+            return valid[choice]
+        else:
+            print("Please respond with 'yes' or 'no' " "(or 'y' or 'n').")
+
+
+def _run_command_with_confirm(command: str, no_confirm: bool) -> bool:
+    if _query_yes_no("Running {}".format(command), no_confirm):
+        return os.system(command) == 0
+
+
+class PackageManager(object):
+    def __init__(self, no_confirm):
+        self.no_confirm = no_confirm
+        pass
+
+    def install(self, dependencies: Dict[str, Set[str]]) -> bool:
+        raise Exception("NotImplementedException")
+
+    def install_compiler(self) -> str:
+        raise Exception("NotImplementedException")
+
+    def _install(self, dependencies: Dict[str, Set[str]], replace_dict: 
Dict[str, Set[str]], install_cmd: str) -> bool:
+        dependencies.update({k: v for k, v in replace_dict.items() if k in 
dependencies})
+        dependencies = self._filter_out_installed_packages(dependencies)
+        dependencies_str = " ".join(str(value) for value_set in 
dependencies.values() for value in value_set)
+        if not dependencies_str or dependencies_str.isspace():
+            return True
+        return _run_command_with_confirm(f"{install_cmd} {dependencies_str}", 
self.no_confirm)
+
+    def _get_installed_packages(self) -> Set[str]:
+        raise Exception("NotImplementedException")
+
+    def _filter_out_installed_packages(self, dependencies: Dict[str, 
Set[str]]):
+        installed_packages = self._get_installed_packages()
+        filtered_packages = {k: (v - installed_packages) for k, v in 
dependencies.items()}
+        for installed_package in installed_packages:
+            filtered_packages.pop(installed_package, None)
+        return filtered_packages
+
+    def run_cmd(self, cmd: str) -> bool:
+        result = subprocess.run(f"{cmd}", shell=True, text=True)
+        return result.returncode == 0
+
+
+class BrewPackageManager(PackageManager):
+    def __init__(self, no_confirm):
+        PackageManager.__init__(self, no_confirm)
+
+    def install(self, dependencies: Dict[str, Set[str]]) -> bool:
+        return self._install(dependencies=dependencies,
+                             install_cmd="brew install",
+                             replace_dict={"patch": set(),
+                                           "jni": {"maven"}})
+
+    def install_compiler(self) -> str:
+        self.install({"compiler": {"llvm"}})
+        return ""
+
+    def _get_installed_packages(self) -> Set[str]:
+        result = subprocess.run(['brew', 'list'], text=True, 
capture_output=True, check=True)
+        lines = result.stdout.splitlines()
+        lines = [line.split('@', 1)[0] for line in lines]
+        return set(lines)
+
+
+class AptPackageManager(PackageManager):
+    def __init__(self, no_confirm):
+        PackageManager.__init__(self, no_confirm)
+
+    def install(self, dependencies: Dict[str, Set[str]]) -> bool:
+        return self._install(dependencies=dependencies,
+                             install_cmd="sudo apt install -y",
+                             replace_dict={"libarchive": {"liblzma-dev"},
+                                           "lua": {"liblua5.1-0-dev"},
+                                           "python": {"libpython3-dev"},
+                                           "libusb": {"libusb-1.0-0-dev", 
"libusb-dev"},
+                                           "libpng": {"libpng-dev"},
+                                           "libpcap": {"libpcap-dev"},
+                                           "jni": {"openjdk-8-jdk", 
"openjdk-8-source", "maven"},
+                                           "gpsd": {"libgps-dev"}})
+
+    def _get_installed_packages(self) -> Set[str]:
+        result = subprocess.run(['dpkg', '--get-selections'], text=True, 
capture_output=True, check=True)
+        lines = [line.split('\t')[0] for line in result.stdout.splitlines()]
+        lines = [line.rsplit(':', 1)[0] for line in lines]
+        return set(lines)
+
+    def install_compiler(self) -> str:
+        if distro.id() == "ubuntu" and int(distro.major_version()) < 22:
+            self.install({"compiler_prereq": {"software-properties-common"}})

Review Comment:
   Most 3rd party repos also need: `apt-transport-https ca-certificates 
software-properties-common`
   
   It may be more noticable with a minimal server install or docker image, but 
I didn't test yet.



##########
bootstrap/main.py:
##########
@@ -0,0 +1,56 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+import tempfile
+
+import argparse
+import pathlib
+
+from cli import main_menu, do_one_click_build
+from minifi_option import parse_minifi_options
+from package_manager import get_package_manager
+
+if __name__ == '__main__':
+    with tempfile.TemporaryDirectory() as cmake_cache_dir:
+        parser = argparse.ArgumentParser()
+        parser.add_argument('--noconfirm', action="store_true", default=False,
+                            help="Bypass any and all “Are you sure?” 
messages.")
+        parser.add_argument('--minifi_options', default="", help="Overrides 
the default minifi options during the "
+                                                                 "initial 
parsing")
+        parser.add_argument('--cmake_options', default="", help="Appends this 
to the final cmake command")
+        parser.add_argument('--skip_compiler_install', action="store_true", 
default=False,
+                            help="Skips the installation of the default 
compiler")

Review Comment:
   Long options usually use dashes as word separator. What do minifi options 
get passed to?



##########
bootstrap/package_manager.py:
##########
@@ -0,0 +1,325 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+import glob
+import os
+import platform
+import subprocess
+import sys
+import re
+from typing import Dict, Set
+
+from distro import distro
+
+
+def _query_yes_no(question: str, no_confirm: bool) -> bool:
+    valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}
+
+    if no_confirm:
+        print("Running {} with noconfirm".format(question))
+        return True
+    while True:
+        print("{} [y/n]".format(question))
+        choice = input().lower()
+        if choice in valid:
+            return valid[choice]
+        else:
+            print("Please respond with 'yes' or 'no' " "(or 'y' or 'n').")
+
+
+def _run_command_with_confirm(command: str, no_confirm: bool) -> bool:
+    if _query_yes_no("Running {}".format(command), no_confirm):
+        return os.system(command) == 0
+
+
+class PackageManager(object):
+    def __init__(self, no_confirm):
+        self.no_confirm = no_confirm
+        pass
+
+    def install(self, dependencies: Dict[str, Set[str]]) -> bool:
+        raise Exception("NotImplementedException")
+
+    def install_compiler(self) -> str:
+        raise Exception("NotImplementedException")
+
+    def _install(self, dependencies: Dict[str, Set[str]], replace_dict: 
Dict[str, Set[str]], install_cmd: str) -> bool:
+        dependencies.update({k: v for k, v in replace_dict.items() if k in 
dependencies})
+        dependencies = self._filter_out_installed_packages(dependencies)
+        dependencies_str = " ".join(str(value) for value_set in 
dependencies.values() for value in value_set)
+        if not dependencies_str or dependencies_str.isspace():
+            return True
+        return _run_command_with_confirm(f"{install_cmd} {dependencies_str}", 
self.no_confirm)
+
+    def _get_installed_packages(self) -> Set[str]:
+        raise Exception("NotImplementedException")
+
+    def _filter_out_installed_packages(self, dependencies: Dict[str, 
Set[str]]):
+        installed_packages = self._get_installed_packages()
+        filtered_packages = {k: (v - installed_packages) for k, v in 
dependencies.items()}
+        for installed_package in installed_packages:
+            filtered_packages.pop(installed_package, None)
+        return filtered_packages
+
+    def run_cmd(self, cmd: str) -> bool:
+        result = subprocess.run(f"{cmd}", shell=True, text=True)
+        return result.returncode == 0
+
+
+class BrewPackageManager(PackageManager):
+    def __init__(self, no_confirm):
+        PackageManager.__init__(self, no_confirm)
+
+    def install(self, dependencies: Dict[str, Set[str]]) -> bool:
+        return self._install(dependencies=dependencies,
+                             install_cmd="brew install",
+                             replace_dict={"patch": set(),
+                                           "jni": {"maven"}})
+
+    def install_compiler(self) -> str:
+        self.install({"compiler": {"llvm"}})
+        return ""

Review Comment:
   Can this use AppleClang from XCode on Mac?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to