Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Make sure the basic CLI tools we need are available and can be executed.
binaries = ['dfu-programmer', 'avrdude', 'dfu-util', 'avr-gcc', 'arm-none-eabi-gcc']
binaries += glob('bin/qmk-*')
ok = True
for binary in binaries:
res = shutil.which(binary)
if res is None:
cli.log.error("{fg_red}QMK can't find %s in your path.", binary)
ok = False
else:
try:
subprocess.run([binary, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5, check=True)
cli.log.info('Found {fg_cyan}%s', binary)
except subprocess.CalledProcessError:
cli.log.error("{fg_red}Can't run `%s --version`", binary)
ok = False
# Determine our OS and run platform specific tests
OS = platform.system()
if OS == "Darwin":
cli.log.info("Detected {fg_cyan}macOS.")
elif OS == "Linux":
cli.log.info("Detected {fg_cyan}Linux.")
if shutil.which('systemctl'):
mm_check = subprocess.run(['systemctl', 'list-unit-files'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10, universal_newlines=True)
if mm_check.returncode == 0:
mm = False
for line in mm_check.stdout.split('\n'):
if 'ModemManager' in line and 'enabled' in line:
# Extract the section, config_key, and value to write from the supplied config_token.
if '=' in config_token:
key, value = config_token.split('=')
else:
key = config_token
value = None
if '.' in key:
section, config_key = key.split('.', 1)
else:
section = key
config_key = None
# Validation
if config_key and '.' in config_key:
cli.log.error('Config keys may not have more than one period! "%s" is not valid.', key)
return False
# Do what the user wants
if section and config_key and value:
# Write a config key
log_string = '%s.%s{fg_cyan}:{fg_reset} %s {fg_cyan}->{fg_reset} %s'
if cli.args.read_only:
log_string += ' {fg_red}(change not written)'
cli.echo(log_string, section, config_key, cli.config[section][config_key], value)
if not cli.args.read_only:
if value == 'None':
del cli.config[section][config_key]
else:
cli.config[section][config_key] = value
elif OS == "Linux":
cli.log.info("Detected {fg_cyan}Linux.")
if shutil.which('systemctl'):
mm_check = subprocess.run(['systemctl', 'list-unit-files'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10, universal_newlines=True)
if mm_check.returncode == 0:
mm = False
for line in mm_check.stdout.split('\n'):
if 'ModemManager' in line and 'enabled' in line:
mm = True
if mm:
cli.log.warn("{bg_yellow}Detected ModemManager. Please disable it if you are using a Pro-Micro.")
else:
cli.log.error('{bg_red}Could not run `systemctl list-unit-files`:')
cli.log.error(mm_check.stderr)
else:
cli.log.warn("Can't find systemctl to check for ModemManager.")
else:
cli.log.info("Assuming {fg_cyan}Windows.")
# Report a summary of our findings to the user
if ok:
cli.log.info('{fg_green}QMK is ready to go')
else:
cli.log.info('{fg_yellow}Problems detected, please fix these problems before proceeding.')
# FIXME(skullydazed): Link to a document about troubleshooting, or discord or something
def json_keymap(cli):
"""Generate a keymap.c from a configurator export.
This command uses the `qmk.keymap` module to generate a keymap.c from a configurator export. The generated keymap is written to stdout, or to a file if -o is provided.
"""
# Error checking
if cli.args.filename == ('-'):
cli.log.error('Reading from STDIN is not (yet) supported.')
cli.print_usage()
exit(1)
if not os.path.exists(qmk.path.normpath(cli.args.filename)):
cli.log.error('JSON file does not exist!')
cli.print_usage()
exit(1)
# Environment processing
if cli.args.output == ('-'):
cli.args.output = None
# Parse the configurator json
with open(qmk.path.normpath(cli.args.filename), 'r') as fd:
user_keymap = json.load(fd)
# Generate the keymap
keymap_c = qmk.keymap.generate(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers'])
if cli.args.output:
output_dir = os.path.dirname(cli.args.output)
if OS == "Darwin":
cli.log.info("Detected {fg_cyan}macOS.")
elif OS == "Linux":
cli.log.info("Detected {fg_cyan}Linux.")
if shutil.which('systemctl'):
mm_check = subprocess.run(['systemctl', 'list-unit-files'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10, universal_newlines=True)
if mm_check.returncode == 0:
mm = False
for line in mm_check.stdout.split('\n'):
if 'ModemManager' in line and 'enabled' in line:
mm = True
if mm:
cli.log.warn("{bg_yellow}Detected ModemManager. Please disable it if you are using a Pro-Micro.")
else:
cli.log.error('{bg_red}Could not run `systemctl list-unit-files`:')
cli.log.error(mm_check.stderr)
else:
cli.log.warn("Can't find systemctl to check for ModemManager.")
else:
cli.log.info("Assuming {fg_cyan}Windows.")
# Report a summary of our findings to the user
if ok:
cli.log.info('{fg_green}QMK is ready to go')
else:
cli.log.info('{fg_yellow}Problems detected, please fix these problems before proceeding.')
mm_check = subprocess.run(['systemctl', 'list-unit-files'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10, universal_newlines=True)
if mm_check.returncode == 0:
mm = False
for line in mm_check.stdout.split('\n'):
if 'ModemManager' in line and 'enabled' in line:
mm = True
if mm:
cli.log.warn("{bg_yellow}Detected ModemManager. Please disable it if you are using a Pro-Micro.")
else:
cli.log.error('{bg_red}Could not run `systemctl list-unit-files`:')
cli.log.error(mm_check.stderr)
else:
cli.log.warn("Can't find systemctl to check for ModemManager.")
else:
cli.log.info("Assuming {fg_cyan}Windows.")
# Report a summary of our findings to the user
if ok:
cli.log.info('{fg_green}QMK is ready to go')
else:
cli.log.info('{fg_yellow}Problems detected, please fix these problems before proceeding.')
# FIXME(skullydazed): Link to a document about troubleshooting, or discord or something
cli.log.info('QMK Doctor is checking your environment.')
# Make sure the basic CLI tools we need are available and can be executed.
binaries = ['dfu-programmer', 'avrdude', 'dfu-util', 'avr-gcc', 'arm-none-eabi-gcc']
binaries += glob('bin/qmk-*')
ok = True
for binary in binaries:
res = shutil.which(binary)
if res is None:
cli.log.error("{fg_red}QMK can't find %s in your path.", binary)
ok = False
else:
try:
subprocess.run([binary, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5, check=True)
cli.log.info('Found {fg_cyan}%s', binary)
except subprocess.CalledProcessError:
cli.log.error("{fg_red}Can't run `%s --version`", binary)
ok = False
# Determine our OS and run platform specific tests
OS = platform.system()
if OS == "Darwin":
cli.log.info("Detected {fg_cyan}macOS.")
elif OS == "Linux":
cli.log.info("Detected {fg_cyan}Linux.")
if shutil.which('systemctl'):
mm_check = subprocess.run(['systemctl', 'list-unit-files'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10, universal_newlines=True)
if mm_check.returncode == 0:
mm = False
def print_bootloader_help():
"""Prints the available bootloaders listed in docs.qmk.fm.
"""
cli.log.info('Here are the available bootloaders:')
cli.echo('\tdfu')
cli.echo('\tdfu-ee')
cli.echo('\tdfu-split-left')
cli.echo('\tdfu-split-right')
cli.echo('\tavrdude')
cli.echo('\tBootloadHID')
cli.echo('\tdfu-util')
cli.echo('\tdfu-util-split-left')
cli.echo('\tdfu-util-split-right')
cli.echo('\tst-link-cli')
cli.echo('For more info, visit https://docs.qmk.fm/#/flashing')
def docs(cli):
"""Spin up a local HTTPServer instance for the QMK docs.
"""
os.chdir('docs')
with http.server.HTTPServer(('', cli.config.docs.port), http.server.SimpleHTTPRequestHandler) as httpd:
cli.log.info("Serving QMK docs at http://localhost:%d/", cli.config.docs.port)
cli.log.info("Press Control+C to exit.")
try:
httpd.serve_forever()
except KeyboardInterrupt:
cli.log.info("Stopping HTTP server...")
finally:
httpd.shutdown()
def pyformat(cli):
"""Format python code according to QMK's style.
"""
try:
subprocess.run(['yapf', '-vv', '-ri', 'bin/qmk', 'lib/python'], check=True)
cli.log.info('Successfully formatted the python code in `bin/qmk` and `lib/python`.')
except subprocess.CalledProcessError:
cli.log.error('Error formatting python code!')