def guess_archive_type(pathname):
    """
    Guess the file type of a Python source distribution archive.

    Checks for known "magic" file headers to identify the type of the archive.
    Previously this used the ``file`` executable, now it checks the magic file
    headers itself. I could have used any of the numerous ``libmagic`` bindings
    on PyPI, but that would add a binary dependency to ``pip-accel`` and I
    don't want that :-).

    :param pathname: The pathname of an existing archive (a string).
    :returns: One of the strings ``gzip``, ``bzip2`` or ``zip`` or the value
              ``None`` when the filename extension cannot be guessed based on
              the file header.
    """
    with open(pathname, 'rb') as handle:
        header = handle.read(2)
    if header.startswith(b'\x1f\x8b'):
        # The gzip compression header is two bytes: 0x1F, 0x8B.
        return 'gzip'
    elif header.startswith(b'BZ'):
        # The bzip2 compression header is two bytes: B, Z.
        return 'bzip2'
    elif header.startswith(b'PK'):
        # According to Wikipedia, ZIP archives don't have an official magic
        # number, but most of the time we'll find two bytes: P, K (for Phil
        # Katz, creator of the format).
        return 'zip'