Simplify RequirementTracker.add(req)
utilize try-except-else structure better
- enables reducing a level of indentation
add comments to describe the flow of the method
drop once-or-twice use variables
@@ -55,21 +55,33 @@ class RequirementTracker(object):
def add(self, req):
# type: (InstallRequirement) -> None
- link = req.link
- info = str(req)
- entry_path = self._entry_path(link)
+ """Add an InstallRequirement to build tracking.
+ """
+
+ # Get the file to write information about this requirement.
+ entry_path = self._entry_path(req.link)
+
+ # Try reading from the file. If it exists and can be read from, a build
+ # is already in progress, so a LookupError is raised.
try:
with open(entry_path) as fp:
- # Error, these's already a build in progress.
- raise LookupError('%s is already being built: %s'
- % (link, fp.read()))
+ contents = fp.read()
except IOError as e:
+ # if the error is anything other than "file does not exist", raise.
if e.errno != errno.ENOENT:
raise
+ else:
+ message = '%s is already being built: %s' % (req.link, contents)
+ raise LookupError(message)
+
+ # If we're here, req should really not be building already.
assert req not in self._entries
+
+ # Start tracking this requirement.
with open(entry_path, 'w') as fp:
- fp.write(info)
+ fp.write(str(req))
self._entries.add(req)
+
logger.debug('Added %s to build tracker %r', req, self._root)
def remove(self, req):
