Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_build(self):
"""
Initialize a new project and try to build it
"""
project = stm32pio.lib.Stm32pio(FIXTURE_PATH, parameters={'project': {'board': TEST_PROJECT_BOARD}})
project.generate_code()
project.pio_init()
project.patch()
self.assertEqual(project.build(), 0, msg="Build failed")
# Create test config
config = configparser.ConfigParser(interpolation=None)
config.read_dict({
'project': {
'platformio_ini_patch_content': config_parameter_user_value,
'board': TEST_PROJECT_BOARD
}
})
# ... save it
with FIXTURE_PATH.joinpath(stm32pio.settings.config_file_name).open(mode='w') as config_file:
config.write(config_file)
# On project creation we should interpret the CLI-provided values as superseding to the saved ones and
# saved ones, in turn, as superseding to the default ones
project = stm32pio.lib.Stm32pio(FIXTURE_PATH, parameters={'project': {'board': cli_parameter_user_value}})
project.pio_init()
project.patch()
# Actually, we can parse the platformio.ini via the configparser but this is simpler in our case
after_patch_content = FIXTURE_PATH.joinpath('platformio.ini').read_text()
self.assertIn(config_parameter_user_value, after_patch_content,
msg="User config parameter has not been prioritized over the default one")
self.assertIn(cli_parameter_user_value, after_patch_content,
msg="User CLI parameter has not been prioritized over the saved one")
def test_start_editor(self):
"""
Call the editors. Use subprocess shell=True as it works on all OSes
"""
project = stm32pio.lib.Stm32pio(FIXTURE_PATH)
editors = {
'atom': {
'Windows': 'atom.exe',
'Darwin': 'Atom',
'Linux': 'atom'
},
'code': {
'Windows': 'Code.exe',
'Darwin': 'Visual Studio Code',
'Linux': 'code'
},
'subl': {
'Windows': 'sublime_text.exe',
'Darwin': 'Sublime',
'Linux': 'sublime'
project = stm32pio.lib.Stm32pio(args.path, parameters={'project': {'board': args.board}},
instance_options={'save_on_destruction': True})
if project.config.get('project', 'board') == '':
raise Exception("PlatformIO board identifier is not specified, it is needed for PlatformIO project "
"creation. Type 'pio boards' or go to https://platformio.org to find an appropriate "
"identifier")
project.generate_code()
project.pio_init()
project.patch()
if args.with_build:
project.build()
if args.editor:
project.start_editor(args.editor)
elif args.subcommand == 'generate':
project = stm32pio.lib.Stm32pio(args.path)
project.generate_code()
if args.with_build:
project.build()
if args.editor:
project.start_editor(args.editor)
elif args.subcommand == 'status':
project = stm32pio.lib.Stm32pio(args.path)
print(project.state)
elif args.subcommand == 'clean':
project = stm32pio.lib.Stm32pio(args.path)
if args.quiet:
project.clean()
else:
while True:
# Main routine
try:
if args.subcommand == 'init':
project = stm32pio.lib.Stm32pio(args.path, parameters={'project': {'board': args.board}},
instance_options={'save_on_destruction': True})
if not args.board:
logger.warning("PlatformIO board identifier is not specified, it will be needed on PlatformIO project "
"creation. Type 'pio boards' or go to https://platformio.org to find an appropriate "
"identifier")
logger.info("project has been initialized. You can now edit stm32pio.ini config file")
if args.editor:
project.start_editor(args.editor)
elif args.subcommand == 'new':
project = stm32pio.lib.Stm32pio(args.path, parameters={'project': {'board': args.board}},
instance_options={'save_on_destruction': True})
if project.config.get('project', 'board') == '':
raise Exception("PlatformIO board identifier is not specified, it is needed for PlatformIO project "
"creation. Type 'pio boards' or go to https://platformio.org to find an appropriate "
"identifier")
project.generate_code()
project.pio_init()
project.patch()
if args.with_build:
project.build()
if args.editor:
project.start_editor(args.editor)
elif args.subcommand == 'generate':
project = stm32pio.lib.Stm32pio(args.path)
project.generate_code()
project.start_editor(args.editor)
elif args.subcommand == 'generate':
project = stm32pio.lib.Stm32pio(args.path)
project.generate_code()
if args.with_build:
project.build()
if args.editor:
project.start_editor(args.editor)
elif args.subcommand == 'status':
project = stm32pio.lib.Stm32pio(args.path)
print(project.state)
elif args.subcommand == 'clean':
project = stm32pio.lib.Stm32pio(args.path)
if args.quiet:
project.clean()
else:
while True:
reply = input(f'WARNING: this operation will delete ALL content of the directory "{project.path}" '
f'except the "{pathlib.Path(project.config.get("project", "ioc_file")).name}" file. '
'Are you sure? (y/n) ')
if reply.lower() in ['y', 'yes', 'true', '1']:
project.clean()
break
elif reply.lower() in ['n', 'no', 'false', '0']:
break
# Library is designed to throw the exception in bad cases so we catch here globally
except Exception:
stm32pio.util.log_current_exception(logger)
project.patch()
if args.with_build:
project.build()
if args.editor:
project.start_editor(args.editor)
elif args.subcommand == 'generate':
project = stm32pio.lib.Stm32pio(args.path)
project.generate_code()
if args.with_build:
project.build()
if args.editor:
project.start_editor(args.editor)
elif args.subcommand == 'status':
project = stm32pio.lib.Stm32pio(args.path)
print(project.state)
elif args.subcommand == 'clean':
project = stm32pio.lib.Stm32pio(args.path)
if args.quiet:
project.clean()
else:
while True:
reply = input(f'WARNING: this operation will delete ALL content of the directory "{project.path}" '
f'except the "{pathlib.Path(project.config.get("project", "ioc_file")).name}" file. '
'Are you sure? (y/n) ')
if reply.lower() in ['y', 'yes', 'true', '1']:
project.clean()
break
elif reply.lower() in ['n', 'no', 'false', '0']:
break
def init_project(self, *args, **kwargs) -> None:
"""
Initialize the underlying Stm32pio project.
Args:
*args: positional arguments of the Stm32pio constructor
**kwargs: keyword arguments of the Stm32pio constructor
"""
try:
self.project = stm32pio.lib.Stm32pio(*args, **kwargs)
except Exception:
stm32pio.util.log_current_exception(self.logger)
if len(args):
self._name = args[0] # use a project path string (as it should be a first argument) as a name
else:
self._name = 'Undefined'
self._state = { 'INIT_ERROR': True } # pseudo-stage
self._current_stage = 'Initializing error'
else:
# Successful initialization. These values should not be used anymore but we "reset" them anyway
self._name = 'Project'
self._state = {}
self._current_stage = 'Initialized'
finally:
# Register some kind of the deconstruction handler
self._finalizer = weakref.finalize(self, self.at_exit, self.workers_pool, self.logging_worker,