Skip to content

Commit

Permalink
Cleaned python scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
fpoussin committed Apr 8, 2020
1 parent 5a124c7 commit f6bf7f5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 54 deletions.
2 changes: 1 addition & 1 deletion tools/mx2conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
__author__ = 'Fabien Poussin'
__version__ = '0.1'

from os.path import expanduser, sep, dirname, abspath
from os.path import dirname, abspath
from argparse import ArgumentParser
import re

Expand Down
8 changes: 2 additions & 6 deletions tools/update_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@
__version__ = '0.1'

import os
import re
from argparse import ArgumentParser
from traceback import print_exc
from shutil import copy


parser = ArgumentParser(description='Generate ChibiOS-Contrib config and Makefiles from ChibiOS')
parser.add_argument('-s', '--src', default='../../ChibiOS', type=str, help="ChibiOS folder")
parser.add_argument('-d', '--dst', default='..', type=str, help='ChibiOS-Contrib folder')

FOLDERS = ['testhal']

def makefile(lines):

def makefile(lines):
for l in range(len(lines)):

if 'CHIBIOS =' in lines[l]:
lines[l] = lines[l][:-1] + '/../ChibiOS\n'
lines.insert(l + 1, 'CHIBIOS_CONTRIB = $(CHIBIOS)/../ChibiOS-Contrib\n')
Expand All @@ -31,7 +29,6 @@ def makefile(lines):


def halconf(lines):

idx_end = lines.index('#endif /* HALCONF_H */\n')
lines.insert(idx_end - 1, '\n')
lines.insert(idx_end - 1, '#include "halconf_community.h"')
Expand All @@ -41,7 +38,6 @@ def halconf(lines):


def mcuconf(lines):

idx_end = lines.index('#endif /* MCUCONF_H */\n')
lines.insert(idx_end - 1, '\n')
lines.insert(idx_end - 1, '#include "mcuconf_community.h"')
Expand Down
103 changes: 56 additions & 47 deletions tools/upgrades/upgrade.py → tools/upgrade.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
__author__ = 'Fabien Poussin'
__version__ = '0.1'

import os
import argparse


parser = argparse.ArgumentParser(description='Project upgrade script for ChibiOS')
parser.add_argument('-p', '--path', dest='path', help='Where project files are located', required=True)
parser.add_argument('-c', '--chibios', dest='chpath', help='Where ChibiOS is located', required=True)
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true')
args = parser.parse_args()
verbose = False

verbose = args.verbose

if not os.path.exists(args.chpath):
print('Invalid Chibios path', args.chpath)
exit(1)

new_chconf = os.path.abspath('{}/os/rt/templates/chconf.h'.format(args.chpath))
new_nilconf = os.path.abspath('{}/os/nil/templates/chconf.h'.format(args.chpath))
new_halconf = os.path.abspath('{}/os/hal/templates/halconf.h'.format(args.chpath))

def find(name, path):
ret = []
Expand All @@ -44,7 +34,7 @@ def get_values(path):
line_num = 0
for l in f.readlines():
if l.startswith('#define') or continuation != None:
if continuation == None:
if continuation is None:
words = l.split(' ')
if len(words) > 2:
defname = words[1]
Expand All @@ -56,7 +46,7 @@ def get_values(path):
else:
values[continuation]['data'].append(l)
# multi line define
if l.rstrip().endswith('\\') and continuation == None:
if l.rstrip().endswith('\\') and continuation is None:
continuation = defname
# one line define
elif not l.rstrip().endswith('\\'):
Expand Down Expand Up @@ -116,35 +106,54 @@ def write_changes(path, source, values):
with open(path, 'w') as f:
f.write(''.join(file_lines))

new_chconf_values = get_values(new_chconf)
new_nilconf_values = get_values(new_nilconf)
new_halconf_values = get_values(new_halconf)

chconf_files = find('chconf.h', args.path)
halconf_files = find('halconf.h', args.path)

for c in chconf_files:
values = get_values(c)
if verbose:
print(c)
for k, v in values.items():
print(k, v)
if is_nil(c):
new_format = new_nilconf_values.copy()
set_values(values, new_format)
write_changes(c, new_nilconf, new_format)
else:
new_format = new_chconf_values.copy()

if __name__ == '__main__':

parser = argparse.ArgumentParser(description='Project upgrade script for ChibiOS')
parser.add_argument('-p', '--path', dest='path', help='Where project files are located', required=True)
parser.add_argument('-c', '--chibios', dest='chpath', help='Where ChibiOS is located', required=True)
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true')
args = parser.parse_args()

verbose = args.verbose

if not os.path.exists(args.chpath):
print('Invalid Chibios path', args.chpath)
exit(1)

new_chconf = os.path.abspath('{}/os/rt/templates/chconf.h'.format(args.chpath))
new_nilconf = os.path.abspath('{}/os/nil/templates/chconf.h'.format(args.chpath))
new_halconf = os.path.abspath('{}/os/hal/templates/halconf.h'.format(args.chpath))

new_chconf_values = get_values(new_chconf)
new_nilconf_values = get_values(new_nilconf)
new_halconf_values = get_values(new_halconf)

chconf_files = find('chconf.h', args.path)
halconf_files = find('halconf.h', args.path)

for c in chconf_files:
values = get_values(c)
if verbose:
print(c)
for k, v in values.items():
print(k, v)
if is_nil(c):
new_format = new_nilconf_values.copy()
set_values(values, new_format)
write_changes(c, new_nilconf, new_format)
else:
new_format = new_chconf_values.copy()
set_values(values, new_format)
write_changes(c, new_chconf, new_format)

for h in halconf_files:
values = get_values(h)
if verbose:
print(h)
for k, v in values.items():
print(k, v)
new_format = new_halconf_values.copy()

set_values(values, new_format)
write_changes(c, new_chconf, new_format)

for h in halconf_files:
values = get_values(h)
if verbose:
print(h)
for k, v in values.items():
print(k, v)
new_format = new_halconf_values.copy()

set_values(values, new_format)
write_changes(h, new_halconf, new_format)
write_changes(h, new_halconf, new_format)

0 comments on commit f6bf7f5

Please sign in to comment.