Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aid requested re following resource callback guide #448

Open
SimonBiggs opened this issue Sep 23, 2021 · 1 comment · May be fixed by #449
Open

Aid requested re following resource callback guide #448

SimonBiggs opened this issue Sep 23, 2021 · 1 comment · May be fixed by #449

Comments

@SimonBiggs
Copy link

SimonBiggs commented Sep 23, 2021

Hi @indygreg,

I am trying to follow the documentation here:
https://pyoxidizer.readthedocs.io/en/latest/pyoxidizer_packaging_resources.html?highlight=resource_callback#using-callbacks-to-influence-resource-attributes

Here is the excerpt from my configuration file:

def resource_callback(policy, resource):
    if type(resource) in ("PythonModuleSource", "PythonPackageResource", "PythonPackageDistributionResource"):
        if resource.package.startswith("rtai"):
            resource.add_location = "in-memory"
        else:
            resource.add_location = "filesystem-relative:lib"

def make_exe():
    dist = default_python_distribution(python_version = "3.9")

    policy = dist.make_python_packaging_policy()
    policy.register_resource_callback(resource_callback)  # This line here is the issue

    python_config = dist.make_python_interpreter_config()
    python_config.module_search_paths = ["$ORIGIN/lib"]
    python_config.run_module = "rtaisite"

    exe = dist.to_python_executable(
        name = "rtai",
        packaging_policy = policy,
        config = python_config,
    )
    exe.windows_runtime_dlls_mode = "always"
    exe.windows_subsystem = "windows"
    exe.add_python_resources(exe.pip_install(["--use-feature", "in-tree-build", "-r", "requirements.txt"]))

    return exe

Essentially my goal is to have all dependencies be added to the library directory with their source code as normal, and have any module that starts with "rtai" be stored within memory.

If I change policy.register_resource_callback(resource_callback) to either:

  • policy.resources_location = "filesystem-relative:lib" or
  • policy.resources_location = "in-memory"

Both of those successfully build (only the filesystem version runs though due to a few of my deps not playing nice),

Nevertheless when I try and follow the resource callback docs and use policy.register_resource_callback(resource_callback) I get the following error in the build:

resolving 1 targets
resolving target install
resolving target exe
resolving Python distribution Url { url: "https://github.com/indygreg/python-build-standalone/releases/download/20210724/cpython-3.9.6-x86_64-unknown-linux-gnu-pgo-20210724T1424.tar.zst", sha256: "343e2d349779efb7d46f7eb01ec6a202bff14293616ce8103bc8060c777bb231" }
Python distribution available at /home/simon/.cache/pyoxidizer/python_distributions/cpython-3.9.6-x86_64-unknown-linux-gnu-pgo-20210724T1424.tar.zst
reading data from Python distribution...
error[PYOXIDIZER_BUILD]: error converting PythonResource to Value: DiagnosedError(Diagnostic { level: Error, message: "Cannot .package on type PythonModuleSource", code: Some("CV00"), spans: [SpanLabel { span: Span { low: Pos(164), high: Pos(180) }, label: Some(".package not supported for type PythonModuleSource"), style: Primary }] })
  --> ./pyoxidizer.bzl:18:11
   |
18 |       exe = dist.to_python_executable(
   |  ___________^
19 | |         name = "rtai",
20 | |         packaging_policy = policy,
21 | |         config = python_config,
22 | |     )
   | |_____^ to_python_executable()


error: error converting PythonResource to Value: DiagnosedError(Diagnostic { level: Error, message: "Cannot .package on type PythonModuleSource", code: Some("CV00"), spans: [SpanLabel { span: Span { low: Pos(164), high: Pos(180) }, label: Some(".package not supported for type PythonModuleSource"), style: Primary }] })

Any pointers about where I may have tripped up?

Thanks :),
Cheers,
Simon

@SimonBiggs
Copy link
Author

I believe after a set of more iterations I have found a solution. The following appears to complete what I was after:

def resource_callback(policy, resource):
    if (
        type(resource) in ("PythonPackageResource", "PythonPackageDistributionResource")
        and resource.package.startswith("rtai")
    ):
        resource.add_location = "in-memory"
        return

    if type(resource) in ("PythonModuleSource") and resource.name.startswith("rtai"):
        resource.add_location = "in-memory"
        return

    resource.add_location = "filesystem-relative:lib"

def make_exe():
    dist = default_python_distribution(python_version = "3.9")

    policy = dist.make_python_packaging_policy()
    policy.resources_location_fallback = "filesystem-relative:lib"
    policy.register_resource_callback(resource_callback)

    python_config = dist.make_python_interpreter_config()
    python_config.module_search_paths = ["$ORIGIN/lib"]
    python_config.run_module = "rtaisite"

    exe = dist.to_python_executable(
        name = "rtai",
        packaging_policy = policy,
        config = python_config,
    )
    exe.windows_runtime_dlls_mode = "always"
    exe.windows_subsystem = "windows"
    exe.add_python_resources(exe.pip_install(["--use-feature", "in-tree-build", "-r", "requirements.txt"]))

    return exe

@SimonBiggs SimonBiggs linked a pull request Sep 24, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant