potiuk commented on a change in pull request #12023:
URL: https://github.com/apache/airflow/pull/12023#discussion_r516008114



##########
File path: scripts/ci/pre_commit/pre_commit_check_setup_installation.py
##########
@@ -0,0 +1,108 @@
+#!/usr/bin/env python
+#
+# 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.
+"""
+Checks if all the libraries in setup.py are listed in installation.rst file
+"""
+
+import os
+import re
+from os.path import dirname
+from typing import Dict, List
+
+AIRFLOW_SOURCES_DIR = os.path.join(dirname(__file__), os.pardir, os.pardir, 
os.pardir)
+SETUP_PY_FILE = 'setup.py'
+DOCS_FILE = 'installation.rst'
+PY_IDENTIFIER = r'[a-zA-Z_][a-zA-Z0-9_\.]*'
+
+
+def get_file_content(*path_elements: str) -> str:
+    file_path = os.path.join(AIRFLOW_SOURCES_DIR, *path_elements)
+    with open(file_path) as file_to_read:
+        return file_to_read.read()
+
+
+def get_extras_from_setup() -> Dict[str, List[str]]:
+    """
+    Returns an array EXTRAS_REQUIREMENTS with aliases from setup.py file in 
format:
+    {'package name': ['alias1', 'alias2], ...}
+    """
+    setup_content = get_file_content(SETUP_PY_FILE)
+
+    extras_section_regex = re.compile(
+        r'^EXTRAS_REQUIREMENTS: Dict[^{]+{([^}]+)}', re.MULTILINE)
+    extras_section = extras_section_regex.findall(setup_content)[0]
+
+    extras_regex = re.compile(
+        
rf'^\s+[\"\']({PY_IDENTIFIER})[\"\']:\s*({PY_IDENTIFIER})[^#\n]*(#\s*TODO.*)?$',
 re.MULTILINE)
+
+    extras_dict: Dict[str, List[str]] = {}
+    for extras in extras_regex.findall(extras_section):
+        package = extras[1]
+        alias = extras[0]
+        if not extras_dict.get(package):
+            extras_dict[package] = []
+        extras_dict[package].append(alias)
+    return extras_dict

Review comment:
       I actually advise against it.
   
    It happened not more than once when I tried to import setup.py  in 
pre-commits and due to some "magic" (version reading for example), it failed 
pretty bad if you did not modify sys.path. 

##########
File path: scripts/ci/pre_commit/pre_commit_check_setup_installation.py
##########
@@ -0,0 +1,108 @@
+#!/usr/bin/env python
+#
+# 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.
+"""
+Checks if all the libraries in setup.py are listed in installation.rst file
+"""
+
+import os
+import re
+from os.path import dirname
+from typing import Dict, List
+
+AIRFLOW_SOURCES_DIR = os.path.join(dirname(__file__), os.pardir, os.pardir, 
os.pardir)
+SETUP_PY_FILE = 'setup.py'
+DOCS_FILE = 'installation.rst'
+PY_IDENTIFIER = r'[a-zA-Z_][a-zA-Z0-9_\.]*'
+
+
+def get_file_content(*path_elements: str) -> str:
+    file_path = os.path.join(AIRFLOW_SOURCES_DIR, *path_elements)
+    with open(file_path) as file_to_read:
+        return file_to_read.read()
+
+
+def get_extras_from_setup() -> Dict[str, List[str]]:
+    """
+    Returns an array EXTRAS_REQUIREMENTS with aliases from setup.py file in 
format:
+    {'package name': ['alias1', 'alias2], ...}
+    """
+    setup_content = get_file_content(SETUP_PY_FILE)
+
+    extras_section_regex = re.compile(
+        r'^EXTRAS_REQUIREMENTS: Dict[^{]+{([^}]+)}', re.MULTILINE)
+    extras_section = extras_section_regex.findall(setup_content)[0]
+
+    extras_regex = re.compile(
+        
rf'^\s+[\"\']({PY_IDENTIFIER})[\"\']:\s*({PY_IDENTIFIER})[^#\n]*(#\s*TODO.*)?$',
 re.MULTILINE)
+
+    extras_dict: Dict[str, List[str]] = {}
+    for extras in extras_regex.findall(extras_section):
+        package = extras[1]
+        alias = extras[0]
+        if not extras_dict.get(package):
+            extras_dict[package] = []
+        extras_dict[package].append(alias)
+    return extras_dict

Review comment:
       I think using regexp in this case is "good enough" 




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to