python cvs check out - python

I am writing an automation script in python and I need to cvs update from script.
In bash my commands look like this:
cvs login
CVS Password: <here i enter the password>
cvs update -P -d
I was using the sub-process module in python to do this, but it fails when it asks for the password.
Any ideas?

The pyCVS module can help you sovle the problem by using a binding.
In general, subprocesses have been, in my experience, much more trouble than just using a library that accomplishes the same thing.

Generally you can pass as arguments like this simplified code:
## program run.py
def print_args(name, passwd):
print name
print passwd
## calling program
import run
input_name = raw_input("Enter name ")
input_passwd = raw_input("Enter password ")
run.print_args(input_name, input_passwd)

Related

Python 3 input() function not allowing input

I haven't really ran into many issues with python but I'm working on a side project and the input() function wasn't working. I created a new file to test out a simple input() function and the same thing happened, the program doesn't take any input and it instead seems to take in the pyenv version or something? Anyone know how to fix this?
# Taking input from the user
name = input("Enter your name")
# Print input
print("Hello", name)
Pasted output from terminal:
Enter your namepyenv shell 2.7.18
Hello pyenv shell 2.7.18

How to pass arguments to terminal?

I have a python script that does a zipping of a few files. For zipping the files with a password I use os.system() function and pass my zipping command ie. zip -j -re <file_path> <to_path>. This command asks for a password on the terminal which I have to put manually on the terminal like this.
Enter password:
Verify password:
I want to hardcode my password and pass it when the terminal asks for a password.
I tried to send the passwords using os.system(password) twice thinking that it will take it but it did not work.
cmd = "zip -j -re {} {}".format(DUMPSPATH,TEMPPATH)
passwrd = 'hello123'
os.system(cmd)
os.system(passwrd)
os.system(passwrd)
How do I send arguments using python to fill passwords automatically?
When you call os.system(), you are sending a command to your shell to run. So calling os.system(passwrd) is sending your password as a standalone command to be run by the shell, which is not what you want here.
The zip program features a -P password argument that allows you to specify the password to encrypt zip files when you initially run the command without having to manually input it for batch jobs. So essentially your code should be changed to something like this:
passwrd = 'hello123'
cmd = "zip -j -re -P {} {} {}".format(passwrd, DUMPSPATH,TEMPPATH)
os.system(cmd)
Another side note, it's recommended to use the subprocess module instead of resorting to os.system, so take a look at subprocess.Popen if you get a chance.
It's nice computer_geek has found the correct command line argument (I can't see it listed over here, but it works), but I absolutely HAVE to post my ludicrous solution (for posterity). NOTE - requires xdotool installed on your system.
import subprocess
DUMPSPATH = ""
TEMPPATH = ""
pwd = "lala"
zipper = subprocess.Popen(['zip','-j','-re',DUMPSPATH,TEMPPATH])
subprocess.run(['xdotool','type',pwd])
subprocess.run(['xdotool','key', 'Return'])
subprocess.run(['xdotool','type',pwd])
subprocess.run(['xdotool','key', 'Return'])
zipper.wait()

How to get bash script take input from python script

I am new to programming in python and wanted to try something out.
I have a bash script, which takes in parameters like User name, Full name etc. This is the bash script I have
#!/bin/sh
echo -n "username: "
read username
echo -n "First name: "
read first
echo -n "Last name: "
read last
echo "$username", "$first", "$last"
I am trying to call the bash script via python. Except I want to enter the parameters in python and it needs to be parsed to the bash script. Any help will be greatly appreciated.
Python code to call the bash script
import os
import sys
os.system("pwd")
os.system("./newuser1.list" )
You are not specific to your python version, but here is something that works with 2.7.
As I understand, you want to take input in the python script and pass it to the bash script. You can use raw_input("Prompt") in python 2, and just input("Prompt") for python 3. When passing the params to the shell script, simply append them to the string passed to the shell.
As such:
import os
user_name = raw_input("Enter username: ")
first_name = raw_input("Enter firstname: ")
last_name = raw_input("Enter lastname: ")
os.system("./test.sh {0} {1} {2}".format(user_name, first_name, last_name))
For the shell script, grab the params with the $1, $2... environment style variables. Put them in variables like below, or just use them directly.
The shell script (For the example i named it test.sh):
#!/bin/sh
userName=$1
firstName=$2
lastName=$3
echo "$userName, $firstName, $lastName"
Is this what you were looking for?

Interactive Python like Interactive Ruby?

I'm trying to load a ".py" file in Python like I did with a ".rb" file in interactive Ruby. This file runs a code which asks for a user's name, and then prints "Hello (user's name), welcome!". I have searched all over but I can't quite find a solution.
I know how to do this in Ruby, but how can I do this in Python?
Ruby Code (test.rb)
print "Enter your name:"
input = gets.chomp
puts "Hello " + input + "! Welcome to Ruby!"
Python Code (test.py)
name = raw_input("What is your name? ")
print ("Hello" + (name) + "Welcome to Python!")
How I run it in interactive ruby (irb)
So how would I do the same in python?
In Python 2 execfile("test.py") is equivelant to the Ruby load "test.rb"
In Python 3 execfile has been removed probably as this isn't usually the recommended way of working. You can do exec(open("test.py").read()) but a more common workflow would be to either just run your script directly:
python test.py
or to import your test.py file as a module
import test
then you can do reload(test) to load in subsequent edits - although this is much reliable than just re-running python test.py each time for various reasons
This assumes test.py is in a directory on your PYTHONPATH (or sys.path) - typically your shells current folder is already added
You just say import file_name in the python interpreter this should help you. Then file_name.function_to_run() of course you don't have to do this step if you have the function calls at the bottom of the script it will execute everything then stop.
From the command-line
You can use the -i option for "interactive". Instead of closing when the script completes, it will give a python interpreter from which you can use the variables defined by your script.
python -i test.py
Within the interpreter
Use exec
exec(open("test.py").read())
Reference thread.
You are using python 3
so you do it like this
name = input("Enter your name: ")
print("Hello " + name + ". Welcome to Python!")

I can't run python file interactivly with terminal

I'm using OSX Mac terminal to run python 2.7.10.
for example:
I have a file called "myfile.py"
so when I want to run it on the terminal it would be like this:
python Desktop/myfile.py
However inside the file I have wrote some functions like
def myFunction(x,y):
return float(x)/y
with this method of running a python script I can not interact with my program and use myFunction to input x and y with different values every time and test myFunction properly.
Thank You,
Try passing -i before the script name
python -i myfile.py
You can learn more about what options are available by running man python.
To quote from the manual:
-i When a script is passed as first argument or the -c option
is used, enter interactive mode after executing the script
or the command. It does not read the $PYTHONSTARTUP file.
This can be useful to inspect global variables or a stack
trace when a script raises an exception.
You can use python -i script.py
This way, script.py is executed and then python enter interactive mode. In interactive mode you can use all functions, classes and variables that was defined in the script.
You can use raw_input() to do that.Your myfile.py code can look like this:
def myFunction(x,y):
return float(x)/y
x = raw_input("Please enter x value: ")
y = raw_input("Please enter y value: ")
print(myFunction(x,y))

Categories