Excute script only from another file

Rick Johnson rantingrickjohnson at gmail.com
Mon Nov 25 21:28:42 EST 2013


On Monday, November 25, 2013 4:52:46 AM UTC-6, Himanshu Garg wrote:

> My motive is "I will give scripts to somebody else and he
> should not run the script directly without running the 
> parent script".

The only sure fire method to prevent a file containing
Python code from executing on a machine with Python
installed is to use an extension that the system will not
associate with Python.

 ScriptFolder/
   script.py
   mod_1.ignore
   mod_2.ignore
   ...

Of course, any other Python module that needs to leverage
the API contained within the "aliased mod files" will need
to do so *manually*:

 1. Read the file from disc
 2. Evaluate the file contents and make callable
 3. Close the file

Sounds like your distributing a Python application and need
a simplistic method by which "non-techies" can run the code,
but as we all know:

 "fools are too cleaver!"

Of course a smarty pants could could change the extension to 
"py[w]", but that would be violating your interface. :) 

Another choice would be to use a package.

 ScriptPackageFolder/
   __init__.py
   __script.py
   mod_1.py
   mod_2.py
   ...

But nothing could stop them from accessing the contents of
the package directory. 

A third alternative would be to install the support modules
in a place that the user would be unlikely to look and then
place a mainscript somewhere that is easily accessible. But
even this method will not prevent the running of py scripts.

Sounds like my first offering is the best. At least with 
that method they would be forced into an explicit act of 
renaming the file before they violate your interface.



More information about the Python-list mailing list