summaryrefslogtreecommitdiffstats
path: root/packaging/sdist/check-link-behavior.py
blob: 34e05023d48e0d2ccd6b52acb7c7b92c2bd81cbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
"""Checks for link behavior required for sdist to retain symlinks."""

from __future__ import (absolute_import, division, print_function)

__metaclass__ = type

import os
import platform
import shutil
import sys
import tempfile


def main():
    """Main program entry point."""
    temp_dir = tempfile.mkdtemp()

    target_path = os.path.join(temp_dir, 'file.txt')
    symlink_path = os.path.join(temp_dir, 'symlink.txt')
    hardlink_path = os.path.join(temp_dir, 'hardlink.txt')

    try:
        with open(target_path, 'w'):
            pass

        os.symlink(target_path, symlink_path)
        os.link(symlink_path, hardlink_path)

        if not os.path.islink(symlink_path):
            abort('Symbolic link not created.')

        if not os.path.islink(hardlink_path):
            # known issue on MacOS (Darwin)
            abort('Hard link of symbolic link created as a regular file.')
    finally:
        shutil.rmtree(temp_dir)


def abort(reason):
    """
    :type reason: str
    """
    sys.exit('ERROR: %s\n'
             'This will prevent symbolic links from being preserved in the resulting tarball.\n'
             'Aborting creation of sdist on platform: %s'
             % (reason, platform.system()))


if __name__ == '__main__':
    main()