Skip to content

Commit

Permalink
Merge pull request #11 from SakiiR/fix_pep8
Browse files Browse the repository at this point in the history
Fixed PEP8 support
  • Loading branch information
nongiach authored Jun 25, 2018
2 parents 75e24c4 + ea241f9 commit 7251c24
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 53 deletions.
80 changes: 53 additions & 27 deletions arm_now/arm_now.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#  ================ Start Argument Parsing ==============================
# ================ Start Argument Parsing ==============================

"""arm_now.
Usage:
arm_now list [--all]
arm_now start [<arch>] [--clean] [-s|--sync] [--offline] [--autostart=<script>] [--add-qemu-options=<options>] [--real-source] [--redir=<port>]...
arm_now start [<arch>] [--clean] [-s|--sync] [--offline]
[--autostart=<script>] [--add-qemu-options=<options>]
[--real-source] [--redir=<port>]...
arm_now clean
arm_now resize <new_size> [--correct]
arm_now install [<arch>] [--clean] [--real-source]
Expand Down Expand Up @@ -47,20 +50,20 @@
import re
from multiprocessing import Pool
import contextlib
import re
from pathlib import Path
from subprocess import check_call

# Exall is an exception manager based on decorator/context/callback
# Exall is an exception manager based on decorator/context/callback
# Check it out: https://github.com/nongiach/exall
from exall import exall, ignore, print_warning, print_traceback, print_error
from exall import exall, ignore
from docopt import docopt

from .utils import *
from .utils import pgreen, pred, porange, maybe_you_meant, which
from .filesystem import Filesystem
from .config import Config, qemu_options, install_opkg
from . import options
from .download import download_image, scrawl_kernel, download, indexof_parse
from .download import download_image, scrawl_kernel, download, indexof_parse


def main():
""" Call the function according to the asked command
Expand All @@ -75,10 +78,10 @@ def main():
do_list(a["--all"])
elif a["start"]:
do_start(a["<arch>"] or "armv5-eabi",
a["--clean"], a["--sync"] or a["-s"],
a["--offline"], a["--redir"],
' '.join(a["--add-qemu-options"]),
a["--autostart"], a["--real-source"])
a["--clean"], a["--sync"] or a["-s"],
a["--offline"], a["--redir"],
' '.join(a["--add-qemu-options"]),
a["--autostart"], a["--real-source"])
elif a["clean"]:
options.clean(Config)
elif a["resize"]:
Expand All @@ -91,7 +94,7 @@ def main():
do_offline()


#  ================ End Argument Parsing ==============================
# ================ End Argument Parsing ==============================

