Beispiel #1
0
 def get_parent_list(self):
     """
     Return all the ancestors of this model as a list ordered by MRO.
     Useful for determining if something is an ancestor, regardless of lineage.
     """
     result = OrderedSet(self.parents)
     for parent in self.parents:
         for ancestor in parent._meta.get_parent_list():
             result.add(ancestor)
     return list(result)
Beispiel #2
0
    def process_rhs(self, compiler, connection):
        db_rhs = getattr(self.rhs, '_db', None)
        if db_rhs is not None and db_rhs != connection.alias:
            raise ValueError(
                "Subqueries aren't allowed across different databases. Force "
                "the inner query to be evaluated using `list(inner_query)`."
            )

        if self.rhs_is_direct_value():
            try:
                rhs = OrderedSet(self.rhs)
            except TypeError:  # Unhashable items in self.rhs
                rhs = self.rhs

            if not rhs:
                raise EmptyResultSet

            # rhs should be an iterable; use batch_process_rhs() to
            # prepare/transform those values.
            sqls, sqls_params = self.batch_process_rhs(compiler, connection, rhs)
            placeholder = '(' + ', '.join(sqls) + ')'
            return (placeholder, sqls_params)
        else:
            if not getattr(self.rhs, 'has_select_fields', True):
                self.rhs.clear_select_clause()
                self.rhs.add_fields(['pk'])
            return super().process_rhs(compiler, connection)
Beispiel #3
0
 def iterative_dfs(self, start, forwards=True):
     """Iterative depth-first search for finding dependencies."""
     visited = []
     stack = [start]
     while stack:
         node = stack.pop()
         visited.append(node)
         stack += sorted(node.parents if forwards else node.children)
     return list(OrderedSet(reversed(visited)))
Beispiel #4
0
 def descendants(self):
     # Use self.key instead of self to speed up the frequent hashing
     # when constructing an OrderedSet.
     if '_descendants' not in self.__dict__:
         descendants = []
         for child in sorted(self.children, reverse=True):
             descendants += child.descendants()
         descendants.append(self.key)
         self.__dict__['_descendants'] = list(OrderedSet(descendants))
     return self.__dict__['_descendants']
Beispiel #5
0
 def ancestors(self):
     # Use self.key instead of self to speed up the frequent hashing
     # when constructing an OrderedSet.
     if '_ancestors' not in self.__dict__:
         ancestors = []
         for parent in sorted(self.parents, reverse=True):
             ancestors += parent.ancestors()
         ancestors.append(self.key)
         self.__dict__['_ancestors'] = list(OrderedSet(ancestors))
     return self.__dict__['_ancestors']
Beispiel #6
0
    def python(self, options):
        import code
        # Set up a dictionary to serve as the environment for the shell, so
        # that tab completion works on objects that are imported at runtime.
        imported_objects = {}
        try:  # Try activating rlcompleter, because it's handy.
            import readline
        except ImportError:
            pass
        else:
            # We don't have to wrap the following import in a 'try', because
            # we already know 'readline' was imported successfully.
            import rlcompleter
            readline.set_completer(
                rlcompleter.Completer(imported_objects).complete)
            # Enable tab completion on systems using libedit (e.g. macOS).
            # These lines are copied from Python's Lib/site.py.
            readline_doc = getattr(readline, '__doc__', '')
            if readline_doc is not None and 'libedit' in readline_doc:
                readline.parse_and_bind("bind ^I rl_complete")
            else:
                readline.parse_and_bind("tab:complete")

        # We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
        # conventions and get $PYTHONSTARTUP first then .pythonrc.py.
        if not options['no_startup']:
            for pythonrc in OrderedSet([
                    os.environ.get("PYTHONSTARTUP"),
                    os.path.expanduser('~/.pythonrc.py')
            ]):
                if not pythonrc:
                    continue
                if not os.path.isfile(pythonrc):
                    continue
                with open(pythonrc) as handle:
                    pythonrc_code = handle.read()
                # Match the behavior of the cpython shell where an error in
                # PYTHONSTARTUP prints an exception and continues.
                try:
                    exec(compile(pythonrc_code, pythonrc, 'exec'),
                         imported_objects)
                except Exception:
                    traceback.print_exc()

        code.interact(local=imported_objects)
Beispiel #7
0
def reorder_suite(suite, classes, reverse=False):
    """
    Reorder a test suite by test type.

    `classes` is a sequence of types

    All tests of type classes[0] are placed first, then tests of type
    classes[1], etc. Tests with no match in classes are placed last.

    If `reverse` is True, sort tests within classes in opposite order but
    don't reverse test classes.
    """
    class_count = len(classes)
    suite_class = type(suite)
    bins = [OrderedSet() for i in range(class_count + 1)]
    partition_suite_by_type(suite, classes, bins, reverse=reverse)
    reordered_suite = suite_class()
    for i in range(class_count + 1):
        reordered_suite.addTests(bins[i])
    return reordered_suite
Beispiel #8
0
 def get_constraints(self, cursor, table_name):
     """
     Retrieve any constraints or keys (unique, pk, fk, check, index) across
     one or more columns.
     """
     constraints = {}
     # Get the actual constraint names and columns
     name_query = """
         SELECT kc.`constraint_name`, kc.`column_name`,
             kc.`referenced_table_name`, kc.`referenced_column_name`
         FROM information_schema.key_column_usage AS kc
         WHERE
             kc.table_schema = DATABASE() AND
             kc.table_name = %s
     """
     cursor.execute(name_query, [table_name])
     for constraint, column, ref_table, ref_column in cursor.fetchall():
         if constraint not in constraints:
             constraints[constraint] = {
                 'columns': OrderedSet(),
                 'primary_key': False,
                 'unique': False,
                 'index': False,
                 'check': False,
                 'foreign_key':
                 (ref_table, ref_column) if ref_column else None,
             }
         constraints[constraint]['columns'].add(column)
     # Now get the constraint types
     type_query = """
         SELECT c.constraint_name, c.constraint_type
         FROM information_schema.table_constraints AS c
         WHERE
             c.table_schema = DATABASE() AND
             c.table_name = %s
     """
     cursor.execute(type_query, [table_name])
     for constraint, kind in cursor.fetchall():
         if kind.lower() == "primary key":
             constraints[constraint]['primary_key'] = True
             constraints[constraint]['unique'] = True
         elif kind.lower() == "unique":
             constraints[constraint]['unique'] = True
     # Now add in the indexes
     cursor.execute("SHOW INDEX FROM %s" %
                    self.connection.ops.quote_name(table_name))
     for table, non_unique, index, colseq, column, type_ in [
             x[:5] + (x[10], ) for x in cursor.fetchall()
     ]:
         if index not in constraints:
             constraints[index] = {
                 'columns': OrderedSet(),
                 'primary_key': False,
                 'unique': False,
                 'check': False,
                 'foreign_key': None,
             }
         constraints[index]['index'] = True
         constraints[index][
             'type'] = Index.suffix if type_ == 'BTREE' else type_.lower()
         constraints[index]['columns'].add(column)
     # Convert the sorted sets to lists
     for constraint in constraints.values():
         constraint['columns'] = list(constraint['columns'])
     return constraints