Skip to content

Commit 0b07c2c

Browse files
committed
two test mappings, build script to create the zips and the json
0 parents  commit 0b07c2c

File tree

5 files changed

+778
-0
lines changed

5 files changed

+778
-0
lines changed

build.py

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
"""
2+
This makes the bundle zip files and the json file.
3+
Next step: upload as artifacts/release files in github.
4+
"""
5+
6+
import datetime
7+
import glob
8+
import json
9+
import os
10+
import shutil
11+
import stat
12+
import subprocess
13+
import sys
14+
import zipfile
15+
16+
import requests
17+
18+
# make the dirs for putting the things in it
19+
TAG = datetime.date.today().strftime("%Y%m%d")
20+
BUILD_DIR = "_build"
21+
BUILD_DEPS = "_build_deps"
22+
BUILD_ZIPS = "_build_zips"
23+
BUNDLE_NAME = "neradoc-keyboard-layouts"
24+
BUNDLE_JSON = os.path.join(BUILD_DIR, f"{BUNDLE_NAME}-{TAG}.json")
25+
# platform dependent
26+
BUNDLE_PATH_NAME = f"{BUNDLE_NAME}-{{platform}}-{TAG}"
27+
BUNDLE_DIR = os.path.join(BUILD_DIR, BUNDLE_PATH_NAME)
28+
BUNDLE_ZIP = os.path.join(BUILD_ZIPS, BUNDLE_PATH_NAME + ".zip")
29+
BUNDLE_LIB_DIR = os.path.join(BUNDLE_DIR, "lib")
30+
# py platform directory
31+
BUNDLE_REQ_DIR = os.path.join(BUNDLE_DIR.format(platform="py"), "requirements")
32+
BUNDLE_ZIP_JSON = os.path.join(
33+
BUNDLE_DIR.format(platform="py"), f"{BUNDLE_NAME}-{TAG}.json"
34+
)
35+
36+
SOURCEDIR = "src"
37+
REQUIREMENTS_FILE = "requirements-modules.txt"
38+
39+
# TODO: retrieve the version number from git
40+
# TODO: give each file a different version number possibly (that of the latest released change)
41+
VERSION_NUMBER = "0.0.1"
42+
THIS_REPOSITORY = "https://github.com/Neradoc/Neradoc-Circuitpython-Keyboard-Layouts"
43+
44+
PLATFORMS = ["mpy6", "mpy7"]
45+
46+
# https://adafruit-circuit-python.s3.amazonaws.com/index.html?prefix=bin/mpy-cross/
47+
# TODO: identify current OS and pick one
48+
MPYCROSS_URL = "https://adafruit-circuit-python.s3.amazonaws.com/bin/mpy-cross/"
49+
MPYCROSSES = {
50+
"darwin": {
51+
"mpy6": "mpy-cross-macos-catalina-6.3.0",
52+
"mpy7": "mpy-cross-macos-universal-7.0.0-alpha.4",
53+
},
54+
"linux": {
55+
"mpy6": "mpy-cross.static-amd64-linux-6.3.0",
56+
"mpy7": "mpy-cross.static-amd64-linux-7.0.0-alpha.4",
57+
},
58+
"win32": {
59+
"mpy6": "mpy-cross.static-x64-windows-6.3.0.exe",
60+
"mpy7": "mpy-cross.static-x64-windows-7.0.0-alpha.4.exe",
61+
},
62+
"raspbian": {
63+
"mpy6": "mpy-cross.static-raspbian-6.3.0",
64+
"mpy7": "mpy-cross.static-raspbian-7.0.0-alpha.4",
65+
},
66+
}
67+
MPYCROSS = MPYCROSSES[sys.platform]
68+
69+
70+
def fmt(path, platform="py"):
71+
"""shortcut for the py directory"""
72+
return path.format(platform=platform)
73+
74+
75+
# find in python
76+
def list_all_files(path):
77+
"""clean list of all files in sub folders"""
78+
pwd = os.getcwd()
79+
os.chdir(path)
80+
liste = [
81+
file
82+
for file in glob.glob(os.path.join("**"), recursive=True)
83+
if os.path.isfile(file)
84+
]
85+
os.chdir(pwd)
86+
return liste
87+
88+
89+
def init_directories():
90+
"""erase and create build directories"""
91+
# create build directories
92+
os.makedirs(BUILD_DEPS, exist_ok=True)
93+
os.makedirs(BUILD_ZIPS, exist_ok=True)
94+
os.makedirs(fmt(BUNDLE_DIR), exist_ok=True)
95+
96+
# cleanup build directories
97+
for platform in ["py"] + PLATFORMS:
98+
bun_dir = BUNDLE_DIR.format(platform=platform)
99+
zip_file = BUNDLE_ZIP.format(platform=platform)
100+
if os.path.isdir(bun_dir):
101+
shutil.rmtree(bun_dir)
102+
if os.path.isfile(zip_file):
103+
os.unlink(zip_file)
104+
105+
106+
def make_bundle_files():
107+
"""create the .py bundle directory"""
108+
# copy all the layouts and keycodes
109+
shutil.copytree(SOURCEDIR, fmt(BUNDLE_LIB_DIR))
110+
111+
# list of the modules
112+
all_modules = [mod.replace(".py", "") for mod in os.listdir(SOURCEDIR)]
113+
114+
json_data = {}
115+
116+
for module in all_modules:
117+
# create the requirements directory for each module
118+
target_dir = os.path.join(BUNDLE_REQ_DIR, module)
119+
os.makedirs(target_dir, exist_ok=True)
120+
# copy the common requirements file
121+
target = os.path.join(target_dir, "requirements.txt")
122+
shutil.copy(REQUIREMENTS_FILE, target)
123+
# create the json entry
124+
json_data[module] = {
125+
"package": False,
126+
"pypi_name": "",
127+
"version": VERSION_NUMBER,
128+
"repo": THIS_REPOSITORY,
129+
"path": "lib/" + module,
130+
"dependencies": ["adafruit_hid"],
131+
"external_dependencies": [],
132+
}
133+
134+
# create the json file
135+
with open(BUNDLE_JSON, "w") as out_file:
136+
json.dump(json_data, out_file, indent=2)
137+
138+
139+
def make_the_mpy_bundles():
140+
"""create the mpy bundle(s) directory(ies) and mpy-cross the modules"""
141+
# copy for the zips
142+
shutil.copy(BUNDLE_JSON, fmt(BUNDLE_ZIP_JSON))
143+
144+
# download the mpycrosses
145+
for cross in MPYCROSS:
146+
cross_file = os.path.join(BUILD_DEPS, MPYCROSS[cross])
147+
if not os.path.isfile(cross_file):
148+
url = MPYCROSS_URL + MPYCROSS[cross]
149+
response = requests.get(url)
150+
with open(cross_file, "wb") as cross_fp:
151+
cross_fp.write(response.content)
152+
fstats = os.stat(cross_file)
153+
os.chmod(cross_file, fstats.st_mode | stat.S_IEXEC)
154+
155+
# duplicate the py dir to mpy6 and mpy7
156+
for platform in PLATFORMS:
157+
cross = os.path.join(BUILD_DEPS, MPYCROSS[platform])
158+
bun_dir = BUNDLE_DIR.format(platform=platform)
159+
lib_dir = BUNDLE_LIB_DIR.format(platform=platform)
160+
shutil.copytree(fmt(BUNDLE_DIR), bun_dir)
161+
# run mpy-cross in each of those
162+
for lib_file in glob.glob(os.path.join(lib_dir, "*.py")):
163+
mpy_file = lib_file.replace(".py", ".mpy")
164+
subprocess.call([cross, lib_file, "-o", mpy_file])
165+
os.unlink(lib_file)
166+
167+
168+
def do_the_zips():
169+
"""finally create the zip files for release"""
170+
# now do the zips
171+
for platform in ["py"] + PLATFORMS:
172+
bun_dir = BUNDLE_DIR.format(platform=platform)
173+
zip_file = BUNDLE_ZIP.format(platform=platform)
174+
all_files = list_all_files(bun_dir)
175+
with zipfile.ZipFile(zip_file, "w") as bundle:
176+
# metadata (bundler version)
177+
# build_metadata = {"build-tools-version": build_tools_version}
178+
# bundle.comment = json.dumps(build_metadata).encode("utf-8")
179+
for ffile in all_files:
180+
bundle.write(os.path.join(bun_dir, ffile), ffile)
181+
182+
183+
if __name__ == "__main__":
184+
init_directories()
185+
make_bundle_files()
186+
make_the_mpy_bundles()
187+
do_the_zips()

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests

requirements-modules.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Adafruit-Circuitpython-HID

0 commit comments

Comments
 (0)