2019-07-16 01:14:27 +06:00
|
|
|
"""Functions that help us work with files and folders.
|
|
|
|
"""
|
2019-09-10 18:14:25 +06:00
|
|
|
import logging
|
2019-07-16 01:14:27 +06:00
|
|
|
import os
|
2020-02-18 01:42:11 +06:00
|
|
|
from pathlib import Path
|
2019-07-16 01:14:27 +06:00
|
|
|
|
2020-02-18 01:42:11 +06:00
|
|
|
from qmk.constants import QMK_FIRMWARE, MAX_KEYBOARD_SUBFOLDERS
|
2019-09-10 18:14:25 +06:00
|
|
|
from qmk.errors import NoSuchKeyboardError
|
|
|
|
|
2020-01-08 02:54:21 +06:00
|
|
|
|
2020-02-18 01:42:11 +06:00
|
|
|
def is_keymap_dir(keymap_path):
|
|
|
|
"""Returns True if `keymap_path` is a valid keymap directory.
|
|
|
|
"""
|
|
|
|
keymap_path = Path(keymap_path)
|
|
|
|
keymap_c = keymap_path / 'keymap.c'
|
|
|
|
keymap_json = keymap_path / 'keymap.json'
|
|
|
|
|
|
|
|
return any((keymap_c.exists(), keymap_json.exists()))
|
|
|
|
|
|
|
|
|
|
|
|
def is_keyboard(keyboard_name):
|
|
|
|
"""Returns True if `keyboard_name` is a keyboard we can compile.
|
|
|
|
"""
|
|
|
|
keyboard_path = QMK_FIRMWARE / 'keyboards' / keyboard_name
|
|
|
|
rules_mk = keyboard_path / 'rules.mk'
|
|
|
|
return rules_mk.exists()
|
|
|
|
|
|
|
|
|
|
|
|
def under_qmk_firmware():
|
|
|
|
"""Returns a Path object representing the relative path under qmk_firmware, or None.
|
|
|
|
"""
|
|
|
|
cwd = Path(os.environ['ORIG_CWD'])
|
|
|
|
|
|
|
|
try:
|
|
|
|
return cwd.relative_to(QMK_FIRMWARE)
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2019-07-16 01:14:27 +06:00
|
|
|
def keymap(keyboard):
|
|
|
|
"""Locate the correct directory for storing a keymap.
|
|
|
|
|
|
|
|
Args:
|
2020-02-18 01:42:11 +06:00
|
|
|
|
2019-07-16 01:14:27 +06:00
|
|
|
keyboard
|
|
|
|
The name of the keyboard. Example: clueboard/66/rev3
|
|
|
|
"""
|
2020-02-18 01:42:11 +06:00
|
|
|
keyboard_folder = Path('keyboards') / keyboard
|
|
|
|
|
|
|
|
for i in range(MAX_KEYBOARD_SUBFOLDERS):
|
|
|
|
if (keyboard_folder / 'keymaps').exists():
|
|
|
|
return (keyboard_folder / 'keymaps').resolve()
|
2019-07-16 01:14:27 +06:00
|
|
|
|
2020-02-18 01:42:11 +06:00
|
|
|
keyboard_folder = keyboard_folder.parent
|
2019-07-16 01:14:27 +06:00
|
|
|
|
2020-02-18 01:42:11 +06:00
|
|
|
logging.error('Could not find the keymaps directory!')
|
2019-07-16 01:14:27 +06:00
|
|
|
raise NoSuchKeyboardError('Could not find keymaps directory for: %s' % keyboard)
|
|
|
|
|
|
|
|
|
|
|
|
def normpath(path):
|
2020-02-18 01:42:11 +06:00
|
|
|
"""Returns a `pathlib.Path()` object for a given path.
|
2019-07-16 01:14:27 +06:00
|
|
|
|
2020-02-18 01:42:11 +06:00
|
|
|
This will use the path to a file as seen from the directory the script was called from. You should use this to normalize filenames supplied from the command line.
|
2019-07-16 01:14:27 +06:00
|
|
|
"""
|
2020-02-18 01:42:11 +06:00
|
|
|
path = Path(path)
|
|
|
|
|
|
|
|
if path.is_absolute():
|
2020-03-14 04:47:04 +06:00
|
|
|
return path
|
2019-07-16 01:14:27 +06:00
|
|
|
|
2020-02-18 01:42:11 +06:00
|
|
|
return Path(os.environ['ORIG_CWD']) / path
|
2020-02-23 10:57:19 +06:00
|
|
|
|
|
|
|
|
|
|
|
def c_source_files(dir_names):
|
|
|
|
"""Returns a list of all *.c, *.h, and *.cpp files for a given list of directories
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
|
|
dir_names
|
|
|
|
List of directories, relative pathing starts at qmk's cwd
|
|
|
|
"""
|
|
|
|
files = []
|
|
|
|
for dir in dir_names:
|
|
|
|
files.extend(file for file in Path(dir).glob('**/*') if file.suffix in ['.c', '.h', '.cpp'])
|
|
|
|
return files
|