Exemple #1
0
def update_file(filename: str, url: str) -> bool:
    """Check and update file compares with remote_url

    Args:
        filename: str. Local filename, normally it's `__file__`
        url: str or urllib.request.Request object. Remote url of raw file content. Use urllib.request.Request object for headers.
    Returns:
        bool: file updated or not
    """
    def compare(s1, s2):
        return s1 == s2, len(s2) - len(s1)

    if not url or not filename:
        return False
    try:
        raw_codes = read_url(url)
        with open(filename, "rb") as f:
            current_codes = f.read().replace(b"\r", b"")
        is_same, diff = compare(current_codes, raw_codes)
        if is_same:
            cit.info("{} is already up-to-date.".format(filename))
            return False
        else:
            cit.ask("A new version is available. Update? (Diff: {})".format(diff))
            if cit.get_choice(["Yes", "No"]) == "Yes":
                with open(filename, "wb") as f:
                    f.write(raw_codes)
                cit.info("Update Success.")
                return True
            else:
                cit.warn("Update Canceled")
                return False
    except Exception as e:
        cit.err("{f} update failed: {e}".format(f=filename, e=e))
        return False
Exemple #2
0
def main():
    ktk.clearScreen()
    if not manage_file_exist():
        cit.err('No manage.py detected. Please run this under projects folder')
        cit.bye()
    while True:
        to_run = show_menu()
        to_run()
Exemple #3
0
def requirements_install():
    """Install necessary modules by pip & requirements.pip"""
    if not os.path.exists('./requirements.pip'):
        cit.err('No requirements.pip detected.')
        cit.bye()
    if 'win' in sys.platform:
        ktk.runCmd('pip3 install -r requirements.pip')
    else:
        ktk.runCmd('sudo pip3 install -r requirements.pip')
Exemple #4
0
def clear_screen():
    """Clear the console screen"""
    if sys.platform.startswith("win"):  # Windows
        os.system("cls")
    elif os.name == "posix":  # Linux and Unix
        os.system("clear")
    elif sys.platform == "darwin":  # macOS
        os.system("clear")
    else:
        cit.err("No clearScreen for " + sys.platform)
Exemple #5
0
 def clearScreen(cls):
     """Clear the screen"""
     if "win32" in sys.platform:
         os.system('cls')
     elif "linux" in sys.platform:
         os.system('clear')
     elif 'darwin' in sys.platform:
         os.system('clear')
     else:
         cit.err("No clearScreen for " + sys.platform)
Exemple #6
0
 def getPyCmd(cls):
     """get OS's python command"""
     if "win32" in sys.platform:
         return 'py'
     elif "linux" in sys.platform:
         return 'python3'
     elif 'darwin' in sys.platform:
         return 'python3'
     else:
         cit.err("No python3 command for " + sys.platform)
Exemple #7
0
def get_config_file(xml_file='./uwsgi.xml'):
    """check if uswgi config file exists"""
    dir_path = os.path.dirname(os.path.abspath(__file__))
    default_xml_file = "{}/uwsgi.xml".format(dir_path)
    if xml_file and os.path.isfile(xml_file):
        cit.info("uwsgi config file: " + xml_file)
        return xml_file
    elif os.path.isfile(default_xml_file):
        return get_config_file(default_xml_file)
    else:
        cit.err("uwsgi config file not found: " + xml_file)
        return None
Exemple #8
0
def get_operation():
    """start a new uwsgi, stop a running uwsgi, or reload the config and codes"""
    operations = ["*** update uwsgiTool ***", "start", "stop", "reload"]
    if len(sys.argv) != 2:
        return cit.get_choice(operations)
    elif sys.argv[1] in operations:
        selected = sys.argv[1]
        cit.info("Selected: {}".format(selected))
        return selected
    else:
        cit.err("Wrong Params: " + sys.argv[1])
        cit.bye()
