<s> __doc__ = """<STR_LIT>""" <EOL> import os , sys , tempfile , shutil <EOL> from . import analyze , token_utils , minification , obfuscate <EOL> py3 = False <EOL> if not isinstance ( sys . version_info , tuple ) : <EOL> if sys . version_info . major == <NUM_LIT:3> : <EOL> py3 = True <EOL> def bz2_pack ( source ) : <EOL> """<STR_LIT>""" <EOL> import bz2 , base64 <EOL> out = "<STR_LIT>" <EOL> first_line = source . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:0> ] <EOL> if analyze . shebang . match ( first_line ) : <EOL> if py3 : <EOL> if first_line . rstrip ( ) . endswith ( '<STR_LIT>' ) : <EOL> first_line = first_line . rstrip ( ) <EOL> first_line += '<STR_LIT:3>' <EOL> out = first_line + '<STR_LIT:\n>' <EOL> compressed_source = bz2 . compress ( source . encode ( '<STR_LIT:utf-8>' ) ) <EOL> out += '<STR_LIT>' <EOL> out += "<STR_LIT>" <EOL> out += base64 . b64encode ( compressed_source ) . decode ( '<STR_LIT:utf-8>' ) <EOL> out += "<STR_LIT>" <EOL> return out <EOL> def gz_pack ( source ) : <EOL> """<STR_LIT>""" <EOL> import zlib , base64 <EOL> out = "<STR_LIT>" <EOL> first_line = source . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:0> ] <EOL> if analyze . shebang . match ( first_line ) : <EOL> if py3 : <EOL> if first_line . rstrip ( ) . endswith ( '<STR_LIT>' ) : <EOL> first_line = first_line . rstrip ( ) <EOL> first_line += '<STR_LIT:3>' <EOL> out = first_line + '<STR_LIT:\n>' <EOL> compressed_source = zlib . compress ( source . encode ( '<STR_LIT:utf-8>' ) ) <EOL> out += '<STR_LIT>' <EOL> out += "<STR_LIT>" <EOL> out += base64 . b64encode ( compressed_source ) . decode ( '<STR_LIT:utf-8>' ) <EOL> out += "<STR_LIT>" <EOL> return out <EOL> def lzma_pack ( source ) : <EOL> """<STR_LIT>""" <EOL> import lzma , base64 <EOL> out = "<STR_LIT>" <EOL> first_line = source . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:0> ] <EOL> if analyze . shebang . match ( first_line ) : <EOL> if py3 : <EOL> if first_line . rstrip ( ) . endswith ( '<STR_LIT>' ) : <EOL> first_line = first_line . rstrip ( ) <EOL> first_line += '<STR_LIT:3>' <EOL> out = first_line + '<STR_LIT:\n>' <EOL> compressed_source = lzma . compress ( source . encode ( '<STR_LIT:utf-8>' ) ) <EOL> out += '<STR_LIT>' <EOL> out += "<STR_LIT>" <EOL> out += base64 . b64encode ( compressed_source ) . decode ( '<STR_LIT:utf-8>' ) <EOL> out += "<STR_LIT>" <EOL> return out <EOL> def prepend ( line , path ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( line , str ) : <EOL> line = line . encode ( '<STR_LIT:utf-8>' ) <EOL> if not line . endswith ( b'<STR_LIT:\n>' ) : <EOL> line += b'<STR_LIT:\n>' <EOL> temp = tempfile . NamedTemporaryFile ( '<STR_LIT:wb>' ) <EOL> temp_name = temp . name <EOL> temp . close ( ) <EOL> with open ( temp_name , '<STR_LIT:wb>' ) as temp : <EOL> temp . write ( line ) <EOL> with open ( path , '<STR_LIT:rb>' ) as r : <EOL> temp . write ( r . read ( ) ) <EOL> shutil . move ( temp_name , path ) <EOL> def zip_pack ( filepath , options ) : <EOL> """<STR_LIT>""" <EOL> import zipfile <EOL> compression_format = zipfile . ZIP_DEFLATED <EOL> cumulative_size = <NUM_LIT:0> <EOL> cumulative_size += os . path . getsize ( filepath ) <EOL> dest = options . pyz <EOL> z = zipfile . ZipFile ( dest , "<STR_LIT:w>" , compression_format ) <EOL> source = open ( filepath ) . read ( ) <EOL> primary_tokens = token_utils . listified_tokenizer ( source ) <EOL> shebang = analyze . get_shebang ( primary_tokens ) <EOL> if not shebang : <EOL> shebang = "<STR_LIT>" <EOL> if py3 : <EOL> if shebang . rstrip ( ) . endswith ( '<STR_LIT>' ) : <EOL> shebang = shebang . rstrip ( ) <EOL> shebang += '<STR_LIT>' <EOL> if not options . nominify : <EOL> source = minification . minify ( primary_tokens , options ) <EOL> temp = tempfile . NamedTemporaryFile ( mode = '<STR_LIT:w>' ) <EOL> temp . write ( source ) <EOL> temp . flush ( ) <EOL> path = os . path . split ( filepath ) [ <NUM_LIT:0> ] <EOL> if not path : <EOL> path = os . getcwd ( ) <EOL> main_py = path + '<STR_LIT>' <EOL> if os . path . exists ( main_py ) : <EOL> z . write ( main_py , '<STR_LIT>' ) <EOL> z . write ( temp . name , os . path . split ( filepath ) [ <NUM_LIT:1> ] ) <EOL> else : <EOL> z . write ( filepath , '<STR_LIT>' ) <EOL> temp . close ( ) <EOL> local_modules = analyze . enumerate_local_modules ( primary_tokens , path ) <EOL> name_generator = None <EOL> if options . obfuscate or options . obf_classes or options . obf_functions or options . obf_variables or options . obf_builtins or options . obf_import_methods : <EOL> identifier_length = int ( options . replacement_length ) <EOL> if options . use_nonlatin : <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> : <EOL> name_generator = obfuscate . obfuscation_machine ( <EOL> use_unicode = True , identifier_length = identifier_length <EOL> ) <EOL> else : <EOL> print ( <EOL> "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> else : <EOL> name_generator = obfuscate . obfuscation_machine ( <EOL> identifier_length = identifier_length ) <EOL> table = [ { } ] <EOL> included_modules = [ ] <EOL> for module in local_modules : <EOL> module = module . replace ( '<STR_LIT:.>' , '<STR_LIT:/>' ) <EOL> module = "<STR_LIT>" % module <EOL> cumulative_size += os . path . getsize ( module ) <EOL> included_modules . append ( module ) <EOL> source = open ( os . path . join ( path , module ) ) . read ( ) <EOL> tokens = token_utils . listified_tokenizer ( source ) <EOL> maybe_more_modules = analyze . enumerate_local_modules ( tokens , path ) <EOL> for mod in maybe_more_modules : <EOL> if mod not in local_modules : <EOL> local_modules . append ( mod ) <EOL> if not options . nominify : <EOL> source = minification . minify ( tokens , options ) <EOL> tokens = token_utils . listified_tokenizer ( source ) <EOL> if name_generator : <EOL> obfuscate . obfuscate ( <EOL> module , <EOL> tokens , <EOL> options , <EOL> name_generator = name_generator , <EOL> table = table <EOL> ) <EOL> result = token_utils . untokenize ( tokens ) <EOL> temp = tempfile . NamedTemporaryFile ( mode = '<STR_LIT:w>' ) <EOL> temp . write ( source ) <EOL> temp . flush ( ) <EOL> z . write ( temp . name , module ) <EOL> temp . close ( ) <EOL> z . close ( ) <EOL> prepend ( shebang , dest ) <EOL> os . chmod ( dest , <NUM_LIT> ) <EOL> pyz_filesize = os . path . getsize ( dest ) <EOL> percent_saved = round ( float ( pyz_filesize ) / float ( cumulative_size ) * <NUM_LIT:100> , <NUM_LIT:2> ) <EOL> print ( '<STR_LIT>' % ( filepath , dest ) ) <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> for module in included_modules : <EOL> print ( '<STR_LIT>' % module ) <EOL> print ( '<STR_LIT>' % percent_saved ) </s>
