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
 
9
 
10
 test:
10
 test:
11
   override:
11
   override:
12
-    - pylint docker-rerun
12
+    - pylint -d invalid-name docker-rerun

+ 39
- 0
docker-rerun View File

1
 #!/usr/bin/python3
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
 import argparse
12
 import argparse
4
 import inspect
13
 import inspect
8
 
17
 
9
 
18
 
10
 def inspect_container(container):
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
     output = subprocess.check_output(['docker', 'inspect',
31
     output = subprocess.check_output(['docker', 'inspect',
12
                                       '--type=container', container])
32
                                       '--type=container', container])
13
     return json.loads(output.decode('utf-8'))[0]
33
     return json.loads(output.decode('utf-8'))[0]
14
 
34
 
15
 
35
 
16
 def handle_binds(container, config):
36
 def handle_binds(container, config):
37
+    """Copies the volume bind (--volume/-v) arguments."""
17
     if container['HostConfig']['Binds']:
38
     if container['HostConfig']['Binds']:
18
         config['args'].extend(['--volume=%s' % bind
39
         config['args'].extend(['--volume=%s' % bind
19
                                for bind in container['HostConfig']['Binds']])
40
                                for bind in container['HostConfig']['Binds']])
20
 
41
 
21
 
42
 
22
 def handle_image(container, config):
43
 def handle_image(container, config):
44
+    """Copies the image argument."""
23
     config['image'] = container['Config']['Image']
45
     config['image'] = container['Config']['Image']
24
 
46
 
25
 
47
 
26
 def handle_name(container, config):
48
 def handle_name(container, config):
49
+    """Copies the name (--name) argument."""
27
     # Trim the leading / off the name. They're equivalent from docker's point
50
     # Trim the leading / off the name. They're equivalent from docker's point
28
     # of view, but having the plain name looks nicer from a human point of view.
51
     # of view, but having the plain name looks nicer from a human point of view.
29
     config['args'].append('--name=%s' % container['Name'][1:])
52
     config['args'].append('--name=%s' % container['Name'][1:])
30
 
53
 
31
 
54
 
32
 def handle_network_mode(container, config):
55
 def handle_network_mode(container, config):
56
+    """Copies the network mode (--net) argument."""
33
     network = container['HostConfig']['NetworkMode']
57
     network = container['HostConfig']['NetworkMode']
34
     if network != 'default':
58
     if network != 'default':
35
         config['args'].append('--net=%s' % network)
59
         config['args'].append('--net=%s' % network)
36
 
60
 
37
 
61
 
38
 def handle_ports(container, config):
62
 def handle_ports(container, config):
63
+    """Copies the port publication (-p) arguments."""
39
     ports = container['HostConfig']['PortBindings']
64
     ports = container['HostConfig']['PortBindings']
40
     if ports:
65
     if ports:
41
         for port, bindings in ports.items():
66
         for port, bindings in ports.items():
52
 
77
 
53
 
78
 
54
 def handle_restart(container, config):
79
 def handle_restart(container, config):
80
+    """Copies the restart policy (--restart) argument."""
55
     policy = container['HostConfig']['RestartPolicy']
81
     policy = container['HostConfig']['RestartPolicy']
56
     if policy and policy['Name'] != 'no':
82
     if policy and policy['Name'] != 'no':
57
         arg = '--restart=%s' % policy['Name']
83
         arg = '--restart=%s' % policy['Name']
61
 
87
 
62
 
88
 
63
 def handle_volumes_from(container, config):
89
 def handle_volumes_from(container, config):
90
+    """Copies the volumes from (--volumes-from) argument."""
64
     if container['HostConfig']['VolumesFrom']:
91
     if container['HostConfig']['VolumesFrom']:
65
         config['args'].extend(['--volumes-from=%s' % cont for
92
         config['args'].extend(['--volumes-from=%s' % cont for
66
                                cont in container['HostConfig']['VolumesFrom']])
93
                                cont in container['HostConfig']['VolumesFrom']])
67
 
94
 
68
 
95
 
69
 def functions():
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
     return [m for m
103
     return [m for m
71
             in inspect.getmembers(sys.modules[__name__])
104
             in inspect.getmembers(sys.modules[__name__])
72
             if inspect.isfunction(m[1])]
105
             if inspect.isfunction(m[1])]
73
 
106
 
74
 
107
 
75
 def handlers():
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
     return [func for (name, func) in functions() if name.startswith('handle_')]
114
     return [func for (name, func) in functions() if name.startswith('handle_')]
77
 
115
 
78
 
116
 
79
 def main():
117
 def main():
118
+    """Script entry point."""
80
     parser = argparse.ArgumentParser(description='Reruns docker containers ' \
119
     parser = argparse.ArgumentParser(description='Reruns docker containers ' \
81
                                                  'with different parameters.')
120
                                                  'with different parameters.')
82
     parser.add_argument('container', type=str, help='The container to rerun')
121
     parser.add_argument('container', type=str, help='The container to rerun')

Loading…
Cancel
Save