1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
#!/usr/bin/env python
"""
cmd.py is a command-line utility that helps with local TCP ports management.
It finds 'good' unused TCP localhost port and remembers the association.
Usage:
port-for <NAME>
port-for --bind <NAME>
port-for --bind <NAME> --port <NUMBER>
port-for <NAME> --port <NUMBER>
port-for --unbind <NAME>
port-for --list
port-for --version
port-for --help
Options:
-h --help Show this screen.
-v, --version Show version.
-b FOO, --bind FOO Find and return a port for FOO; this is an alias for
'port-for FOO'.
-p PORT, --port PORT (Optional) specific port number for the --bind command.
-u FOO, --unbind FOO Remove association for FOO.
-l, --list List all associated ports.
"""
import sys
from typing import Optional
import port_for
from port_for.docopt import docopt
store = port_for.PortStore()
def _list() -> None:
for app, port in store.bound_ports():
sys.stdout.write("%s: %s\n" % (app, port))
def _bind(app: str, port: Optional[str] = None) -> None:
bound_port = store.bind_port(app, port)
sys.stdout.write("%s\n" % bound_port)
def _unbind(app: str) -> None:
store.unbind_port(app)
def main() -> None:
"""port-for executable entrypoint."""
args = docopt(
__doc__,
version="port-for %s" % port_for.__version__,
) # type: ignore[no-untyped-call]
if args["<NAME>"]:
_bind(args["<NAME>"], args["--port"])
elif args["--bind"]:
_bind(args["--bind"], args["--port"])
elif args["--list"]:
_list()
elif args["--unbind"]:
_unbind(args["--unbind"])
if __name__ == "__main__":
main()
|