-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.py
212 lines (183 loc) · 6.98 KB
/
common.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python3
"""
clipi:
Emulate, organize, burn, manage a variety of sbc distributions for Raspberry Pi
Written by Jess Sullivan
@ https://github.com/Jesssullivan/clipi
@ https://transscendsurvival.org/
"""
import os
import subprocess
import sys
from random import random
from sys import platform
from time import sleep
from zipfile import ZipFile
import requests
from shutil import which
import xmltodict
class common(object):
bin_url = "http://clipi-bins.s3.amazonaws.com/"
@staticmethod
def is_installed(cmd):
print('checking if ' + cmd + ' is present...')
if not which(cmd):
print("didn't find " + cmd)
return False
else:
return True
@classmethod
def dep_install(cls, dep, brew_dep=None):
if platform == "linux" or platform == "linux2":
print("environment: detected Linux, continuing with apt-get....")
subprocess.Popen('sudo apt-get install ' + dep + ' -y', shell=True).wait()
# todo: maybe prompt for other package manager options
if brew_dep is None:
brew_dep = dep
elif platform == 'darwin':
print("environment: detected osx, not completely tested yet, YMMV")
if cls.is_installed('brew'):
print('attempting brew install of ' + brew_dep + '...... \n')
subprocess.Popen('brew install' + brew_dep, shell=True).wait()
else:
print("brew package manager not detected, attempting to install brew now...")
brew_string = str(
'/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)')
subprocess.Popen(brew_string,
shell=True).wait()
@staticmethod
def ensure_dir(dirname='image'):
if not os.path.isdir(dirname):
os.mkdir(dirname)
@classmethod
def ensure_bins(cls):
if not os.path.isdir('bin'):
print('adding /bin....')
os.mkdir('bin')
sleep(.2)
print('fetching binary list from S3....')
index = requests.get(cls.bin_url).content
with open('bin/index.xml', 'wb') as f:
f.write(index)
sleep(.2)
print('parsing response....')
with open('bin/index.xml') as fd:
doc = xmltodict.parse(fd.read())
sleep(.2)
print('downloading....')
Keys = doc['ListBucketResult']['Contents']
for f in Keys:
item = f['Key']
cmd = str("wget -O bin/" + item + " " + cls.bin_url + item)
subprocess.Popen(cmd, shell=True).wait()
sleep(.2)
print('done.')
@classmethod
def main_install(cls):
if not cls.is_installed(cmd='wget'):
cls.dep_install(dep='wget')
if not cls.is_installed(cmd='qemu-system-arm'):
cls.dep_install(dep='qemu-system-arm')
if not cls.is_installed(cmd='qemu-system-aarch64'):
cls.dep_install(dep='qemu-system-aarch64')
if not cls.is_installed(cmd='dd'):
cls.dep_install(dep='dd')
if not cls.is_installed(cmd='nmap'):
cls.dep_install(dep='nmap')
if not cls.is_installed(cmd='p7zip'):
cls.dep_install(dep='p7zip')
# if not cls.is_installed(cmd='texinfo'):
# cls.dep_install(dep='texinfo')
# if not cls.is_installed(cmd='qemu-system-aarch64'):
# cls.dep_install(dep='qemu-system-aarch64')
@classmethod
def unzip(cls, input, output):
if input.split('.')[-1] == 'zip':
with ZipFile(input, 'r') as zip_ref:
zip_ref.extractall(output)
sleep(.1)
return 0
elif input.split('.')[-1] == '7z':
if not cls.is_installed(cmd='p7zip'):
print('installing p7zip to extract this image...')
cls.dep_install(dep='p7zip')
cls.dep_install(dep='p7zip-full')
sleep(.1)
print('attempting to extract image from 7z...')
cmd = str('7z e ' + input + ' -o' + output + ' -aoa')
subprocess.Popen(cmd, shell=True).wait()
sleep(.1)
return 0
elif 'gz' in input:
print('attempting to extract image from .gz...')
cmd = str('gunzip ' + input)
subprocess.Popen(cmd, shell=True).wait()
sleep(.1)
return 0
elif 'xz' in input:
print('attempting to extract image from .xz...')
cmd = str('unxz ' + input)
subprocess.Popen(cmd, shell=True).wait()
sleep(.1)
return 0
@staticmethod
def restart(execute=None):
clipi_path = os.path.abspath(__file__).split('<')[0]
print('...\n')
sleep(.1)
print('...\n')
sys.stdout.flush()
if execute is None:
cmd = 'python3 ' + clipi_path + 'clipi.py '
else:
cmd = 'python3 ' + clipi_path + 'clipi.py ' + str(execute)
print(cmd)
proc = subprocess.Popen(cmd, shell=True)
print('re-executed clipi! ' +
'\n - @ pid ' + str(proc.pid))
@classmethod
def cleanup(cls):
# removes as admin from shell to avoid a wonky super python user xD
subprocess.Popen('sudo rm -rf image .pi', shell=True).wait()
print()
for x in range(3):
print('...\n')
sleep(.1)
print('complete. \n\n')
"""
Network & bridging functions have not been implemented yet
"""
@staticmethod
def get_network_depends():
if platform == 'darwin':
print('cannot install network bridge depends on mac OSX')
return 0
else:
print('make sure /network is ready to install....')
subprocess.Popen('sudo chmod u+x network/apt_net_depends.sh', shell=True).wait()
print('installing.....')
subprocess.Popen('./network/apt_net_depends.sh', shell=True).wait()
sleep(.1)
print('done.')
@staticmethod
def new_mac():
oui_bits = [0x52, 0x54, 0x00]
for x in range(256):
mac = oui_bits + [
random.randint(0x00, 0xff),
random.randint(0x00, 0xff),
random.randint(0x00, 0xff)]
return ':'.join(["%02x" % x for x in mac])
@staticmethod
def check_bridge():
CLIPINET = "read CLIPINET <<< $(ip -o link | awk '$2 != " + '"lo:"' + " {print $2}')"
if platform == 'darwin':
print('bridge networking not available for mac OSX')
quit()
else:
print('checking bridge network.....')
subprocess.Popen(CLIPINET, shell=True).wait()
subprocess.Popen('sudo chmod u+x network/up_bridge.sh', shell=True).wait()
sleep(.1)
subprocess.Popen('sudo ./network/up_bridge.sh', shell=True)
sleep(.1)