Exemplo n.º 1
0
class Shell():
    def __init__(self):
        # option from command line
        self.verbose = False
        self.log = Log()
        # relevant directories
        self.home = os.path.expanduser('~')
        self.working = os.getcwd()
        self.main = None
        try:
            exeFile = os.path.abspath(sys.modules['__main__'].__file__)
            srcFile = os.path.realpath(exeFile)  # resolve symlinks
            self.main = os.path.dirname(srcFile)
        except:
            self.log.warn('no main file path, interactive interpreter')
        self.find = _Find(self)
        # operating system type
        self.os = _which_os()

    def respond(self, cmd_str_or_lst, shell=False, strip=False):
        cmd = ' '.join(cmd_str_or_lst) if (type(cmd_str_or_lst) == type(
            [])) else cmd_str_or_lst
        if self.verbose: print(cmd)
        byte_output = check_output(cmd, shell=shell)
        string_output = byte_output.decode('utf-8')
        if strip: return string_output.strip()
        return string_output

    def basename(self, path):
        basename = os.path.basename(path)
        if self.verbose: print('Basename of ', path, ' is ', basename)
        return basename

    def cd(self, path):
        if self.verbose: print('Changing directory to: ', path)
        os.chdir(path)

    def chrome(self, url):
        if self.os == 'linux':
            self.command([
                'google-chrome-stable', '--disable-features=NetworkService',
                url, '&>/dev/null'
            ])
        elif self.os == 'mac':
            self.command(['open -a', r'Google\ Chrome', url])

    def command(self, cmd_list):
        if isinstance(cmd_list, str): cmd = cmd_list
        else: cmd = ' '.join(cmd_list)
        if self.verbose: print(cmd)
        status = os.system(cmd)
        return status

    def cp(self, from_file, to_file):
        if self.verbose: print('Copying from ', from_file, ' to ', to_file)
        if self.exists(to_file): self.rm(to_file)
        if os.path.isdir(from_file): shutil.copytree(from_file, to_file)
        else: shutil.copyfile(from_file, to_file)

    def rm(self, path):
        if self.verbose: print('Recursively removing: ', path)
        if os.path.isdir(path): shutil.rmtree(path)
        else: os.remove(path)

    def dirname(self, path):
        dirname = os.path.dirname(path)
        if self.verbose: print('Dirname of ', path, ' is ', dirname)
        return dirname

    def exists(self, path):  # check for path of directory
        if self.verbose: print('Checking the existance of path: ', path)
        return os.path.exists(path.strip())

    def is_dir(self, path):  # check for path of directory
        if self.verbose: print('Checking if is dir, path: ', path)
        return os.path.isdir(path.strip())

    def kill(self, command_name):
        if self.os == 'mac':
            pid = self.pid(command_name)
        else:
            self.log.error(
                'Shell.kill is not yet implemented for this operating system')
        status = self.command(['kill', '-9', pid])
        if status == 0:
            self.log.validate('killed ' + command_name + ' running with id ' +
                              pid)

    def link(self, src, dest):
        os.symlink(src, dest)

    def readlink(self, link_path):
        source = os.readlink(link_path)
        if self.exists(source):
            if self.verbose:
                self.log.validate('the symlink at, ' + link_path +
                                  ', resolved')
            return source
        if self.verbose:
            print('link, ' + link_path + ', exists, but did not resolve')
        return False

    def ls(self, path):
        if self.verbose: print('Listing files in directory: ', path)
        return os.listdir(path)

    def make_executable(self, file):
        self.command(['chmod +x', file])

    def mkdir(self, path):
        if self.is_dir(path):
            self.log.validate('directory, ' + path + ', already exists')
            return
        os.mkdir(path)
        if self.verbose: print('made directory: ', path)

    def mv(self, from_path, to_path):
        try:
            os.rename(from_path, to_path)
            if self.verbose:
                print('renaming directory: ', from_path, ' to ', to_path)
        except Exception as e:
            raise e

    def pid(self, command_name):
        try:
            if self.os == 'mac':
                pid = self.respond(['pgrep', command_name],
                                   shell=True,
                                   strip=True)
            else:
                self.log.error(
                    'Shell.pid is not yet implemented for this operating system'
                )
        except:
            self.log.error(
                'failed to get a process id for the command name: ' +
                command_name)
        return pid

    def vim(self, name, SystemEditor=False):
        if SystemEditor: vim = os.environ.get('EDITOR', 'vim')  # create editor
        else: vim = 'vim'
        if type(name) == type([]):
            if len(name) > 1: raise TypeError('too many input arguments')
            if len(name) == 0:
                self.command([vim])
                return 0
            else:
                name = name[0]
        if self.is_dir(name):
            self.command([vim, name])
            return 0
        try:
            open_file_with_vim(vim, name, 'r+')  # open file to read
        except:
            open_file_with_vim(vim, name, 'w+')  # open file to write
        return 0
Exemplo n.º 2
0
        print("Arguments ERROR.")
        print("opt:\t" + str(opt))
        print("val:\t" + str(val))
        sys.exit(1)

src_path = work_dir + "/src"
tools_path = src_path + "/tools"
sys.path.append(src_path)
sys.path.append(tools_path)
#Cumtomize Moduls
import settings
from tools.Platform import *
from tools.MD5 import *
from tools.Log import *
from tools.SeleniumSpider import *
from tools.BS4Spider import *
from tools.RequestsSpider import *
from tools.Proxy import *
from tools.ListTool import *
from tools.MultiProcess import *
from tools.StringTool import *
from tools.Network import *

script_path = os.path.split(os.path.split(os.path.realpath(__file__))[0])[0]

logger = Log("./", "./log.log")
logger.init()
logger.info("info")
logger.error("error")
logger.warning("war")
Exemplo n.º 3
0
log = Log()

emailToVerify = "*****@*****.**"

server_port = None
server_name = None
server_encryption = 'smtp'
user_name = None
password = None
debug_level = 0

email_verify = email_verificator(emailToVerify)

if email_verify is False:
    log.error("Email invalid" + emailToVerify)
    raise ValueError("Email invalid")

domain = domain_extract(emailToVerify) if server_name is None else server_name

try:
    records = dns.resolver.query(domain, 'MX')
except NoAnswer as e:
    log.error(message=e)
    raise ValueError("Error response: {message}".format(message=e))

mx_record = MxRecord()

for mx in records:
    mx_record.add(str(mx.preference), str(mx.exchange))