tags:

views:

131

answers:

3

Hello, I have a question. I have been really trying to learn Python. For a project, I want to make an ncurses GUI for my backup server. My backup server runs rdiff-backup, and I want to have the ncurses take in variable names and plug them into my script. I have been trying to do a lot of reading so I don't ask dumb questions.

Here is my function for running the script:

def runScript():
# Cannot concatenate 'str' and 'list' objects
#script = rdiff + rdiffArgs

script = rdiff + ' ' + rdiffVerbosity + ' ' + rdiffStatistics \
         + ' ' + clientName + '@' + clientHost + '::' + clientDir \
         + ' ' + serverDir

os.system(script)

What I originally thought would be neat was to add all the variables into a list, so I could just run say

script = rdiff + rdiffArgs

Is there a better way to do this without all the space concatenation?

Thanks for your assistance

EDIT: Let me post the whole script so far. I wasn't very clear and I really appreciate your help and patience

  #!/usr/bin/env python



import os
import smtplib


# Global variables
rdiff = '/usr/bin/rdiff-backup'
rdiffVerbosity = '-v5'
rdiffStatistics = '--print-statistics'
emailSmtp = 'smtp.gmail.com'
smtpPort = '465'
emailUsername = 'reports'
emailPassword = '3kc9dl'
emailTo = '[email protected]'
emailFrom = '[email protected]'
serverName = 'root'
serverHost = 'SV-Datasafe'
serverDir = '/srv/backup/SV-Samba01'
clientName = 'root'
clientHost = 'SV-Samba01'
clientDir = '/srv'
rdiffArgs = rdiffArgs = [rdiffVerbosity, rdiffStatistics, \
                         clientName + '@' + clientHost + '::' \
                         +clientDir + ' ' + serverDir]
time = ''
dateStamp = datetime.now()



def sendEmail():
    subject = dateStamp + clientName
    body = clientDir + ' on ' + clientHost + ' backed up to ' + serverName + \
           ' in the directory ' + serverDir + ' on ' + dateStamp
    message = """\
    From: %s
    To: %s
    Subject: %s
    %s
    """ % (emailFrom, emailTo, subject, body)


    deliverEmail = smtplib.SMTP(emailSmtp, port=smtpPort)
    deliverEmail.login(emailUsername, emailPassword)

def runScript():
    # Cannot concatenate 'str' and 'list' objects
    #script = rdiff + rdiffArgs

    script = rdiff + ' ' + rdiffVerbosity + ' ' + rdiffStatistics \
             + ' ' + clientName + '@' + clientHost + '::' + clientDir \
             + ' ' + serverDir

    os.system(script)

    # TODO:: Logging
+2  A: 

You join paths using os.path.join

You concatenate strings like so: "".join(['a', 'b']) or ", ".join(['c', 'd'])

Which part is difficult? I am not sure I understand the question 100%

Is this it?

script = rdiff + " ".join(rdiffArgs)
Hamish Grubijan
Man, I better get +10 rep for this answer.
Hamish Grubijan
Thank you !!!!!!!!!
Hamish Grubijan
Thanks lpthnc for your input, I have have taken it to heart :)
Dan
Hey man, looking at your script - this is what you really want: http://bytes.com/topic/python/answers/34481-keyword-substitution-string
Hamish Grubijan
Thank you for the link
Dan
+4  A: 

you can use format specifiers

def runScript():
    script = "%s %s %s@%s %s::%s %s" %(rdiff,rdiffVerbosity,rdiffStatistics,clientName,clientHost,clientDir,serverDir)    
    os.system(script)

or say your rdiffArgs is already in a list

rdiffArgs = [rdiffVerbosity,rdiffStatistics,clientName,clientHost,clientDir,serverDir]

you can join them with a space

rdiffArgs = ' '.join(rdiffArgs)

lastly, just so you might want to know, you can import rdiff in your script , since rdiff-backup is written in Python

from rdiff_backup.Main import Main as backup
task=['/etc', '/tmp/backup']
backup(task)

the above backs up /etc/ to /tmp/backup. That way, you don't have to make system call to rdiff-backup. Of course, this is up to you. making system call is sometimes easier

ghostdog74
Oh wow, I did not know that. Thank you very much
Dan
os.system is a bad choice here. for example consider what happens if the clientdir or the serverdir has a space in the name
gnibbler
+5  A: 

try to use subprocess module and pass arguments as list e.g.

client = clientName + '@' + clientHost + '::' + clientDir
cmd = [rdiff, rdiffVerbosity, rdiffStatistics, client , serverDir]
p = Popen(cmd ", shell=True)
print os.waitpid(p.pid, 0)[1]

or if have args already as list use something like this

cmd = [rdiff] + args
Anurag Uniyal
Hmm, the subprocess module looks very good for what I want to do. Popen looks especially nice. Thank you
Dan
yes Dan. and you shouldn't be using os.system
Anurag Uniyal