Part-Python plugins and encryption

Hi,

My plugin uses a few Python scripts to perform specific calculations. These scripts, together with a local Python environment, are saved within the plugin folder.

Until now I have shared my plugin encrypting only the Ruby files. Now I am starting a wider testing process and I would like to be able to protect the whole code.

I know I can use py2App and py2exe to create Python executable, but I was wondering whether there was a recommended approach for cases like mine.

Thanks

I do not believe this will be allowed in the EW Store. Nor should Python ever be installed in SketchUp’s “Plugins” folder. It is just not the place for it.

Python is so much like Ruby, it is (usually) quite easy to translate Python scripts into Ruby. That is what I did for the Hypocycloid Cam script that was written to output a DXF file. I translated it into SketchUp Ruby, so it created a group containing closed “poly-arc” for the cam profile.

The Ruby (rbs) scrambler and newer (rbe) encrypter can not scramble nor encrypt python files directly. And they cannot reverse this at runtime directly. The python code would have to be wrapped as a Ruby string. Then somehow at runtime this string would have to be passed to a python interpreter. This would use standard IO. Standard IO can be easily “seen” or intercepted.

This defeats the idea of using encryption.

If you must use Python, just use the user’s Python install. I think it creates a “PYPATH” environment variable so you can get the path to it’s interpreter.

It is much better that the user install Python themselves, than you do.

From the latest couple of SourceForge newletters:

For our March “Staff Pick” Project of the Month, we selected WinPython, an open-source portable distribution of the Python programming language for Windows XP/7/8/10 and scientific and educational usage. You can install as many WinPython distributions as you want on the same machine: each one is isolated and self-consistent. These installations can even be different versions of Python and have different architectures (32-bit/64-bit).
Download for WinPython:
https://sourceforge.net/projects/winpython/files/latest.

Yes, I kinda suspected it, but I am not going to release the plugin anytime soon. This approach makes the distribution during testing much easier as there are several specific Python libraries that the user needs to install and I would like to remove any obstacle for them to start to use the plugin.

The reason I must use Python is that I use several scientific libraries that are usually nicely wrapped in Python and not in Ruby. And even if they were probably I could not compile them in SketchUp anyway :slight_smile:

I will look into WinPython. Thanks

If they require compilation, you do it outside SketchUp, using standalone Ruby installs.

Ruby 2.0.0-p247 (both 32-bit and 64-bit,) and the two DevKits (32-bit and 64-bit,) then use a version manager like uru, to set the current “system Ruby”.

1st the 32-bit. Set the 32-bit Ruby as current using uru (or whatever RVM you like.).
Enter the “DevKit32” directory.
And run it’s “dk.rb” via:
ruby dk.rb init
then:
ruby dk.rb review
Make sure it only wants to modfiy the 32-bit Ruby.
and:
ruby dk.rb install

Then compile a 32-bit library or gem.

Repeat after setting the 64-bit Ruby as current, but enter the “DevKit64” directory before calling the “dk.rb” script.

Note that you only need to “connect” each “system” Ruby install with the appropriate DevKit once.

You copy the compiled so files into distribution “bitness” sub-folders of your extension.

Do the same on a Mac machine if you can. (Only 64-bit needed.)

What are these libraries ? If Ruby wrapping is needed, then that is the real issue.

If they are already pre-compiled DLLs, then they can be accessed using Fiddle.

1 Like

A few from SciPy or NetworkX for example. Then other packages to manage OpenFOAM.

Good tip about Fiddle. I will look into it.

for most mac users, adding a period in front of the filename will protect the code as well as anything…

what they can’t see they won’t open…

although, I tend to have a .bin hidden folder and keep compiled bin scripts in there…

it’s best to make sure they are signed or do a chomd or both…

  OSX = Sketchup.platform == :platform_osx unless defined? JcB::OSX
  BHT = File.join(__dir__, '.bin/bhtxt') if OSX && !defined? JcB::BHT
  File.chmod(0755, BHT) if OSX && !File.executable?(BHT)

and similar with an app

          if JcB::OSX and @speed_bump
            sb = File.join(__dir__, '.bin/SpeedBump.app').inspect
            File.chmod(0755, sb) unless !File.executable?(sb)
            spawn("open #{sb}" )
            sleep 0.5
          end

I would be inclined to use system Python and only have additional libs in my plugins subfolder…

john

Well, I have been looking into Fiddle (and FFI) and it has been a revelation. Now I can use directly the precompiled C libraries used by the Python packages and I have much more control. In some calculations I have an increase in speed by 100 times because I don’t need to transfer large files between Ruby and Python. I just wanted to thank you.

Oh, and the icing on the cake is that I don’t need to encrypt anymore!

1 Like

Both are wrappers into the libffi library.
The Ruby FFI gem is no longer maintained.
Fiddle is it’s standard library replacement.

Hi @roji
Could you expand a bit on what you did at the end to use your python code/libs for sketchup?
I am in a similar situation but haven’t got yet the revelation;-)

Your profile says you are using the Free web edition which cannot use extensions, Ruby or otherwise.

But for the sake of answering the question for desktop developers …

Ruby’s Fiddle library is a wrapper around libffi (the Foreign Function Interface) …

The Fiddle library comes distributed with SketchUp’s Ruby since 2014.

Here is a tutorial blog on HoneyBadger …

Thank you for your answer.
I am using the free web edition but may switch to the desktop one if worthwhile.
I understand what fiddle is (Similar to Overview — CFFI 1.14.6 documentation in python) yet I am missing the global picture of integrating python code/script via fiddle. Is it about calling networkX or numpy via fiddle? Or about starting a python interpreter via fiddle? Or yet something else?

No. The original poster actually wanted to use compiled DLLs that happen to come with the Python install. (Ie, an example of a true set of “foreign functions”.)

Eventually the coder would use these from within SketchUp’s embedded Ruby process using Fiddle.

There is no need. Ruby can do anything that Python can do. They are very similar scripting languages with just a bit different lexicon.

SketchUp (desktop) comes with embedded Ruby … ie, there is already a Ruby interpreter running inside SketchUp that must be controlled by the SketchUp application. I don’t think you can just start up another interpreter process inside SketchUp. You might do that in it’s own outside process. There are some extensions that do little file ops in Visual Basic scripts that run outside of SketchUp. Also command scripts are popular on both platforms.

I myself am not familiar with these libraries, and I did not actually see if there were Ruby gems available at the time. (This topic thread is 5 years old now.)

A quick search (now) of the RubyGems repository shows that there are …