forked from eringr/pidock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pidock.py
executable file
·97 lines (85 loc) · 2.93 KB
/
pidock.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
# Copyright (C) 2020 Boulder Engineering Studio
# Author: Erin Hensel <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import sys
import argparse
import subprocess
_default_dev='/dev/mmcblk0'
_default_host='raspberrypi'
_default_passwd='raspberry'
_default_img='raspbian.img'
scripts = {
'extract': 'support/1-extract.sh',
'build': 'support/2-build.sh',
'compose': 'support/3-compose.sh',
'flash': 'support/4-flash.sh',
}
def run_script(script, args=[]):
command = ['/bin/bash', scripts[script]] + args
proc = subprocess.Popen(command)
proc.wait()
if proc.returncode == 0:
return True
else:
print('Encountered error {} running {}'.format(
proc.returncode,
script
))
return False
def main(args):
dev_prompt_actions = ['all', 'flash']
if args.action in dev_prompt_actions and not args.dev:
print(
'WARNING: This will overwrite the contents of {}'.format(
_default_dev
)
)
response = input('Proceed? [y/N]: ')
if response.lower() != 'y':
print('aborting')
return
device = args.dev if args.dev else _default_dev
host = args.host if args.host else _default_host
passwd = args.passwd if args.passwd else _default_passwd
img = args.img if args.img else _default_img
all_actions = [
('extract', lambda: run_script('extract', [img])),
('build', lambda: run_script('build', [passwd])),
('compose', lambda: run_script('compose', [host])),
('flash', lambda: run_script('flash', [device])),
]
actions = None
if args.action == 'all':
actions = all_actions
else:
actions = [a for a in all_actions if a[0] == args.action]
for action in actions:
print('Doing {}'.format(action[0]))
if not action[1]():
print('Failed {}'.format(action[0]))
break
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'action',
choices=['all', 'extract', 'build', 'compose', 'flash']
)
parser.add_argument('--host', type=str)
parser.add_argument('--dev', type=str)
parser.add_argument('--passwd', type=str)
parser.add_argument('--img', type=str)
args = parser.parse_args()
main(args)