| 1 | #!/usr/bin/python |
|---|
| 2 | |
|---|
| 3 | import adodb, os, sys, getopt |
|---|
| 4 | |
|---|
| 5 | alterncapps = "/var/alternc/apps" |
|---|
| 6 | host = "localhost" |
|---|
| 7 | database = "alternc" |
|---|
| 8 | username = "alternc" |
|---|
| 9 | password = "nJWuYCgj0K" |
|---|
| 10 | |
|---|
| 11 | def usage(): |
|---|
| 12 | print "update_apps.py usage:" |
|---|
| 13 | print " -c --cron cron mode" |
|---|
| 14 | print " -u [appname] --update=[appname] update installation of appname" |
|---|
| 15 | |
|---|
| 16 | def 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 | |
|---|
| 30 | def 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 | |
|---|
| 36 | def 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 | |
|---|
| 42 | def 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 | |
|---|
| 57 | def 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 | |
|---|
| 67 | def 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 | |
|---|
| 80 | def 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 | |
|---|
| 91 | try: |
|---|
| 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 | |
|---|
| 102 | except getopt.GetoptError, err: |
|---|
| 103 | print str(err) |
|---|
| 104 | usage() |
|---|
| 105 | sys.exit(2) |
|---|
| 106 | |
|---|
| 107 | |
|---|