Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a color logging module #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 23 additions & 25 deletions arm_now/arm_now.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@
import contextlib
from pathlib import Path
from subprocess import check_call
from .logging import logger

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

from .utils import pgreen, pred, porange, maybe_you_meant, which
from .utils import maybe_you_meant, which
from .filesystem import Filesystem
from .config import Config, qemu_options, install_opkg
from . import options
Expand Down Expand Up @@ -122,7 +123,7 @@ def run_qemu(arch, kernel, dtb, rootfs, add_qemu_options):
dtb = "" if not os.path.exists(dtb) else "-dtb {}".format(dtb)
options = qemu_options[arch][1].format(arch=arch, kernel=kernel, rootfs=rootfs, dtb=dtb)
arch = qemu_options[arch][0]
print("Starting qemu-system-{}".format(arch))
logger.debug("Starting qemu-system-{}".format(arch))
qemu_config = "-serial stdio -monitor /dev/null {add_qemu_options}".format(add_qemu_options=add_qemu_options)
cmd = """stty intr ^]
export QEMU_AUDIO_DRV="none"
Expand All @@ -134,7 +135,7 @@ def run_qemu(arch, kernel, dtb, rootfs, add_qemu_options):
-no-reboot
stty intr ^c
""".format(arch=arch, qemu_config=qemu_config, options=options, dtb=dtb)
pgreen(cmd)
logger.info("{}".format(cmd))
os.system(cmd)


Expand All @@ -159,35 +160,31 @@ 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)
logger.error("I don't know this arch='{}' yet".format(arch))
logger.warning("maybe you meant: {}".format(maybe_you_meant(arch, qemu_options.keys()) or qemu_options.keys()))
sys.exit(1)
if is_already_created(arch):
porange("WARNING: {} already exists, use --clean to restart with a fresh filesystem".format(Config.DIR))
logger.warning("{} already exists, use --clean to restart with a fresh filesystem".format(Config.DIR))
return
with contextlib.suppress(FileExistsError):
os.mkdir(Config.DIR)
download_image(arch, dest=Config.DIR, real_source=real_source)
pgreen("[+] Installed")
logger.info("Installed")

# def do_install_real_source(arch, clean=False):
# """ download and setup filesystem and kernel
# """
# 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)
# logger.error("I don't know this arch='{}' yet".format(arch))
# logger.info("maybe you meant: {}".format(maybe_you_meant(arch, qemu_options.keys()) or qemu_options.keys()),)
# sys.exit(1)
# 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)
# logger.error("couldn't download files for this arch")
# sys.exit(1)
# if is_already_created(arch):
# porange("WARNING: {} already exists, use --clean to restart with a fresh filesystem".format(Config.DIR))
# logger.warning("{} already exists, use --clean to restart with a fresh filesystem".format(Config.DIR))
# return
# with contextlib.suppress(FileExistsError):
# os.mkdir(Config.DIR)
Expand All @@ -196,7 +193,7 @@ def do_install(arch, clean, real_source):
# download(rootfs, Config.ROOTFS, Config.DOWNLOAD_CACHE_DIR)
# with open(Config.DIR + "/arch", "w") as F:
# F.write(arch)
# pgreen("[+] Installed")
# logger.info("Installed")


def config_filesystem(rootfs, arch, real_source):
Expand Down Expand Up @@ -238,7 +235,7 @@ def check_dependencies_or_exit():
which("unzip", ubuntu="apt-get install unzip", arch="pacman -S unzip")
]
if not all(dependencies):
print("requirements missing, plz install them", file=sys.stderr)
logger.error("requirements missing, plz install them", file=sys.stderr)
sys.exit(1)


Expand All @@ -248,10 +245,11 @@ def check_dependencies_or_exit():
def convert_redir_to_qemu_args(redir):
for r in redir:
if not re_redir.match(r):
pred("ERROR: Invalid argument: --redir {}".format(r))
print("example:")
print("\tredirect tcp host 8000 to guest 80: --redir tcp:8000::80")
print("\tredirect udp host 4444 to guest 44: --redir udp:4444::44")
logger.error(("ERROR: Invalid argument: --redir {}\n"
"Example: \n"
"\tredirect tcp host 8000 to guest 80: --redir tcp:8000::80\n"
"\tredirect udp host 4444 to guest 44: --redir udp:4444::44\n"
).format(r))
sys.exit(1)
return ''.join(map("-redir {} ".format, redir))

Expand All @@ -272,7 +270,7 @@ def test_arch(arch):
arch = arch[:-1]
kernel, dtb, rootfs = scrawl_kernel(arch)
if kernel and rootfs:
print("{}: OK".format(arch))
logger.info("{}: OK".format(arch))


def do_list(all=False):
Expand All @@ -289,14 +287,14 @@ def do_list(all=False):

