
    3j1                        d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z
ddlmZ ddlmZ ddlmZ ddlmZ  ej"                  d      Zej&                   G d	 d
             Zd ZddZd Zedk(  r e        yy)a#  
Multiprocess testing.  Tests all doctests and Test unittest objects in all
modules that are imported when running "import music21".  Runs a thread on
each core of a multicore system unless there are more than 2 cores, in which
case it runs on n-1 cores.

N.B. this gets a slightly different set of modules than test/test.py does
because the `imp` module is not available for threaded processing.  Running
both modules gives great coverage of just about everything -- do that before
building a new release.

Run test/testDocumentation after this.
    )annotationsN)environment)common)
testRunner)
commonTestztest.multiprocessTestc                      e Zd ZU dZded<   dZded<   dZded<   dZded<   dZded<   dZ	ded	<   dZ
ded
<   dZded<   dZded<   y)ModuleResponseNz
str | None
returnCodezt.Anyfp
moduleNamesuccessr   errorsfailurestestsRunrunTime)__name__
__module____qualname__r
   __annotations__r   r   r   r   r   r   r   r        J/DATA/.local/lib/python3.12/site-packages/music21/test/multiprocessTest.pyr	   r	   (   s\    JBJGUJFEHeHeGUr   r	   c                <   | d   }| d   }d}t        j                          }|j                  |      }t        j                  d|d|d       |j	                  |      }|dk(  r(| d}t        j                  |       t        d	||
      S |dk(  r(| d}t        j                  |       t        d||
      S 	 |j	                  |d      }t        j                         }	t        |d      st        j                  | d       n:t        j                  j                  |j                        }
|	j                  |
       	 t        j                  |      }|	j                  |       t        j                   |	       t        j                  d       t        j"                  |      }	 |j%                  |	      }g }|j&                  D ]  }|j)                  |d           g }|j*                  D ]  }|j)                  |d           t-        dt        j                          |z
  z        dz  }t        d|||j/                         t1        |      |||j2                  |	      S # t        $ r t        j                  | d       Y w xY w# t4        $ r?}t        j                  d| d| d       t        d||d t1        |            cY d }~S d }~ww xY w# t4        $ r=}t        j                  d| d| d       t        d|t1        |            cY d }~S d }~ww xY w) Nr      Fzrunning fp=z moduleObject=
skipz is skipped 
Skipped)r
   r   r   	notInTreezI is in the music21 directory but not imported in music21. Skipped -- fix!	NotInTree)addM21Testz has no Test classz cannot load Doctestszrunning Tests...
)	verbosity
         $@TestsRun)	r
   r   r   r   r   r   r   r   r   z*** Exception in running : z...
TrappedException)r
   r   r   r   r   z*** Large Exception in running LargeException)r
   r   r   )timegetModuleWithoutImpenvironLocal
printDebug_getNamePeriodr	   r   defaultDoctestSuitehasattrunittestdefaultTestLoaderloadTestsFromTestCaser!   addTests
ValueErrorr   fixDoctestsMusic21TestRunnerrunr   appendr   roundwasSuccessfulstrr   	Exception)argsmodGathr   r"   	timeStartmoduleObject
namePeriodr   r   s1s2s3runner
testResultr   er   fr   excps                       r   runOneModuleWithoutImprJ   7   s   1gG	aBI		I..r2Llre?\O2>?''+JvD'(r7KK		$lcd 	 	(WMM74++Bu+=
++- |V,##|n4F$GH++AA,BSBSTBKKO	//=BKKO
 	r" 45--	B	$BJ F&&ad# 'H((!% )B$))+	"9:;dBG!Z%'-7*4*B*B*D-0_)/+3+5+>+>*13 3'  	##|n4I$JK	8  	$##&?
|2dVSX$YZ!-?%'-7*.-0Y	$ $	$  4"A"RvU ST)9!#),T4 	44so   :BK &I" 'A K (B9J
 "!JK JK 
	K4KKK KK 	L2LLLc                `   t        j                          t        j                  }t	        j                         }t        j                         }t        d| d| d       t        j                  d      d}j                  }t        j                  |      5 }|j                  t        fd|D              }	d}
d	}d	}g }|
r	 |	j                  d
      }|dk\  rt        d       ||j                  |j                  }|j!                  dd      }nd}|j"                  }|t%        |j"                  dz        dz  }|j&                  s,|j(                  s t        d| d|j*                   d| d       nOt-        |j&                        }t-        |j(                        }t        d| d|j*                   d| d| d| d       d	}|d
z  }|j/                  |       |
rddd       |t        _        t5        ||       y# t        j0                  $ r |d
z  }|dk(  r|d	kD  rt        dd       n|dk(  rt        dd       |dz  d	k(  rt        t3        |      dz   dd       ||kD  rE|d	kD  r@t        dd       d }
t5        |||       |j7                          t        j8                          Y t:        $ r& d }
|j7                          |j=                          Y t>        $ r7}|d
z  }tA        d!t3        |      "      }|j/                  |       Y d}~<d}~ww xY w# 1 sw Y   BxY w)#z>
    Run all tests. Group can be "test" and/or "external"
    z	Creating z) processes for multiprocessing (omitting z processors)T)useExtendedd   )	processesc              3  &   K   | ]  }|f 
 y w)Nr   ).0r   	modGathers     r   	<genexpr>z!mainPoolRunner.<locals>.<genexpr>   s     $Jzi_zs   r   r   )timeout    N	___init__r#   r$   z				r&   
 tests in z secsz tests, z errors z failures in zDelay in processing, seconds: )endz2Starting first modules, should take 5-10 seconds:  )rX   flushz
Too many delays, giving up.)rZ   FUntrappedException)r
   r   )!r   testImportssysstderrr)   r   cpusprintModuleGathermodulePathsmultiprocessingPoolimap_unorderedrJ   nextr   replacer   r9   r   r   r   lenr8   TimeoutErrorr;   printSummarycloseexitStopIterationjoinr<   r	   )	testGrouprestoreEnvironmentDefaultsleaveOutnormalStdErrorr?   poolSize
maxTimeout
pathsToRunpoolres
continueIttimeoutseventsProcessedsummaryOutput	newResultmnrtnumErrnumFailrI   exceptionLogrQ   s                        @r   mainPoolRunnerr      s    ZZN		I{{}H	IhZH
R^
_`''D9IJ&&J 
			1T !!"8$Jz$JL 
03HHQH/	q="I( ++7&11ZZR8"**B~"9#4#4r#9:TA(//	8J8J!HRD93E3E2FjQSPTTY"Z[%()9)9%:F&))*<*<&=G!HRD93E3E2Fh%+HHWI]2$e#U V1$$$Y//  
2D  CJ	:69 #// Aq=_q%8:C]NTVWa<1$#h-#-2TBj(_q-@9F!&J 	:FJJLHHJ  "


		 31$-3"4y  $$\223u 
