1+ from setuptools import setup
2+ from setuptools .command .build import build
3+ from setuptools .command .build_ext import build_ext
4+ from setuptools .command .build_py import build_py
5+ from setuptools .command .egg_info import egg_info
6+ import tempfile
7+ import atexit
8+ import shutil
9+ import sys
10+
11+ # Create a single temp directory for all build operations
12+ _TEMP_BUILD_DIR = None
13+
14+ def get_temp_build_dir (pkg_name ):
15+ global _TEMP_BUILD_DIR
16+ if _TEMP_BUILD_DIR is None :
17+ _TEMP_BUILD_DIR = tempfile .mkdtemp (prefix = f'{ pkg_name } _build_' )
18+ atexit .register (lambda : shutil .rmtree (_TEMP_BUILD_DIR , ignore_errors = True ))
19+ return _TEMP_BUILD_DIR
20+
21+ class TempDirBuildMixin :
22+ def initialize_options (self ):
23+ super ().initialize_options ()
24+ temp_dir = get_temp_build_dir (self .distribution .get_name ())
25+ self .build_base = temp_dir
26+
27+ class TempDirEggInfoMixin :
28+ def initialize_options (self ):
29+ super ().initialize_options ()
30+ temp_dir = get_temp_build_dir (self .distribution .get_name ())
31+ self .egg_base = temp_dir
32+
33+ class CustomBuild (TempDirBuildMixin , build ):
34+ pass
35+
36+ class CustomBuildPy (TempDirBuildMixin , build_py ):
37+ pass
38+
39+ class CustomBuildExt (TempDirBuildMixin , build_ext ):
40+ pass
41+
42+ class CustomEggInfo (TempDirEggInfoMixin , egg_info ):
43+ def initialize_options (self ):
44+ # Don't use temp dir for editable installs
45+ if '--editable' in sys .argv or '-e' in sys .argv :
46+ egg_info .initialize_options (self )
47+ else :
48+ super ().initialize_options ()
49+
50+ setup (
51+ name = "fine_python_flake8" ,
52+ cmdclass = {
53+ 'build' : CustomBuild ,
54+ 'build_py' : CustomBuildPy ,
55+ 'build_ext' : CustomBuildExt ,
56+ 'egg_info' : CustomEggInfo ,
57+ }
58+ )
0 commit comments