+
    	:it  c                   s    R t ^ RIt^ RIt ^ RIHt ^ RIt^ RIt^ RI	t	^ RI
t
^RIHt R t ! R R4      tR t ! R R	4      tR#   ] d
    ^ RIHt  LAi ; i)
zU
Utilities for dealing with the compilation of modules and creation
of module tress.
N)getstatusoutput)defaultsc                s    \         P                  ! RV 4      p\        V4      ^ 8X  d    V ^ ,          P                  4       '       d   R# R# )z'Check that a module name is valid.
    z[^a-zA-Z0-9_\-\.]TF)reZfindalllenZisalpha)modnameZms   & 4/usr/lib/python3.14/site-packages/sepolgen/module.pyis_valid_namer   (   s8     	

'1A
1v{wqz))++    c                   sT   a  ] tR t^1t o R tR tR tR tR tR t	R t
RR	 ltR
tV tR# )
ModuleTreec                s     Wn         R V n        R # N)r   dirname)selfr      &&r   __init__ZModuleTree.__init__2   s    r   c                s    V P                   # r	   r
   r      &r   dir_nameZModuleTree.dir_name6   s    ||r   c                X    V P                   R ,           V P                  ,           R,           # )/z.ter
   r   r   r   r   te_nameZModuleTree.te_name9       ||c!DLL0588r   c                r   )r   z.fcr   r   r   r   fc_nameZModuleTree.fc_name<   r   r   c                r   )r   z.ifr   r   r   r   if_nameZModuleTree.if_name?   r   r   c                r   )r   .ppr   r   r   r   package_nameZModuleTree.package_nameB   r   r   c                s(    V P                   R ,           # )z	/Makefiler   r   r   r   makefile_nameZModuleTree.makefile_nameE   s    ||k))r   Nc                s`   VR ,           V P                   ,           V n        \        P                  ! V P                  4       \	        V P                  4       R4      pV'       d   VP                  RV,           4       M+VP                  R\        P                  ! 4       ,           4       VP                  4        \	        V P                  4       R4      P                  4        \	        V P                  4       R4      P                  4        \	        V P                  4       R4      P                  4        R# )r   Zwzinclude N)r   r
   osZmkdirZopenr   writer   refpolicy_makefileZcloser   r   r   )r   Zparent_dirnameZmakefile_includeZfds   &&& r   createZModuleTree.createH   s    %+dll:
$$$&,HHZ"223HHZ("="="??@

 	T\\^S!'')T\\^S!'')T\\^S!'')r   r   r	   )__name__
__module____qualname____firstlineno__r   r   r   r   r   r   r   r   __static_attributes____classdictcell__Z__classdict__   @r   r   r   1   s2     9999** *r   r   c                 s    \         P                  P                  \         P                  P                  V 4      ^,          4      ^ ,          # )i   )r   ZpathZsplitextsplit)
sourcenamer   r   modname_from_sourcenamer*   X   s.    77BGGMM*5a89!<<r   c                   s\   a  ] tR t^[t o RtRR ltR tR tR tRR lt	R t
R	 tR
 tRtV tR# )ModuleCompilera  ModuleCompiler eases running of the module compiler.

The ModuleCompiler class encapsulates running the commandline
module compiler (checkmodule) and module packager (semodule_package).
You are likely interested in the create_module_package method.

Several options are controlled via parameters (only effects the
non-refpol builds):

 .mls          [boolean] Generate an MLS module (by passed -M to
               checkmodule). True to generate an MLS module, false
               otherwise.
               
 .module       [boolean] Generate a module instead of a base module.
               True to generate a module, false to generate a base.
               
 .checkmodule  [string] Fully qualified path to the module compiler.
               Default is /usr/bin/checkmodule.
               
 .semodule_package [string] Fully qualified path to the module
               packager. Defaults to /usr/bin/semodule_package.
 .output       [file object] File object used to write verbose
               output of the compilation and packaging process.
Nc                s    \         P                  ! 4       V n        RV n        RV n        RV n        Wn        RV n        \        P                  ! 4       V n
        RV n        R# )zwCreate a ModuleCompiler instance, optionally with an
output file object for verbose output of the compilation process.
Tz/usr/bin/checkmodulez/usr/bin/semodule_package z/usr/bin/makeN)selinuxZis_selinux_mls_enabledmlsmodulecheckmodulesemodule_packageoutputlast_outputr   r   refpol_makefilemake)r   r2   r   r   r   ZModuleCompiler.__init__t   sP     1131 ;'::<#	r   c                sz    V P                   '       d#   V P                   P                  VR ,           4       Wn        R# )Z
N)r2   r   r3   )r   Zstrr   r   oZModuleCompiler.o   s'    ;;;KKcDj)r   c                sd    V P                  V4       \        V4      w  r#V P                  V4       V# r	   )r6   r    )r   commandrcr2      &&  r   runZModuleCompiler.run   s)    w$W-
v	r   c                s    VP                  R4      p\        V4      ^8  d   \        RV4      hRP                  V^ R 4      pVR,           pVR,           pWE3# )zGenerate the module and policy package filenames from
a source file name. The source file must be in the form
of "foo.te". This will generate "foo.mod" and "foo.pp".

