-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeConfigFiles.py
54 lines (40 loc) · 1.65 KB
/
makeConfigFiles.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
# Purpose: Generates Config.h and certificates.h files from templates
# Preconditions: root.pem, cert.pem and privkey.pem files exist in current directory
# Synopsis: python makeConfigFiles.py doobeedoo 19 'myendpoint.iot.us-east-1.amazonaws.com' lights
# Notes:
# 1 - No error handling
# 2 - Written for Python 2.7 but seems to work fine with Python 3.7
# 3 - If you're dealing with a bunch of devices, it's easiest to call this from a bash script that has
# thing-specific parameters and copies the files into the right folder for deployment
import os, sys
cwd = os.path.split(os.getcwd())[1]
# Make the Config.h file
with open("Config.h.template", "r") as file:
filedata = file.read()
filedata = filedata.replace('|device id|', cwd)
filedata = filedata.replace('|access point password|', sys.argv[1])
filedata = filedata.replace('|num pixels|', sys.argv[2])
filedata = filedata.replace('|endpoint|', sys.argv[3])
filedata = filedata.replace('|mqtt topic|', sys.argv[4])
with open("Config.h", "w") as file:
file.write(filedata)
# Make the certificates.h file
rootdata = ""
certdata = ""
privdata = ""
def nl(f):
nData = ""
with open(f, "r") as data:
for line in data:
nData += (line.replace("\n", ("\\n\\" + "\n" if ("-END" not in line) else "\\n" ) )) if "\n" in line else (line + "\\n")
return nData
rootdata = nl("root.pem")
certdata = nl("cert.pem")
privdata = nl("privkey.pem")
with open("certificates.h.template", "r") as certificates:
cdata = certificates.read()
cdata = cdata.replace('|root|', rootdata)
cdata = cdata.replace('|cert|', certdata)
cdata = cdata.replace('|priv|', privdata)
with open("certificates.h", "w") as wCert:
wCert.write(cdata)