Wait for a system command executed in the background

Discussion in 'Python' started by Brice, Nov 5, 2006.

  1. Brice

    Brice Guest

    Hello


    I am new to python and I am really a bad programmer...

    As a linux user, I use quodlibet for listening to music. Quodlibet
    allows me to write plugins in python.

    I am trying to write a plugins to burn a playlist by using cdrecord, and
    this either in dao or tao mode.

    The basic of this plugin is the following:

    - make a temp directory
    - convert the songs in my playlist into wave files in the temp directory
    using mplayer and renaming them with 3 digits number so that the order
    of my playlist is conserved
    - normalize the wave files
    - burn the cd
    - delete the temp directory.


    At the time, I have a working solution (not fully developped by
    myself...), but in this solution os.system is used to start the
    different shell commands and the commands do not have a & at the end.
    Therefore during the whole execution of the plugin, quodlibet is not
    responsive.

    What I would like to is program the whole such that I can use & at the
    end of the shell commands and each command to be executed one after the
    other! (I tried to put & simply at the end of each shell command with
    the result that each where started at the same time)

    Here is the plugin:

    ###########################################################################


    import os, string, re
    import util
    import sys
    import shutil
    from qltk import ErrorMessage
    from shutil import copy
    from plugins.songsmenu import SongsMenuPlugin

    class _Base(SongsMenuPlugin):
    PLUGIN_ICON = 'gtk-cdrom'
    PLUGIN_VERSION = '0.1'

    # XXX : pb potentiel: si deux utilisateurs utilisent ce plugin
    # en meme temps, ils vont se marcher sur les pieds...
    destdir = "/tmp/cd_QL"

    convert_cmd = "mplayer -ao pcm:file=%(dest)s %(source)s"
    normalize_cmd = "normalize -m %(path)s/*.wav"
    burn_cmd = "cdrecord -v -eject %(flags)s -pad -audio
    %(path)s/*.wav"
    burn_flags = ''

    def plugin_songs(self, songs):
    if not self.check_mplayer():
    return

    self.setup()
    try:
    try:
    for num, song in enumerate(songs):
    self.makewav(song, num)
    self.normalize()
    self.burn()
    except Exception, e:
    ErrorMessage(
    None,
    "Unknown error",
    "An error occured while processing files:\n%s." % e
    ).run()
    finally:
    self.teardown()

    def check_mplayer(self):
    if not util.iscommand("mplayer"):
    ErrorMessage(
    None,
    "Mplayer not found",
    "Mplayer is used to convert files in wave files."
    ).run()
    return False
    return True

    def setup(self):
    os.mkdir(self.destdir)

    def makewav(self, song, num):
    source = song['~filename'].replace(" ", "\
    ").replace("'","\\'")
    dest = os.path.join(self.destdir, "%03d.wav" % num)
    cmd = self.convert_cmd % dict(source=source, dest=dest)
    os.system(cmd)

    def normalize(self):
    os.system(self.normalize_cmd % dict(path=self.destdir))

    def burn(self):
    flags = self.burn_flags
    cmd = self.burn_cmd % dict(path=self.destdir, flags=flags)
    os.system(cmd)

    def teardown(self):
    shutil.rmtree(self.destdir)


    class DAO(_Base):
    PLUGIN_ID = 'Burn CD w/o blanks'
    PLUGIN_NAME = _('Burn CD w/o blanks')
    PLUGIN_DESC = 'Burn CD w/o blanks.'
    burn_flags = '-dao'


    class TAO(_Base):
    PLUGIN_ID = 'Burn CD with blanks'
    PLUGIN_NAME = _('Burn CD with blanks')
    PLUGIN_DESC = 'Burn CD with blanks.'

    # on exporte seulement ces deux classes
    __all__ = ['DAO', 'TAO']



    #############################################################################



    and yeah, please consider that I am new to python and do understand
    something to the thread!


    Thanks in advance

    --
    Brice
    Arch Linux (Linux user nb. 372699)
    -----
    "Unix IS user friendly, it is just selective about who his friends are"
     
    Brice, Nov 5, 2006
    #1
    1. Advertising

  2. On Sun, 5 Nov 2006 18:32:39 +0100, Brice
    <mealier_brice@NO_SPAM_yahoo.fr> declaimed the following in
    comp.lang.python:


    >
    > At the time, I have a working solution (not fully developped by
    > myself...), but in this solution os.system is used to start the
    > different shell commands and the commands do not have a & at the end.
    > Therefore during the whole execution of the plugin, quodlibet is not
    > responsive.
    >

    Might it not be easier to just write each of those commands to a
    temporary file, then at the end submit that file as a shell script that
    runs in the background? {Maybe the last command should be one the
    deletes the file itself?}

    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Nov 5, 2006
    #2
    1. Advertising

  3. Brice

    Brice Guest

    On 2006-11-05, Dennis Lee Bieber <> wrote:
    > On Sun, 5 Nov 2006 18:32:39 +0100, Brice
    ><mealier_brice@NO_SPAM_yahoo.fr> declaimed the following in
    > comp.lang.python:
    >
    >
    >>
    >> At the time, I have a working solution (not fully developped by
    >> myself...), but in this solution os.system is used to start the
    >> different shell commands and the commands do not have a & at the end.
    >> Therefore during the whole execution of the plugin, quodlibet is not
    >> responsive.
    >>

    > Might it not be easier to just write each of those commands to a
    > temporary file, then at the end submit that file as a shell script that
    > runs in the background? {Maybe the last command should be one the
    > deletes the file itself?}
    >


    At the time being I don't see other possible solution. Let's see if
    someone else's got an idea.

    Thx

    --
    Brice
    Arch Linux (Linux user nb. 372699)
    -----
    "Unix IS user friendly, it is just selective about who his friends are"
     
    Brice, Nov 5, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samy
    Replies:
    0
    Views:
    435
  2. Huey

    How to make "fork/wait" to WAIT longer?

    Huey, Mar 1, 2004, in forum: C Programming
    Replies:
    1
    Views:
    1,989
    -berlin.de
    Mar 1, 2004
  3. Sami Viitanen
    Replies:
    1
    Views:
    366
    Fredrik Lundh
    Aug 7, 2003
  4. Bob
    Replies:
    10
    Views:
    876
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Feb 23, 2007
  5. sayoyo Sayoyo
    Replies:
    1
    Views:
    239
    Reacher
    Feb 15, 2008
Loading...

Share This Page