Returns a tuple with (modname, policypackage).
Z.z,invalid sourcefile name %s (must end in .te)z.modr   i)r(   r   RuntimeErrorjoin)r   r)   Z	splitnameZbasenamer   packagenames   &&    r   gen_filenamesZModuleCompiler.gen_filenames   s_     $$S)	y>AMzZZ88IaO,V#&%%r   c                s    V'       d   V P                  V4       R# V P                  V4      w  r4V P                  W4       V P                  W44       \        P
                  ! V4       R# )a4  Create a module package saved in a packagename from a
sourcename.

The create_module_package creates a module package saved in a
file named sourcename (.pp is the standard extension) from a
source file (.te is the standard extension). The source file
should contain SELinux policy statements appropriate for a
base or non-base module (depending on the setting of .module).

Only file names are accepted, not open file objects or
descriptors because the command line SELinux tools are used.

On error a RuntimeError will be raised with a descriptive
error message.
N)refpol_buildr>   compilepackager   Zunlink)r   r)   Z	refpolicyr   r=      &&&  r   create_module_packageZ$ModuleCompiler.create_module_package   sL      j)#'#5#5j#A GLL-LL.IIgr   c                s    V P                   R ,           V P                  ,           pV P                  V4      pV^ 8w  d   \        RV P                  ,          4      hR# )z -f compilation failed:
%sN)r5   r4   r:   r;   r3   )r   r)   r7   r8   r9   r   r?   ZModuleCompiler.refpol_build   sN    ))f$t';';;XXg 784;K;KKLL r   c                s   V P                   .pV P                  '       d   VP                  R 4       V P                  '       d   VP                  R4       VP                  R4       VP                  V4       VP                  V4       V P	                  RP                  V4      4      pV^ 8w  d   \        RV P                  ,          4      hR# )z-M-m-o rD   N)r0   r.   appendr/   r:   r<   r;   r3   )r   r)   r   sr8   rB   r   r@   ZModuleCompiler.compile   s    888HHTN;;;HHTN			XXchhqk"784;K;KKLL r   c                s.   V P                   .pVP                  R 4       VP                  V4       VP                  R4       VP                  V4       V P                  RP                  V4      4      pV^ 8w  d   \	        RV P
                  ,          4      hR# )rF   rE   rG   zpackaging failed [%s]N)r1   rH   r:   r<   r;   r3   )r   r   r=   rI   r8   rB   r   rA   ZModuleCompiler.package   sw    ""#				XXchhqk"769I9IIJJ r   )r0   r3   r5   r.   r/   r2   r4   r1   r	   )T)r    r!   r"   r#   __doc__r   r6   r:   r>   rC   r?   r@   rA   r$   r%   r&   r'   r   r+   r+   [   s;     0$
&"0MM	K 	Kr   r+   )rJ   r   ZtempfileZ
subprocessr    ZImportErrorZcommandsr   Zos.pathZshutilr-   r,   r   r   r   r*   r+   ) r   r   <module>rK      sc   (
 
 )* 
    %* %*N={K {K}  )()s   A AA