How to copy files in python? - python

Basically, what I want to do is this (in a psuedo bash-ish code)
#create ramdisk raid
diskutil erasevolume HFS+ "r1" `hdiutil attach -nomount ram://4661720`;
diskutil erasevolume HFS+ "r2" `hdiutil attach -nomount ram://4661720`;
diskutil createRAID stripe SpeedDisk HFS+ /Volumes/r1 /Volumes/r2;
#copy minecraft server files to ramdisk
cp minecraft_Server /Volumes/SpeedDisk
#start minecraft_server
cd /Volumes/SpeedDisk/minecraft_server
java -Xms2G -Xmx2G -jar minecraft_server.jar nogui
#once I stop the server, copy the files to my harddrive
cd ~
cp /Volumes/SpeedDisk/minecraft_server minecraft_server/
I'm not sure about how to do this ^ in real life :p I was considering using python but it seems like there are problems with os.system for copying files.
Also, I would like to know if there is a way for me to eject the ramdisks when I am done. This is all going to be done in Mac OS X Leopard. The reason I'm doing all of this is to speed up my minecraft server a bit without buying an SSD.

I was considering using python but it seems like there are problems with os.system for copying files.
...then use the right tool for the job:
shutil.copytree()

Shell scripting seems to be the best solution for this kind of problem ( assuming that you want this to work on a single platform mac osx ). Write a shell script with these commands and use that script everytime you want to execute these commands.

Related

How to edit Fedora Server system files from a script

I'm trying to be clever and use a script to set specific configuration values in system files like /etc/ssh/sshd_config, /etc/audit/audit_rules, etc. when I deploy a new Fedora Server 36. The script may run as root.
The tools at my disposal are bash and python.
In my head it would look something like:
#!/pseudo code
for line in /path/to/firefox/policies.json
do
set.PopupBlocking({"Allow":"","Default":true,"Locked":true})
done
OR
sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_conf
next setting
I've also looked at using jq .container[].InstallAddonsPermission: {"Default": false} /path/to/firefox/policies.json.
What is the best practice for this type of task? Is there a python method that would work better than bash?
TIA!

How do I associate python with py files in bash

