SQLAlchemy "def print_usage()" in Python 3 error - python

I'm following this link https://gist.github.com/thoas/1589496 that copies table from a database to a destination database.
I'm having error with this one
def print_usage():
print """
Usage: %s -f source_server -t destination_server
-f, -t = driver://user[:password]#host[:port]/database
Example: %s -f oracle://someuser:PaSsWd#db1/TSH1 \\
-t mysql://root#db2:3307/reporting
""" % (sys.argv[0], sys.argv[0])
in which I modify following the format for Python 3:
def print_usage():
print("%s -f mysql://root#localhost:3306/db_test -t mysql://root#localhost:3306/db_test1" % (sys.argv[0], sys.argv[0]))
I wanted to test first in MySQL but there is an error
TypeError: not all arguments converted during string formatting
What is the problem on my string? Thanks!

you have only one %s in your format string while you have two variables to put in string.
more details:
when you do
print("%s -f mysql://root#localhost:3306/db_test -t mysql://root#localhost:3306/db_test1" % (sys.argv[0], sys.argv[0]))
you replace each '%s' with a value from (sys.argv[0], sys.argv[0]) so you must have the same number of '%s' and values in the tuple.
try change your code to this:
print("%s -f mysql://root#localhost:3306/db_test -t mysql://root#localhost:3306/db_test1" % (sys.argv[0], ))

Related

Python: how to format a long very complex string to subprocess.run function?

I need to run a couple of complex bash commands from Python. One of them is:
top -n1 -b | awk -vtime="$(date +%Y-%m-%d-%T)" 'NR==7{printf("Time\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",$1,$5,$6,$9,$10,$11,$NF)}' > ~/logs/perfmon.log
This command prints a header to a file, using the top command, and formats the string using awk.
The second command finds the process named process_name and continuously records its performance with the top command to the created file. The command is NOT awaited in Python.
The problem is that the command itself contains both double quotes " and apostrophe ', as well as the escape characters \t and \n. And it's a long string.
I've tried to define the command string as f-string and put double \ before the escape characters, I've tried to use the r-string, but with no success.
Either the header line is printed without tabs between words, or the command is misinterpreted by the awk command.
I attach my function below. Both commands need some help :)
I'd appreciate you checking the solution before posting here.
def execute_performance_monitoring(sample_frequency: int = default_top_frequency) -> None:
today = datetime.today()
date = today.strftime('%Y-%m-%d-%H-%M')
process_name = "find_and_log_me"
logs_dir = Path(logs_path)
full_file_name = logs_dir / f'{process_name}_{date}.log'
print_header_command = (
'top -n1 -b'
' | awk -vtime="$(date +%Y-%m-%d-%T)" \'NR==7{{printf("Time %s %s %s %s %s %s %s\\n",$1,$5,$6,$9,$10,$11,$NF)}}\''
f' > {full_file_name}'
)
print_perfmon_command = (
f'top -b -o +%CPU -d {sample_frequency}'
'| awk -vtime="$(date +%m-%d-%Y-%T)" \'NR>8{{printf("%s %s %s %s %s %s %s %s\\n"'
',\'time,$1,$5,$6,$9,$10,$11,$NF)}}\''
f' | grep --line-buffered {process_name} >> {full_file_name} &'
)
subprocess.run(print_header_command)
subprocess.run(
print_perfmon_command,
shell=True,
stdin=None,
stdout=None,
stderr=None,
close_fds=True,
)
After many tries and days without a success, I've moved the commands to a bash script that takes the relevant parameters, and Python script does the validation job, and calls the bash script with parameters.

3 arguments for 3 tokens but not enough arguments error

If I create a user named Talbot on an ubuntu server, I'm able to manually change ownership of this user's directory by running this command as sudo
chown talbot:talbot -R /home/talbot
I try to do the same thing in the last line of this Fabric python script by doing
sudo("chown %s:%s -R /home/%s" % new_user,new_user, new_user)
However, it keeps giving me this error
sudo("chown %s:%s -R /home/%s" % new_user, new_user,new_user)
TypeError: not enough arguments for format string
Can you explain why this error is happening?
def user_add(new_user, passwd=False):
"""Add new user"""
with settings(hide('running', 'stdout', 'stderr'), warn_only=True):
# if is_host_up(env.host):
if not passwd:
passwd = generate_passwd()
if not sudo("useradd -p $(echo '%s' | openssl passwd -1 -stdin) %s" % (passwd, new_user)).failed:
run('echo "{user} ALL=(ALL:ALL) ALL" >> /etc/sudoers'.format(user=new_user))
if env.host== '107.XX.XX.XXX':
sudo("mkdir /home/%s" % new_user, warn_only=True)
sudo("chown %s:%s -R /home/%s" % new_user,new_user, new_user)
All of your arguments for your format string need to be encased in a single tuple. So instead of
sudo("chown %s:%s -R /home/%s" % new_user,new_user, new_user)
You should use
sudo("chown %s:%s -R /home/%s" % (new_user,new_user, new_user))

