<s> import compileall <EOL> import imp <EOL> import os <EOL> import py_compile <EOL> import shutil <EOL> import struct <EOL> import tempfile <EOL> from test import test_support <EOL> import unittest <EOL> class CompileallTests ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . directory = tempfile . mkdtemp ( ) <EOL> self . source_path = os . path . join ( self . directory , '<STR_LIT>' ) <EOL> self . bc_path = self . source_path + ( '<STR_LIT:c>' if __debug__ else '<STR_LIT:o>' ) <EOL> with open ( self . source_path , '<STR_LIT:w>' ) as file : <EOL> file . write ( '<STR_LIT>' ) <EOL> self . source_path2 = os . path . join ( self . directory , '<STR_LIT>' ) <EOL> self . bc_path2 = self . source_path2 + ( '<STR_LIT:c>' if __debug__ else '<STR_LIT:o>' ) <EOL> shutil . copyfile ( self . source_path , self . source_path2 ) <EOL> def tearDown ( self ) : <EOL> shutil . rmtree ( self . directory ) <EOL> def data ( self ) : <EOL> with open ( self . bc_path , '<STR_LIT:rb>' ) as file : <EOL> data = file . read ( <NUM_LIT:8> ) <EOL> mtime = int ( os . stat ( self . source_path ) . st_mtime ) <EOL> compare = struct . pack ( '<STR_LIT>' , imp . get_magic ( ) , mtime ) <EOL> return data , compare <EOL> @ unittest . skipUnless ( hasattr ( os , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def recreation_check ( self , metadata ) : <EOL> """<STR_LIT>""" <EOL> py_compile . compile ( self . source_path ) <EOL> self . assertEqual ( * self . data ( ) ) <EOL> with open ( self . bc_path , '<STR_LIT:rb>' ) as file : <EOL> bc = file . read ( ) [ len ( metadata ) : ] <EOL> with open ( self . bc_path , '<STR_LIT:wb>' ) as file : <EOL> file . write ( metadata ) <EOL> file . write ( bc ) <EOL> self . assertNotEqual ( * self . data ( ) ) <EOL> compileall . compile_dir ( self . directory , force = False , quiet = True ) <EOL> self . assertTrue ( * self . data ( ) ) <EOL> def test_mtime ( self ) : <EOL> self . recreation_check ( struct . pack ( '<STR_LIT>' , imp . get_magic ( ) , <NUM_LIT:1> ) ) <EOL> def test_magic_number ( self ) : <EOL> self . recreation_check ( b'<STR_LIT>' ) <EOL> def test_compile_files ( self ) : <EOL> for fn in ( self . bc_path , self . bc_path2 ) : <EOL> try : <EOL> os . unlink ( fn ) <EOL> except : <EOL> pass <EOL> compileall . compile_file ( self . source_path , force = False , quiet = True ) <EOL> self . assertTrue ( os . path . isfile ( self . bc_path ) and not os . path . isfile ( self . bc_path2 ) ) <EOL> os . unlink ( self . bc_path ) <EOL> compileall . compile_dir ( self . directory , force = False , quiet = True ) <EOL> self . assertTrue ( os . path . isfile ( self . bc_path ) and os . path . isfile ( self . bc_path2 ) ) <EOL> os . unlink ( self . bc_path ) <EOL> os . unlink ( self . bc_path2 ) <EOL> def test_main ( ) : <EOL> test_support . run_unittest ( CompileallTests ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
