Changeset 2466


Ignore:
Timestamp:
02/15/09 22:25:57 (4 years ago)
Author:
nahuel
Message:

Manage shell options:

-c, --cron for cron mode
-u [appname], --update=[appname] to make file upgrade of an application in all installs

Permit files upgrade in applications

File:
1 edited

Legend:

Unmodified
Added
Removed
  • alternc-apps/trunk/bin/update_apps.py

    r2463 r2466  
    11#!/usr/bin/python 
    22 
    3 import adodb, os 
     3import adodb, os, sys, getopt 
    44 
    55alterncapps  = "/var/alternc/apps" 
     
    77database     = "alternc" 
    88username     = "alternc" 
    9 password     = "" 
     9password     = "nJWuYCgj0K" 
    1010 
     11def usage(): 
     12    print "update_apps.py usage:" 
     13    print "  -c --cron cron mode" 
     14    print "  -u [appname] --update=[appname] update installation of appname" 
    1115 
    12 def generateFileList(application): 
     16def generateFileList(path): 
    1317    directories = [] 
    1418    fileslist   = [] 
    15     for root, dirs, files in os.walk(alterncapps + "/" + application): 
    16         root = root.replace(alterncapps + "/" + application , "") 
     19    for root, dirs, files in os.walk(path): 
     20        root = root.replace(path , "") 
    1721 
    1822        if root != '': 
     
    3640        os.symlink(fromfile, tofile) 
    3741 
     42def updateFiles(fromfiles,tofiles,frompath,path): 
     43    # we recreate the directories 
     44    for dir in fromfiles['dirs']: 
     45        if dir not in tofiles['dirs']: 
     46            createDirIfNotExist(os.path.normpath(path + "/" + dir)) 
     47 
     48    for file in fromfiles['files']: 
     49        if file not in tofiles['files']: 
     50            installFile(os.path.normpath(frompath + "/" + file),os.path.normpath(path + "/" + file)) 
     51 
     52    for file in tofiles['files']: 
     53        filepath = os.path.normpath(path + "/" + file) 
     54        if not os.path.exists(filepath): 
     55            os.unlink(filepath) 
     56 
    3857def installApplication(application, path): 
    39     files = generateFileList(application) 
     58    fromfiles = generateFileList(alterncapps + "/" + application) 
     59    tofiles = generateFileList(path) 
     60    print tofiles 
    4061    for dir in files['dirs']: 
    4162        createDirIfNotExist(path + dir) 
     
    4465        installFile(alterncapps + "/" + application + "/" + file, path + "/" + file) 
    4566 
    46 conn = adodb.NewADOConnection('mysql') 
    47 conn.Connect(host, username, password,database); 
     67def updateApplication(appname): 
     68    print "Upgrading : "  + appname 
    4869 
    49 for row in conn.Execute('SELECT uid, application, path FROM applications WHERE installed = 0'): 
    50     uid         = row[0] 
    51     application = row[1] 
    52     path        = row[2] 
    53     installApplication(application,path) 
    54     conn.Execute('UPDATE applications SET installed = 1 WHERE uid = %s AND application = %s AND path = %s',(uid,application,path)) 
     70    conn = adodb.NewADOConnection('mysql') 
     71    conn.Connect(host, username, password,database); 
    5572 
     73    files   = generateFileList(alterncapps + "/" + appname) 
     74  
     75    for row in conn.Execute('SELECT uid, application, path FROM applications WHERE application = %s AND installed = 1',(appname)): 
     76        tofiles = generateFileList(row[2]) 
     77        updateFiles(files, tofiles, os.path.normpath(alterncapps + "/" + appname), row[2]) 
     78 
     79 
     80def runCron(): 
     81    conn = adodb.NewADOConnection('mysql') 
     82    conn.Connect(host, username, password,database); 
     83 
     84    for row in conn.Execute('SELECT uid, application, path FROM applications WHERE installed = 0'): 
     85        uid         = row[0] 
     86        application = row[1] 
     87        path        = row[2] 
     88        installApplication(application,path) 
     89        conn.Execute('UPDATE applications SET installed = 1 WHERE uid = %s AND application = %s AND path = %s',(uid,application,path)) 
     90 
     91try: 
     92    optlist, args = getopt.getopt(sys.argv[1:],'cu:', ["cron","update="]) 
     93    for option, arg in optlist: 
     94        if option in ("--cron","-c"): 
     95            runCron() 
     96        elif option in ("--update","-u"): 
     97            updateApplication(arg) 
     98        else: 
     99            usage() 
     100            exit(2) 
     101 
     102except getopt.GetoptError, err: 
     103    print str(err) 
     104    usage() 
     105    sys.exit(2) 
     106 
     107 
Note: See TracChangeset for help on using the changeset viewer.