0

Hey brilliant minds out there,

I'm trying to get this Python script that I found working and can't figure out if it's just me or if the original poster didn't get the code right. This script is supposed to enable automatic conflict resolution in Unison by utilising the merge command in the prefs file by taking the two conflicting files and duplicating one of them with a datestamp in the filename. The original posing is here but there wasn't any indentation so I've had to go through and do it manually just by watching the errors that pop up. The error that I can't seem to get around now is

    File "/bin/unison_merge.py", line 5, in <module>
        PATH, CURRENT1, CURRENT2, NEW = sys.argv[1:]
ValueError: need more than 0 values to unpack

I'm hoping that someone out there will be able to help me out.

I've included the entire script below in the hope that it will help and that any other errors will be noticed :).

#!/usr/bin/env python2.7

import sys, os, datetime, os, filecmp

PATH, CURRENT1, CURRENT2, NEW = sys.argv[1:]

# see http://www.cis.upenn.edu/~bcpierce/unison/download/releases/stable/unison-manual.html#merge

promote_remote = False
backup_file_color = "red"

def is_suffix(a, b): return b[-len(a):] == a

def merge(PATH, CURRENT1, CURRENT2, NEW, promote_remote):
# CURRENT1 is copy of local, CURRENT2 is copy of remote

    if filecmp.cmp(CURRENT1, CURRENT2, shallow = False):
# special case -- files have same contents
# not a real conflict. just use local copy, no backup
        print "merge of identical files"
    os.link(CURRENT1, NEW)
    return

# PATH is relative to unison root.
# We need to know absolute path.
# We get it, assuming CURRENT1 is an absolute path
# referring to a file in the same subdirectory as PATH.

assert CURRENT1[0] == '/', "CURRENT1 (%s) is not absolute path" % CURRENT1

PATH_dir, PATH_tail = os.path.split(PATH)
ABS_dir = os.path.dirname(CURRENT1)

assert is_suffix(PATH_dir, ABS_dir), "%s not suffix of %s!" % (PATH_dir, ABS_dir)

ABS_PATH = os.path.join(ABS_dir, PATH_tail)

timestamp = datetime.datetime.now().strftime("%y%m%d_%H%M")
(root, ext) = os.path.splitext(PATH_tail)
for counter in range(100):
    counter = " %d" % counter if counter else ""
    filename = "%s @%s%s%s" % (root, timestamp, counter, ext)
    BACKUP = os.path.join(ABS_dir, filename)
    if not os.path.exists(BACKUP): break
else:
    assert False, "too many existing backups %s" % BACKUP

# promote_remote = False
# seems to retain file props, saving update in next sync?

    print "CONFLICT:", ABS_PATH

if promote_remote:
# resolve conflict by using remote copy, while backing up local to
    BACKUP
    CURRENT1, CURRENT2 = CURRENT2, CURRENT1
    print "CONFLICT remote saved as", filename
else:
    print "CONFLICT local saved as", filename

assert os.path.isfile(CURRENT1)
assert not os.path.exists(NEW)
assert not os.path.exists(BACKUP)

os.link(CURRENT1, BACKUP)
os.link(CURRENT2, NEW)

if backup_file_color and backup_file_color != 'none':
    mac_color_file(BACKUP, backup_file_color)

# note: coloring the tmp file NEW is useless - not propagated
# coloring the current file ABS_PATH causes UNISON to complain

# chmod -w BACKUP
# os.chmod(BACKUP, stat.S_IRUSR)

# just for coloring file in mac Finder
def mac_color_file(file, color):
    if not os.path.exists("/usr/bin/osascript"): return
color_map = {
"none":0,
"orange":1,
"red":2,
"yellow":3,
"blue":4,
"purple":5,
"green":6,
"gray":7,
}
assert color in color_map
assert file[0] == '/', 'absolute path required'
assert os.path.exists(file)
#see http://stackoverflow.com/questions/2435580/tagging-files-with-colors-in-os-x-finder-from-shell-scripts
#osascript -e "tell application \"Finder\" to set label index of alias POSIX
file ("$filename\" to $label")
cmd = '''/usr/bin/osascript -e 'tell application "Finder" to set label index of alias POSIX file "%s" to %d' > /dev/null ''' % (file, color_map[color])
try:
    retcode = subprocess.call(cmd, shell=True)
    if retcode < 0:
        print >>sys.stderr, "mac_color_file child was terminated by signal", retcode
    elif retcode > 0:
        print >>sys.stderr, "mac_color_file child returned", retcode