def do_show():
if not os.path.isfile(Config.ARCH) or not os.path.isfile(Config.ROOTFS):
pred("File missing")
logger.error("File missing")
return
with open(Config.ARCH) as F:
arch = F.read()
print(" Info ".center(80, "~"))
size = os.path.getsize(Config.ROOTFS)
pgreen("arch = {}".format(arch))
pgreen("rootfs size = {}M".format(size // (1024 * 1024)))
logger.info("arch = {}".format(arch))
logger.info("rootfs size = {}M".format(size // (1024 * 1024)))
Filesystem(Config.ROOTFS).ls("/root")
print("~" * 80)

Expand Down
10 changes: 5 additions & 5 deletions arm_now/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from exall import exall, ignore
from pySmartDL import SmartDL
from .config import Config
from .utils import pred
from .logging import logger


@exall(os.mkdir, FileExistsError, ignore)
Expand All @@ -23,10 +23,10 @@ def download(url, filename, cache_directory):
if os.path.exists(filename):
return
elif os.path.exists(filename_cache):
print("Already downloaded")
logger.info("Already downloaded")
shutil.copyfile(filename_cache, filename)
else:
print("\nDownloading {} from {}".format(filename, url))
logger.info("\nDownloading {} from {}".format(filename, url))
os.mkdir(cache_directory)
# wget.download(url, out=filename_cache)
obj = SmartDL(url, filename_cache)
Expand Down Expand Up @@ -103,7 +103,7 @@ def get_link_filetype(link):
return "dtb"
elif "Image" in link or "vmlinux" in link or "linux.bin" in link:
return "kernel"
print("ERROR: I don't know this kind of file {}".format(link), file=sys.stderr)
logger.error("I don't know this kind of file {}".format(link), file=sys.stderr)
# os.kill(0, 9)
return None

Expand All @@ -121,7 +121,7 @@ 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)
logger.error("Couldn't download files for this arch")
sys.exit(1)
download(kernel, dest + Config.kernel, Config.DOWNLOAD_CACHE_DIR)
if dtb:
Expand Down
99 changes: 29 additions & 70 deletions arm_now/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import magic
from exall import exall, print_error
from .utils import pred, porange, pgreen
from .config import Config
from .logging import logger


def Filesystem(path):
Expand All @@ -16,7 +16,7 @@ def Filesystem(path):
return Cpio(path, filemagic)
if "tar" in filemagic:
return Tar(path, filemagic)
pred("UnknownFileSystem {}".format(filemagic))
logger.error("UnknownFileSystem {}".format(filemagic))
sys.exit(1)


Expand All @@ -36,7 +36,7 @@ def get(self, src, dest):

def rm(self, filename):
def e2rm_warning(_exception):
porange("WARNING: e2rm file already suppressed")
logger.warning("e2rm file already suppressed")
with exall(subprocess.check_call, subprocess.CalledProcessError, e2rm_warning):
subprocess.check_call(["e2rm", self.rootfs + ":" + filename])

Expand All @@ -52,7 +52,7 @@ def sed(self, regex, path, right=444):
Insecure !! command injection here but regex is not exposed to user input
"""
with tempfile.TemporaryDirectory() as tempdir:
print("Tempdir {}".format(tempdir))
logger.info("Tempdir {}".format(tempdir))
new = tempdir + "/new"
old = tempdir + "/old"
self.get(path, old)
Expand All @@ -66,69 +66,28 @@ def resize(self, size):
subprocess.check_call(["e2fsck", "-fy", self.rootfs])
subprocess.check_call(["resize2fs", self.rootfs])
subprocess.check_call(["ls", "-lh", self.rootfs])
pgreen("[+] Resized to {size}".format(size=size))
logger.info("Resized to {size}".format(size=size))

@exall(subprocess.check_call, subprocess.CalledProcessError, print_error)
def correct(self):
porange("[+] Correcting ... (be patient)".format(size=size))
logger.info("Correcting ... (be patient)")
subprocess.check_call("mke2fs -F -b 1024 -m 0 -g 272".split() + [Config.ROOTFS])

def check(self):
try:
print(" Checking the filesystem ".center(80, "+"))
logger.info(" Checking the filesystem ".center(80, "+"))
subprocess.check_call(["e2fsck", "-vfy", self.rootfs])
except subprocess.CalledProcessError as e:
print(e)
logger.error("Exception: {}".format(e))
if str(e).find("returned non-zero exit status 1."):
porange("It's ok but next time poweroff")
logger.warning("It's ok but next time poweroff")

def ls(self, path):
ls_cmd = ["e2ls", self.rootfs + ":" + path]
print((" " + " ".join(ls_cmd) + " ").center(80, "~"))
subprocess.check_call(ls_cmd)


class Cpio:
def __init__(self, path):
self.rootfs = path

def implemented(self):
return False

def __call__(self, t):
pred("__CALL__ {}".format(t))

def put(self, src, dest, right=444):
porange("put is not implented for {}".format(self.rootfs))

def get(self, src, dest):
porange("get is not implented for {}".format(self.rootfs))

def rm(self, filename, on_error=print_error):
porange("rm is not implented for {}".format(self.rootfs))

def create(self, dest, content, right=444):
porange("create is not implented for {}".format(self.rootfs))

def sed(self, regex, path, right=444):
porange("sed is not implented for {}".format(self.rootfs))

@exall(subprocess.check_call, subprocess.CalledProcessError, print_error)
def resize(self, size):
subprocess.check_call(["qemu-img", "resize", self.rootfs, size])
subprocess.check_call(["ls", "-lh", self.rootfs])
pgreen("[+] Resized to {size}".format(size=size))

def correct(self, regex, path, right=444):
porange("correct is not implented for {}".format(self.rootfs))

def check(self):
porange("check is not implented for {}".format(self.rootfs))

def ls(self, path):
porange("ls is not implented for {}".format(self.rootfs))


class Cpio:
def __init__(self, path, filemagic):
self.rootfs = path
Expand All @@ -138,37 +97,37 @@ def implemented(self):
return False

def __call__(self, t):
pred("__CALL__ {}".format(t))
logger.error("__CALL__ {}".format(t))

def put(self, src, dest, right=444):
porange("put is not implented for {}".format(self.filemagic))
logger.warning("put is not implented for {}".format(self.filemagic))

def get(self, src, dest):
porange("get is not implented for {}".format(self.filemagic))
logger.warning("get is not implented for {}".format(self.filemagic))

def rm(self, filename, on_error=print_error):
porange("rm is not implented for {}".format(self.filemagic))
logger.warning("rm is not implented for {}".format(self.filemagic))

def create(self, dest, content, right=444):
porange("create is not implented for {}".format(self.filemagic))
logger.warning("create is not implented for {}".format(self.filemagic))

def sed(self, regex, path, right=444):
porange("sed is not implented for {}".format(self.filemagic))
logger.warning("sed is not implented for {}".format(self.filemagic))

@exall(subprocess.check_call, subprocess.CalledProcessError, print_error)
def resize(self, size):
subprocess.check_call(["qemu-img", "resize", self.rootfs, size])
subprocess.check_call(["ls", "-lh", self.rootfs])
pgreen("[+] Resized to {size}".format(size=size))
logger.info("Resized to {}".format(size))

def correct(self, regex, path, right=444):
porange("correct is not implented for {}".format(self.filemagic))
logger.warning("correct is not implented for {}".format(self.filemagic))

def check(self):
porange("check is not implented for {}".format(self.filemagic))
logger.warning("check is not implented for {}".format(self.filemagic))

def ls(self, path):
porange("ls is not implented for {}".format(self.filemagic))
logger.warning("ls is not implented for {}".format(self.filemagic))


class Tar:
Expand All @@ -180,34 +139,34 @@ def implemented(self):
return False

def __call__(self, t):
pred("__CALL__ {}".format(t))
logger.error("__CALL__ {}".format(t))

def put(self, src, dest, right=444):
porange("put is not implented for {}".format(self.filemagic))
logger.warning("put is not implented for {}".format(self.filemagic))

def get(self, src, dest):
porange("get is not implented for {}".format(self.filemagic))
logger.warning("get is not implented for {}".format(self.filemagic))

def rm(self, filename, on_error=print_error):
porange("rm is not implented for {}".format(self.filemagic))
logger.warning("rm is not implented for {}".format(self.filemagic))

def create(self, dest, content, right=444):
porange("create is not implented for {}".format(self.filemagic))
logger.warning("create is not implented for {}".format(self.filemagic))

def sed(self, regex, path, right=444):
porange("sed is not implented for {}".format(self.filemagic))
logger.warning("sed is not implented for {}".format(self.filemagic))

@exall(subprocess.check_call, subprocess.CalledProcessError, print_error)
def resize(self, size):
subprocess.check_call(["qemu-img", "resize", self.rootfs, size])
subprocess.check_call(["ls", "-lh", self.rootfs])
pgreen("[+] Resized to {size}".format(size=size))
logger.info("Resized to {size}".format(size=size))

def correct(self, regex, path, right=444):
porange("correct is not implented for {}".format(self.filemagic))
logger.warning("correct is not implented for {}".format(self.filemagic))

def check(self):
porange("check is not implented for {}".format(self.filemagic))
logger.warning("check is not implented for {}".format(self.filemagic))

def ls(self, path):
porange("ls is not implented for {}".format(self.filemagic))
logger.warning("ls is not implented for {}".format(self.filemagic))
8 changes: 8 additions & 0 deletions arm_now/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-

import logging
import coloredlogs


logger = logging.getLogger(name="arm_now")
coloredlogs.install(level='DEBUG', logger=logger, fmt="[%(levelname)s]: %(message)s")