I'm using <https://pyarmor.readthedocs.io> to prot...
# general
f
I'm using https://pyarmor.readthedocs.io to protect pex-shipped code. pyarmor obfuscates each Python source individually and each input .py gets an output .py of the same name. So I end up with a duplicate code base, means same 1st level imports and same source root. I'm running pyarmor as
adhoc_tool
. How can I tell pants I want to build a pex from the pyarmor output and set an entry point. pants (rightfully) complains about duplicates
IntrinsicError: Can only merge Directories with no duplicates, but found 2 duplicate entries in ...
. I tried to introduce a parent package in the pyarmor output. That basically works, but breaks absolute import statements in the original code in the distributed pex. It also needs a source root for the new parent package (which does only appear as adhoc_tool output), so not ideal. Any elegant ideas?
s
I can suggest 2 possible solutions: • Try to use one resolve for the original code and a different resolve for the generated code. Then create a pex for the second resolve • Before your adhoc tool replace the imports with a different package like this
s/from mypackage/from gen_mypackage/
and proceed with your idea about parent package
f
Thanks for the advice. • For the first option, when I use
entry_point
in the
pex_binary
, it picks the original code, not the adhoc_tool output from pyarmor. However when I use the
executable
field and point to a py file, it will package the pyarmor output. This is confusing. I guess pants will infer the first dependency it finds for a given
entry_point
and pull in all those dependencies. Does not do that however for
executable
mode.
I also want to build a
python_distribution
from the pyarmor output which does not have a resolve field.
python_distribution
does not have a
resolve
field, so this won't work.
s
I'm not sure what is your setup is, but I imagine something like
Copy code
adhoc_tool(
  name="main.py",
  execution_dependencies=["//src/python/mypackage:main.py"],
  output_files=["//generated/mypackage/main.py"],
)
experimental_wrap_as_python_sources(
  name="gen_main.py",
  inputs=[":main.py"],
)
python_distribution(name="dist", dependencies=[":gen_main.py"])
not the adhoc_tool output from pyarmor
you're probably not wrapping adhoc_tool as python sources, which means all the machinery for pex_binary won't work
f
That's basically what I'm doing. In your code example above, if I use an
entry_point
in the
python_distribution
such as
Copy code
python_distribution(
    name="dist",
    dependencies=[":gen_main.py"]
    entry_points={
        "console_scripts": {"cli": "mypackage.main:main"},
    },
Then pants will add the dependency for mypackage.main from the original files. It will produce the error I stated at the beginning about duplicate files. I think I will need to provide a full example.
s
anyway, at this point you can probably make it work by explicitly listing the files you want to pyarmor, but if you want it to work automatically for the whole package with only BUILD file tweaking, it becomes a pretty complicated exercise, I'm not even sure it's possible. So I would recommend looking at writing your own pants plugin for pyarmor