except OSError, e:
    print >>sys.stderr, "mac_color_file child failed:", e

### main ###

merge(PATH, CURRENT1, CURRENT2, NEW, promote_remote)
smithjw
  • 103
  • 4

3 Answers3

0

The try block doesn't have an accompanying except.

This tutorial page clarifies the use of the try..except structure: http://docs.python.org/tutorial/errors.html#handling-exceptions

ChrisGuest
  • 3,398
  • 4
  • 32
  • 53
  • Whoops, left that bit out. But now the error that I'm seeing is File "/bin/unison_merge.py", line 5, in PATH, CURRENT1, CURRENT2, NEW = sys.argv[1:] ValueError: need more than 0 values to unpack – smithjw Jun 21 '12 at 05:12
  • @smithjw: The program needs 4 command line inputs which gets unpacked to the variables mentioned. – pyfunc Jun 21 '12 at 05:13
0

This is just a half hearted attempt.

The indentation were really off and I am not sure what the program is trying to do.

I hope that there is a single big function to be kicked off.

To run the program from command line, you must provide command line arguments.

python test.py x y c v 

In this case:

PATH, CURRENT1, CURRENT2, NEW  = 'x', 'y', 'c', 'z'

Program

#!/usr/bin/env python2.7

import sys, os, datetime, os, filecmp

print sys.argv
PATH, CURRENT1, CURRENT2, NEW = sys.argv[1:]

# see http://www.cis.upenn.edu/~bcpierce/unison/download/releases/stable/unison-manual.html#merge

promote_remote = False
backup_file_color = "red"

def is_suffix(a, b): 
    return b[-len(a):] == a

def merge(PATH, CURRENT1, CURRENT2, NEW, promote_remote):
    # CURRENT1 is copy of local, CURRENT2 is copy of remote

    if filecmp.cmp(CURRENT1, CURRENT2, shallow = False):
    # special case -- files have same contents
    # not a real conflict. just use local copy, no backup
        print "merge of identical files"
        os.link(CURRENT1, NEW)
        return

    # PATH is relative to unison root.
    # We need to know absolute path.
    # We get it, assuming CURRENT1 is an absolute path
    # referring to a file in the same subdirectory as PATH.

    assert CURRENT1[0] == '/', "CURRENT1 (%s) is not absolute path" % CURRENT1

    PATH_dir, PATH_tail = os.path.split(PATH)
    ABS_dir = os.path.dirname(CURRENT1)

    assert is_suffix(PATH_dir, ABS_dir), "%s not suffix of %s!" % (PATH_dir, ABS_dir)

    ABS_PATH = os.path.join(ABS_dir, PATH_tail)

    timestamp = datetime.datetime.now().strftime("%y%m%d_%H%M")
    (root, ext) = os.path.splitext(PATH_tail)
    for counter in range(100):
        counter = " %d" % counter if counter else ""
        filename = "%s @%s%s%s" % (root, timestamp, counter, ext)
        BACKUP = os.path.join(ABS_dir, filename)
        if not os.path.exists(BACKUP): 
            break
        else:
            assert False, "too many existing backups %s" % BACKUP

    # promote_remote = False
    # seems to retain file props, saving update in next sync?

    print "CONFLICT:", ABS_PATH

    if promote_remote:
        # resolve conflict by using remote copy, while backing up local to BACKUP
        CURRENT1, CURRENT2 = CURRENT2, CURRENT1
        print "CONFLICT remote saved as", filename
    else:
        print "CONFLICT local saved as", filename

    assert os.path.isfile(CURRENT1)
    assert not os.path.exists(NEW)
    assert not os.path.exists(BACKUP)

    os.link(CURRENT1, BACKUP)
    os.link(CURRENT2, NEW)

    if backup_file_color and backup_file_color != 'none':
        mac_color_file(BACKUP, backup_file_color)

    # note: coloring the tmp file NEW is useless - not propagated
    # coloring the current file ABS_PATH causes UNISON to complain

    # chmod -w BACKUP
    # os.chmod(BACKUP, stat.S_IRUSR)

    # just for coloring file in mac Finder
    def mac_color_file(file, color):
        if not os.path.exists("/usr/bin/osascript"): 
        return

    color_map = {
    "none":0,
    "orange":1,
    "red":2,
    "yellow":3,
    "blue":4,
    "purple":5,
    "green":6,
    "gray":7,
    }

    assert color in color_map
    assert file[0] == '/', 'absolute path required'
    assert os.path.exists(file)
    #see http://stackoverflow.com/questions/2435580/tagging-files-with-colors-in-os-x-finder-from-shell-scripts
    #osascript -e "tell application \"Finder\" to set label index of alias POSIX
    file ("$filename\" to $label")
    cmd = '''/usr/bin/osascript -e 'tell application "Finder" to set label index of alias POSIX file "%s" to %d' > /dev/null ''' % (file, color_map[color])

    try:
        retcode = subprocess.call(cmd, shell=True)
        if retcode < 0:
            print >>sys.stderr, "mac_color_file child was terminated by signal", retcode
        elif retcode > 0:
            print >>sys.stderr, "mac_color_file child returned", retcode
    except:
        pass

