1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

how to share a small stand-alone non-interactive python script?

Discussion in 'Mac Programming' started by bvmou, Apr 4, 2008.

  1. macrumors newbie

    #1
    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.

    PHP:
    #!/usr/bin/python

    import osgetpassshutil

    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(filepathdepthflist=[]):
        
    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
    ():
        
    getOSSpecificFilepath()
        
    allFiles(a1)
        return 
    b

    def getDevicelist
    ():
        
    getDevicepath()
        
    os.walk(a)
        
    = [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
    (filelistmtimelistmtimedict):
        
    tmplist = []
        
    sorted(mtimelist)
        for 
    item in a:
            
    tmplist.append(mtimedict[item])
        return 
    tmplist

    def defaultCast
    ():
        
    getFilelist()
        
    getMtimeList(a)
        
    getMtimeDict(a)
        
    getUpdatedList(abc)
        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()
        
    thoseThatFit()
        
    getDevicelist()
        
    = [item.split(os.sep) for item in a]
        
    = [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[counterin b:
                
    counter+=1
            elif 
    'm4v' in a[counter]:
                
    counter+=1
            
    else:
                print 
    'adding'a[counter], '...'
                
    shutil.copy2(a[counter], dp)
                
    counter += 1

    checkDevicepath
    ()
    cpFunction()
     
  2. Moderator emeritus

    kainjow

    #2
    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.
     
  3. macrumors regular

    #3
    There are also various third-party tools for converting Python (and other) scripts into double-clickable .app bundles, e.g. Platypus might be a good choice here, although there are other options such as py2app available depending on your needs.
     
  4. macrumors newbie

    #4
    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?
     
  5. macrumors newbie

    #5
    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
     

Share This Page