Docopt function argument issues

Im not entirely sure how to call my tasklist function with all the required arguments. Im guessing im getting the syntax wrong. Can anyone point out my stupidity please?
CODE
#!/usr/bin/python
"""
Description:
Used for checking users logged into a list of servers.
Usage:
hunter.py (-u <username>) (-p <password>) (-d <domain>) (-s <FILE>)
hunter.py -h | --help
hunter.py --version
Options:
-u --username
-h --help Show this screen.
--version Show version.
-p --password
-d --domain
-s --serverfile=FILE
"""
from docopt import docopt
import subprocess
from subprocess import CalledProcessError
def tasklist(serverlist, domain, username):
for serverl in serverlist():
try:
print "%s Services Listed Below" % serverl
cmd = 'tasklist /V /S %s -u%s\%s /FI "USERNAME eq $s"' % serverl, domain, username, domain
print cmd
subprocess.check_output(cmd)
except CalledProcessError as e:
print(e.returncode)
def getservers(servers):
slist = open(servers).readlines()
return [s.replace('\n', '') for s in slist]
if __name__ == "__main__":
arguments = docopt(__doc__, version='0.1a')
print arguments
serverlist = getservers(arguments['--serverfile'])
print serverlist
tasklist(serverlist,(arguments['<domain>'], ['<username>']))
COMMANDLINE EXAMPLE
c:\Python27>hunter.py -u xpuser -p xpuser -d WORKGROUP -s servers.txt
TRACEBACK
{'--domain': True,
'--help': False,
'--password': True,
'--serverfile': 'servers.txt',
'--username': True,
'--version': False,
'<domain>': 'WORKGROUP',
'<password>': 'xpuser',
'<username>': 'xpuser'}
['192.168.1.122', '192.168.1.121']
Traceback (most recent call last):
File "C:\Python27\hunter.py", line 44, in <module>
tasklist(serverlist,(arguments['<domain>'], ['<username>']))
TypeError: tasklist() takes exactly 3 arguments (2 given)
tasklist() function requires 3 arguments, you are passing just two. Replace:
tasklist(serverlist,(arguments['<domain>'], ['<username>']))
with:
tasklist(serverlist, arguments['<domain>'], arguments['<username>'])
Also, you don't need to call serverlist, it's a list, omit the parenthesis:
for serverl in serverlist:
Also, replace:
cmd = 'tasklist /V /S %s -u%s\%s /FI "USERNAME eq $s"' % serverl, domain, username, domain
with:
cmd = 'tasklist /V /S %s -u%s\%s /FI "USERNAME eq %s"' % (serverl, domain, username, domain)
Hope that helps.

mp3info giving an error with variable filename

I have been using mp3info to calculate my file length using the following syntax:
mp3info -p "%S" /path/to/file
whenever I use the code with the filename I get the correct output:
mp3info -p "%S" far_from_love.mp3
However, on storing the filename in a string variable and then using the variable I get an error:
SyntaxError: invalid syntax
Could anyone tell me how to correctly use the command with a variable filename?
this is the python code which uses mp3info
listing=os.listdir("C:\\Python27")
for f in listing:
if fnmatch.fnmatch(f,'*.mp3'):
ext=f[:-4] #extract name of file without extension
WAVE_OUTPUT_FILENAME="%s.wav"%ext
print WAVE_OUTPUT_FILENAME#save output filename as wav extension
print f
x=os.popen('mp3info -p "%S" f).read()
print x
x=os.popen('mp3info -p "%S" f).read()
is missing a closing quote:
x=os.popen('mp3info -p "%S" ' + f).read()
You may also want to use the safer subprocess module:
import subprocess
x = subprocess.check_output(['mp3info', '-p', '%S', f])

Python TypeError unsupported operand type(s) for %: 'file' and 'unicode'

I'm working on a django field validation and I can't figure out why I'm getting a type error for this section:
def clean_tid(self):
data = self.cleaned_data['tid']
stdout_handel = os.popen("/var/www/nsmweb/jre1.6.0_14/bin/java -jar /var/www/nsmweb/sla.jar -t %s grep -v DAN") % data
result = stdout_handel.read()
Do I have to convert data somehow before I can pass it in as a string variable?
Check your parenthesis.
Wrong
stdout_handel = os.popen("/var/www/nsmweb/jre1.6.0_14/bin/java -jar /var/www/nsmweb/sla.jar -t %s grep -v DAN") % data
Might be right.
stdout_handel = os.popen("/var/www/nsmweb/jre1.6.0_14/bin/java -jar /var/www/nsmweb/sla.jar -t %s grep -v DAN" % data )
Just a small tip - it's better to use subprocess module and Popen class instead of os.popen function. More details here (docs).

Categories