source: alternc-apps/trunk/bin/update_apps.py @ 2466

Revision 2466, 3.2 KB checked in by nahuel, 4 years ago (diff)

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

Line 
1#!/usr/bin/python
2
3import adodb, os, sys, getopt
4
5alterncapps  = "/var/alternc/apps"
6host         = "localhost"
7database     = "alternc"
8username     = "alternc"
9password     = "nJWuYCgj0K"
10
11def usage():
12    print "update_apps.py usage:"
13    print "  -c --cron cron mode"
14    print "  -u [appname] --update=[appname] update installation of appname"
15
16def generateFileList(path):
17    directories = []
18    fileslist   = []
19    for root, dirs, files in os.walk(path):
20        root = root.replace(path , "")
21
22        if root != '':
23            directories.append(root)
24
25        for file in files:
26            fileslist.append(root + "/" + file )
27
28    return {'files': fileslist, 'dirs': directories}
29
30def createDirIfNotExist(dir):
31    if not os.path.exists(dir):
32        os.mkdir(dir)
33        os.chmod(dir,0770)
34        os.chown(dir,33,-1)
35
36def installFile(fromfile, tofile):
37    if not os.path.exists(tofile):
38        fromfile = os.path.normpath(fromfile)
39        tofile   = os.path.normpath(tofile)
40        os.symlink(fromfile, tofile)
41
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
57def installApplication(application, path):
58    fromfiles = generateFileList(alterncapps + "/" + application)
59    tofiles = generateFileList(path)
60    print tofiles
61    for dir in files['dirs']:
62        createDirIfNotExist(path + dir)
63
64    for file in files['files']:
65        installFile(alterncapps + "/" + application + "/" + file, path + "/" + file)
66
67def updateApplication(appname):
68    print "Upgrading : "  + appname
69
70    conn = adodb.NewADOConnection('mysql')
71    conn.Connect(host, username, password,database);
72
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 TracBrowser for help on using the repository browser.