diff --git a/app/controllers/api/v1/exports_controller.rb b/app/controllers/api/v1/exports_controller.rb index 2077f79e..fa8faeee 100644 --- a/app/controllers/api/v1/exports_controller.rb +++ b/app/controllers/api/v1/exports_controller.rb @@ -24,7 +24,7 @@ def new with_egmdo: with_egmdo, with_runops: with_runops, with_unittests: with_unittests) send_data content, filename: filename when "gemseo", "gemseo_pkg" - ggen = WhatsOpt::GemseoGenerator.new(mda) + ggen = WhatsOpt::GemseoGenerator.new(mda, pkg_format: (format == "gemseo_pkg")) begin content, filename = ggen.generate(user_agent: user_agent, with_run: with_run, with_server: with_server, with_egmdo: with_egmdo, with_runops: with_runops, with_unittests: with_unittests) diff --git a/app/lib/whats_opt/gemseo_generator.rb b/app/lib/whats_opt/gemseo_generator.rb index 62165978..c0571147 100644 --- a/app/lib/whats_opt/gemseo_generator.rb +++ b/app/lib/whats_opt/gemseo_generator.rb @@ -14,29 +14,45 @@ class DisciplineNotFoundException < StandardError class NotYetImplementedError < StandardError end - def initialize(mda) - super(mda) + def initialize(mda, pkg_format: false) + super(mda, pkg_format: pkg_format) @prefix = "gemseo" @framework = "gemseo" end # sqlite_filename: nil, with_run: true, with_server: true, with_runops: true def _generate_code(gendir, options = {}) - opts = { with_server: false, with_run: true, with_unittests: false }.merge(options) + opts = { with_server: false, with_run: true, with_unittests: false, with_src_dir: false }.merge(options) + if opts[:with_src_dir] + src_dir = File.join(gendir, "src") + Dir.mkdir(src_dir) unless Dir.exist?(src_dir) + else + src_dir = gendir + end + pkg_dir = package_dir? ? File.join(src_dir, @impl.py_modulename) : src_dir + Dir.mkdir(pkg_dir) unless Dir.exist?(pkg_dir) + @mda.disciplines.nodes.each do |disc| if disc.has_sub_analysis? raise NotYetImplementedError.new("Cannot generate code for sub_analysis #{disc.name}") - _generate_sub_analysis(disc, gendir, opts) + _generate_sub_analysis(disc, pkg_dir, opts) else - _generate_discipline(disc, gendir, opts) + _generate_discipline(disc, pkg_dir, opts) raise NotYetImplementedError.new("Cannot generate code for unit test #{disc.name}") if opts[:with_unittests] end end - _generate_main(gendir, opts) + _generate_main(pkg_dir, opts) _generate_run_scripts(gendir, opts) if opts[:with_server] || (!@check_only && @mda.has_remote_discipline?(@remote_ip)) raise NotYetImplementedError.new("Cannot generate code for server") end + if opts[:with_egmdo] || @driver_name =~ /egmdo|egdoe/ + raise NotYetImplementedError.new("Cannot generate code for egmdo") + end + if package_dir? && @framework == "gemseo" + _generate_package_files(gendir) + end + @genfiles end @@ -70,5 +86,17 @@ def _generate_run_scripts(gendir, options = {}) end end end + + def _generate_package_files(gendir) + if @mda.packaged? + # Package is attached, use it! + @genfiles |= WhatsOpt::PackageExtractor.new(@mda).extract(gendir) + else + # no package => generate package skeleton + _generate(".gitignore", "package/gitignore.erb", gendir, no_comment: true) + _generate("README.md", "package/README.md.erb", gendir, no_comment: true) + _generate("pyproject.toml", "package/pyproject.toml.erb", gendir, no_comment: true) + end + end end end diff --git a/app/lib/whats_opt/templates/gemseo/gemseo_analysis.py.erb b/app/lib/whats_opt/templates/gemseo/gemseo_analysis.py.erb index 6b71e4d3..4d8c4f05 100644 --- a/app/lib/whats_opt/templates/gemseo/gemseo_analysis.py.erb +++ b/app/lib/whats_opt/templates/gemseo/gemseo_analysis.py.erb @@ -1,4 +1,4 @@ -from <%= @impl.py_full_modulename %>_base import <%= @impl.py_classname %>MDABase, <%= @impl.py_classname %>DOEScenarioBase, <%= @impl.py_classname %>MDOScenarioBase +from <%= @pkg_prefix %><%= @impl.py_full_modulename %>_base import <%= @impl.py_classname %>MDABase, <%= @impl.py_classname %>DOEScenarioBase, <%= @impl.py_classname %>MDOScenarioBase class <%= @impl.py_classname %>MDA(<%= @impl.py_classname %>MDABase): """ A GEMSEO MDA to encapsulate <%= @impl.py_classname %> analysis """ diff --git a/app/lib/whats_opt/templates/gemseo/gemseo_analysis_base.py.erb b/app/lib/whats_opt/templates/gemseo/gemseo_analysis_base.py.erb index 5c7171b5..cabfca67 100644 --- a/app/lib/whats_opt/templates/gemseo/gemseo_analysis_base.py.erb +++ b/app/lib/whats_opt/templates/gemseo/gemseo_analysis_base.py.erb @@ -6,7 +6,7 @@ import numpy as np from numpy import nan <% @mda.all_plain_disciplines.each do |disc| -%> -from <%= disc.impl.py_full_modulename %> import <%= disc.impl.py_classname %> +from <%= @pkg_prefix %><%= disc.impl.py_full_modulename %> import <%= disc.impl.py_classname %> <% end -%> from gemseo.mda.jacobi import MDAJacobi diff --git a/app/lib/whats_opt/templates/gemseo/gemseo_discipline.py.erb b/app/lib/whats_opt/templates/gemseo/gemseo_discipline.py.erb index a6a2dddc..cb8c110e 100644 --- a/app/lib/whats_opt/templates/gemseo/gemseo_discipline.py.erb +++ b/app/lib/whats_opt/templates/gemseo/gemseo_discipline.py.erb @@ -1,5 +1,5 @@ import numpy as np -from <%= @discipline.impl.py_full_modulename %>_base import <%= @discipline.impl.py_classname %>Base +from <%= @pkg_prefix %><%= @discipline.impl.py_full_modulename %>_base import <%= @discipline.impl.py_classname %>Base class <%= @discipline.impl.py_classname %>(<%= @discipline.impl.py_classname %>Base): """ A class to encapsulate <%= @discipline.impl.py_classname %> discipline """ diff --git a/app/lib/whats_opt/templates/gemseo/run_doe.py.erb b/app/lib/whats_opt/templates/gemseo/run_doe.py.erb index 40bfcdbd..908c19d7 100644 --- a/app/lib/whats_opt/templates/gemseo/run_doe.py.erb +++ b/app/lib/whats_opt/templates/gemseo/run_doe.py.erb @@ -1,6 +1,6 @@ from optparse import OptionParser from gemseo import configure_logger -from <%= @impl.py_modulename %> import <%= @impl.py_classname %>DOEScenario +from <%= @pkg_prefix %><%= @impl.py_modulename %> import <%= @impl.py_classname %>DOEScenario parser = OptionParser() parser.add_option("-n", "--ncases", type="int", diff --git a/app/lib/whats_opt/templates/gemseo/run_mda.py.erb b/app/lib/whats_opt/templates/gemseo/run_mda.py.erb index 2dd2eb37..4a1c0b6f 100644 --- a/app/lib/whats_opt/templates/gemseo/run_mda.py.erb +++ b/app/lib/whats_opt/templates/gemseo/run_mda.py.erb @@ -1,5 +1,5 @@ import numpy as np -from <%= @impl.py_modulename %> import <%= @impl.py_classname %>MDA +from <%= @pkg_prefix %><%= @impl.py_modulename %> import <%= @impl.py_classname %>MDA from mda_init import initialize mda = <%= @impl.py_classname %>MDA() diff --git a/app/lib/whats_opt/templates/gemseo/run_mdo.py.erb b/app/lib/whats_opt/templates/gemseo/run_mdo.py.erb index f6591b30..85c28ea8 100644 --- a/app/lib/whats_opt/templates/gemseo/run_mdo.py.erb +++ b/app/lib/whats_opt/templates/gemseo/run_mdo.py.erb @@ -1,5 +1,5 @@ from gemseo import configure_logger -from <%= @impl.py_modulename %> import <%= @impl.py_classname %>MDOScenario +from <%= @pkg_prefix %><%= @impl.py_modulename %> import <%= @impl.py_classname %>MDOScenario configure_logger() diff --git a/app/lib/whats_opt/templates/package/pyproject.toml.erb b/app/lib/whats_opt/templates/package/pyproject.toml.erb index d4c968e5..42defc4d 100644 --- a/app/lib/whats_opt/templates/package/pyproject.toml.erb +++ b/app/lib/whats_opt/templates/package/pyproject.toml.erb @@ -10,7 +10,7 @@ authors = [ ] description = "Package <%= @mda.name %> Analysis" readme = "README.md" -requires-python = ">=3.7" +requires-python = ">=3.9" classifiers = [ "Programming Language :: Python :: 3", ]