This is an automated email from the ASF dual-hosted git repository. machristie pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/airavata-cookiecutter-django-output-view.git
commit 8aca829df908cfdf45be9888347c7aedae482c7f Author: Marcus Christie <[email protected]> AuthorDate: Wed Jun 2 10:30:33 2021 -0400 Add some sanity checks: should be run from within the custom django app root directory --- cookiecutter.json | 1 + hooks/post_gen_project.py | 10 ++++++++- hooks/pre_gen_project.py | 26 ++++++++++++++++++++++ .../__init__.py | 0 .../{{cookiecutter.project_slug}}.py | 0 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/cookiecutter.json b/cookiecutter.json index 7765419..0d2f1da 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -3,6 +3,7 @@ "project_slug": "{{ cookiecutter.project_name | slugify(separator='_')}}", "project_short_description": "{{ cookiecutter.project_name }} generates data for an output view in the Airavata Django Portal", "output_view_provider_class_name": "{{ cookiecutter.project_name | title | replace(' ', '') }}Provider", + "custom_django_app_module_name": "", "output_views_directory_name": "output_views", "output_view_display_type": ["image", "link", "html"], "number_of_output_files": ["single (URI)", "multiple (URI_COLLECTION)"] diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 919288f..bcbdc86 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -1,6 +1,8 @@ import os import sys +from setuptools.config import read_configuration + def find_setup_cfg_file(): currdir = os.getcwd() @@ -41,9 +43,15 @@ def find_last_line_of_entry_points(setup_cfg_lines): # Return None if we couldn't find it return entry_points_end if entry_points_end > 0 else None +def get_django_app_package_name(setup_cfg_file): + setup_cfg_dict = read_configuration(setup_cfg_file) + entry_point = setup_cfg_dict['options']['entry_points']['airavata.djangoapp'][0] + django_app_package_name = entry_point.split('=')[0].strip() + return django_app_package_name + def insert_output_view_provider(setup_cfg_lines, index, insert_entry_point_group=False): updated_lines = setup_cfg_lines.copy() - updated_lines.insert(index+1, " {{cookiecutter.project_slug}} = FIXME.{{cookiecutter.output_views_directory_name}}:{{cookiecutter.output_view_provider_class_name}}" + os.linesep) + updated_lines.insert(index+1, " {{cookiecutter.project_slug}} = {{cookiecutter.custom_django_app_module_name}}.{{cookiecutter.output_views_directory_name}}:{{cookiecutter.output_view_provider_class_name}}" + os.linesep) if insert_entry_point_group: updated_lines.insert(index+1, "airavata.output_view_providers =" + os.linesep) return updated_lines diff --git a/hooks/pre_gen_project.py b/hooks/pre_gen_project.py new file mode 100644 index 0000000..b7885d6 --- /dev/null +++ b/hooks/pre_gen_project.py @@ -0,0 +1,26 @@ +import os +import sys + + +# make sure setup.cfg is one directory up +cwd = os.getcwd() +django_app_dir = os.path.dirname(cwd) +if not os.path.isfile(os.path.join(django_app_dir, "setup.cfg")): + print(f"ERROR: Could not find setup.cfg file in {django_app_dir}", file=sys.stderr) + print(f"Are you sure you are running this cookiecutter in an Airavata Django app directory? The setup.cfg should be in the current directory.", file=sys.stderr) + sys.exit(1) + +# make sure {{cookiecutter.custom_django_app_module_name}} looks like django app directory +django_app_module_dir = os.path.join(django_app_dir, "{{cookiecutter.custom_django_app_module_name}}") +if not os.path.isfile(os.path.join(django_app_module_dir, "apps.py")): + # Try to find the django app module + directories = [d for d in os.listdir(django_app_dir) if os.path.isdir(os.path.join(django_app_dir, d))] + candidate = None + for directory in directories: + if os.path.isfile(os.path.join(django_app_dir, directory, 'apps.py')): + candidate = directory + break + print(f"ERROR: {{cookiecutter.custom_django_app_module_name}} doesn't look like a Django app module", file=sys.stderr) + if candidate is not None: + print(f"Did you mean '{candidate}' instead of '{{cookiecutter.custom_django_app_module_name}}'?", file=sys.stderr) + sys.exit(1) diff --git a/{{cookiecutter.custom_django_app_module_name}}/{{cookiecutter.output_views_directory_name}}/__init__.py b/{{cookiecutter.custom_django_app_module_name}}/{{cookiecutter.output_views_directory_name}}/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/{{cookiecutter.output_views_directory_name}}/{{cookiecutter.project_slug}}.py b/{{cookiecutter.custom_django_app_module_name}}/{{cookiecutter.output_views_directory_name}}/{{cookiecutter.project_slug}}.py similarity index 100% rename from {{cookiecutter.output_views_directory_name}}/{{cookiecutter.project_slug}}.py rename to {{cookiecutter.custom_django_app_module_name}}/{{cookiecutter.output_views_directory_name}}/{{cookiecutter.project_slug}}.py
