-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsettingsfile_update.py
executable file
·65 lines (60 loc) · 2.16 KB
/
settingsfile_update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
import os
import tarfile
import json
import sys
try:
# python 3
from urllib.parse import urlencode
from urllib.request import urlopen, urlretrieve
except ImportError:
# python 2
from urllib import urlencode, urlretrieve
from urllib2 import urlopen # type: ignore
baseUrl = "https://rr123.applanga.com/"
def fix_unicode(data):
if isinstance(data, str):
return data.encode('utf-8')
elif isinstance(data, dict):
data = dict((fix_unicode(k), fix_unicode(data[k])) for k in data)
elif isinstance(data, list):
for i in range(0, len(data)):
data[i] = fix_unicode(data[i])
return data
if len(sys.argv) < 2 :
print("Applanga Automatic Settings File Update: Specify project directory.")
sys.exit(1)
projectPath = sys.argv[1];
#this file has to be in your project root dir
print("-> searching for applanga settingfiles in " + projectPath)
for root, dirs, files in os.walk(projectPath):
for file in files:
if file.endswith(".applanga"):
try:
path = os.path.join(root,file)
print("--> found: '%s' in '%s'" % (file, path[len(os.getcwd())+1: len(path)]))
tar = tarfile.open(path)
alData = json.load(tar.extractfile(tar.getmember("app.applanga")))
appId = alData["_id"];
apiSecret = alData["apiSecret"]
lastVersion = alData["__v"]
if not "groupIds" in alData.keys():
print("Error: Your settingsfile is to old please update manually once before using the auto update script.")
continue
groupIds = alData["groupIds"]
except (Exception):
print("Settingsfile parsing error")
else:
try:
url = "%sv1/projects/%s/updateSettings?apiSecret=%s&lastVersion=%s&%s" % (baseUrl, appId, apiSecret, lastVersion, urlencode({'groupIds[]': fix_unicode(groupIds)}, True))
responseJson = json.loads(urlopen(url).read())
if responseJson["update"] == True:
os.remove(path)
if not os.path.isfile(path):
urlretrieve(responseJson["settings"], path)
print("---> Settingsfile updated!")
else:
print ("---> Settingsfile up-to-date")
tar.close()
except (Exception):
print("Settingsfile update error:", sys.exc_info())