2	1sP   +L#DHL#B.L 2L#4*L L#!L ),LL#L  L##L-c                	   d}| D cg c]  }|j                    }}|D ]%  }||vst        d|      }| j                  |       ' d}g }	g }
g }g }| D ]  }t        |       |j                  dk(  r|	j                  d|j                           =|j                  dk(  r |j                  d|j                    d       l|j                  d	k(  r|j                  d
|j                           |j                  dk(  r9|j                  d|j
                   d|j                    d|j                          |j                  dk(  r-|j                  d|j                    d|j                          |j                  dk(  r |j                  d|j                           M|j                  dk(  r2|j
                  dk(  sm|j                  d|j
                   d       |j                  dk(  r||j                  z  }|j                  r:|
j                  |j
                   d|j                   d|j                   d       |j                  }|j                  }|j                  d|j
                   dt        |       dt        |       d|j                   d|j                   d       |D ]$  }||d z   z  }|j                  t        |             & |D ]$  }||d z   z  }|j                  t        |             & |j                  d!|         |d"z  }|	D ]
  }||d z   z  } |
D ]
  }||d z   z  } |D ]
  }||d z   z  } |D ]
  }||d z   z  } |d#z  }t        j                         |z
  }|d$| d|d%d&z  }t         j"                  j%                          t        |       t         j"                  j%                          dd l}t(        j*                  j-                  t.        j1                         d'      }t3        |d(d)*      5 }|j5                  |       |j5                  d+|j&                  j7                         j9                  d,      z          d d d        t        d-|z          y c c}w # 1 sw Y   xY w).NrU   NoResult)r
   r   r   r   z	Skipped: zSilent test fail for z: Run separately!r[   z(Untrapped Exception for unknown module: r'   zTrapped Exception for module z, at r&   r(   zLarge Exception for file ImportErrorzImport Error for r   zNot in Tree Error: rY   r%   z successfully ran rW   z secondsz
-----------------------------
z had z ERRORS and z FAILURES in z tests after z( seconds:
-----------------------------
r   zUnknown return code zL

---------------SUMMARY---------------------------------------------------
zJ-------------------------------------------------------------------------
zRan z.4fz	 seconds
zlastResults.txtwzutf-8)encodingzRun at z%Y-%m-%d %H:%M:%SzResults at )r   r	   r8   r`   r
   r   r   rF   r   r   r   r   r   rh   r;   r)   r]   stdoutrZ   datetimeospathrn   r+   getRootTempDiropenwritenowstrftime)r{   r?   ru   outStrisummaryOutputTwor   failLog
totalTestsskippedSummarysuccessSummaryerrorsFoundSummaryotherSummarymoduleResponse
errorsListfailuresListrG   rH   lineelapsedTimer   lastResultss                         r   rj   rj      s   F&34mm4%%$
rBG  ) 
 JNNL'n$$	1!!In.?.?-@"AB&&*4"78I8I7JJ[ \]&&*>>"J>K\K\J] ^_&&*<</0I0I/J K$''(!,,-/
 &&*::+N,=,=+>b!,,-/ &&-7"3N4E4E3F GH&&+5((B.##&9.:S:S9TTU$VW&&*4.111J%%%%%0011C%../z%--.h8 ,22
-66"))7%001:'|<()%../}%--. /66 $Aa$h&F&--c!f5 $ &Aa$h&F&--c!f5 & "6~6F GHi (l __F$+ $+ $+ "$+ #
[[F))+	)K
ZL
;s*;:FFFJJ	&MJJ'',,|::<>OPK	k3	1Q			H--113<<=PQQR 
2 
-+
%&q 5h 
2	1s   Q2AQ77R __main__))testFr   )__doc__
__future__r   dataclassesrc   r   r]   r)   r0   typingtmusic21r   r   music21.testr   r   Environmentr+   	dataclassr	   rJ   r   rj   r   r   r   r   <module>r      s    #   	 
      # #&{&&'>?	 	 	L4^V7rZ'z z 	 r   