code: def write_if_allowed(filename: str,
                       content: str,
                       overwrite: bool = False,
                       mock: bool = False) -> None:
      
      # Check we're allowed
      if not overwrite and exists(filename):
          fail("File exists, not overwriting: {!r}".format(filename))
  
      # Make the directory, if necessary
      directory = dirname(filename)
      if not mock:
          mkdir_p(directory)
  
      # Write the file
      log.info("Writing to {!r}", filename)
      if mock:
          log.warning("Skipping writes as in mock mode")
      else:
          with open(filename, "wt") as outfile:
              outfile.write(content)
docs: Writes the contents to a file, if permitted.
  
      Args:
          filename: filename to write
          content: contents to write
          overwrite: permit overwrites?
          mock: pretend to write, but don't
  
      Raises:
          RuntimeError: if file exists but overwriting not permitted