Пример #1
0
 def test_recursion(self):
     path = join(mig_root, 'multidb')
     results = list(get_file_list(path))
     self.assertEquals(len(results), 4)
     self.assertTrue(join(path, 'default', '0001.sql') in results)
     self.assertTrue(join(path, 'default', '0002_foo.py') in results)
     self.assertTrue(join(path, 'other', '0001.sql') in results)
     self.assertTrue(join(path, 'other', '0002_bar.sql') in results)
Пример #2
0
def get_all_migrations(path, databases=None):
    """
    Returns a dictionary of database => [migrations] representing all
    migrations contained in ``path``.
    """
    # database: [(number, full_path)]
    possible_migrations = defaultdict(list)
    
    try:
        in_directory = sorted(get_file_list(path))
    except OSError:
        import traceback
        print "An error occurred while reading migrations from %r:" % path
        traceback.print_exc()
        return {}
    
    # Iterate through our results and discover which migrations are
    # actually runnable
    for full_path in in_directory:
        child_path, script = os.path.split(full_path)
        name, ext = os.path.splitext(script)
        
        # the database component is default if this is in the root directory
        # is <directory> if in a subdirectory
        if path == child_path:
            db = DEFAULT_DB_ALIAS
        else:
            db = os.path.split(child_path)[-1]
        
        # filter by database if set
        if databases and db not in databases:
            continue
        
        match = MIGRATION_NAME_RE.match(name)
        if match is None:
            raise MigrationError("Invalid migration file prefix %r "
                                 "(must begin with a number)" % name)
        
        number = int(match.group(1))
        if ext in [".sql", ".py"]:
            possible_migrations[db].append((number, full_path))
    
    return possible_migrations
Пример #3
0
def get_file_list(path, max_depth=1, cur_depth=0):
    """
    Recursively returns a list of all files up to ``max_depth``
    in a directory.
    """
    if os.path.exists(path):
        for name in os.listdir(path):
            if name.startswith('.'):
                continue
            
            full_path = os.path.join(path, name)
            if os.path.isdir(full_path):
                if cur_depth == max_depth:
                    continue
                
                file_list = get_file_list(full_path, max_depth, cur_depth + 1)
                for result in file_list:
                    yield result
            else:
                yield full_path