Skip to content

Commit

Permalink
add Windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
ussserrr committed Nov 7, 2018
1 parent 4f7152e commit e81ab38
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 38 deletions.
5 changes: 3 additions & 2 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
cubemx_path = "/usr/local/STMicroelectronics/STM32Cube/STM32CubeMX/STM32CubeMX"
# Windows is not implemented yet
elif my_os == 'Windows':
logger.error("Windows is not supported!")
sys.exit()
cubemx_path = "C:/Program Files/STMicroelectronics/STM32Cube/STM32CubeMX/STM32CubeMX.exe"
# logger.error("Windows is not supported!")
# sys.exit()

cubemx_script_filename = 'cubemx-script'

Expand Down
47 changes: 29 additions & 18 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@
import settings
import util

project_path = '?'
if settings.my_os in ['Darwin', 'Linux']:
project_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'stm32pio-test')
# Windows is not implemented yet
# elif settings.my_os == 'Windows':
# project_path = '?'

project_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'stm32pio-test')
board = 'nucleo_f031k6'



def clean_run(test):
def wrapper(self):
util.clean(project_path)
Expand Down Expand Up @@ -66,9 +60,18 @@ def test_patch_platformio_ini(self):
util.patch_platformio_ini(project_path)

with open(os.path.join(project_path, 'platformio.ini'), mode='rb') as platformio_ini:
# '2' means that we count from the end of the file. This feature works only in binary file mode
platformio_ini.seek(-len(settings.platformio_ini_patch_text), 2)
platformio_ini_patched_str = platformio_ini.read(len(settings.platformio_ini_patch_text)).decode('utf-8')
# '2' in seek() means that we count from the end of the file. This feature works only in binary file mode
# In Windows additional '\r' is appended to every '\n' so we need to count them for the correct calculation
if settings.my_os == 'Windows':
platformio_ini.seek(-(len(settings.platformio_ini_patch_text) +
settings.platformio_ini_patch_text.count('\n')), 2)
platformio_ini_patched_str = platformio_ini.read(len(settings.platformio_ini_patch_text) +
settings.platformio_ini_patch_text.count('\n'))
platformio_ini_patched_str = platformio_ini_patched_str.replace(b'\r', b'').decode('utf-8')
else:
platformio_ini.seek(-len(settings.platformio_ini_patch_text), 2)
platformio_ini_patched_str = platformio_ini.read(
len(settings.platformio_ini_patch_text)).decode('utf-8')

self.assertEqual(platformio_ini_patched_str, settings.platformio_ini_patch_text,
msg="'platformio.ini' patching error")
Expand All @@ -84,7 +87,8 @@ def test_build(self):
util.pio_init(project_path, board)
util.patch_platformio_ini(project_path)

result = subprocess.run(['platformio', 'run'], cwd=project_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result = subprocess.run(['platformio', 'run'],
cwd=project_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Or, for Python 3.7 and above:
# result = subprocess.run(['platformio', 'run'], cwd=project_path, capture_output=True)

Expand All @@ -100,13 +104,20 @@ def test_run_editor(self):
util.start_editor(project_path, 'vscode')
util.start_editor(project_path, 'sublime')
time.sleep(1) # wait a little bit for apps to start
result = subprocess.run(['ps', '-A'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8')
# Or, for Python 3.7 and above:
# result = subprocess.run(['ps', '-A'], capture_output=True, encoding='utf-8')

self.assertIn('Atom', result.stdout)
self.assertIn('Visual Studio Code', result.stdout)
self.assertIn('Sublime', result.stdout)

if settings.my_os == 'Windows':
result = subprocess.run(['wmic', 'process', 'get', 'description'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8')
self.assertIn('atom.exe', result.stdout)
self.assertIn('Code.exe', result.stdout)
self.assertIn('sublime_text.exe', result.stdout)
else:
result = subprocess.run(['ps', '-A'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8')
# Or, for Python 3.7 and above:
# result = subprocess.run(['ps', '-A'], capture_output=True, encoding='utf-8')
self.assertIn('Atom', result.stdout)
self.assertIn('Visual Studio Code', result.stdout)
self.assertIn('Sublime', result.stdout)


@clean_run
Expand Down
42 changes: 24 additions & 18 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def generate_code(project_path):
else:
logger.info("successful code generation")

if settings.my_os == 'Windows':
shutil.rmtree(os.path.join(project_path, 'MXTmpFiles'), ignore_errors=True)



def pio_init(project_path, board):
Expand All @@ -78,7 +81,7 @@ def pio_init(project_path, board):
sys.exit()
else:
if board not in result.stdout.split():
logger.error("wrong STM32 board. Run 'pio boards' for possible names")
logger.error("wrong STM32 board. Run 'platformio boards' for possible names")
sys.exit()

logger.info("starting PlatformIO project initialization...")
Expand Down Expand Up @@ -113,16 +116,11 @@ def patch_platformio_ini(project_path):

logger.debug("patching 'platformio.ini' file...")

if settings.my_os in ['Darwin', 'Linux']:
with open(f"{project_path}/platformio.ini", mode='a') as platformio_ini_file:
platformio_ini_file.write(settings.platformio_ini_patch_text)

shutil.rmtree(os.path.join(project_path, 'include'), ignore_errors=True)
shutil.rmtree(os.path.join(project_path, 'src'), ignore_errors=True)
with open(os.path.join(project_path, 'platformio.ini'), mode='a') as platformio_ini_file:
platformio_ini_file.write(settings.platformio_ini_patch_text)

# Windows
else:
logger.warning("Windows is not supported")
shutil.rmtree(os.path.join(project_path, 'include'), ignore_errors=True)
shutil.rmtree(os.path.join(project_path, 'src'), ignore_errors=True)

logger.info("'platformio.ini' patched")

Expand All @@ -139,14 +137,22 @@ def start_editor(project_path, editor):

# TODO: handle errors if there is no editor

logger.info("starting editor...")
logger.info("starting an editor...")

if editor == 'atom':
subprocess.run(['atom', project_path])
elif editor == 'vscode':
subprocess.run(['code', project_path])
elif editor == 'sublime':
subprocess.run(['subl', project_path])
if settings.my_os == 'Windows':
if editor == 'atom':
subprocess.run(['atom', project_path], shell=True)
elif editor == 'vscode':
subprocess.run(['code', project_path], shell=True)
elif editor == 'sublime':
subprocess.run(['subl', project_path], shell=True)
else:
if editor == 'atom':
subprocess.run(['atom', project_path])
elif editor == 'vscode':
subprocess.run(['code', project_path])
elif editor == 'sublime':
subprocess.run(['subl', project_path])



Expand All @@ -164,7 +170,7 @@ def clean(project_path):

for item in folder_content:
if os.path.isdir(os.path.join(project_path, item)):
shutil.rmtree(os.path.join(project_path, item))
shutil.rmtree(os.path.join(project_path, item), ignore_errors=True)
logger.debug('del ./' + item)
elif os.path.isfile(os.path.join(project_path, item)):
os.remove(os.path.join(project_path, item))
Expand Down

0 comments on commit e81ab38

Please sign in to comment.