2018-09-18 17:23:56 +02:00
|
|
|
# build.py
|
|
|
|
# pre-build script, setting up build environment
|
|
|
|
|
2018-09-19 14:20:52 +02:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import os.path
|
2018-09-18 17:23:56 +02:00
|
|
|
import requests
|
|
|
|
from os.path import basename
|
|
|
|
from platformio import util
|
|
|
|
|
|
|
|
Import("env")
|
|
|
|
|
2018-09-19 14:20:52 +02:00
|
|
|
# get platformio environment variables
|
2018-09-18 17:23:56 +02:00
|
|
|
project_config = util.load_project_config()
|
2018-09-19 14:20:52 +02:00
|
|
|
|
2019-05-26 17:46:33 +02:00
|
|
|
# get platformio source path
|
2019-05-29 20:13:39 +02:00
|
|
|
srcdir = env.get("PROJECTSRC_DIR").replace("\\", "/")
|
2019-05-26 17:46:33 +02:00
|
|
|
|
|
|
|
# check if lmic config file is present in source directory
|
2019-05-29 20:13:39 +02:00
|
|
|
lmicconfig = project_config.get("common", "lmicconfigfile")
|
|
|
|
lmicconfigfile = os.path.join (srcdir, lmicconfig)
|
2019-05-26 17:46:33 +02:00
|
|
|
if os.path.isfile(lmicconfigfile) and os.access(lmicconfigfile, os.R_OK):
|
|
|
|
print "Parsing LMIC configuration from " + lmicconfigfile
|
|
|
|
else:
|
|
|
|
sys.exit("Missing file " + lmicconfigfile + ", please create it! Aborting.")
|
|
|
|
|
|
|
|
# check if lora key file is present in source directory
|
2019-05-29 18:49:01 +02:00
|
|
|
lorakeyfile = os.path.join (srcdir, project_config.get("common", "lorakeyfile"))
|
2019-05-26 17:46:33 +02:00
|
|
|
if os.path.isfile(lorakeyfile) and os.access(lorakeyfile, os.R_OK):
|
|
|
|
print "Parsing LORAWAN keys from " + lorakeyfile
|
2018-09-19 14:20:52 +02:00
|
|
|
else:
|
2019-05-26 17:46:33 +02:00
|
|
|
sys.exit("Missing file " + lorakeyfile + ", please create it! Aborting.")
|
2018-09-19 14:20:52 +02:00
|
|
|
|
2019-05-26 17:46:33 +02:00
|
|
|
# check if ota key file is present in source directory
|
2019-05-29 18:49:01 +02:00
|
|
|
otakeyfile = os.path.join (srcdir, project_config.get("common", "otakeyfile"))
|
2019-05-26 17:46:33 +02:00
|
|
|
if os.path.isfile(otakeyfile) and os.access(otakeyfile, os.R_OK):
|
|
|
|
print "Parsing OTA keys from " + otakeyfile
|
2018-09-19 14:20:52 +02:00
|
|
|
else:
|
2019-05-26 17:46:33 +02:00
|
|
|
sys.exit("Missing file " + otakeyfile + ", please create it! Aborting.")
|
2018-09-19 14:20:52 +02:00
|
|
|
|
2019-05-26 17:46:33 +02:00
|
|
|
# parse ota key file
|
2018-09-18 17:23:56 +02:00
|
|
|
mykeys = {}
|
2019-05-26 17:46:33 +02:00
|
|
|
with open(otakeyfile) as myfile:
|
2018-09-18 17:23:56 +02:00
|
|
|
for line in myfile:
|
|
|
|
key, value = line.partition("=")[::2]
|
|
|
|
mykeys[key.strip()] = str(value).strip()
|
|
|
|
|
2019-05-26 17:46:33 +02:00
|
|
|
# get bintray user credentials from ota key file
|
2018-09-18 17:23:56 +02:00
|
|
|
user = mykeys["BINTRAY_USER"]
|
|
|
|
repository = mykeys["BINTRAY_REPO"]
|
|
|
|
apitoken = mykeys["BINTRAY_API_TOKEN"]
|
|
|
|
|
2019-05-26 17:46:33 +02:00
|
|
|
# get bintray upload parameters from platformio environment
|
2018-09-18 17:23:56 +02:00
|
|
|
version = project_config.get("common", "release_version")
|
|
|
|
package = str(env.get("PIOENV"))
|
|
|
|
|
2018-09-19 14:20:52 +02:00
|
|
|
# put bintray user credentials to platformio environment
|
2018-09-18 17:23:56 +02:00
|
|
|
env.Replace(BINTRAY_USER=user)
|
|
|
|
env.Replace(BINTRAY_REPO=repository)
|
|
|
|
env.Replace(BINTRAY_API_TOKEN=apitoken)
|
|
|
|
|
2018-09-19 14:20:52 +02:00
|
|
|
# get runtime credentials and put them to compiler directive
|
2019-05-29 20:13:39 +02:00
|
|
|
env.Append(BUILD_FLAGS=[
|
|
|
|
u'-DWIFI_SSID=\\"' + mykeys["OTA_WIFI_SSID"] + '\\"',
|
|
|
|
u'-DWIFI_PASS=\\"' + mykeys["OTA_WIFI_PASS"] + '\\"',
|
|
|
|
u'-DBINTRAY_USER=\\"' + mykeys["BINTRAY_USER"] + '\\"',
|
|
|
|
u'-DBINTRAY_REPO=\\"' + mykeys["BINTRAY_REPO"] + '\\"',
|
|
|
|
u'-DBINTRAY_PACKAGE=\\"$PIOENV\\"',
|
|
|
|
u'-DARDUINO_LMIC_PROJECT_CONFIG_H=' + lmicconfig,
|
|
|
|
u'-I \"' + srcdir + '\"'
|
2018-09-18 17:23:56 +02:00
|
|
|
])
|
|
|
|
|
|
|
|
# function for pushing new firmware to bintray storage using API
|
|
|
|
def publish_bintray(source, target, env):
|
|
|
|
firmware_path = str(source[0])
|
|
|
|
firmware_name = basename(firmware_path)
|
|
|
|
url = "/".join([
|
|
|
|
"https://api.bintray.com", "content",
|
|
|
|
user, repository, package, version, firmware_name
|
|
|
|
])
|
|
|
|
|
|
|
|
print("Uploading {0} to Bintray. Version: {1}".format(
|
|
|
|
firmware_name, version))
|
|
|
|
print(url)
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
"Content-type": "application/octet-stream",
|
|
|
|
"X-Bintray-Publish": "1",
|
|
|
|
"X-Bintray-Override": "1"
|
|
|
|
}
|
|
|
|
|
|
|
|
r = requests.put(
|
|
|
|
url,
|
|
|
|
data=open(firmware_path, "rb"),
|
|
|
|
headers=headers,
|
|
|
|
auth=(user, apitoken))
|
|
|
|
|
|
|
|
if r.status_code != 201:
|
|
|
|
print("Failed to submit package: {0}\n{1}".format(
|
|
|
|
r.status_code, r.text))
|
|
|
|
else:
|
|
|
|
print("The firmware has been successfuly published at Bintray.com!")
|
|
|
|
|
|
|
|
# put build file name and upload command to platformio environment
|
|
|
|
env.Replace(
|
|
|
|
PROGNAME="firmware_" + package + "_v%s" % version,
|
2018-09-24 16:36:11 +02:00
|
|
|
UPLOADCMD=publish_bintray)
|