Exemple #9
0
def requirements_install():
    """Install necessary modules by pip with requirements.pip

    Globals:
        PIP_REQUIREMENTS: the filename of requirements.pip
    """
    if not os.path.exists('./{}'.format(PIP_REQUIREMENTS)):
        cit.err('No {} detected.'.format(PIP_REQUIREMENTS))
        cit.bye()
    if 'win' in sys.platform:
        ktk.runCmd('pip3 install -r {}'.format(PIP_REQUIREMENTS))
    else:
        ktk.runCmd('sudo pip3 install -r {}'.format(PIP_REQUIREMENTS))
Exemple #10
0
def run_operation(oprtn, config_file, pid_file, log_file):
    if "start" == oprtn:
        if os.path.exists(pid_file):
            cit.ask('uwsgi is already running, start a new one? (Y/n)\n(Doing this will overwrite pid_file)')
            if cit.get_input().lower() != 'y':
                cit.info('User canceled start operation')
                return False
        ktk.runCmd("sudo uwsgi -x '{c}' --pidfile '{p}' --daemonize '{d}'".format(c=config_file, p=pid_file, d=log_file))
    elif "stop" == oprtn:
        ktk.runCmd("sudo uwsgi --stop " + pid_file)
        ktk.runCmd("sudo rm " + pid_file)
    elif "reload" == oprtn:
        ktk.runCmd("sudo uwsgi --reload " + pid_file)
    elif "*** update uwsgiTool ***" == oprtn:
        update_uwsgitool()
    else:
        cit.err("Wrong operation: " + oprtn)
        cit.bye()
def copy(from_path: str, to_path: str) -> bool:
    # deal from
    if not os.path.isabs(from_path):
        current_dir = os.path.dirname(os.path.realpath(__file__))
        from_path = os.path.join(current_dir, from_path)
    cit.info('From\t: {}'.format(from_path))
    if not os.path.isfile(from_path):
        cit.err("config file does not exists, copy cancelled")
        return False
    # deal to
    cit.info('To\t: {}'.format(to_path))
    if os.path.isfile(to_path):
        cit.err('target file exists, copy cancelled')
        return False
    cit.info('Copying file ...')
    shutil.copyfile(from_path, to_path)
    cit.info('Changing file owner ...')
    current_user = getpass.getuser()
    shutil.chown(to_path, user=current_user)
    return True
Exemple #12
0
def copy_my_file(config_name, to_):
    # deal from
    if not os.path.isabs(config_name):
        current_dir = os.path.dirname(os.path.realpath(__file__))
        from_ = os.path.join(current_dir, config_name)
    else:
        from_ = config_name
    cit.info('From\t: {}'.format(from_))
    if not os.path.isfile(from_):
        cit.err("config file does not exists, copy cancelled", lvl=1)
        cit.bye()
    # deal to
    cit.info('To\t: {}'.format(to_))
    if os.path.isfile(to_):
        cit.err('target file exists, copy cancelled', lvl=1)
        cit.bye()
    cit.info('Copying file ...')
    shutil.copyfile(from_, to_)
    cit.info('Changing file owner ...')
    current_user = getpass.getuser()
    shutil.chown(to_, user=current_user)
Exemple #13
0
    def updateFile(cls, file_, url):
        """Check and update file compares with remote_url

        Args:
            file_: str. Local filename. Normally it's __file__
            url: str. Remote url of raw file content. Normally it's https://raw.github.com/...
        Returns:
            bool: file updated or not
        """
        def compare(s1, s2):
            return s1 == s2, len(s2) - len(s1)

        if not url or not file_:
            return False
        try:
            req = urllib.request.urlopen(url)
            raw_codes = req.read()
            with open(file_, 'rb') as f:
                current_codes = f.read().replace(b'\r', b'')
            is_same, diff = compare(current_codes, raw_codes)
            if is_same:
                cit.info("{} is already up-to-date.".format(file_))
                return False
            else:
                cit.ask("A new version is available. Update? (Diff: {})".format(diff))
                if cit.get_choice(['Yes', 'No']) == 'Yes':
                    with open(file_, 'wb') as f:
                        f.write(raw_codes)
                    cit.info("Update Success.")
                    return True
                else:
                    cit.warn("Update Canceled")
                    return False
        except Exception as e:
            cit.err("{f} update failed: {e}".format(f=file_, e=e))
            return False
