2018-09-18 17:23:56 +02:00
|
|
|
# build.py
|
2019-06-30 17:48:24 +02:00
|
|
|
# pre-build script, setting up build environment and fetch hal file for user's board
|
2018-09-18 17:23:56 +02:00
|
|
|
|
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
|
2019-09-18 16:27:09 +02:00
|
|
|
from SCons.Script import DefaultEnvironment
|
2018-09-18 17:23:56 +02:00
|
|
|
|
2019-06-23 15:50:39 +02:00
|
|
|
try:
|
|
|
|
import configparser
|
|
|
|
except ImportError:
|
|
|
|
import ConfigParser as configparser
|
|
|
|
|
2018-09-19 14:20:52 +02:00
|
|
|
# get platformio environment variables
|
2019-09-18 16:27:09 +02:00
|
|
|
env = DefaultEnvironment()
|
2019-06-23 15:50:39 +02:00
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read("platformio.ini")
|
2018-09-19 14:20:52 +02:00
|
|
|
|
2019-05-26 17:46:33 +02:00
|
|
|
# get platformio source path
|
2021-12-21 17:56:59 +01:00
|
|
|
srcdir = env.get("PROJECT_SRC_DIR")
|
2019-05-26 17:46:33 +02:00
|
|
|
|
2019-06-29 15:03:55 +02:00
|
|
|
# get hal path
|
|
|
|
haldir = os.path.join (srcdir, "hal")
|
|
|
|
|
|
|
|
# check if hal file is present in source directory
|
|
|
|
halconfig = config.get("board", "halfile")
|
|
|
|
halconfigfile = os.path.join (haldir, halconfig)
|
|
|
|
if os.path.isfile(halconfigfile) and os.access(halconfigfile, os.R_OK):
|
2019-07-23 12:06:37 +02:00
|
|
|
print("Parsing hardware configuration from " + halconfigfile)
|
2019-06-29 15:03:55 +02:00
|
|
|
else:
|
|
|
|
sys.exit("Missing file " + halconfigfile + ", please create it! Aborting.")
|
|
|
|
|
2019-05-26 17:46:33 +02:00
|
|
|
# check if lmic config file is present in source directory
|
2019-06-23 15:50:39 +02:00
|
|
|
lmicconfig = config.get("common", "lmicconfigfile")
|
2019-05-29 20:13:39 +02:00
|
|
|
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):
|
2019-07-23 12:06:37 +02:00
|
|
|
print("Parsing LMIC configuration from " + lmicconfigfile)
|
2019-05-26 17:46:33 +02:00
|
|
|
else:
|
|
|
|
sys.exit("Missing file " + lmicconfigfile + ", please create it! Aborting.")
|
|
|
|
|
|
|
|
# check if lora key file is present in source directory
|
2019-06-23 15:50:39 +02:00
|
|
|
lorakeyfile = os.path.join (srcdir, config.get("common", "lorakeyfile"))
|
2019-05-26 17:46:33 +02:00
|
|
|
if os.path.isfile(lorakeyfile) and os.access(lorakeyfile, os.R_OK):
|
2019-07-23 12:06:37 +02:00
|
|
|
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-06-23 15:50:39 +02:00
|
|
|
otakeyfile = os.path.join (srcdir, config.get("common", "otakeyfile"))
|
2019-05-26 17:46:33 +02:00
|
|
|
if os.path.isfile(otakeyfile) and os.access(otakeyfile, os.R_OK):
|
2019-07-23 12:06:37 +02:00
|
|
|
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-06-30 17:48:24 +02:00
|
|
|
# parse hal file
|
2018-09-18 17:23:56 +02:00
|
|
|
mykeys = {}
|
2019-06-30 17:48:24 +02:00
|
|
|
with open(halconfigfile) as myfile:
|
|
|
|
for line in myfile:
|
|
|
|
line2 = line.strip("// ")
|
|
|
|
key, value = line2.partition(" ")[::2]
|
|
|
|
mykeys[key.strip()] = str(value).strip()
|
|
|
|
myboard = mykeys["board"]
|
|
|
|
myuploadspeed = mykeys["upload_speed"]
|
|
|
|
env.Replace(BOARD=myboard)
|
|
|
|
env.Replace(UPLOAD_SPEED=myuploadspeed)
|
2020-05-09 22:49:00 +02:00
|
|
|
print('\033[94m' + "Target board: " + myboard + " @ " + myuploadspeed + "bps" + '\033[0m')
|
2019-09-18 16:27:09 +02:00
|
|
|
|
|
|
|
# re-set partition table
|
|
|
|
mypartitiontable = config.get("env", "board_build.partitions")
|
|
|
|
board = env.BoardConfig(myboard)
|
|
|
|
board.manifest['build']['partitions'] = mypartitiontable
|
|
|
|
print('\033[94m' + "Partition table: " + mypartitiontable + '\033[0m')
|
2019-06-30 17:48:24 +02:00
|
|
|
|
2020-05-09 22:49:00 +02:00
|
|
|
# set display library
|
|
|
|
if "display_library" in mykeys:
|
|
|
|
mydisplay = mykeys["display_library"]
|
|
|
|
env.Append(display_library=mydisplay)
|
|
|
|
print('\033[94m' + "Display library: " + mydisplay + '\033[0m')
|
|
|
|
|
2019-06-30 17:48:24 +02:00
|
|
|
# parse ota key file
|
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()
|
|
|
|
|
2021-03-22 23:10:07 +01:00
|
|
|
# usage of paxexpress: see https://github.com/paxexpress/docs
|
2020-08-16 13:46:37 +02:00
|
|
|
|
2021-03-22 23:10:07 +01:00
|
|
|
# get paxexpress credentials from ota key file
|
|
|
|
user = mykeys["PAXEXPRESS_USER"]
|
|
|
|
repository = mykeys["PAXEXPRESS_REPO"]
|
|
|
|
apitoken = mykeys["PAXEXPRESS_API_TOKEN"]
|
2018-09-18 17:23:56 +02:00
|
|
|
|
2021-03-22 23:10:07 +01:00
|
|
|
# get paxexpress upload parameters from platformio environment
|
2019-06-23 15:50:39 +02:00
|
|
|
version = config.get("common", "release_version")
|
2019-06-29 15:03:55 +02:00
|
|
|
package, dummy = halconfig.partition(".")[::2]
|
2018-09-18 17:23:56 +02:00
|
|
|
|
2021-03-22 23:10:07 +01:00
|
|
|
# put paxexpress user credentials to platformio environment
|
|
|
|
env.Replace(PAXEXPRESS_USER=user)
|
|
|
|
env.Replace(PAXEXPRESS_REPO=repository)
|
|
|
|
env.Replace(PAXEXPRESS_API_TOKEN=apitoken)
|
2018-09-18 17:23:56 +02:00
|
|
|
|
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"] + '\\"',
|
2021-03-22 23:10:07 +01:00
|
|
|
u'-DPAXEXPRESS_USER=\\"' + mykeys["PAXEXPRESS_USER"] + '\\"',
|
|
|
|
u'-DPAXEXPRESS_REPO=\\"' + mykeys["PAXEXPRESS_REPO"] + '\\"',
|
|
|
|
u'-DPAXEXPRESS_PACKAGE=\\"' + package + '\\"',
|
2019-05-29 20:13:39 +02:00
|
|
|
u'-DARDUINO_LMIC_PROJECT_CONFIG_H=' + lmicconfig,
|
|
|
|
u'-I \"' + srcdir + '\"'
|
2018-09-18 17:23:56 +02:00
|
|
|
])
|
|
|
|
|
2021-03-22 23:10:07 +01:00
|
|
|
# function for pushing new firmware to paxexpress storage using API
|
|
|
|
def publish_paxexpress(source, target, env):
|
2018-09-18 17:23:56 +02:00
|
|
|
firmware_path = str(source[0])
|
|
|
|
firmware_name = basename(firmware_path)
|
|
|
|
url = "/".join([
|
2021-03-04 16:02:24 +01:00
|
|
|
"https://pax.express", "content",
|
2018-09-18 17:23:56 +02:00
|
|
|
user, repository, package, version, firmware_name
|
|
|
|
])
|
|
|
|
|
2021-03-22 23:10:07 +01:00
|
|
|
print("Uploading {0} to PAX.express. Version: {1}".format(
|
2018-09-18 17:23:56 +02:00
|
|
|
firmware_name, version))
|
|
|
|
print(url)
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
"Content-type": "application/octet-stream",
|
|
|
|
"X-Bintray-Publish": "1",
|
|
|
|
"X-Bintray-Override": "1"
|
|
|
|
}
|
|
|
|
|
2019-05-30 11:14:23 +02:00
|
|
|
r = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
r = requests.put(url,
|
|
|
|
data=open(firmware_path, "rb"),
|
|
|
|
headers=headers,
|
|
|
|
auth=(user,apitoken))
|
|
|
|
r.raise_for_status()
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
|
|
sys.stderr.write("Failed to submit package: %s\n" %
|
|
|
|
("%s\n%s" % (r.status_code, r.text) if r else str(e)))
|
|
|
|
env.Exit(1)
|
|
|
|
|
2021-03-22 23:10:07 +01:00
|
|
|
print("Firmware has been successfully published at PAX.express!")
|
2018-09-18 17:23:56 +02:00
|
|
|
|
|
|
|
# put build file name and upload command to platformio environment
|
|
|
|
env.Replace(
|
|
|
|
PROGNAME="firmware_" + package + "_v%s" % version,
|
2021-03-22 23:10:07 +01:00
|
|
|
UPLOADCMD=publish_paxexpress)
|