• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

datajoint / datajoint-python / #12898

pending completion
#12898

push

travis-ci

web-flow
<a href="https://github.com/datajoint/datajoint-python/commit/<a class=hub.com/datajoint/datajoint-python/commit/715ab40552f63cd79723ed2830c6691b2cb228b9">715ab4055<a href="https://github.com/datajoint/datajoint-python/commit/715ab40552f63cd79723ed2830c6691b2cb228b9">">Merge </a><a class="double-link" href="https://github.com/datajoint/datajoint-python/commit/<a class="double-link" href="https://github.com/datajoint/datajoint-python/commit/0a4f193031d8b1e14b09ec62d83c5def3b7421b0">0a4f19303</a>">0a4f19303</a><a href="https://github.com/datajoint/datajoint-python/commit/715ab40552f63cd79723ed2830c6691b2cb228b9"> into 3b6e84588">3b6e84588</a>

69 of 69 new or added lines in 9 files covered. (100.0%)

3052 of 3381 relevant lines covered (90.27%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

47.06
/datajoint/admin.py
1
import pymysql
1✔
2
from getpass import getpass
1✔
3
from .connection import conn
1✔
4
from .settings import config
1✔
5
from .utils import user_choice
1✔
6
import logging
1✔
7

8
logger = logging.getLogger(__name__.split(".")[0])
1✔
9

10

11
def set_password(
12
    new_password=None, connection=None, update_config=None
13
):  # pragma: no cover
14
    connection = conn() if connection is None else connection
15
    if new_password is None:
16
        new_password = getpass("New password: ")
17
        confirm_password = getpass("Confirm password: ")
18
        if new_password != confirm_password:
19
            logger.warn("Failed to confirm the password! Aborting password change.")
20
            return
21
    connection.query("SET PASSWORD = PASSWORD('%s')" % new_password)
22
    logger.info("Password updated.")
23

24
    if update_config or (
25
        update_config is None and user_choice("Update local setting?") == "yes"
26
    ):
27
        config["database.password"] = new_password
28
        config.save_local(verbose=True)
29

30

31
def kill(restriction=None, connection=None, order_by=None):  # pragma: no cover
32
    """
33
    view and kill database connections.
34

35
    :param restriction: restriction to be applied to processlist
36
    :param connection: a datajoint.Connection object. Default calls datajoint.conn()
37
    :param order_by: order by a single attribute or the list of attributes. defaults to 'id'.
38

39
    Restrictions are specified as strings and can involve any of the attributes of
40
    information_schema.processlist: ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO.
41

42
    Examples:
43
        dj.kill('HOST LIKE "%compute%"') lists only connections from hosts containing "compute".
44
        dj.kill('TIME > 600') lists only connections in their current state for more than 10 minutes
45
    """
46

47
    if connection is None:
48
        connection = conn()
49

50
    if order_by is not None and not isinstance(order_by, str):
51
        order_by = ",".join(order_by)
52

53
    query = (
54
        "SELECT * FROM information_schema.processlist WHERE id <> CONNECTION_ID()"
55
        + ("" if restriction is None else " AND (%s)" % restriction)
56
        + (" ORDER BY %s" % (order_by or "id"))
57
    )
58

59
    while True:
60
        print("  ID USER         HOST          STATE         TIME    INFO")
61
        print("+--+ +----------+ +-----------+ +-----------+ +-----+")
62
        cur = (
63
            {k.lower(): v for k, v in elem.items()}
64
            for elem in connection.query(query, as_dict=True)
65
        )
66
        for process in cur:
67
            try:
68
                print(
69
                    "{id:>4d} {user:<12s} {host:<12s} {state:<12s} {time:>7d}  {info}".format(
70
                        **process
71
                    )
72
                )
73
            except TypeError:
74
                print(process)
75
        response = input('process to kill or "q" to quit > ')
76
        if response == "q":
77
            break
78
        if response:
79
            try:
80
                pid = int(response)
81
            except ValueError:
82
                pass  # ignore non-numeric input
83
            else:
84
                try:
85
                    connection.query("kill %d" % pid)
86
                except pymysql.err.InternalError:
87
                    logger.warn("Process not found")
88

89

90
def kill_quick(restriction=None, connection=None):
1✔
91
    """
92
    Kill database connections without prompting. Returns number of terminated connections.
93

94
    :param restriction: restriction to be applied to processlist
95
    :param connection: a datajoint.Connection object. Default calls datajoint.conn()
96

97
    Restrictions are specified as strings and can involve any of the attributes of
98
    information_schema.processlist: ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO.
99

100
    Examples:
101
        dj.kill('HOST LIKE "%compute%"') terminates connections from hosts containing "compute".
102
    """
103
    if connection is None:
×
104
        connection = conn()
×
105

106
    query = (
×
107
        "SELECT * FROM information_schema.processlist WHERE id <> CONNECTION_ID()"
108
        + ("" if restriction is None else " AND (%s)" % restriction)
109
    )
110

111
    cur = (
×
112
        {k.lower(): v for k, v in elem.items()}
113
        for elem in connection.query(query, as_dict=True)
114
    )
115
    nkill = 0
×
116
    for process in cur:
×
117
        connection.query("kill %d" % process["id"])
×
118
        nkill += 1
×
119
    return nkill
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc