1212from subprocess import Popen , PIPE , STDOUT , call
1313
1414
15-
1615def get_immediate_subdirectories (a_dir ):
1716 return [(os .path .join (a_dir , name )) for name in os .listdir (a_dir )
1817 if os .path .isdir (os .path .join (a_dir , name ))]
1918
2019
21- TMP_DIR = ""
22-
23- def ParallelExtractDir (args , dir ):
24- ExtractFeaturesForDir (args , dir , "" )
20+ def ParallelExtractDir (args , tmpdir , dir_ ):
21+ ExtractFeaturesForDir (args ,tmpdir , dir_ , "" )
2522
2623
27- def ExtractFeaturesForDir (args , dir , prefix ):
24+ def ExtractFeaturesForDir (args , tmpdir , dir_ , prefix ):
2825 command = ['java' , '-cp' , args .jar , 'JavaExtractor.App' ,
2926 '--max_path_length' , str (args .max_path_length ), '--max_path_width' , str (args .max_path_width ),
30- '--dir' , dir , '--num_threads' , str (args .num_threads )]
31-
27+ '--dir' , dir_ , '--num_threads' , str (args .num_threads )]
3228 # print command
3329 # os.system(command)
3430 kill = lambda process : process .kill ()
35- outputFileName = TMP_DIR + prefix + dir .split ('/' )[- 1 ]
31+ outputFileName = tmpdir + prefix + dir_ .split ('/' )[- 1 ]
3632 failed = False
3733 with open (outputFileName , 'a' ) as outputFile :
38- sleeper = subprocess .Popen (command , stdout = outputFile , stderr = subprocess .PIPE )
34+ sleeper = subprocess .Popen (command , stdout = outputFile , stderr = subprocess .PIPE , )
3935 timer = Timer (600000 , kill , [sleeper ])
40-
4136 try :
4237 timer .start ()
4338 stdout , stderr = sleeper .communicate ()
@@ -48,32 +43,31 @@ def ExtractFeaturesForDir(args, dir, prefix):
4843 if len (stderr ) > 0 :
4944 print (sys .stderr , stderr , file = sys .stdout )
5045 else :
51- print (sys .stderr , 'dir: ' + str (dir ) + ' was not completed in time' , file = sys .stdout )
46+ print (sys .stderr , 'dir: ' + str (dir_ ) + ' was not completed in time' , file = sys .stdout , flush = True )
5247 failed = True
53- subdirs = get_immediate_subdirectories (dir )
48+ subdirs = get_immediate_subdirectories (dir_ )
5449 for subdir in subdirs :
55- ExtractFeaturesForDir (args , subdir , prefix + dir .split ('/' )[- 1 ] + '_' )
50+ ExtractFeaturesForDir (args , subdir , prefix + dir_ .split ('/' )[- 1 ] + '_' )
5651 if failed :
5752 if os .path .exists (outputFileName ):
5853 os .remove (outputFileName )
5954
6055
6156def ExtractFeaturesForDirsList (args , dirs ):
62- global TMP_DIR
63- TMP_DIR = "./tmp/feature_extractor%d/" % (os .getpid ())
64- if os .path .exists (TMP_DIR ):
65- shutil .rmtree (TMP_DIR , ignore_errors = True )
66- os .makedirs (TMP_DIR )
57+ tmp_dir = f"./tmp/feature_extractor{ os .getpid ()} /"
58+ if os .path .exists (tmp_dir ):
59+ shutil .rmtree (tmp_dir , ignore_errors = True )
60+ os .makedirs (tmp_dir )
6761 try :
6862 p = multiprocessing .Pool (4 )
69- p .starmap (ParallelExtractDir , zip (itertools .repeat (args ), dirs ))
63+ p .starmap (ParallelExtractDir , zip (itertools .repeat (args ),itertools . repeat ( tmp_dir ), dirs ))
7064 #for dir in dirs:
7165 # ExtractFeaturesForDir(args, dir, '')
72- output_files = os .listdir (TMP_DIR )
66+ output_files = os .listdir (tmp_dir )
7367 for f in output_files :
74- os .system ("cat %s/%s" % (TMP_DIR , f ))
68+ os .system ("cat %s/%s" % (tmp_dir , f ))
7569 finally :
76- shutil .rmtree (TMP_DIR , ignore_errors = True )
70+ shutil .rmtree (tmp_dir , ignore_errors = True )
7771
7872
7973if __name__ == '__main__' :
0 commit comments