Safe to change python file during run time? - python

I have python 3 file that run a SVN deployment. Basically run "python3 deploy.py update" and following things happen:
Close site
Backup Ignore but secure files
SVN revert -R .
SVN update
Trigger tasks
Open site
That all sounds simple and logical, but for one thought going around my head "SVN is writing files, including python files and sub module helpers that are trigger the SVN subprocess"
I understand that python files are read and processed and only through some tricky reload will python reload. And I understand if SVN change python source then update would only take effect on next run.
But question is "should keep this structure or move file to root and run SVN to be safe side"
Applies to GIT or any python changes

From what I know it is safe to change python (i.e. .py) file while python is running, after .pyc file has been created by python (i.e. your situation). You can even remove .py file and run .pyc just fine.
On the other hand SVN revert -R . is dangerous here as it would attempt removing .pyc files, so either screw up your python or fail by itself.

Related

Can I read code from a previously run python script

I ran a python file using the command python file.py and it executed successfully. Right after I managed to delete the file, can I recover the code that was run in the previous command? I still have the terminal open and have not typed anything else into it. Running ubuntu.
if you had imported the file instead of running it directly, a .pyc file would have been created containing the compiled bytecode which you could easily transform back into the regular python code (minus the comments.) However, you did run the script directly, so there is no .pyc file.
If you deleted the file in a GUI file browser, the file might have been sent into a "trash bin" where you might be able to recover it from.
Assuming you deleted the file using the "rm" command in the terminal, however, the data might still be on the disk provided it hasn't been overwritten ("deleting" files normally just marks the data to be overwritten on the disk)
If you're lucky, you might be able to recover it. The process isn't exactly simple, though, and there's no guarantee that the file hasn't already been overwritten since the disk is pretty much in constant use when you're using the system. More info on that here:
https://help.ubuntu.com/community/DataRecovery
There's also a handy utility called lsof which you can use to recover a 'deleted' file if there's still an open file handle for it:
https://support.hpe.com/hpsc/doc/public/display?docId=emr_na-c00833030
Also, in the future, I recommend using "rm -i" instead of just rm with no options as that will at least prompt you to confirm if you're sure you want to delete something first. You can also make an alias for this in your shell so that regular rm just points to 'rm -i'

Does python interpreter keep py or pyc file of currently running script in a temporary folder?

After 3 intensive hour, I was testing my script on terminal. However, my editor messed up and it overwrote my script when it was being still executed on terminal. Well, I didn't terminate running script, so I was wondering that does python interpreter keep the currently running file in a temporary folder or somewhere else so that I can recover my script?
Python tries to cache your .pyc files. How that's done has changed over time (see PEP 3147 -- PYC Repository Directories. Top level scripts are not cached but imported ones are. So, you may not have one.
.pyc files are compiled byte codes so its not just a question of renaming it .py and you can't figure them out just by looking at them. There are decompilers out there like the one refenced here:
Decompiling .pyc files.
Personally, I create mercurial repos for my scripts and check them in frequently.... because I've made a similar mistake a time or two. git, svn, and etc... are other popular tools for maintaining repos.
Depending on your operating system and editor, you may have a copy in Trash or even saved by the editor. You may also be able to "roll back" the file system.
If you're running Linux, you may still be able to find a handle to open files in the /proc/ directory if the process is still running. This handle will keep the file from being deleted. Details see: https://superuser.com/questions/283102/how-to-recover-deleted-file-if-it-is-still-opened-by-some-process

Recompile all Python files in directory

I have a Python directory with a number of .py files. I recently compiled them into .pyc files using python -m compileall. I have now changed some of the source files and would like to recompile, writing over the old .pyc files.
Is there a quick way to do this from the command line without having to manually delete all existing .pyc files?
You just have to run python -m compileall again. It will overwrite older .pyc files. You can pass it the -f switch to force rebuild even if timestamps are up to date (as per the documentation).
When the source code has changed, new .pyc files are automatically created when you run the program again. Therefore I wouldn't worry about compiling, but focus your attention on the code itself.. :)

Cannot find .pyc files for django/apache/mod_wsgi

I am running a web app on python2.7 with mod_wsgi/apache. Everything is fine but I can't find any .pyc files. Do they not get generated with mod_wsgi?
By default apache probably doesn't have any write access to your django app directory which is a good thing security wise.
Now Python will byte recompile your code once every apache restart then cache it in memory.
As it is a longlive process it is ok.
Note: if you really really want to have those pyc, give a write access to your apache user to the source directory.
Note2: This can create a hell lot of confusion when you start with manage.py a test instance shared by apache as this will create those pyc as root and will keep them if you then run apache despite a source code change.
When a module is imported for the first time, or when the source is more recent than the current compiled file, a .pyc file containing the compiled code will usually be created in the same directory as the .py file.
So if you are not importing the module then no files will be created.
Besides this, a .pyc file may not be created is permissions problems with the directory. This can happen, for example, if you develop as one user but run as another, such as if you are testing with a web server. Creation of a .pyc file is automatic if you’re importing a module and Python has the ability (permissions, free space, etc.) to write the compiled module back to the directory.
Note - Running a script is not considered an import and no .pyc will be created.
If you need to create a .pyc file for a module that is not imported, you can use the py_compile and compileall modules.

Fileconveyor Installation

With Fileconveyor limited documentation I'm confused as to where it installs after I've run the pip command as follows on their website Fileconveyor.org.
Bottom line: Anyone have luck installing Fileconveyor on Debian 6 for integration with Drupal 6 and the CND Module?
I can't figure out where to put my settings.xml file.
Thanks,
Curtis
The documentation does give indication of where things are put, but it isn't entirely clear in that we expect an "installation" to move things to certain destinations, such as /usr/bin. In reality, Fileconveyor is installed in the very same directory as wherever the git clone placed it.
The settings file (which must be cp from a file named "config.sample.xml") is in a folder 'conveyor' within the main 'conveyor' folder.
The link where you can read about this is https://github.com/wimleers/fileconveyor
It reads in part: "The sample configuration file (config.sample.xml) should be self explanatory. Copy this file to config.xml, which is the file File Conveyor will look for,
and edit it to suit your needs."
Starting it doesn't actually invoke any command with the name 'fileconveyor', which I previously mentioned is what one might expect from a typical installation. Another instruction from the above link reads:
"Starting File Conveyor
File Conveyor must be started by starting its arbitrator (which links
everything together; it controls the file system monitor, the processor
chains, the transporters and so on). You can start the arbitrator like this:
python /path/to/fileconveyor/arbitrator.py"
In my case the command is 'python ~/src/conveyor/conveyor/arbitrator.py'
In retrospect I might reinstall in another directory in case I ever empty my ~/src folder which is the folder I use to initially download items to compile and install, then clean. I wasn't expecting it to end up being the installation folder for Fileconveyor.
Hope this helps.

Categories