Technical Answer - Protecting code in python

Steven D'Aprano steve at REMOVEME.cybersource.com.au
Thu Mar 22 03:07:30 EDT 2007


On Wed, 21 Mar 2007 20:59:57 -0400, Bart Willems wrote:

> Or the consultant wants to be sure that the two dimwitted nimcumpoops at 
> the office who /think/ they can write code don't screw up the script 
> when they're trying to 'fix' or 'improve' something. And blame the 
> consultant if the script all of a sudden stops working 'we did not do 
> anything. It just stopped working all of a sudden'.
> Or he doesn't want to expose the passwords needed to logon to the 
> database server. Or whatever.
> 
> So, there are reasons enough why someone wants to render his code 
> immutable and/or unreadable *to some extent*.

And that's what I've been asking. How to protect the code depends on what
you are trying to protect it from. Why is that so hard to understand?

In the above scenarios, one solution is:

chmod a-w script.py

If they don't have write permission, they can't "improve" it by adding
bugs. If they take the time and effort to give themselves write
permission, then they have accepted responsibility for breaking it.


> The OP clearly states that he does not want a discussion as to why to 
> protect the code.

The OP might not want such a discussion, but without it, any answers he
gets are sure to be bad answers except by accident.



> All he wants is something that turns 'readable, 
> changeable python' into 'unreadable, immutable python'.

chown scriptuser script.py  # a unique user
chmod a-rwx script.py
chmod u+rx script.py

I believe that fully meets the functional requirements. Where shall I send
the invoice?


-- 
Steven D'Aprano 




More information about the Python-list mailing list