potiuk commented on code in PR #22971:
URL: https://github.com/apache/airflow/pull/22971#discussion_r850292186


##########
scripts/tools/initialize_virtualenv.py:
##########
@@ -0,0 +1,136 @@
+# 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 os
+import shutil
+import subprocess
+import sys
+from pathlib import Path
+
+if __name__ not in ("__main__", "__mp_main__"):
+    raise SystemExit(
+        "This file is intended to be executed as an executable program. You 
cannot use it as a module."
+        f"To run this script, run the ./{__file__} command"
+    )
+
+
+def clean_up_airflow_home(airflow_home: Path):
+    if airflow_home.exists():
+        print(f"Removing {airflow_home}")
+        shutil.rmtree(airflow_home, ignore_errors=True)
+
+
+def check_if_in_virtualenv() -> bool:
+    return hasattr(sys, 'real_prefix') or (hasattr(sys, 'base_prefix') and 
sys.base_prefix != sys.prefix)
+
+
+def pip_install_requirements() -> int:
+    """
+    install the requirements of the current python version.
+    return 0 if success, anything else is an error.
+    """
+    version = get_python_version()
+    constraint = (
+        
f"https://raw.githubusercontent.com/apache/airflow/constraints-main/constraints-{version}.txt";
+    )
+    pip_install_command = ["pip", "install", "-e", ".[devel-all]", 
"--constraint", constraint]
+    e = subprocess.run(pip_install_command)
+    return e.returncode
+
+
+def get_python_version() -> str:
+    """
+    return the version of python we are running.
+    """
+    major = sys.version_info[0]
+    minor = sys.version_info[1]
+    return f"{major}.{minor}"
+
+
+def main():
+    """
+    Setup local virtual environment.
+    """
+    airflow_home_dir = os.environ.get("AIRFLOW_HOME", 
Path(__file__).parents[4] / "airflow")
+    airflow_sources = str(Path.cwd())
+    if not check_if_in_virtualenv():
+        print(
+            "Local virtual environment not activated.\nPlease create and 
activate it "
+            "first. (for example using 'python3 -m venv venv && source 
venv/bin/activate')"
+        )
+        sys.exit(1)
+
+    print("Initializing environment...")
+    print(f"This will remove the folder {airflow_home_dir} and reset all the 
databases!")
+    response = input("Are you sure? (y/N/q)")
+    if response != "y":
+        sys.exit(2)
+
+    print(f"\nWiping and recreating {airflow_home_dir}")
+
+    if airflow_home_dir == airflow_sources:
+        print("AIRFLOW_HOME and Source code are in the same path")
+        print(
+            f"When running this script it will delete all files in path 
{airflow_home_dir} "
+            "to clear dynamic files like config/logs/db"
+        )
+        print("Please move the airflow source code elsewhere to avoid 
deletion")
+
+        sys.exit(3)
+
+    clean_up_airflow_home(airflow_home_dir)
+
+    print("\nInstalling requirements...")
+    return_code = pip_install_requirements()
+
+    if return_code != 0:
+        print("Error installing the requirements")
+        print("Try running the command below and rerun virtualenv 
installation\n")

Review Comment:
   When we add configurable extra,  we should extend the message a bit. 
Something like:
   
   ```
   This command can fail when you install it with the default "devel-all" 
extras. But if you need to quickly install airlfow venve and work on a subset 
of Airflow code it might be enough to install basic "devel"  requirements.
   
   You can do it via:
   
   ./scripts/tools/initialize_virtualenv.py "devel"
   
   You can also install extras for only small subset of extras, for example:
   
   ./scripts/tools/initialize_virtualenv.py "devel,google"
   
   ```



-- 
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: [email protected]

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

Reply via email to