if __file__ == '__main__':
    merge(PATH, CURRENT1, CURRENT2, NEW, promote_remote)
pyfunc
  • 65,343
  • 15
  • 148
  • 136
  • Thanks for the attempt, the indentation looks much better now but I'm still getting the `ValueError: too many values to unpack` referring to line 5 – smithjw Jun 21 '12 at 05:21
  • @smithjw: As I mentioned you need to provide command line arguments. Run the program as following: python test.py x y c v – pyfunc Jun 21 '12 at 05:24
  • I'm running it through Unison which should be passing along `PATH CURRENT1 CURRENT2 NEW` to the script to rename and duplicate the files. – smithjw Jun 21 '12 at 05:29
  • 1
    @smithjw: just add print sys.argv before the first line and see what it provides. I added it in my answer. This will tell you what it is passing – pyfunc Jun 21 '12 at 05:30
  • At first I was still getting the `ValueError`s but I found that Unison was passing `PATH` as multiple values as the filename had spaces in it. Once I corrected that, the script would execute but the Files weren't merged/duplicated because the program didn't chance any of the temp files. I suspect that there is something more wrong with the script that is causing it not to rename the temp files. Hopefully the person who wrote the script will getback to me but I'm not holding my breath as it was posted over a year ago. – smithjw Jun 21 '12 at 05:50
  • @smithjw: I tried my best to get it corrected. I do not have much understanding of what the program does. But it is best to start from where the values are being passed and then add line by line the code and see what it does and what it should do. Keep correcting it that way and you will get to the correct or improved solution. – pyfunc Jun 21 '12 at 05:52
0

Here's the current script. Note that it expects to be invoked with four arguments as described in the unison documentation. To trigger it, add a line like the following to your unison .prf file:

merge = Name * -> /Users/neal/Bin/daemons/unison_merge.py 'PATH' CURRENT1 CURRENT2 NEW

The single quotes are (were?) necessary to work around a bug in unison.

Here's the script:

#!/usr/bin/env python2.7

# see http://www.cis.upenn.edu/~bcpierce/unison/download/releases/stable/unison-manual.html#merge

import sys, os, datetime, os, stat, subprocess, filecmp

# log = open("/Users/neal/unison_merge.log", "a")
# sys.stdout = log
# sys.stderr = log

# work around bug in unison adds quotes incorrectly
# something to do with blanks in filenames

def clean(x):
    if len(x) < 3: return x
    if x[0] == "'": x = x[1:]
    if x[-1] == "'": x = x[:-1]
    return x

sys.argv = [clean(x) for x in sys.argv]

