Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ python3 create_requirement_images.py bundle [path to example].py
python3 create_requirement_images.py bundle Adafruit_CircuitPythonBundle/libraries/helpers/wiz/wiz_buttons_controller.py
```

### Architecture Specific .mpy Files
Files built for different architectures of the same module, like `_foo.armv7emsp.mpy` and `_foo.rv32imc.mpy`, are bracketed together with an "OR" badge in the screenshot to show that only one of them is needed.

### Help Command
The help command will list all possible commands and arguments.

Expand Down
155 changes: 152 additions & 3 deletions create_requirement_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from multiprocessing import Pool
import json
import os
import re

import click
from PIL import Image, ImageDraw, ImageFont
Expand Down Expand Up @@ -38,6 +39,27 @@
TEXT_COLOR = "#B0B0B0"
HIDDEN_TEXT_COLOR = "#808080"

OR_MARKER_COLOR = "#E0A030"
OR_MARKER_BADGE_TEXT_COLOR = "#303030"
OR_MARKER_LABEL = "choose one"

# Architectures that native .mpy files can be built for
MPY_ARCHITECTURES = (
"x86",
"x64",
"armv6",
"armv6m",
"armv7m",
"armv7em",
"armv7emsp",
"armv7emdp",
"xtensa",
"xtensawin",
"rv32imc",
"rv64imc",
)
ARCH_MPY_PATTERN = re.compile(rf"^(.+)\.({'|'.join(MPY_ARCHITECTURES)})\.mpy$")

with open("latest_bundle_data.json", "r", encoding="utf-8") as f:
bundle_data = json.load(f)

Expand All @@ -51,6 +73,7 @@ def asset_path(asset_name):


font = ImageFont.truetype(asset_path("Roboto-Regular.ttf"), 24)
small_font = ImageFont.truetype(asset_path("Roboto-Regular.ttf"), 18)
right_triangle = Image.open(asset_path("img/right_triangle.png"))
down_triangle = Image.open(asset_path("img/down_triangle.png"))

Expand Down Expand Up @@ -91,6 +114,41 @@ def asset_path(asset_name):
_file_icon.load()


def arch_mpy_module(file_name):
"""Return the module name of an arch specific .mpy file, otherwise None"""
match = ARCH_MPY_PATTERN.match(file_name)
return match.group(1) if match else None


def file_sort_key(file_name):
"""Sort key that keeps the arch specific .mpy files of a module together"""
return (arch_mpy_module(file_name) or file_name, file_name)


def find_arch_variant_groups(file_names):
"""
Find the runs of files that are builds of one module for different
architectures. file_names must already be sorted with file_sort_key.

:return: A list of (first index, last index) tuples.
"""
groups = []
start = 0
while start < len(file_names):
module = arch_mpy_module(file_names[start])
end = start
while (
module is not None
and end + 1 < len(file_names)
and arch_mpy_module(file_names[end + 1]) == module
):
end += 1
if end > start:
groups.append((start, end))
start = end + 1
return groups


def generate_requirement_image(
project_files, libs, image_name
): # pylint: disable=too-many-statements, too-many-locals
Expand Down Expand Up @@ -170,6 +228,71 @@ def make_line(
font=font,
)

def make_or_markers(file_names, position):
"""
Bracket each group of arch specific .mpy files together with an "OR"
badge to show that only one file of the group is needed.

:param file_names: Names drawn on consecutive rows
:param position: Position of the first row, as passed to make_line
"""
groups = find_arch_variant_groups(file_names)
if not groups:
return

longest_name = max(
font.getlength(file_names[i])
for first, last in groups
for i in range(first, last + 1)
)
bracket_left = position[0] + 30 + int(longest_name) + 10
bracket_right = bracket_left + 8
badge_left = bracket_right + 8
badge_size = (40, 22)

for first, last in groups:
top = position[1] + first * LINE_SPACING + 6
bottom = position[1] + (last + 1) * LINE_SPACING - 6
middle = (top + bottom) // 2

draw.line(
[
(bracket_left, top),
(bracket_right, top),
(bracket_right, bottom),
(bracket_left, bottom),
],
fill=OR_MARKER_COLOR,
width=2,
)
draw.line(
[(bracket_right, middle), (badge_left, middle)],
fill=OR_MARKER_COLOR,
width=2,
)
draw.rounded_rectangle(
[
(badge_left, middle - badge_size[1] // 2),
(badge_left + badge_size[0], middle + badge_size[1] // 2),
],
radius=badge_size[1] // 2,
fill=OR_MARKER_COLOR,
)
draw.text(
(badge_left + badge_size[0] // 2, middle),
"OR",
fill=OR_MARKER_BADGE_TEXT_COLOR,
anchor="mm",
font=small_font,
)
draw.text(
(badge_left + badge_size[0] + 8, middle),
OR_MARKER_LABEL,
fill=OR_MARKER_COLOR,
anchor="lm",
font=small_font,
)

def make_header(position, project_files, files_and_libs):
# pylint: disable=too-many-locals, too-many-branches
# Static files
Expand Down Expand Up @@ -237,7 +360,8 @@ def make_header(position, project_files, files_and_libs):
project_folders_to_draw[cur_file[0]] = cur_file[1]

begin_y_offset = 7 if context["added_settings_toml"] else 6
for i, file in enumerate(sorted(project_files_to_draw)):
project_files_to_draw.sort(key=file_sort_key)
for i, file in enumerate(project_files_to_draw):
cur_file_extension = file.split(".")[-1]

cur_file_icon = FILE_TYPE_ICON_MAP.get(cur_file_extension, file_empty_icon)
Expand All @@ -252,6 +376,14 @@ def make_header(position, project_files, files_and_libs):
)
rows_added += 1

make_or_markers(
project_files_to_draw,
(
position[0] + INDENT_SIZE * 2,
position[1] + (LINE_SPACING * begin_y_offset),
),
)

extra_rows = 0
for i, file in enumerate(sorted(project_folders_to_draw.keys())):
if len(project_folders_to_draw[file]) > 0:
Expand All @@ -271,7 +403,15 @@ def make_header(position, project_files, files_and_libs):
triangle_icon=triangle_to_use,
)
rows_added += 1
for sub_file in sorted(project_folders_to_draw[file]):
sub_files_to_draw = sorted(project_folders_to_draw[file], key=file_sort_key)
make_or_markers(
sub_files_to_draw,
(
position[0] + INDENT_SIZE * 3,
position[1] + (LINE_SPACING * (begin_y_offset + rows_added)),
),
)
for sub_file in sub_files_to_draw:
extra_rows += 1
cur_file_extension = sub_file.split(".")[-1]
cur_file_icon = FILE_TYPE_ICON_MAP.get(cur_file_extension, folder_icon)
Expand Down Expand Up @@ -358,7 +498,7 @@ def get_dependencies(libraries):

def sort_libraries(libraries):
package_list, file_list = get_dependencies(libraries)
return sorted(package_list) + sorted(file_list)
return sorted(package_list) + sorted(file_list, key=file_sort_key)

def count_files(files_list):
_count = 0
Expand Down Expand Up @@ -393,6 +533,15 @@ def make_libraries(libraries, position):
triangle_icon=triangle_icon,
)

make_or_markers(
libraries,
(
position[0] + INDENT_SIZE * 3,
position[1]
+ (LINE_SPACING * (1 if context["added_settings_toml"] else 0)),
),
)

def make_sd_dir(position):
make_line(
"sd",
Expand Down
12 changes: 10 additions & 2 deletions get_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
]
SHOWN_FILETYPES_EXAMPLE = [s for s in SHOWN_FILETYPES if s != "py"]

HIDDEN_DIRECTORIES = ("__pycache__",)


def get_bundle(bundle_url, bundle_data_file):
"""Download the Adafruit and Community bundles data"""
Expand Down Expand Up @@ -157,11 +159,14 @@ def get_files_for_project(project_name):
found_files.add(file)

for _dir in root_level[1]:
if _dir in HIDDEN_DIRECTORIES:
continue
dir_tuple = (_dir, tuple())
for cur_tuple in os.walk(project_dir):
if cur_tuple[0].split("/")[-1] == _dir:
for _sub_dir in cur_tuple[1]:
dir_tuple = (dir_tuple[0], dir_tuple[1] + (_sub_dir,))
if _sub_dir not in HIDDEN_DIRECTORIES:
dir_tuple = (dir_tuple[0], dir_tuple[1] + (_sub_dir,))
if len(cur_tuple[2]) < SUBDIRECTORY_FILECOUNT_LIMIT:
for _sub_file in cur_tuple[2]:
dir_tuple = (dir_tuple[0], dir_tuple[1] + (_sub_file,))
Expand Down Expand Up @@ -221,11 +226,14 @@ def get_files_for_example(example_path):
found_files.add(file)

for _dir in root_level[1]:
if _dir in HIDDEN_DIRECTORIES:
continue
dir_tuple = (_dir, tuple())
for cur_tuple in os.walk(example_dir):
if cur_tuple[0].split("/")[-1] == _dir:
for _sub_dir in cur_tuple[1]:
dir_tuple = (dir_tuple[0], dir_tuple[1] + (_sub_dir,))
if _sub_dir not in HIDDEN_DIRECTORIES:
dir_tuple = (dir_tuple[0], dir_tuple[1] + (_sub_dir,))
for _sub_file in cur_tuple[2]:
if _sub_file.split(".")[-1] in SHOWN_FILETYPES_EXAMPLE:
dir_tuple = (dir_tuple[0], dir_tuple[1] + (_sub_file,))
Expand Down
Loading