Browse Source

Add docstrings.

tags/v0.1.1
Chris Smith 7 years ago
parent
commit
400ff56572
2 changed files with 40 additions and 1 deletions
  1. 1
    1
      circle.yml
  2. 39
    0
      docker-rerun

+ 1
- 1
circle.yml View File

@@ -9,4 +9,4 @@ dependencies:
9 9
 
10 10
 test:
11 11
   override:
12
-    - pylint docker-rerun
12
+    - pylint -d invalid-name docker-rerun

+ 39
- 0
docker-rerun View File

@@ -1,4 +1,13 @@
1 1
 #!/usr/bin/python3
2
+"""Re-runs a docker container using the same arguments as before.
3
+
4
+Given the name of a container, the previous arguments are determined
5
+and reconstructed by looking at the `docker inspect` output.
6
+
7
+Each function named `handle_*` handles one configuration option,
8
+reading the relevant information from the inspect output and adding
9
+the relevant command line flags to the config.
10
+"""
2 11
 
3 12
 import argparse
4 13
 import inspect
@@ -8,34 +17,50 @@ import sys
8 17
 
9 18
 
10 19
 def inspect_container(container):
20
+    """Uses `docker inspect` to get details about the given container.
21
+
22
+    Args:
23
+        container (str): The name of the container to inspect.
24
+
25
+    Returns:
26
+        dict: Detailed information about the container.
27
+
28
+    Raises:
29
+        CalledProcessError: An error occurred talking to Docker.
30
+    """
11 31
     output = subprocess.check_output(['docker', 'inspect',
12 32
                                       '--type=container', container])
13 33
     return json.loads(output.decode('utf-8'))[0]
14 34
 
15 35
 
16 36
 def handle_binds(container, config):
37
+    """Copies the volume bind (--volume/-v) arguments."""
17 38
     if container['HostConfig']['Binds']:
18 39
         config['args'].extend(['--volume=%s' % bind
19 40
                                for bind in container['HostConfig']['Binds']])
20 41
 
21 42
 
22 43
 def handle_image(container, config):
44
+    """Copies the image argument."""
23 45
     config['image'] = container['Config']['Image']
24 46
 
25 47
 
26 48
 def handle_name(container, config):
49
+    """Copies the name (--name) argument."""
27 50
     # Trim the leading / off the name. They're equivalent from docker's point
28 51
     # of view, but having the plain name looks nicer from a human point of view.
29 52
     config['args'].append('--name=%s' % container['Name'][1:])
30 53
 
31 54
 
32 55
 def handle_network_mode(container, config):
56
+    """Copies the network mode (--net) argument."""
33 57
     network = container['HostConfig']['NetworkMode']
34 58
     if network != 'default':
35 59
         config['args'].append('--net=%s' % network)
36 60
 
37 61
 
38 62
 def handle_ports(container, config):
63
+    """Copies the port publication (-p) arguments."""
39 64
     ports = container['HostConfig']['PortBindings']
40 65
     if ports:
41 66
         for port, bindings in ports.items():
@@ -52,6 +77,7 @@ def handle_ports(container, config):
52 77
 
53 78
 
54 79
 def handle_restart(container, config):
80
+    """Copies the restart policy (--restart) argument."""
55 81
     policy = container['HostConfig']['RestartPolicy']
56 82
     if policy and policy['Name'] != 'no':
57 83
         arg = '--restart=%s' % policy['Name']
@@ -61,22 +87,35 @@ def handle_restart(container, config):
61 87
 
62 88
 
63 89
 def handle_volumes_from(container, config):
90
+    """Copies the volumes from (--volumes-from) argument."""
64 91
     if container['HostConfig']['VolumesFrom']:
65 92
         config['args'].extend(['--volumes-from=%s' % cont for
66 93
                                cont in container['HostConfig']['VolumesFrom']])
67 94
 
68 95
 
69 96
 def functions():
97
+    """Lists all functions defined in this module.
98
+
99
+    Returns:
100
+        list of (str,function): List of (name, function) pairs for each
101
+        function defined in this module.
102
+    """
70 103
     return [m for m
71 104
             in inspect.getmembers(sys.modules[__name__])
72 105
             if inspect.isfunction(m[1])]
73 106
 
74 107
 
75 108
 def handlers():
109
+    """Lists all handlers defined in this module.
110
+
111
+    Returns:
112
+        list of function: All handlers (handle_* funcs) defined in this module.
113
+    """
76 114
     return [func for (name, func) in functions() if name.startswith('handle_')]
77 115
 
78 116
 
79 117
 def main():
118
+    """Script entry point."""
80 119
     parser = argparse.ArgumentParser(description='Reruns docker containers ' \
81 120
                                                  'with different parameters.')
82 121
     parser.add_argument('container', type=str, help='The container to rerun')

Loading…
Cancel
Save