A common question is "how do I make my python script executable without explicitly calling python on the command line?", and the answer is chmod +x it and then add #!/usr/bin/env python at the start of the script.
That is not the question I'm asking.
What I would like to do is tell bash, or python, or whatever is responsible for file-handling to treat all .py files that have the execute bit set as if they have the shebang at the beginning whether or not they actually do.
I understand that in Windows this can be done, and apparently in Gnome for the use-case where you double-click on a .py script from the GUI. I could have sworn I remembered hearing about an equivalent way of specifying a handler from the shell.
Why I want to know how to do this (if it's actually possible):
Not every system uses shebang and I don't want to clutter up files in a cross-platform project with it.
If I'm submitting a patch to a project I don't own, it's slightly obnoxious for me to put stuff unrelated to the patch into it for my own convenience.
Thanks.
Do you mean binfmt_misc?
binfmt_misc is a capability of the Linux kernel which allows arbitrary executable file formats to be recognized and passed to certain user space applications, such as emulators and virtual machines.
So you want to register an entry to it, so everytime you want to execute a .py file, the kernel will pass it to /usr/bin/python.
You can do it by trying something like this
# load the binfmt_misc module
if [ ! -d /proc/sys/fs/binfmt_misc ]; then
/sbin/modprobe binfmt_misc
fi
if [ ! -f /proc/sys/fs/binfmt_misc/register ]; then
mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
fi
echo ':Python:E::py::/usr/bin/python:' > /proc/sys/fs/binfmt_misc/register
If you're using Debian-based distribution, you have to install binfmt-support.
You can add :Python:E::py::/usr/bin/python: to /etc/binfmt.d/python.conf so it's permenent after reboot.
Rio6's answer is correct. Only it is supported on practically no operating systems. You will need binfmt, you can compile it yourself from source at This git address

Way to run Maven from Python script?

(I am using Windows.)
I am trying to run maven from a python script. I have this:
import subprocess
mvn="C:\\_home\\apache-maven-2.2.1\\bin\\mvn.bat --version"
p = subprocess.Popen(mvn, shell=True, stdout = subprocess.PIPE)
stdout, stderr = p.communicate()
print p.returncode # is 0 if success
It works fine, but I am wondering about the following:
Better ways to add parameters instead of appending the string.
Maybe some specific way to run maven without the above.
A way to show the output (currently it only prints a 1 or 0 based on failure/success).
What I am trying to accomplish long term (I note this in case someone has a better method) is to make a simple script to build a list of projects and move another list of files (jars/other modified things) to a folder to deploy to VMs, it's a huge pain to do manually. I have this working in a batch script no sweat, I am just curious to learn Python and wonder if it'd be easier to manage because I could just make a couple of lists and iterate over each of the locations rather than have a line for each task in the batch script.
(Short version of what my batch script looks like.)
#set version=7.8.3
#set staging_folder=C:\Users\me\Desktop\staging
#set stage_was=%staging_folder%\was
#set stage_ear=%stage_was%\stuffui.ear
#set stage_war=%stage_ear%\stuff-%version%.war
:: delete stage contents
call del /s /q %staging_folder%
call rmdir /s /q %stage_was%
:: make folders
call mkdir %stage_ear%
call mkdir %stage_war%\WEB-INF\lib
:: maven builds
call mvn -f C:\workspace\pom.xml -pl proj1,proj2 clean install
:: copy to stage
call xcopy C:\workspace\proj1\target\thing1.jar %stage_ear%\ /i /y
call xcopy C:\workspace\proj2\target\thing2.jar %stage_ear%\ /i /y
call xcopy C:\workspace\proj2\target\thing2.jar %stage_war%\WEB-INF\lib\ /i /y
There is the Apache Maven Invoker API.
Mark's answer to Accessing Java API information with Python mentions:
Jython which is Python run on the Java VM.
See my answers there for an example on how to use the Maven Invoker (from within Java in this particular case).
Re:
in case someone has a better method
Re:
move another list of files (jars/other modified things) to a folder
Have you considered using Maven itself (the copy-resources goal of its Resources Plugin in this particular case)?
Re:
I am just curious to learn Python
Since you are working with Java anyway: Have you considered Groovy as the scripting language of your choice?
Some of its advantages:
The Java language is a subset of the Groovy language. I.e. every Java code is also Groovy code. You don't have to use the full-fledged Groovy syntax from the beginning while learning it.
It can be scripted.
It is supported as scripting and DSL in tools like Jenkins.

Python - running a script as a service with a name other than 'python'

I have a set of python scripts which I run as a daemon services. These all work great, but when all the scripts are running and I use top -u <USER>, I see all my scripts running as python.
I would really like to know which script is running under which process id. So is there any way to execute a python script as a different process name?
I'm stuck here, and I'm not ever sure what terms to Google. :-)
Note: I'm using Ubuntu Linux. Not sure if the OS matters or not.
Try using setproctitle. It should work fine on Linux.
Don't have a linux system here to test this on appropriately, but if the above doesn't work, you should be able to use the same trick they use for things like gzip etc.
The script has to tell what to run it at the top like this:
#!/usr/local/bin/python
Use a softlink like this:
ln -s /usr/local/bin/python ~/bin/myutil
Then just change your script to
#!~/bin/myutil
and it should show up that way instead. You may need to use a hard link instead of a soft link.
Launching a python script using the python script itself (and file associations and/or shell magic) is not very portable, but you can use similar methods on nearly any OS.
The easiest way to get this is using she bang. The first line of your python script should be:
#!/usr/bin/python
or
#!/usr/bin/python3
depending upon whether you use python or python3
and then assign executable permissions to the script as follows:
chmod +x <scriptname>
and then run the script as
./scriptname
this will show up as scriptname in top.

In Amazon EC2, how do I make it run a python script when I "clone" that instance?

Suppose I have a script in
/home/myuser/go.py
How do I run that script, when a new instance is booted? (I'm used to using the point-and-click control panel Amazon has...)
I'm gonna try my nonexistent Linux skills here - create a shell script that runs your go.py and add a symlink to the shell script in /etc/init.d/
/home/myser/go.sh
#!/bin/bash
python /home/myuser/go.py
symlink
ln -s /etc/init.d/go.sh /home/myuser/go.sh
After reading up a bit myself, /etc/rc.local is probably a better place for this. Just edit it and add /home/myuser/go.sh there (again, make sure your go.sh is executable).

Categories