PDA

View Full Version : how to share a small stand-alone non-interactive python script?




bvmou
Apr 4, 2008, 04:26 AM
I wrote a small no-input python program to sync recent iTunes podcasts to a BlackBerry; it works great from a terminal but I would like to share it with my old man and whoever. The only output is dialogue as files copy. Is there a way to make a desktop or dock shortcut to something that runs in a shell? No suggestion is too obvious, I am just back to the Mac after ages away. Script posted below, any help very much appreciated.

#!/usr/bin/python

import os, getpass, shutil

def getOSSpecificFilepath():
if os.path.exists('/boot/grub'):
return '/home/'+getpass.getuser()+'/podcasts/current/'
elif os.path.exists('/Users'):
return '/Users/'+getpass.getuser()+'/Music/iTunes/iTunes Music/Podcasts/'
elif os.path.exists(r'C:\\'):
return 'C:\\Users\\'+getpass.getuser()+'\\music\\itunes\\itunes music\\podcasts\\'
else:
print 'problem getting os specific filepath'

def getDevicepath():
if os.path.exists('/media/BB'):
return '/media/BB/BlackBerry/music/podcasts/'
elif os.name is 'posix':
return '/Volumes/BB/BlackBerry/music/Podcasts/'
elif os.name is 'nt':
return r'E:\\BlackBerry\\music\\Podcasts\\'
else:
print 'problem getting os device filepath'

def checkDevicepath():
if not os.path.exists(getDevicepath()):
os.mkdir(getDevicepath())

def allFiles(filepath, depth, flist=[]):
fpath=os.walk(filepath)
fpath=[item for item in fpath]
while depth < len(fpath):
for item in fpath[depth][-1]:
flist.append(fpath[depth][0]+os.sep+item)
depth+=1
return flist

def getFilelist():
a = getOSSpecificFilepath()
b = allFiles(a, 1)
return b

def getDevicelist():
a = getDevicepath()
b = os.walk(a)
c = [item for item in b]
return c[0][-1]

def getMtimeList(filelist):
tmplist = []
for item in filelist:
tmplist.append(os.stat(item).st_mtime)
return tmplist

def getMtimeDict(filelist):
tmpdict = {}
for item in filelist:
tmpdict[os.stat(item).st_mtime] = item
return tmpdict

def getUpdatedList(filelist, mtimelist, mtimedict):
tmplist = []
a = sorted(mtimelist)
for item in a:
tmplist.append(mtimedict[item])
return tmplist

def defaultCast():
a = getFilelist()
b = getMtimeList(a)
c = getMtimeDict(a)
d = getUpdatedList(a, b, c)
return d

def thoseThatFit(fulllist=defaultCast()):
counter = -1
tmplist = []
tmpnum = 0
targetsize = 1000000000
while tmpnum < targetsize:
tmplist.append(fulllist[counter])
tmpnum += os.stat(fulllist[counter]).st_size
if abs(counter) < len(fulllist):
counter -= 1
else:
return tmplist
break
return tmplist


def cpFunction():
dp = getDevicepath()
a = thoseThatFit()
b = getDevicelist()
c = [item.split(os.sep) for item in a]
c = [item[-1] for item in c]
counter=0
for item in b:
if item not in c:
try:
os.remove(dp+item)
print 'removed', item
except:
continue
while counter <= (len(a)-1):
if c[counter] in b:
counter+=1
elif 'm4v' in a[counter]:
counter+=1
else:
print 'adding', a[counter], '...'
shutil.copy2(a[counter], dp)
counter += 1

checkDevicepath()
cpFunction()



kainjow
Apr 4, 2008, 08:31 AM
You can give it a .command extension. This will then make it run in Terminal when double-clicked. However you need to give it the correct permissions to execute.

hhas
Apr 4, 2008, 12:34 PM
There are also various third-party tools for converting Python (and other) scripts into double-clickable .app bundles, e.g. Platypus (http://www.sveinbjorn.org/platypus) might be a good choice here, although there are other options such as py2app available depending on your needs.

bvmou
Apr 4, 2008, 01:56 PM
Thanks guys, the .command extension was the missing bit for me (that should say it all), I am about to have a coffee and try the platypus thing as well.

As far as permissions on the user's side, will they have to chmod +x it, and could I put that in some sort of installer script?

bvmou
Apr 4, 2008, 02:27 PM
Platypus (http://www.sveinbjorn.org/platypus) might be a good choice here, although there are other options such as py2app available depending on your needs.

Platypus is unbelievable, it is absolutely perfect for this use. I just got something going 1/3 through the cup of coffee. Thanks so much to both