2

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
5
  • Not sure exactly what your question is, but from my understanding you want rdiffArgs to be a list of strings, and add the literal 'rdiff' to the front of that? I think you're looking for: script = [rdiff] + rdiffArgs Or ''.join([rdiff] + rdiffArgs) if you want it as a string. Commented Jan 9, 2010 at 3:30
  • That would result in a list. The asker probably wants the resulting string because that is what's fed to os.system(). Commented Jan 9, 2010 at 3:32
  • 1
    os.system() shouldn't be used, so any answer that enables this should be considered incorrect. Commented Jan 9, 2010 at 4:11
  • Thanks for all your help everyone, hopefully I can help others soon =) Commented Jan 9, 2010 at 5:00
  • You have got to use a dictionary-based substitution, man! bytes.com/topic/python/answers/… Commented Jan 9, 2010 at 5:07

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

os.system is a bad choice here. for example consider what happens if the clientdir or the serverdir has a space in the name
5

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

1 Comment

Hmm, the subprocess module looks very good for what I want to do. Popen looks especially nice. Thank you
3

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)

3 Comments

Man, I better get +10 rep for this answer.
Thanks lpthnc for your input, I have have taken it to heart :)
Hey man, looking at your script - this is what you really want: bytes.com/topic/python/answers/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.