USB Syncing: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "<source lang="python"> import os, time initial = set(os.listdir("/media")) while True: media = set(os.listdir("/media")) new = media.difference(initial) if (len(...")
 
No edit summary
Line 43: Line 43:
http://stackoverflow.com/questions/17830333/start-raspberry-pi-without-login
http://stackoverflow.com/questions/17830333/start-raspberry-pi-without-login


== Getting it to work on a PI ==
Getting it to work on a pi required some altering. First we needed to install the usbmount package.
  sudo apt-get install usbmount
This however creates a set of /media/usb0, /media/usb1 mount points that are always there. So we can't simply check to see if new file names exist. The fix is to alter the usbmount options to make sure that the auto-mounted files are owned by the "pi" user, and then to filter the list to only check those owned by pi (aka uid 1000).
<source lang="python">
import os, time
initial = set(os.listdir("/media"))
def checknew ():
    files = os.listdir("/media")
    files = [x for x in files if os.stat("/media/"+x).st_uid == 1000]
    return set(files).difference(initial)
def do(cmd):
    print cmd
    os.system(cmd)
while True:
    new = checknew()
    if (len(new) == 2):
        sticks = list(new)
        print "TIME TO SYNC"
        do("mplayer -loop 0 sticksync/sync.ogg &")
        # rsync 0 => 1
        cmd = "rsync -rv /media/{0}/ /media/{1}/ ".format(sticks[0], sticks[1])
        do(cmd)
        # rsync 1 => 0
        cmd = "rsync -rv /media/{1}/ /media/{0}/ ".format(sticks[0], sticks[1])
        do(cmd)
        do("pumount /media/{0}".format(sticks[0]))
        do("pumount /media/{0}".format(sticks[1]))
        do("killall mplayer")
        while True:
            print "waiting for less than two sticks"
            new = checknew()
            if (len(new) < 2):
                break
            time.sleep(1)
        # os.system(cmd)
    else:
        print "nothing to do"
    time.sleep(1)
</source>
<source lang="bash">
usbmount options...
</source>
[[Category:Cookbook]]
[[Category:Cookbook]]

Revision as of 18:34, 13 January 2014

import os, time

initial = set(os.listdir("/media"))

while True:
    media = set(os.listdir("/media"))
    new = media.difference(initial)
    if (len(new) == 2):
        sticks = list(new)
        print "TIME TO SYNC"
        os.system("mplayer -loop 0 sticksync/sync.ogg &")

        # rsync 0 => 1
        cmd = "rsync -rv /media/{0}/ /media/{1}/ ".format(sticks[0], sticks[1])
        print cmd
        os.system(cmd)

        # rsync 1 => 0
        cmd = "rsync -rv /media/{1}/ /media/{0}/ ".format(sticks[0], sticks[1])
        print cmd
        os.system(cmd)
        os.system("killall mplayer")

        while True:
            print "waiting for less than two sticks"
            media = set(os.listdir("/media"))
            new = media.difference(initial)
            if (len(new) < 2):
                break
            time.sleep(1)

        # os.system(cmd)
    else:
        print "nothing to do"

    time.sleep(1)

https://archive.org/details/EqueTheartoflisteningSync

Auto login on pi: http://stackoverflow.com/questions/17830333/start-raspberry-pi-without-login

Getting it to work on a PI

Getting it to work on a pi required some altering. First we needed to install the usbmount package.

  sudo apt-get install usbmount

This however creates a set of /media/usb0, /media/usb1 mount points that are always there. So we can't simply check to see if new file names exist. The fix is to alter the usbmount options to make sure that the auto-mounted files are owned by the "pi" user, and then to filter the list to only check those owned by pi (aka uid 1000).

import os, time

initial = set(os.listdir("/media"))

def checknew ():
    files = os.listdir("/media")
    files = [x for x in files if os.stat("/media/"+x).st_uid == 1000]
    return set(files).difference(initial)

def do(cmd):
    print cmd
    os.system(cmd)

while True:
    new = checknew()
    if (len(new) == 2):
        sticks = list(new)
        print "TIME TO SYNC"
        do("mplayer -loop 0 sticksync/sync.ogg &")

        # rsync 0 => 1
        cmd = "rsync -rv /media/{0}/ /media/{1}/ ".format(sticks[0], sticks[1])
        do(cmd)
        # rsync 1 => 0
        cmd = "rsync -rv /media/{1}/ /media/{0}/ ".format(sticks[0], sticks[1])
        do(cmd)

        do("pumount /media/{0}".format(sticks[0]))
        do("pumount /media/{0}".format(sticks[1]))
        do("killall mplayer")

        while True:
            print "waiting for less than two sticks"
            new = checknew()
            if (len(new) < 2):
                break
            time.sleep(1)

        # os.system(cmd)
    else:
        print "nothing to do"

    time.sleep(1)
usbmount options...