Exemple #14
0
    run_by_py3('manage.py check')


@register('i18n: Make Messages (.po)')
@cit.as_session
def make_messages():
    """Django i18n Make .po Messaages File"""
    run_by_py3('manage.py makemessages')


@register('i18n: Compile Messages (.mo)')
@cit.as_session
def compile_messages():
    """Django i18n Compile .po files into .mo files"""
    run_by_py3('manage.py compilemessages')


if __name__ == '__main__':
    cit.echo('Django Tool: version {}'.format(__version__))
    cit.br()
    if not manage_file_exist():
        cit.err('No manage.py detected. Please run this under projects folder')
        cit.bye()
    try:
        while True:
            to_run = show_menu()
            to_run()
    except KeyboardInterrupt:
        cit.info('Thanks for using. Bye bye!')
        cit.bye(0)
#!/usr/bin/env python3
#
# Auto Parse Dante Log
#
import re
import os
import consoleiotools as cit
import KyanToolKit
ktk = KyanToolKit.KyanToolKit()

log_path = '/var/log/dante.log'
backup_path = '/var/log/dante.log.backup'
if not os.path.exists(log_path):
    cit.err("Please enter a valid log path.")
    cit.bye()

# go through the log
clients = {}
with open(log_path) as f:
    for ln in f:
        pattern = re.compile(r': ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*45\.32\.45\.176\.10080')
        matches = pattern.findall(ln)
        if matches:
            key = matches[0]
            value = clients.setdefault(key, 0)
            clients[key] = value + 1

# print
clients_count = len(clients)
if clients_count == 0:
    cit.err("0 client logged").bye()
ktk.needPlatform('win')
ip_list = [
    '80.239.173.156',
    '80.239.173.151',
    '184.50.87.33',
    '184.51.198.91',
    '184.51.198.73',
    '213.248.126.138',
    '213.248.126.137',
    '213.248.126.155',
    '111.108.54.16',
    '52.76.139.242',
]
if not ip_list:
    cit.err('Ip List is empty').bye()
vpn_route = '10.100.0.1'  # duetime
cit.info('VPN route = {}'.format(vpn_route))

# get mode, delete / set / print
available_modes = ['set', 'delete', 'print']
cit.ask("Choose mode:")
mode = cit.get_choice(available_modes)
if mode not in available_modes:
    cit.err('Mode {} is not supported, available modes: {}'.format(mode, available_modes)).bye()
cit.info('Mode = {}'.format(mode))

if mode == 'set':
    for ip in ip_list:
        cmd = 'route add {ip} mask 255.255.255.255 {vpn_route}'.format(vpn_route=vpn_route, ip=ip)
        ktk.runCmd(cmd)
Exemple #17
0
 def test_err(self):
     with patch("sys.stdout", new=StringIO()) as fake_out:
         cit.err("ABC")
         self.assertEqual(fake_out.getvalue(), "| (Error) ABC\n")
Exemple #18
0
import sys

import consoleiotools as cit
import KyanToolKit
ktk = KyanToolKit.KyanToolKit()

# -Pre-conditions Check-------------------------------------------
ktk.needPlatform("linux")

# -set params-----------------------------------------------------
# config file
uwsgi_xml = "./uwsgi.xml"
if os.path.isfile(uwsgi_xml):
    cit.info("uwsgi config file: " + uwsgi_xml)
else:
    cit.err("uwsgi config file not found: " + uwsgi_xml)
# pid file
dir_name = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
pid_file = "/var/run/uwsgi_{}.pid".format(dir_name)
if os.path.exists(pid_file):
    cit.warn("uwsgi is running @ " + pid_file)
else:
    cit.info("No uwsgi running")
# choice
operations = ["start", "stop", "reload"]
oprtn = ""
if len(sys.argv) != 2:
    oprtn = cit.get_choice(operations)
elif sys.argv[1] in operations:
    oprtn = sys.argv[1]
else:
 def test_err(self):
     cit.err("ABC")
     self.assertEqual(self.fakeout.readline(ansi=False), "| (Error) ABC\n")