pioman.py 1.71 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import argparse
import os


def getproj(args):
    hasproj = lambda d: any(x for x in os.listdir(d) if x.endswith(".ino")\
                                    or x.endswith(".pde"))
    dirs = [d for d in os.listdir(".") if os.path.isdir(d) and hasproj(d)]
    if dirs:
        return dirs[0]
    else:
        print('[Error] Unable to find Arduino IDE project directory here!\nIt must have a ".ino" or ".pde" file in it.')
        raise SystemExit(1)


def getbaud(args):
    args.project_dir = args.project_dir or getproj(args)
    ino_filename = [x for x in os.listdir(args.project_dir)
                      if x.endswith('ino') or x.endswith('pde')][0]
    for line in open(os.path.join(args.project_dir, ino_filename)):
        if line.strip().startswith('#define  BAUDRATE'):
            print(line.strip().split(' ')[-1])


def build(args):
    args.project_dir = args.project_dir or getproj(args)
    SCRIPT_BUILD =\
    """\
    mkdir -p {pio_dir}
    rm -rf {pio_dir}/src
    platformio init --project-dir {pio_dir} --board {board}
    rm -rf {pio_dir}/src/*
    cp -rf {project_dir}/* {pio_dir}/src/
    cd {pio_dir} && platformio run --environment {board} --target upload
    """.format(**vars(args))
    print(SCRIPT_BUILD)
    os.system(SCRIPT_BUILD)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='''\
    Utility for building Arduino IDE based projects with PlatformIO''')
    parser.add_argument('command', choices=['build', 'getproj', 'getbaud'])
    parser.add_argument('-p', '--project-dir', default=None)
    parser.add_argument('-d', '--pio-dir', default='.pio')
    parser.add_argument('-b', '--board', default='uno')
    args = parser.parse_args()
    globals()[args.command](args)