forked from sYmphoman/minimo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextra_script.py
More file actions
93 lines (77 loc) · 3.61 KB
/
Copy pathextra_script.py
File metadata and controls
93 lines (77 loc) · 3.61 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import requests, sys, json, os
from os.path import basename
from shutil import copyfile
import re
import time, datetime
Import("env")
try:
import configparser
except ImportError:
import ConfigParser as configparser
project_config = configparser.ConfigParser()
project_config.read("platformio.ini")
version = project_config.get("ota", "JsonVersion")
host = project_config.get("ota", "JsonHost")
port = project_config.get("ota", "JsonPort")
bin = project_config.get("ota", "JsonBin")
def myconverter(o):
if isinstance(o, datetime.datetime):
return o.__str__() # return "{}-{}-{}".format(o.year, o.month, o.day)
def after_build(source, target, env):
print("----------------------------------------------------------------------------------------------------------")
firmware_path = str(source[0])
envName = env["PIOENV"]
dstMainPath = env["PROJECT_DIR"] + "\\ota_updates\\" + envName + "\\"
dstFilename = "firmware_v" + version + ".bin"
finalDstFullPath = dstMainPath + dstFilename
if False: #os.path.isfile(finalDstFullPath):
print ("This firmware version already exists ... increase the firmware_version or delete the files if you want to generate a new version.")
else:
print("Copying bin file...")
print(finalDstFullPath)
os.makedirs(os.path.dirname(dstMainPath), exist_ok=True)
copyfile(firmware_path , finalDstFullPath)
modified = os.path.getmtime(finalDstFullPath)
dateFile = datetime.datetime.fromtimestamp(modified)
# Generating JSON version file
print("Generating JSON version file...")
payload = {}
payload['type'] = envName
payload['version'] = version
payload['host'] = host
payload['port'] = port
payload['bin'] = bin + envName + "/" + dstFilename
try:
with open(dstMainPath + "firmware_v" + version + ".json", 'w') as outfile:
json.dump(payload, outfile, default = myconverter)
except:
print("[ERROR] Error when generating the JSON version file...")
fullPayload = {}
fullPayload['versions'] = []
# r=root, d=directories, f = files
for r, d, f in os.walk(dstMainPath):
for file in f:
if file.endswith(".bin"):
filePath = os.path.join(r, file)
print(filePath)
fileName = os.path.basename(filePath)
#modificationDate = os.path.getmtime(filePath)
# remove file extension
versionNumber = os.path.splitext(fileName)[0]
# split with 'v"
versionNumber = re.split('v', versionNumber)[1]
versionElement = {}
versionElement['version'] = versionNumber
#modificationDateStr = datetime.datetime.fromtimestamp(modificationDate)
#year,month,day,hour,minute,second=time.localtime(modificationDate)[:-3]
versionElement['date'] = os.stat(filePath)[-2]
fullPayload['versions'].append(versionElement)
try:
with open(dstMainPath + "firmware.json", 'w') as outfile:
json.dump(fullPayload, outfile, default = myconverter)
except:
print("[ERROR] Error when generating the JSON version file...")
print("----------------------------------------------------------------------------------------------------------")
env.AddPostAction("buildprog", after_build)
env.AddPreAction("upload", after_build)
#env.Replace(PROGNAME="firmware_v%s" % version)