
    P1i                         d Z ddlZddlZddlZddlmZ ej                  j                  dd        ej                          ddl	m
Z
 ddlmZ ddlmZ d	 Zd
 Zedk(  r e         e        yy)z8
Test script to debug subprocess spawning from the API.
    N)settingsDJANGO_SETTINGS_MODULEziching.settings)get_user_model)RequestFactory)PersonRelationsAPIViewc                     t        d       t               } | j                  j                  d      }t        d|j                   d|j
                  xs |j                   d       t        d|j                          t               }|j                  d      }||_	        t        d	       t               }|j                  |      }t        d
|j                          t        d|j                          |j                          t        d|j                          ddl}t        d       |j                  d       ddl}	 |j#                  ddgdd      }d|j$                  v rMt        d       |j$                  j'                  d      D ]#  }	d|	v st        d|	j)                                 % nt        d       t        d       t        d       y# t*        $ r}
t        d|
        Y d}
~
3d}
~
ww xY w)z*Test the subprocess spawning functionalityz)=== Testing Subprocess Spawning Debug ===   )idzTesting with user: z ()zCurrent state: z/api/bazi/person-relations/z
--- Calling API View ---zResponse status: zResponse data: zUser state after API call: r   Nz(
--- Waiting for subprocess to start ---   psauxT)capture_outputtextrecalc_bazi_relationsu%   ✓ Background process found running:
z  u   ✗ No background process foundzError checking processes: z
=== Test Complete ===z2Check Django logs for subprocess spawning details.)printr   objectsgetr
   emailusernamegroup_relations_stater   userr   status_codedatarefresh_from_dbtimesleep
subprocessrunstdoutsplitstrip	Exception)Userr   factoryrequestviewresponser   r   resultlinees              J/home/cursorai/projects/iching/iching/tests/../../test_subprocess_debug.pytest_subprocess_spawningr.      s   	
56 D<<q!D	y4::+F*Gq
IJ	OD667
89 Gkk78GGL 

&'!#Dxx H	h223
45	OHMM?
+, 		'(B(B'C
DE 	
56JJqM 
0udN"fmm39:++D1*d2Btzz|n-. 2 34 

#$	
>?	  0*1#.//0s   AG *G 	G7G22G7c                  l   t        d       ddl} ddlt        }t        d|        j                  j                  |      }t        d|        j                  j                  |d      }t        d|        t        dj                  j                  |              j                  j                  |      r$t        d	| d
 j                  |      dd         t        d       	 j                  j                         }d|d<   t        d|j                  d              t        dt        j                          ddl}d| d|j                  dd       d| d}|j                  ddd      5 }|j!                  |       |j"                  ddd        j$                  d       | j'                  dg||| j(                  | j(                  d      }fd}	ddl}
|
j-                  |	d      }|j/                          t        d|j0                          ddl}|j5                  d        |j7                         7t        d!       |j9                          |j;                          t        d"       yt        d#|j<                          |j?                         \  }}|rt        d$|jA                  d%d&'              |r t        d(|jA                  d%d&'              yy# 1 sw Y   ZxY w# tB        $ r,}t        d)|        ddl"}|jG                          Y d}~yd}~ww xY w)*z4Test subprocess spawning directly to debug the issuez+
=== Testing Direct Subprocess Spawning ===r   NzCurrent file: zCalculated cwd: z	manage.pyzmanage.py path: zmanage.py exists: z	Files in z: 
   z!
--- Testing Subprocess Spawn ---development
DJANGO_ENVzEnvironment: DJANGO_ENV=zPython executable: z#!/bin/bash
cd "zo"

# Activate the virtual environment
source venv/bin/activate

# Set environment variables
export DJANGO_ENV="z"
export PYTHONPATH="a4  "
export DJANGO_SETTINGS_MODULE="iching.settings"

# Execute the management command and log the result
python manage.py recalc_bazi_relations --user 1 --force > logs/recalc_bazi_relations_wrapper.log 2>&1

# Log completion status
echo "Command completed at $(date)" >> logs/recalc_bazi_relations_wrapper.log
wz.shF)modesuffixdeletei  z	/bin/bashT)envcwdr!   stderrstart_new_sessionc                  d    dd l } | j                  d       	  j                         y #  Y y xY w)Nr      )r   r   unlink)r   osscript_paths    r-   cleanup_scriptz.test_direct_subprocess.<locals>.cleanup_script   s.    JJqM		+&s   + /)targetdaemonzProcess spawned with PID: r	   u   ✓ Process is runningzProcess terminatedu   ✗ Process exited with code: zstderr: zutf-8ignore)errorszstdout: zError spawning subprocess: )$r   r   r>   __file__pathdirnamejoinexistslistdirenvironcopyr   sys
executabletempfileNamedTemporaryFilewritenamechmodPopenPIPE	threadingThreadstartpidr   r   poll	terminatewait
returncodecommunicatedecoder$   	traceback	print_exc)r   current_filer8   manage_pathr7   rO   script_contentfprocessr@   rV   cleanup_threadr   r!   r9   r,   r`   r>   r?   s                    @@r-   test_direct_subprocessrh   D   s   	
89 L	N<.
)* ''//,
'C	SE
"# '',,sK0K	[M
*+	rww~~k:;
<= 
ww~~c	#bC"!5 678 

./Mjjoo)L()>(?@A#CNN#345 	E 
 GGL-89 :5 & ((c%(NRSGGN#&&K O
 	e$ ""$
JOOJOO_c # e
	 	")))M*7;;-89 	

1<<>!*+LLN&'273E3E2FGH$002NFFwx!H IJKwx!H IJK S ONX  +A3/0s9   /BK> ;K1C(K> A-K> 1K;6K> >	L3"L..L3__main__)__doc__r>   rM   djangodjango.confr   rK   
setdefaultsetupdjango.contrib.authr   django.testr   api.views_person_relationsr   r.   rh   __name__     r-   <module>ru      sm    
 
    

  .0A B  . & =/@bgR z rt   