def do_start(arch, clean, sync, offline, redir, add_qemu_options, autostart, real_source):
"""Setup and start a virtualmachine using qemu.
Expand All @@ -106,11 +109,14 @@ def do_start(arch, clean, sync, offline, redir, add_qemu_options, autostart, rea
do_install(arch, clean, real_source)
fs = Filesystem(Config.ROOTFS)
config_filesystem(Config.ROOTFS, arch, real_source)
if sync: options.sync_upload(Config.ROOTFS, src=".", dest="/root")
if sync:
options.sync_upload(Config.ROOTFS, src=".", dest="/root")
options.autostart(Config.ROOTFS, autostart)
run_qemu(arch, Config.KERNEL, Config.DTB, Config.ROOTFS, add_qemu_options)
fs.check()
if sync: options.sync_download(Config.ROOTFS, "/root.tar", ".")
if sync:
options.sync_download(Config.ROOTFS, "/root.tar", ".")


def run_qemu(arch, kernel, dtb, rootfs, add_qemu_options):
dtb = "" if not os.path.exists(dtb) else "-dtb {}".format(dtb)
Expand All @@ -131,6 +137,7 @@ def run_qemu(arch, kernel, dtb, rootfs, add_qemu_options):
pgreen(cmd)
os.system(cmd)


def is_already_created(arch):
""" if the current kernel and rootfs is not the same arch then delete them """
if not os.path.exists(Config.DIR + "/arch"):
Expand All @@ -145,18 +152,23 @@ def is_already_created(arch):
options.clean(Config)
return False


def do_install(arch, clean, real_source):
""" download and setup filesystem and kernel
"""
if clean: options.clean(Config)
if clean:
options.clean(Config)
if arch not in qemu_options:
pred("ERROR: I don't know this arch='{}' yet".format(arch), file=sys.stderr)
porange("maybe you meant: {}".format(maybe_you_meant(arch, qemu_options.keys()) or qemu_options.keys()), file=sys.stderr)
porange("maybe you meant: {}".format(maybe_you_meant(arch,
qemu_options.keys()
) or qemu_options.keys()), file=sys.stderr)
sys.exit(1)
if is_already_created(arch):
porange("WARNING: {} already exists, use --clean to restart with a fresh filesystem".format(Config.DIR))
return
with contextlib.suppress(FileExistsError): os.mkdir(Config.DIR)
with contextlib.suppress(FileExistsError):
os.mkdir(Config.DIR)
download_image(arch, dest=Config.DIR, real_source=real_source)
pgreen("[+] Installed")

Expand All @@ -166,7 +178,9 @@ def do_install(arch, clean, real_source):
# if clean: options.clean(Config)
# if arch not in qemu_options:
# pred("ERROR: I don't know this arch='{}' yet".format(arch), file=sys.stderr)
# porange("maybe you meant: {}".format(maybe_you_meant(arch, qemu_options.keys()) or qemu_options.keys()), file=sys.stderr)
# porange("maybe you meant: {}".format(maybe_you_meant(arch,
# qemu_options.keys()
# ) or qemu_options.keys()), file=sys.stderr)
# sys.exit(1)
# kernel, dtb, rootfs = scrawl_kernel(arch)
# if kernel is None or rootfs is None:
Expand All @@ -184,17 +198,18 @@ def do_install(arch, clean, real_source):
# F.write(arch)
# pgreen("[+] Installed")


def config_filesystem(rootfs, arch, real_source):
fs = Filesystem(rootfs)
fs.rm('/etc/init.d/S40network')
fs.rm('/etc/init.d/S90tests')
fs.rm('/etc/issue')
fs.create("/etc/issue",
'Welcome to arm_now\n')
fs.create("/etc/issue",
"Welcome to arm_now\n")
fs.create("/etc/init.d/S95_how_to_kill_qemu",
'echo -e "\033[0;31mpress ctrl+] to kill qemu\033[0m"\n',
right=555)
fs.create("/etc/init.d/S40_network","""
'echo -e "\033[0;31mpress ctrl+] to kill qemu\033[0m"\n',
right=555)
fs.create("/etc/init.d/S40_network", """
IFACE=$(ip a | grep -o ':.*: ' | grep -v ': lo: ' | grep -o '[^ :@]*' | head -n 1)
ifconfig "$IFACE" 10.0.2.15
route add default gw 10.0.2.2
Expand All @@ -212,19 +227,25 @@ def config_filesystem(rootfs, arch, real_source):
""", right=555)
fs.sed('s/init.d\/S/init.d\/K/g', '/etc/init.d/rcK', right=755)


def check_dependencies_or_exit():
dependencies = [
which("e2cp", ubuntu="apt-get install e2tools", arch="yaourt -S e2tools"),
which("qemu-system-arm", ubuntu="apt-get install qemu", kali="apt-get install qemu-system", arch="pacman -S qemu-arch-extra"),
which("qemu-system-arm",
ubuntu="apt-get install qemu",
kali="apt-get install qemu-system",
arch="pacman -S qemu-arch-extra"),
which("unzip", ubuntu="apt-get install unzip", arch="pacman -S unzip")
]
if not all(dependencies):
print("requirements missing, plz install them", file=sys.stderr)
sys.exit(1)


re_redir = re.compile(r"(tcp|udp):\d+::\d+")


def convert_redir_to_qemu_args(redir):
qemu_redir = []
for r in redir:
if not re_redir.match(r):
pred("ERROR: Invalid argument: --redir {}".format(r))
Expand All @@ -246,12 +267,14 @@ def do_resize(size, correct):
if correct:
fs.correct()


def test_arch(arch):
arch = arch[:-1]
kernel, dtb, rootfs = scrawl_kernel(arch)
if kernel and rootfs:
print("{}: OK".format(arch))


def do_list(all=False):
""" List all compactible cpu architecture
"""
Expand All @@ -261,7 +284,8 @@ def do_list(all=False):
url = "https://toolchains.bootlin.com/downloads/releases/toolchains/"
all_arch = indexof_parse(url)
p = Pool(10)
ret = p.map(test_arch, all_arch)
p.map(test_arch, all_arch)