try:
    PATH, CURRENT1, CURRENT2, NEW = sys.argv[1:]
except:
    print "usage: ", " ".join("[%s]" % x for x in sys.argv)
    raise

backup_file_color = 'red'  # for mac
promote_remote = False

def is_suffix(a, b): return b[-len(a):] == a

def mac_color_file(file, color):
    if not os.path.exists("/usr/bin/osascript"): return
    color_map = {
        "none":0,
        "orange":1,
        "red":2,
        "yellow":3,
        "blue":4,
        "purple":5,
        "green":6,
        "gray":7,
        }
    assert color in color_map
    assert file[0] == '/', 'absolute path required'
    assert os.path.exists(file)
    #see http://stackoverflow.com/questions/2435580/tagging-files-with-colors-in-os-x-finder-from-shell-scripts
    #osascript -e "tell application \"Finder\" to set label index of alias POSIX file \"$filename\" to $label"
    cmd = '''/usr/bin/osascript -e 'tell application "Finder" to set label index of alias POSIX file "%s" to %d' > /dev/null ''' % (file, color_map[color])
    try:
        retcode = subprocess.call(cmd, shell=True)
        if retcode < 0:
            print >>sys.stderr, "mac_color_file child was terminated by signal", -retcode
        elif retcode > 0:
            print >>sys.stderr, "mac_color_file child returned", retcode
    except OSError, e:
        print >>sys.stderr, "mac_color_file child failed:", e

def merge(PATH, CURRENT1, CURRENT2, NEW, promote_remote):
    # CURRENT1 is copy of local, CURRENT2 is copy of remote

    # PATH is relative to unison root.
    # We need to know absolute path.
    # We get it, assuming CURRENT1 is an absolute path
    # referring to a file in the same subdirectory as PATH.

    if filecmp.cmp(CURRENT1, CURRENT2, shallow = False):
        # special case -- files have same contents
        # not a real conflict.  just use local copy, no backup
        print "merge of identical files"
        os.link(CURRENT1, NEW)
        return

    assert CURRENT1[0] == '/', "CURRENT1 (%s) is not absolute path" % CURRENT1

    PATH_dir, PATH_tail = os.path.split(PATH)
    ABS_dir = os.path.dirname(CURRENT1)

    assert is_suffix(PATH_dir, ABS_dir), "%s not suffix of %s!" % (PATH_dir, ABS_dir)

    ABS_PATH = os.path.join(ABS_dir, PATH_tail)

    timestamp = datetime.datetime.now().strftime("%y%m%d_%H%M")
    (root, ext) = os.path.splitext(PATH_tail)
    for counter in range(100):
        counter = " %d" % counter if counter else ""
        filename = "%s @%s%s%s" % (root, timestamp, counter, ext)
        BACKUP = os.path.join(ABS_dir, filename)
        if not os.path.exists(BACKUP): break
    else:
        assert False, "too many existing backups %s" % BACKUP

    # promote_remote = False
    # seems to retain file props, saving update in next sync?

    print "CONFLICT:", ABS_PATH

    if promote_remote:
        # resolve conflict by using remote copy, while backing up local to BACKUP
        CURRENT1, CURRENT2 = CURRENT2, CURRENT1
        print "CONFLICT remote saved as", filename
    else:
        print "CONFLICT local saved as", filename

    assert os.path.isfile(CURRENT1)
    assert not os.path.exists(NEW)
    assert not os.path.exists(BACKUP)

    os.link(CURRENT1, BACKUP)
    os.link(CURRENT2, NEW)

    if backup_file_color and backup_file_color != 'none':
        mac_color_file(BACKUP, backup_file_color)

    # note: coloring the tmp file NEW is useless - not propagated
    # coloring the current file ABS_PATH causes UNISON to complain

    # chmod -w BACKUP
    # os.chmod(BACKUP, stat.S_IRUSR)

### main ###

try:
    merge(PATH, CURRENT1, CURRENT2, NEW, promote_remote)
except Exception as e:
    print >>sys.stderr, "ERROR in unison_merge.py"
    print >>sys.stderr, "ERROR:", str(e)
Neal Young
  • 159
  • 6