55             "    https://pypi.python.org/pypi/setuptools" )
66
77import  contextlib 
8+ from  contextlib  import  ExitStack 
89import  os 
910from  pathlib  import  Path 
1011import  shutil 
2425
2526
2627@contextlib .contextmanager  
27- def  patched_path (path , * pairs ):
28-     orig_contents   =   contents  =  path .read_text ()
29-     for  old ,  new  in  pairs :
30-         contents   =   contents . replace ( old ,  new )
28+ def  patched_path (path , old ,  new ):
29+     contents  =  path .read_text ()
30+     if  old   not  in contents :
31+         raise   Exception ( f"Invalid patch:  { old } " 
3132    try :
32-         path .write_text (contents )
33+         path .write_text (contents . replace ( old ,  new ) )
3334        yield 
3435    finally :
35-         path .write_text (orig_contents )
36+         path .write_text (contents )
37+ 
38+ 
39+ patches  =  [
40+     ("Makefiles/Makefile_linux_shared" ,
41+      "$(COMPILE_FLAGS)" , "$(COMPILE_FLAGS) $(CFLAGS)" ),
42+     ("System.cpp" ,  # redeal issue 19. 
43+      "free -k | tail -n+3 | head -n1 | awk '{print $NF}'" ,
44+      r"grep -Po 'MemAvailable:\\s*\\K[0-9]*' /proc/meminfo || " 
45+      r"grep -Po 'MemFree:\\s*\\K[0-9]*' /proc/meminfo" ),
46+     ("dds.cpp" ,  # dds issue #91. 
47+      "FreeMemory();" , "" ),
48+     ("Makefiles/Makefile_Mac_clang_shared" ,
49+      "$(COMPILE_FLAGS)" , "$(COMPILE_FLAGS) $(CFLAGS)" ),
50+     ("Makefiles/Makefile_Mac_clang_shared" ,
51+      "$(LINK_FLAGS)" , "$(LINK_FLAGS) -lc++" ),
52+ ]
3653
3754
3855class  build_ext (build_ext ):
@@ -54,20 +71,14 @@ def build_extensions(self):
5471    git submodule init && git submodule update 
5572
5673On a Unix system, do not use the zip archives from github.""" )
57-             if  sys .platform .startswith ("linux" ):
58-                 # Patch dds issue #91. 
59-                 with  patched_path (
60-                          dds_src  /  "Makefiles/Makefile_linux_shared" ,
61-                          ("$(COMPILE_FLAGS)" , "$(COMPILE_FLAGS) $(CFLAGS)" )), \
62-                      patched_path (dds_src  /  "dds.cpp" , ("FreeMemory();" , "" )):
74+             with  ExitStack () as  stack :
75+                 for  name , old , new  in  patches :
76+                     stack .enter_context (patched_path (dds_src  /  name , old , new ))
77+                 if  sys .platform .startswith ("linux" ):
6378                    subprocess .check_call (
6479                        ["make" , "-f" , "Makefiles/Makefile_linux_shared" ,
6580                         "THREADING=" , "THREAD_LINK=" ], cwd = dds_src )
66-             elif  sys .platform  ==  "darwin" :
67-                 with  patched_path (
68-                         dds_src  /  "Makefiles/Makefile_Mac_clang_shared" ,
69-                         ("$(COMPILE_FLAGS)" , "$(COMPILE_FLAGS) $(CFLAGS)" ),
70-                         ("$(LINK_FLAGS)" , "$(LINK_FLAGS) -lc++" )):
81+                 elif  sys .platform  ==  "darwin" :
7182                    subprocess .check_call (
7283                        ["make" , "-f" , "Makefiles/Makefile_Mac_clang_shared" ,
7384                         "CC=gcc" , "THREADING=" , "THREAD_LINK=" ], cwd = dds_src )
0 commit comments