def do_show():
if not os.path.isfile(Config.ARCH) or not os.path.isfile(Config.ROOTFS):
Expand All @@ -272,10 +296,11 @@ def do_show():
print(" Info ".center(80, "~"))
size = os.path.getsize(Config.ROOTFS)
pgreen("arch = {}".format(arch))
pgreen("rootfs size = {}M".format(size // (1024 * 1024) ))
pgreen("rootfs size = {}M".format(size // (1024 * 1024)))
Filesystem(Config.ROOTFS).ls("/root")
print("~" * 80)


@exall(os.makedirs, FileExistsError, ignore)
def do_offline():
URL = "https://github.com/nongiach/arm_now_templates/archive/master.zip"
Expand All @@ -289,5 +314,6 @@ def do_offline():
check_call("mv arm_now_templates-master/* .", shell=True)
check_call("rm -rf arm_now_templates-master/ README.md master.zip", shell=True)


if __name__ == "__main__":
main()
6 changes: 3 additions & 3 deletions arm_now/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Config:
DOWNLOAD_CACHE_DIR = "/tmp/arm_now"

qemu_options = {
"aarch64": ["aarch64", "-M virt -cpu cortex-a57 -smp 1 -kernel {kernel} -append 'root=/dev/vda console=ttyAMA0' -netdev user,id=eth0 -device virtio-net-device,netdev=eth0 -drive file={rootfs},if=none,format=raw,id=hd0 -device virtio-blk-device,drive=hd0"],
"aarch64": ["aarch64", "-M virt -cpu cortex-a57 -smp 1 -kernel {kernel} -append 'root=/dev/vda console=ttyAMA0' -netdev user,id=eth0 -device virtio-net-device,netdev=eth0 -drive file={rootfs},if=none,format=raw,id=hd0 -device virtio-blk-device,drive=hd0"],
"armv5-eabi": ["arm", "-M vexpress-a9 -kernel {kernel} -sd {rootfs} -append 'root=/dev/mmcblk0 console=ttyAMA0 rw physmap.enabled=0 noapic'"], # check log
"armv6-eabihf": ["arm", "-M vexpress-a9 -kernel {kernel} -sd {rootfs} -append 'root=/dev/mmcblk0 console=ttyAMA0 rw physmap.enabled=0 noapic'"], # check log
"armv7-eabihf": ["arm", "-M vexpress-a9 -kernel {kernel} -sd {rootfs} -append 'root=/dev/mmcblk0 console=ttyAMA0 rw physmap.enabled=0 noapic'"], # check log
Expand All @@ -22,7 +22,7 @@ class Config:
"microblazeel": ["microblazeel", "-kernel {kernel} -hda {rootfs} -append 'root=/dev/sda console=tty0 rw physmap.enabled=0 noapic'"], # check log
"mips32": ["mips", "-kernel {kernel} -hda {rootfs} -append 'root=/dev/hda console=ttyS0 rw physmap.enabled=0 noapic'"], # check log
"mips32el": ["mipsel", "-kernel {kernel} -hda {rootfs} -append 'root=/dev/hda console=ttyS0 rw physmap.enabled=0 noapic'"], # check log
"mips32r5el": ["mipsel", "-machine malta -cpu P5600 -kernel {kernel} -drive file={rootfs},format=raw -append 'root=/dev/hda rw'"],
"mips32r5el": ["mipsel", "-machine malta -cpu P5600 -kernel {kernel} -drive file={rootfs},format=raw -append 'root=/dev/hda rw'"],
"mips32r6el": ["mipsel", "-M malta -cpu mips32r6-generic -kernel {kernel} -drive file={rootfs},format=raw -append root=/dev/hda -net nic,model=pcnet -net user"],
"mips64-n32": ["mips64", "-kernel {kernel} -hda {rootfs} -append 'root=/dev/hda console=ttyS0 rw physmap.enabled=0 noapic'"], # check log
"mips64el-n32": ["mips64el", "-kernel {kernel} -hda {rootfs} -append 'root=/dev/hda console=ttyS0 rw physmap.enabled=0 noapic'"], # check log
Expand All @@ -40,7 +40,7 @@ class Config:
# "x86-64-core-i7":["x86_64", "-kernel {kernel} -hda {rootfs} -append 'root=/dev/sda console=ttyS0 rw physmap.enabled=0 noapic'"], # old
"x86-64-core-i7" : ["x86_64", "-M pc -kernel {kernel} -drive file={rootfs},if=virtio,format=raw -append 'root=/dev/vda rw console=ttyS0' -net nic,model=virtio -net user"],
# "x86-core2" : ["i386", "-kernel {kernel} -hda {rootfs} -append 'root=/dev/sda console=ttyS0 rw physmap.enabled=0 noapic -net nic,model=virtio -net user'"],
"x86-core2": ["i386", "-M pc -kernel {kernel} -drive file={rootfs},if=virtio,format=raw -append 'root=/dev/vda rw console=ttyS0' -net nic,model=virtio -net user"], # fix opkg
"x86-core2": ["i386", "-M pc -kernel {kernel} -drive file={rootfs},if=virtio,format=raw -append 'root=/dev/vda rw console=ttyS0' -net nic,model=virtio -net user"], # fix opkg
"x86-i686":["i386", "-M pc -kernel {kernel} -drive file={rootfs},if=virtio,format=raw -append 'root=/dev/vda rw console=ttyS0' -net nic,model=virtio -net user"],
"xtensa-lx60": ["xtensa", "-M lx60 -cpu dc233c -monitor null -nographic -kernel {kernel} -monitor null"]
}
Expand Down
24 changes: 18 additions & 6 deletions arm_now/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
from collections import defaultdict
from pathlib import Path

# Exall is an exception manager based on decorator/context/callback
# Exall is an exception manager based on decorator/context/callback
# Check it out: https://github.com/nongiach/exall
from exall import exall, ignore, print_warning, print_traceback, print_error
from exall import exall, ignore
from pySmartDL import SmartDL
from .config import Config
from .utils import pred


@exall(os.mkdir, FileExistsError, ignore)
def download(url, filename, cache_directory):
filename_cache = url.split('/')[-1]
filename_cache = ''.join([ c for c in filename_cache if c.isdigit() or c.isalpha() ])
filename_cache = ''.join([c for c in filename_cache if c.isdigit() or c.isalpha()])
filename_cache = cache_directory + "/" + filename_cache
if os.path.exists(filename):
return
Expand All @@ -31,13 +33,14 @@ def download(url, filename, cache_directory):
obj.start()
shutil.copyfile(filename_cache, filename)


def scrawl_kernel(arch):
re_href = re.compile('href="?({arch}[^ <>"]*)"?'.format(arch=arch))
url = "https://toolchains.bootlin.com/downloads/releases/toolchains/{arch}/test-system/".format(arch=arch)
response = requests.get(url + "?C=M;O=D")
text = response.text
links = re_href.findall(text)
links_dict = defaultdict(lambda : defaultdict(dict))
links_dict = defaultdict(lambda: defaultdict(dict))
for link in links:
version = get_link_version(link)
libc = get_link_libc(link)
Expand All @@ -64,29 +67,35 @@ def scrawl_kernel(arch):
dtb = target.get("dtb", None)
rootfs = target.get("rootfs", None)
kernel = target.get("kernel", None)

return kernel, dtb, rootfs


def indexof_parse(url):
re_href = re.compile('\[DIR\].*href="?([^ <>"]*)"?')
response = requests.get(url)
text = response.text
links = re_href.findall(text)
return links


libc = ["uclibc", "glibc", "musl"]


def get_link_libc(link):
for i_libc in libc:
if i_libc in link:
return i_libc
return None


def get_link_version(link):
if "bleeding-edge" in link:
return "bleeding-edge"
else:
return "stable"


def get_link_filetype(link):
if ".cpio" in link or ".ext2" in link or "rootfs" in link:
return "rootfs"
Expand All @@ -98,6 +107,7 @@ def get_link_filetype(link):
# os.kill(0, 9)
return None


@exall(os.mkdir, FileExistsError, ignore)
def download_from_github(arch):
templates = str(Path.home()) + "/.config/arm_now/templates/"
Expand All @@ -106,14 +116,16 @@ def download_from_github(arch):
URL = "https://github.com/nongiach/arm_now_templates/raw/master/"
download(URL + filename, templates + filename, Config.DOWNLOAD_CACHE_DIR)


def download_image(arch, *, dest, real_source):
if real_source:
kernel, dtb, rootfs = scrawl_kernel(arch)
if kernel is None or rootfs is None:
pred("ERROR: couldn't download files for this arch", file=sys.stderr)
sys.exit(1)
download(kernel, dest + Config.kernel, Config.DOWNLOAD_CACHE_DIR)
if dtb: download(dtb, dest + Config.dtb, Config.DOWNLOAD_CACHE_DIR)
if dtb:
download(dtb, dest + Config.dtb, Config.DOWNLOAD_CACHE_DIR)
download(rootfs, dest + Config.rootfs, Config.DOWNLOAD_CACHE_DIR)
else:
download_from_github(arch)
Expand Down
Loading

0 comments on commit 7251c24

Please sign in to comment.