def __init__(self): from auto_everything.io import IO from auto_everything.base import OS from auto_everything.terminal import Terminal from auto_everything.disk import Disk self._io = IO() self._os = OS() self._t = Terminal() self._disk = Disk()
def __init__(self, username="******"): self.__username = username if os.getuid() != 0: print("\n I only got my super power if you run me with sudo!") exit() if not os.path.exists("/home/{username}".format(username=self.__username)) and self.__username != 'root': print("\n You just give me a wroung username!") exit() self._io = IO() self._t = Terminal()
def __init__(self): self._t = Terminal() self.file_modification_dict = {}
class OS(): """ This is for system stuff """ def __init__(self): self._io = IO() self._t = Terminal() def list_python_packages(self): self._io.make_sure_sudo_permission() installed_packages = self._t.run_command("pip3 list").lower() return installed_packages def install_python_package(self, package_name, force=False): self._io.make_sure_sudo_permission() package_name = package_name.strip(" \n").replace('_', '-').lower() installed_packages = self.list_python_packages() if (force is True) or (package_name not in installed_packages): self._t.run( "sudo pip3 install {name} --upgrade".format(name=package_name)) def uninstall_python_package(self, package_name, force=False): self._io.make_sure_sudo_permission() package_name = package_name.strip(" \n").replace('_', '-').lower() installed_packages = self.list_python_packages() if (force is True) or (package_name in installed_packages): self._t.run( "sudo pip3 uninstall {name} -y".format(name=package_name)) def list_packages(self): """ Return a list of apt packages which installed in your computer """ self._io.make_sure_sudo_permission() installed_packages = self._t.run_command("apt list").lower() return installed_packages def install_package(self, package_name, force=False): """ Parameters ---------- package_name: string the package you want to install """ self._io.make_sure_sudo_permission() package_name = package_name.strip(" \n").replace('_', '-').lower() installed_packages = self.list_packages() if (force is True) or (package_name not in installed_packages): self._t.run( "sudo apt install {name} -y --upgrade".format(name=package_name)) def uninstall_package(self, package_name, force=False): """ Parameters ---------- package_name: string the package you want to uninstall """ self._io.make_sure_sudo_permission() package_name = package_name.strip(" \n").replace('_', '-').lower() installed_packages = self.list_packages() if (force is True) or (package_name in installed_packages): self._t.run("sudo apt purge {name} -y".format(name=package_name))
def __init__(self): self._io = IO() self._t = Terminal()
class Super(): """ This is for sudo operations in linux """ def __init__(self, username="******"): self.__username = username if os.getuid() != 0: print("\n I only got my super power if you run me with sudo!") exit() if not os.path.exists("/home/{username}".format(username=self.__username)) and self.__username != 'root': print("\n You just give me a wroung username!") exit() self._io = IO() self._t = Terminal() def __get_service_config(self, py_file_path): working_dir = os.path.dirname(py_file_path) display_number = self._t.run_command("who") if "not found" in display_number: display = "" else: display_number = display_number.split("(")[-1].split(")")[0] display = "Environment=DISPLAY=" + display_number if self.__username == "root": display = "" content = """ [Unit] Description=auto_everything deamon After=re-local.service [Service] Type=simple User={username} WorkingDirectory={working_dir} {display} ExecStart=/usr/bin/python3 {py_file_path} Restart=always RestartSec=5 StartLimitBurst=100000 StartLimitInterval=1s [Install] WantedBy=multi-user.target """.format(username=self.__username, working_dir=working_dir, py_file_path=py_file_path, display=display) return content def start_service(self, name, py_file_path=None): """ start or create a linux service after this, the py_file will keep running as long as the computer is running Parameters ---------- name: string service name py_file_path: string a path leads to a python script """ service_path = "/etc/systemd/system/{name}.service".format(name=name) reload_command = "systemctl daemon-reload\n" enable_command = "systemctl enable {name}\n".format(name=name) restart_command = "systemctl restart {name}\n".format(name=name) # stop_command = "systemctl stop {name}\n".format(name=name) cheack_command = "systemctl status {name} | cat\n".format(name=name) if py_file_path is None: if not self._t.exists(service_path): print( "You have to give me a python script path, so I can help you run it!") exit() else: self._t.run(restart_command) else: py_file_path = self._t.fix_path( py_file_path, username=self.__username) py_file_path = os.path.abspath(py_file_path) if not self._t.exists(service_path): config = self.__get_service_config(py_file_path) self._io.write(service_path, config) self._t.run(reload_command) self._t.run(enable_command) self._t.run(restart_command) print(config) print('\n' + '-' * 11 + '\n') else: old_config = self._io.read(service_path) new_config = self.__get_service_config(py_file_path) if old_config != new_config: self._io.write(service_path, new_config) self._t.run(reload_command) self._t.run(enable_command) self._t.run(restart_command) print(new_config) print('\n' + '-' * 11 + '\n') else: self._t.run(restart_command) time.sleep(1) print("\n".join(self._t.run_command( cheack_command).split("\n")[:6])) def stop_service(self, name): """ stop or cancel a linux service after this, the py_file will stop running Parameters ---------- name: string service name """ service_path = "/etc/systemd/system/{name}.service".format(name=name) stop_command = "systemctl stop {name}\n".format(name=name) disable_command = "systemctl disable {name}\n".format(name=name) cheack_command = "systemctl status {name} | cat\n".format(name=name) if not self._t.exists(service_path): print("You even haven't starting a service yet!") exit() else: self._t.run(stop_command) self._t.run(disable_command) time.sleep(1) print("\n".join(self._t.run_command( cheack_command).split("\n")[:6])) self._t.run_command('sudo rm {}'.format(service_path)) def service(self, name, py_file_path): """ start or stop service after start, the python script will running forever Parameters ---------- name: string service name py_file_path: string a path leads to a python script """ service_path = "/etc/systemd/system/{name}.service".format(name=name) cheack_command = "systemctl status {name} | cat\n".format(name=name) if not self._t.exists(service_path): self.start_service(name, py_file_path) else: status = ": inactive" in "\n".join(self._t.run_command( cheack_command).split("\n")[:5]) if status: self.start_service(name) else: self.stop_service(name)
def service(self, name, py_file_path): """ start or stop service after start, the python script will running forever Parameters ---------- name: string service name py_file_path: string a path leads to a python script """ service_path = "/etc/systemd/system/{name}.service".format(name=name) cheack_command = "systemctl status {name} | cat\n".format(name=name) if not self._t.exists(service_path): self.start_service(name, py_file_path) else: status = ": inactive" in "\n".join(self._t.run_command( cheack_command).split("\n")[:5]) if status: self.start_service(name) else: self.stop_service(name) if __name__ == "__main__": t = Terminal()
hasGoogletrans = False try: from googletrans import Translator translator = Translator() hasGoogletrans = True except Exception as e: print(e) text = "sudo pip3 install googletrans==3.1.0a0" print(f"Please install googletrans it by using: \n\n{text}") print() print(f"After that, you probobly need proxychains: \n\n sudo apt install proxychains") print() print("sudo vim /etc/proxychains.conf") py = Python() t = Terminal(debug=True) class Tools(): def push(self, comment): if "/CS/" in t.run_command("pwd") or "/Business/" in t.run_command("pwd"): t.run(f""" git config --global user.name "yingshaoxo" git config --global user.email "*****@*****.**" git add . git commit -m "{comment}" git push origin """) else: if hasGoogletrans: try:
from auto_everything.web import Selenium from time import sleep from auto_everything.terminal import Terminal t = Terminal() t.kill("chrome") my_selenium = Selenium("https://www.google.com", headless=False, user_data_dir="/home/yingshaoxo/.config/google-chrome/") d = my_selenium.driver # get input box xpath = '/html/body/div[1]/div[3]/form/div[2]/div[1]/div[1]/div/div[2]/input' elements = my_selenium.wait_until_exists(xpath) # text inputing elements[0].send_keys('\b' * 20, "yingshaoxo") # click search button elements = my_selenium.wait_until_exists('//input[@value="Google Search"]') if len(elements): elements[0].click( ) # d.execute_script("arguments[0].click();", elements[0]) # exit sleep(30) d.quit()
class Python(): """ Python model was intended to simplify python development """ def __init__(self): from auto_everything.io import IO from auto_everything.base import OS from auto_everything.terminal import Terminal from auto_everything.disk import Disk self._io = IO() self._os = OS() self._t = Terminal() self._disk = Disk() def list_python_packages(self): """ Return a list of python packages which installed in your computer """ return self._os.list_python_packages() def install_package(self, package_name): """ Parameters ---------- package_name: string the python package you want to install """ self._os.install_python_package(package_name) def uninstall_package(self, package_name): """ Parameters ---------- package_name: string the python package you want to uninstall """ self._os.uninstall_python_package(package_name) class loop(): def __init__(self, interval=1, thread=False): """ new_thread: do you want to open a new thread? True/False """ self.thread = thread self.interval = interval def __call__(self, func): """ func: a function which you want to run forever """ def new_function(*args, **kwargs): def while_function(): while 1: try: func(*args, **kwargs) time.sleep(self.interval) except Exception as e: print(e) if self.thread is False: while_function() else: threading.Thread(target=while_function).start() return new_function def help(self, object_): """ get help information about class or function """ from inspect import signature if callable(object_): arguments = str(signature(object_)) print(object_.__name__ + arguments) doc = object_.__doc__ if doc: print(doc, '\n') else: methods = dir(object_) private_methods = [] public_methods = [] for method in methods: if method[:1] == "_": private_methods.append(method) else: public_methods.append(method) print(private_methods, '\n') pprint(public_methods) """ callable_list = inspect.getmembers(object_, predicate=inspect.ismethod) + inspect.getmembers(object_, predicate=inspect.isclass) callable_list = [(one, one[1].__name__ + str(signature(one[1]))) for one in callable_list] not_callable_list = dir(object_) for one in callable_list: if one[0] in not_callable_list: not_callable_list.remove(one[0]) not_callable_list = [(one, "") for one in not_callable_list] private_methods = [] public_methods = [] for one in not_callable_list + callable_list: if one[0][:1] == "_": private_methods.append(one[0]) else: public_methods.append(one) print(private_methods, "\n") [print(one[0], one[1]) for one in public_methods] """ def fire(self, class_name): """ fire is a function that will turn any Python class into a command line interface """ from fire import Fire Fire(class_name) def make_it_runnable(self, py_file_path=None): """ make python file runnable after use this function, you can run the py_file by: ./your_py_script_name.py """ if py_file_path is None or self._t.exists(py_file_path): py_file_path = os.path.join(self._t.current_dir, sys.argv[0].strip('./')) if os.path.exists(py_file_path): codes = self._io.read(py_file_path) expected_first_line = '#!/usr/bin/env {}'.format( self._t.py_executable) if codes.split('\n')[0] != expected_first_line: codes = expected_first_line + '\n' + codes self._io.write(py_file_path, codes) self._t.run_command('chmod +x {}'.format(py_file_path)) if not self._disk.executable(py_file_path): self._t.run_command('chmod +x {}'.format(py_file_path)) def make_it_global_runnable(self, py_file_path=None, executable_name=None): """ make python file global runnable after use this function, you can run the py_file at anywhere by: your_py_script_name.py """ self.make_it_runnable(py_file_path) auto_everything_config_folder = "~/.auto_everything" bin_folder = os.path.expanduser( os.path.join(auto_everything_config_folder, "bin")) if not self._t.exists(bin_folder): self._t.run_command(f"mkdir -p {bin_folder}") if py_file_path is None or not self._t.exists(py_file_path): py_file_path = os.path.join(self._t.current_dir, sys.argv[0].strip('./')) is_the_first_running = False runnable_path = None if os.path.exists(py_file_path): if executable_name == None: _, executable_name = os.path.split(py_file_path) runnable_path = os.path.join(bin_folder, executable_name) if not os.path.exists(runnable_path): is_the_first_running = True # remove links that the real file has been moved, or, remove links that match this py_file_path but with a different executable name files = os.listdir(bin_folder) [ self._t.run(f"cd {bin_folder}; rm {file}") for file in files if not os.path.exists(os.path.join(bin_folder, file)) ] files = self._disk.get_files(bin_folder, recursive=False) [ self._t.run(f"rm {file}") for file in files if os.path.realpath(file) == py_file_path and file != runnable_path ] self._t.run_command(f"ln -s {py_file_path} {runnable_path}") bashrc_path = self._t.fix_path(f"~/.bashrc") bashrc_target_line = f'export PATH="$PATH:{bin_folder}"' bashrc = self._io.read(bashrc_path) if bashrc_target_line not in bashrc.split("\n"): bashrc = bashrc + "\n" + bashrc_target_line self._t.run_command(f"touch {bashrc_path}") self._io.write(bashrc_path, bashrc) if is_the_first_running and runnable_path: print( f"\n\n------------------\n\nYou could run \n\n{runnable_path.split('/')[-1]} -- --completion\n\nto get bash completion scripts" ) def print(self, data, limit=20): """ print `function help info` or print `dict` with length limit (So you could see the structure easily) """ if callable(data): self.help(data) else: data = copy.deepcopy(data) def infinite_loop(the_data): the_type = type(the_data) if the_type == str: return the_data[:limit] + "..." elif the_type == dict: for key, value in the_data.items(): the_data[key] = infinite_loop(value) return the_data elif the_type == list: return [infinite_loop(element) for element in the_data] else: return the_data data = infinite_loop(data) pprint(data)
#!/usr/bin/env /usr/bin/python3 from auto_everything.terminal import Terminal from auto_everything.disk import Disk from auto_everything.base import IO from auto_everything.base import Python import json t = Terminal() disk = Disk() io = IO() py = Python() def seperate(): print() print("-------") print() DEVICE = "/dev/ttyUSB0" PRE_COMMAND = f"ampy -p {DEVICE} " LS = PRE_COMMAND + "ls" DELETE = PRE_COMMAND + "rm " PUT = PRE_COMMAND + "put " micropython_files = [ name.strip("/") for name in t.run_command(LS).split("\n") if name.strip() != "" and name[-3:] == ".py" ] print(micropython_files) if len(micropython_files) == 0: exit()
metavar='FILENAME', help='audio file to store recording to') parser.add_argument('-m', '--model', type=str, metavar='MODEL_PATH', help='Path to the model') parser.add_argument('-d', '--device', type=int_or_str, help='input device (numeric ID or substring)') parser.add_argument('-r', '--samplerate', type=int, help='sampling rate') args = parser.parse_args(remaining) from auto_everything.terminal import Terminal terminal = Terminal() def control(text): print(text) if "open" in text: terminal.run("firefox", wait=False) if "go" in text: terminal.kill("firefox", wait=False) if "hi" in text: print("\n\n\n\n hi~~~~~~ \n\n\n\n\n") try: if args.model is None: args.model = "model"
import os from auto_everything.terminal import Terminal from auto_everything.disk import Disk from auto_everything.network import Network import logging from PIL import Image from matplotlib import pyplot as plt import cv2 import numpy as np from tensorflow import keras os.environ['KERAS_BACKEND'] = 'tensorflow' terminal = Terminal() disk = Disk() network = Network() # Static directory of this module STATIC_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "static") # Root directory of the project ROOT_DIR = os.path.abspath(terminal.fix_path("~/Pornstar")) # Root directory of the project if not terminal.exists(ROOT_DIR): terminal.run(f"mkdir {ROOT_DIR}") logging.basicConfig(filename=os.path.join(ROOT_DIR, "_main.log"), level=logging.DEBUG, filemode='w', format='%(levelname)s - %(message)s')
#!/usr/bin/env python from auto_everything.terminal import Terminal from auto_everything.io import IO from os import path from sys import platform t = Terminal() io_ = IO() if platform == "linux" or platform == "linux2": pass elif platform == "darwin": io_.append(t.fix_path("~/.zshrc"), """ source ~/.bashrc """) elif platform == "win32": print("Sorry, we don't support windows") exit() if t.run_command("echo $DESKTOP_SESSION").strip() != "": IS_DESKTOP = True else: IS_DESKTOP = False print("script start...\n\n") # 1 print("install building tools...") c = """ sudo apt install -y build-essential cmake
from auto_everything.disk import Disk from auto_everything.terminal import Terminal from auto_everything.python import Python terminal = Terminal(debug=False) disk = Disk() python = Python()