Merge lp:~mbruzek/charm-helpers/host-docstrings into lp:charm-helpers

Proposed by Matt Bruzek on 2016-01-15
Status: Merged
Merged at revision: 518
Proposed branch: lp:~mbruzek/charm-helpers/host-docstrings
Merge into: lp:charm-helpers
Diff against target: 195 lines (+39/-25)
1 file modified
charmhelpers/core/host.py (+39/-25)
To merge this branch: bzr merge lp:~mbruzek/charm-helpers/host-docstrings
Reviewer Review Type Date Requested Status
Charles Butler (community) 2016-01-15 Approve on 2016-01-15
Review via email: mp+282788@code.launchpad.net

Description of the Change

I got tired of going to pypi and finding incomplete documentation for the host module. We implemented one of the context managers (chdir) in our own charm because we did not know about the one in charm-helpers.

This proposal adds docstrings to all methods, and normalizes the docstring format to what I understand is the pipy recommendations.

I ran flake8 and generated the docs to test these changes. All methods for the host module are now documented. Please review and let me know if there are any issues.

To post a comment you must log in.
Charles Butler (lazypower) wrote :

+1 LGTM

Thanks for updating the docstrings matt! This is extremely helpful

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/core/host.py'
2--- charmhelpers/core/host.py 2016-01-06 14:04:21 +0000
3+++ charmhelpers/core/host.py 2016-01-15 17:21:05 +0000
4@@ -160,13 +160,13 @@
5
6
7 def init_is_systemd():
8+ """Return True if the host system uses systemd, False otherwise."""
9 return os.path.isdir(SYSTEMD_SYSTEM)
10
11
12 def adduser(username, password=None, shell='/bin/bash', system_user=False,
13 primary_group=None, secondary_groups=None):
14- """
15- Add a user to the system.
16+ """Add a user to the system.
17
18 Will log but otherwise succeed if the user already exists.
19
20@@ -174,7 +174,7 @@
21 :param str password: Password for user; if ``None``, create a system user
22 :param str shell: The default shell for the user
23 :param bool system_user: Whether to create a login or system user
24- :param str primary_group: Primary group for user; defaults to their username
25+ :param str primary_group: Primary group for user; defaults to username
26 :param list secondary_groups: Optional list of additional groups
27
28 :returns: The password database entry struct, as returned by `pwd.getpwnam`
29@@ -300,14 +300,12 @@
30
31
32 def fstab_remove(mp):
33- """Remove the given mountpoint entry from /etc/fstab
34- """
35+ """Remove the given mountpoint entry from /etc/fstab"""
36 return Fstab.remove_by_mountpoint(mp)
37
38
39 def fstab_add(dev, mp, fs, options=None):
40- """Adds the given device entry to the /etc/fstab file
41- """
42+ """Adds the given device entry to the /etc/fstab file"""
43 return Fstab.add(dev, mp, fs, options=options)
44
45
46@@ -363,8 +361,7 @@
47
48
49 def file_hash(path, hash_type='md5'):
50- """
51- Generate a hash checksum of the contents of 'path' or None if not found.
52+ """Generate a hash checksum of the contents of 'path' or None if not found.
53
54 :param str hash_type: Any hash alrgorithm supported by :mod:`hashlib`,
55 such as md5, sha1, sha256, sha512, etc.
56@@ -379,10 +376,9 @@
57
58
59 def path_hash(path):
60- """
61- Generate a hash checksum of all files matching 'path'. Standard wildcards
62- like '*' and '?' are supported, see documentation for the 'glob' module for
63- more information.
64+ """Generate a hash checksum of all files matching 'path'. Standard
65+ wildcards like '*' and '?' are supported, see documentation for the 'glob'
66+ module for more information.
67
68 :return: dict: A { filename: hash } dictionary for all matched files.
69 Empty if none found.
70@@ -394,8 +390,7 @@
71
72
73 def check_hash(path, checksum, hash_type='md5'):
74- """
75- Validate a file using a cryptographic checksum.
76+ """Validate a file using a cryptographic checksum.
77
78 :param str checksum: Value of the checksum used to validate the file.
79 :param str hash_type: Hash algorithm used to generate `checksum`.
80@@ -410,6 +405,7 @@
81
82
83 class ChecksumError(ValueError):
84+ """A class derived from Value error to indicate the checksum failed."""
85 pass
86
87
88@@ -515,7 +511,7 @@
89
90
91 def list_nics(nic_type=None):
92- '''Return a list of nics of given type(s)'''
93+ """Return a list of nics of given type(s)"""
94 if isinstance(nic_type, six.string_types):
95 int_types = [nic_type]
96 else:
97@@ -557,12 +553,13 @@
98
99
100 def set_nic_mtu(nic, mtu):
101- '''Set MTU on a network interface'''
102+ """Set the Maximum Transmission Unit (MTU) on a network interface."""
103 cmd = ['ip', 'link', 'set', nic, 'mtu', mtu]
104 subprocess.check_call(cmd)
105
106
107 def get_nic_mtu(nic):
108+ """Return the Maximum Transmission Unit (MTU) for a network interface."""
109 cmd = ['ip', 'addr', 'show', nic]
110 ip_output = subprocess.check_output(cmd).decode('UTF-8').split('\n')
111 mtu = ""
112@@ -574,6 +571,7 @@
113
114
115 def get_nic_hwaddr(nic):
116+ """Return the Media Access Control (MAC) for a network interface."""
117 cmd = ['ip', '-o', '-0', 'addr', 'show', nic]
118 ip_output = subprocess.check_output(cmd).decode('UTF-8')
119 hwaddr = ""
120@@ -584,7 +582,7 @@
121
122
123 def cmp_pkgrevno(package, revno, pkgcache=None):
124- '''Compare supplied revno with the revno of the installed package
125+ """Compare supplied revno with the revno of the installed package
126
127 * 1 => Installed revno is greater than supplied arg
128 * 0 => Installed revno is the same as supplied arg
129@@ -593,7 +591,7 @@
130 This function imports apt_cache function from charmhelpers.fetch if
131 the pkgcache argument is None. Be sure to add charmhelpers.fetch if
132 you call this function, or pass an apt_pkg.Cache() instance.
133- '''
134+ """
135 import apt_pkg
136 if not pkgcache:
137 from charmhelpers.fetch import apt_cache
138@@ -603,19 +601,27 @@
139
140
141 @contextmanager
142-def chdir(d):
143+def chdir(directory):
144+ """Change the current working directory to a different directory for a code
145+ block and return the previous directory after the block exits. Useful to
146+ run commands from a specificed directory.
147+
148+ :param str directory: The directory path to change to for this context.
149+ """
150 cur = os.getcwd()
151 try:
152- yield os.chdir(d)
153+ yield os.chdir(directory)
154 finally:
155 os.chdir(cur)
156
157
158 def chownr(path, owner, group, follow_links=True, chowntopdir=False):
159- """
160- Recursively change user and group ownership of files and directories
161+ """Recursively change user and group ownership of files and directories
162 in given path. Doesn't chown path itself by default, only its children.
163
164+ :param str path: The string path to start changing ownership.
165+ :param str owner: The owner string to use when looking up the uid.
166+ :param str group: The group string to use when looking up the gid.
167 :param bool follow_links: Also Chown links if True
168 :param bool chowntopdir: Also chown path itself if True
169 """
170@@ -639,15 +645,23 @@
171
172
173 def lchownr(path, owner, group):
174+ """Recursively change user and group ownership of files and directories
175+ in a given path, not following symbolic links. See the documentation for
176+ 'os.lchown' for more information.
177+
178+ :param str path: The string path to start changing ownership.
179+ :param str owner: The owner string to use when looking up the uid.
180+ :param str group: The group string to use when looking up the gid.
181+ """
182 chownr(path, owner, group, follow_links=False)
183
184
185 def get_total_ram():
186- '''The total amount of system RAM in bytes.
187+ """The total amount of system RAM in bytes.
188
189 This is what is reported by the OS, and may be overcommitted when
190 there are multiple containers hosted on the same machine.
191- '''
192+ """
193 with open('/proc/meminfo', 'r') as f:
194 for line in f.readlines():
195 if line:

Subscribers

People subscribed via source and target branches