2016-03-23 5 views
-1

私はmodel_mommyのテストを書いています。これは非常に便利なdjangoの偽のオブジェクトです。私は、スクリプトを自立させるための速い方法を望んでおり、djangoプロジェクトであなたのカスタムアプリケーションのテストだけを書かなければなりません。現在、あなたが書いたことのないauthtaggingのような、あなたが使っているすべてのアプリのテストを見つけて書くことができます。ママを使用する場合はスクリプトを使用できます(またはミキサーを変更した場合でも動作します)。どのアプリが実際に私のものであるかを知るためには、乱雑なスムーズな方法は何ですか?os.walkは、カスタムアプリとサードパーティ/近親相姦アプリを区別します。 django

https://gist.github.com/codyc4321/81cbb25f99f2af709c03

+0

ありがとう本当に不明確ということでしょうか?私は 'django.apps.apps'機能の中でカスタムとサードパーティのアプリケーションを区別したいと思っています。それはまったく不明瞭ではないようです – codyc4321

答えて

0
class ModelRunner(object): 

    def __init__(self, starting_path): 
     self.start_path = starting_path 

    @property 
    def model_files(self): 
     model_files = [] 
     for root, dirs, files in os.walk(self.start_path): 
      for f in files: 
       if self.is_regular_models_file(f): 
        filename = os.path.join(root, f) 
        model_files.append(filename) 
      for d in dirs: 
       if self.is_models_dir(d): 
        model_files.extend(self.get_models_files_from_models_folder(os.path.join(root, d))) 
     return model_files 

    def get_models_files_from_models_folder(self, filepath): 
     for root, _, files in os.walk(filepath): 
      model_files = [] 
      for f in files: 
       if f not in ['__init__.py'] and '.pyc' not in f: 
        filename = os.path.join(root, f) 
        model_files.append(filename) 
      return model_files 

    @property 
    def apps(self): 
     apps = [] 
     for f in self.model_files: 
      apps.append(self.get_app_name_from_file(f)) 
     return apps 

    def get_app_name_from_file(self, filepath): 

     def find_models_dir(path): 
      head, name = os.path.split(path) 
      return path if name == 'models' else find_models_dir(head) 

     if self.is_regular_models_file(filepath): 
      head, _ = os.path.split(filepath) 
      _, app_name = os.path.split(head) 
      return app_name 
     else: 
      return find_models_dir(filepath) 

    def is_regular_models_file(self, filepath): 
     return get_filename(filepath) == 'models.py' 

    def is_models_dir(self, filepath): 
     return get_filename(filepath) == 'models' 
関連する問題