<s> import itertools , os , os . path , platform , shutil , subprocess , sys , tarfile , zipfile <EOL> import settings <EOL> negative = itertools . count ( - <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> UNCONFIGURED = negative . next ( ) <EOL> BUNDLE_MISSING = negative . next ( ) <EOL> UNSAFE_DELETE = negative . next ( ) <EOL> BAD_BUNDLE = negative . next ( ) <EOL> SRC_INSTALL_FAILED = negative . next ( ) <EOL> positive = itertools . count ( <NUM_LIT:1> ) <EOL> RECOMPILE_FAILED = positive . next ( ) <EOL> BIN_INSTALL_FAILED = positive . next ( ) <EOL> REOBFUSCATE_FAILED = positive . next ( ) <EOL> PACKAGE_FAILED = positive . next ( ) <EOL> if settings . UNCONFIGURED : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> sys . exit ( UNCONFIGURED ) <EOL> absolute = lambda rawpath : os . path . abspath ( os . path . expanduser ( rawpath ) ) <EOL> relative = lambda relpath : absolute ( os . path . join ( BASE , relpath ) ) <EOL> BASE = absolute ( settings . BASE ) <EOL> USER = relative ( settings . USER ) <EOL> TARGET = relative ( settings . TARGET ) <EOL> SOURCE_BUNDLE = relative ( settings . SOURCE_BUNDLE ) <EOL> os . chdir ( BASE ) <EOL> if not os . path . exists ( USER ) : <EOL> os . makedirs ( USER ) <EOL> with open ( os . path . join ( USER , "<STR_LIT>" ) , "<STR_LIT:w>" ) as catfile : <EOL> catfile . write ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not os . path . exists ( TARGET ) : <EOL> os . makedirs ( TARGET ) <EOL> MCP_SRC_REL = "<STR_LIT:src>" <EOL> MCP_SRC = relative ( MCP_SRC_REL ) <EOL> MCP_SRC_CLIENT = os . path . join ( MCP_SRC , "<STR_LIT>" ) <EOL> MCP_SRC_SERVER = os . path . join ( MCP_SRC , "<STR_LIT>" ) <EOL> MCP_BIN = relative ( "<STR_LIT>" ) <EOL> MCP_BIN_CLIENT = os . path . join ( MCP_BIN , "<STR_LIT>" ) <EOL> MCP_BIN_SERVER = os . path . join ( MCP_BIN , "<STR_LIT>" ) <EOL> MCP_REOBF = relative ( "<STR_LIT>" ) <EOL> MCP_REOBF_CLIENT = os . path . join ( MCP_REOBF , "<STR_LIT>" ) <EOL> MCP_REOBF_SERVER = os . path . join ( MCP_REOBF , "<STR_LIT>" ) <EOL> WINDOWS = ( platform . system ( ) == "<STR_LIT>" ) <EOL> if WINDOWS : <EOL> RECOMPILE = relative ( "<STR_LIT>" ) <EOL> else : <EOL> RECOMPILE = relative ( "<STR_LIT>" ) <EOL> if WINDOWS : <EOL> REOBFUSCATE = relative ( "<STR_LIT>" ) <EOL> else : <EOL> REOBFUSCATE = relative ( "<STR_LIT>" ) <EOL> class Project ( object ) : <EOL> def __init__ ( self , directory ) : <EOL> self . dir = directory <EOL> self . name = self . get_config ( "<STR_LIT>" ) or os . path . basename ( directory ) <EOL> self . version = self . get_config ( "<STR_LIT>" ) <EOL> self . package_name = self . get_config ( "<STR_LIT>" ) <EOL> self . hide_source = self . get_config ( "<STR_LIT>" , is_boolean = True ) <EOL> self . package_command = self . get_config ( "<STR_LIT>" ) <EOL> def get_config ( self , setting , is_boolean = False ) : <EOL> filename = os . path . join ( self . dir , "<STR_LIT>" , setting ) <EOL> exists = os . path . isfile ( filename ) <EOL> if is_boolean : <EOL> return exists <EOL> elif not exists : <EOL> return None <EOL> else : <EOL> return open ( filename ) . read ( ) . strip ( ) <EOL> @ classmethod <EOL> def load_obfuscation ( cls ) : <EOL> cls . client_obfuscation = cls . _load_obfuscation ( "<STR_LIT>" ) <EOL> cls . server_obfuscation = cls . _load_obfuscation ( "<STR_LIT>" ) <EOL> @ classmethod <EOL> def _load_obfuscation ( cls , filename ) : <EOL> obfuscation = { } <EOL> lines = open ( os . path . join ( BASE , filename ) ) . readlines ( ) <EOL> for line in lines : <EOL> if line . startswith ( "<STR_LIT>" ) : <EOL> prefix , obfuscated , plain = line . split ( ) <EOL> if os . path . sep != "<STR_LIT:/>" : <EOL> obfuscated = obfuscated . replace ( "<STR_LIT:/>" , os . path . sep ) <EOL> plain = plain . replace ( "<STR_LIT:/>" , os . path . sep ) <EOL> obfuscation [ plain ] = obfuscated <EOL> return obfuscation <EOL> @ staticmethod <EOL> def collect_projects ( root , projects ) : <EOL> """<STR_LIT>""" <EOL> for ( dir , subdirs , files ) in os . walk ( root , followlinks = True ) : <EOL> if "<STR_LIT>" in files : <EOL> del subdirs [ : ] <EOL> print "<STR_LIT>" % dir <EOL> elif "<STR_LIT>" in files : <EOL> pass <EOL> print "<STR_LIT>" % dir <EOL> else : <EOL> projects . append ( Project ( dir ) ) <EOL> del subdirs [ : ] <EOL> print "<STR_LIT>" % dir <EOL> def copy_files ( self , source , dest , failcode ) : <EOL> for ( source_dir , subdirs , files ) in os . walk ( source , followlinks = True ) : <EOL> dest_dir = os . path . join ( dest , os . path . relpath ( source_dir , source ) ) <EOL> if not os . path . exists ( dest_dir ) : <EOL> os . makedirs ( dest_dir ) <EOL> for file in files : <EOL> try : <EOL> shutil . copy2 ( os . path . join ( source_dir , file ) , dest_dir ) <EOL> except shutil . WindowsError : <EOL> pass <EOL> def install ( self ) : <EOL> """<STR_LIT>""" <EOL> did_something = False <EOL> src = os . path . join ( self . dir , "<STR_LIT:src>" ) <EOL> if os . path . isdir ( src ) : <EOL> common = os . path . join ( src , "<STR_LIT>" ) <EOL> if os . path . isdir ( common ) and os . listdir ( common ) : <EOL> self . copy_files ( common , MCP_SRC_CLIENT , SRC_INSTALL_FAILED ) <EOL> self . copy_files ( common , MCP_SRC_SERVER , SRC_INSTALL_FAILED ) <EOL> did_something = True <EOL> client = os . path . join ( src , "<STR_LIT>" ) <EOL> if os . path . isdir ( client ) and os . listdir ( client ) : <EOL> self . copy_files ( client , MCP_SRC_CLIENT , SRC_INSTALL_FAILED ) <EOL> did_something = True <EOL> server = os . path . join ( src , "<STR_LIT>" ) <EOL> if os . path . isdir ( server ) and os . listdir ( server ) : <EOL> self . copy_files ( server , MCP_SRC_SERVER , SRC_INSTALL_FAILED ) <EOL> did_something = True <EOL> return did_something <EOL> def install_precompiled ( self ) : <EOL> """<STR_LIT>""" <EOL> did_something = False <EOL> bin = os . path . join ( self . dir , "<STR_LIT>" ) <EOL> if os . path . isdir ( bin ) : <EOL> common = os . path . join ( bin , "<STR_LIT>" ) <EOL> if os . path . isdir ( common ) and os . listdir ( common ) : <EOL> self . copy_files ( common , MCP_BIN_CLIENT , BIN_INSTALL_FAILED ) <EOL> self . copy_files ( common , MCP_BIN_SERVER , BIN_INSTALL_FAILED ) <EOL> did_something = True <EOL> client = os . path . join ( bin , "<STR_LIT>" ) <EOL> if os . path . isdir ( client ) and os . listdir ( client ) : <EOL> self . copy_files ( client , MCP_BIN_CLIENT , BIN_INSTALL_FAILED ) <EOL> did_something = True <EOL> server = os . path . join ( bin , "<STR_LIT>" ) <EOL> if os . path . isdir ( server ) and os . listdir ( server ) : <EOL> self . copy_files ( server , MCP_BIN_SERVER , BIN_INSTALL_FAILED ) <EOL> did_something = True <EOL> return did_something <EOL> def get_package_file ( self , server = False ) : <EOL> if self . package_name is not None : <EOL> filename = self . package_name <EOL> else : <EOL> if self . version is not None : <EOL> filename = "<STR_LIT>" % ( self . name , self . version ) <EOL> else : <EOL> filename = "<STR_LIT:%s>" % self . name <EOL> if server : <EOL> filename += "<STR_LIT>" <EOL> filename += "<STR_LIT>" <EOL> return os . path . join ( TARGET , filename ) <EOL> @ staticmethod <EOL> def collect_files ( root ) : <EOL> all_files = set ( ) <EOL> if not os . path . isdir ( root ) : <EOL> return all_files <EOL> for ( dir , subdirs , files ) in os . walk ( root , followlinks = True ) : <EOL> for file in files : <EOL> full_name = os . path . join ( dir , file ) <EOL> relative_name = os . path . relpath ( full_name , root ) <EOL> all_files . add ( relative_name ) <EOL> return all_files <EOL> @ classmethod <EOL> def map_to_class ( cls , files , server = False ) : <EOL> """<STR_LIT>""" <EOL> if server : <EOL> obfuscation = cls . server_obfuscation <EOL> else : <EOL> obfuscation = cls . client_obfuscation <EOL> classes = [ ] <EOL> for file in files : <EOL> identifier , ext = os . path . splitext ( file ) <EOL> if ext . lower ( ) != "<STR_LIT>" : <EOL> continue <EOL> if identifier in obfuscation : <EOL> identifier = obfuscation [ identifier ] <EOL> prefix = os . path . join ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:src>" , "<STR_LIT>" ) <EOL> if identifier . startswith ( prefix ) : <EOL> identifier = identifier [ len ( prefix ) : ] <EOL> classes . append ( identifier + "<STR_LIT>" ) <EOL> return classes <EOL> def zip ( self , archive_name , files = None , clean = False ) : <EOL> if clean : <EOL> mode = "<STR_LIT:w>" <EOL> else : <EOL> mode = "<STR_LIT:a>" <EOL> with zipfile . ZipFile ( archive_name , mode ) as archive : <EOL> if files is None : <EOL> for dir , subdirs , files in os . walk ( "<STR_LIT:.>" , followlinks = True ) : <EOL> for file in files : <EOL> archive . write ( os . path . join ( dir , file ) ) <EOL> else : <EOL> for file in files : <EOL> archive . write ( file ) <EOL> def package ( self ) : <EOL> """<STR_LIT>""" <EOL> def call_or_die ( cmd , shell = False ) : <EOL> exit = subprocess . call ( cmd , shell = shell ) <EOL> if exit != <NUM_LIT:0> : <EOL> print "<STR_LIT>" % cmd <EOL> print "<STR_LIT>" % project . name <EOL> sys . exit ( PACKAGE_FAILED ) <EOL> if project . package_command is not None : <EOL> call_or_die ( project . package_command , shell = True ) <EOL> return True <EOL> else : <EOL> client_dir = os . path . join ( self . dir , "<STR_LIT:src>" , "<STR_LIT>" ) <EOL> client_sources = self . collect_files ( client_dir ) <EOL> server_dir = os . path . join ( self . dir , "<STR_LIT:src>" , "<STR_LIT>" ) <EOL> server_sources = self . collect_files ( server_dir ) <EOL> common_dir = os . path . join ( self . dir , "<STR_LIT:src>" , "<STR_LIT>" ) <EOL> common_sources = self . collect_files ( common_dir ) <EOL> client_sources . update ( common_sources ) <EOL> server_sources . update ( common_sources ) <EOL> client_classes = self . map_to_class ( client_sources ) <EOL> server_classes = self . map_to_class ( server_sources , server = True ) <EOL> os . chdir ( MCP_REOBF_CLIENT ) <EOL> client_package = self . get_package_file ( ) <EOL> client_created = False <EOL> if client_classes : <EOL> if os . path . exists ( client_package ) : <EOL> os . remove ( client_package ) <EOL> self . zip ( client_package , client_classes , clean = True ) <EOL> client_created = True <EOL> os . chdir ( MCP_REOBF_SERVER ) <EOL> server_package = self . get_package_file ( server = True ) <EOL> server_created = False <EOL> if server_classes : <EOL> if os . path . exists ( server_package ) : <EOL> os . remove ( server_package ) <EOL> self . zip ( server_package , server_classes , clean = True ) <EOL> server_created = True <EOL> if not ( client_created or server_created ) : <EOL> print "<STR_LIT>" <EOL> return False <EOL> common_resources = os . path . join ( self . dir , "<STR_LIT>" , "<STR_LIT>" ) <EOL> if os . path . isdir ( common_resources ) : <EOL> os . chdir ( common_resources ) <EOL> if client_created : <EOL> self . zip ( client_package ) <EOL> if server_created : <EOL> self . zip ( server_package ) <EOL> client_resources = os . path . join ( self . dir , "<STR_LIT>" , "<STR_LIT>" ) <EOL> if os . path . isdir ( client_resources ) : <EOL> os . chdir ( client_resources ) <EOL> if client_created : <EOL> self . zip ( client_package ) <EOL> server_resources = os . path . join ( self . dir , "<STR_LIT>" , "<STR_LIT>" ) <EOL> if os . path . isdir ( server_resources ) : <EOL> os . chdir ( server_resources ) <EOL> if server_created : <EOL> self . zip ( server_package ) <EOL> if self . hide_source : <EOL> return True <EOL> common_source = os . path . join ( self . dir , "<STR_LIT:src>" , "<STR_LIT>" ) <EOL> if os . path . isdir ( common_source ) and os . listdir ( common_source ) : <EOL> os . chdir ( common_source ) <EOL> if client_created : <EOL> self . zip ( client_package ) <EOL> if server_created : <EOL> self . zip ( server_package ) <EOL> client_source = os . path . join ( self . dir , "<STR_LIT:src>" , "<STR_LIT>" ) <EOL> if os . path . isdir ( client_source ) and os . listdir ( client_source ) : <EOL> os . chdir ( client_source ) <EOL> if client_created : <EOL> self . zip ( client_package ) <EOL> server_source = os . path . join ( self . dir , "<STR_LIT:src>" , "<STR_LIT>" ) <EOL> if os . path . isdir ( server_source ) and os . listdir ( server_source ) : <EOL> os . chdir ( server_source ) <EOL> if server_created : <EOL> self . zip ( server_package ) <EOL> return True <EOL> print "<STR_LIT>" <EOL> if not os . path . exists ( SOURCE_BUNDLE ) : <EOL> sys . stdout . write ( "<STR_LIT>" ) <EOL> answer = sys . stdin . readline ( ) . lower ( ) <EOL> if answer . startswith ( "<STR_LIT:y>" ) : <EOL> print "<STR_LIT>" <EOL> with tarfile . open ( SOURCE_BUNDLE , "<STR_LIT>" ) as archive : <EOL> archive . add ( MCP_SRC_REL ) <EOL> print "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> sys . exit ( BUNDLE_MISSING ) <EOL> else : <EOL> if os . path . exists ( MCP_SRC ) : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print MCP_SRC <EOL> sys . stdout . write ( "<STR_LIT>" ) <EOL> answer = sys . stdin . readline ( ) . lower ( ) <EOL> if not answer . startswith ( "<STR_LIT:y>" ) : <EOL> print "<STR_LIT>" <EOL> sys . exit ( UNSAFE_DELETE ) <EOL> shutil . rmtree ( MCP_SRC ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> with tarfile . open ( SOURCE_BUNDLE , "<STR_LIT>" ) as archive : <EOL> archive . extractall ( ) <EOL> print "<STR_LIT>" <EOL> print <EOL> print "<STR_LIT>" <EOL> projects = [ ] <EOL> if not os . path . isdir ( USER ) : <EOL> print "<STR_LIT>" <EOL> else : <EOL> Project . collect_projects ( USER , projects ) <EOL> count = <NUM_LIT:0> <EOL> for project in projects : <EOL> if project . install ( ) : <EOL> count += <NUM_LIT:1> <EOL> print "<STR_LIT>" % count <EOL> print <EOL> print "<STR_LIT>" <EOL> exit = subprocess . call ( RECOMPILE , shell = True ) <EOL> if exit != <NUM_LIT:0> : <EOL> print "<STR_LIT>" <EOL> sys . exit ( RECOMPILE_FAILED ) <EOL> count = <NUM_LIT:0> <EOL> for project in projects : <EOL> if project . install_precompiled ( ) : <EOL> count += <NUM_LIT:1> <EOL> print "<STR_LIT>" % count <EOL> exit = subprocess . call ( REOBFUSCATE , shell = True ) <EOL> if exit != <NUM_LIT:0> : <EOL> print "<STR_LIT>" <EOL> sys . exit ( REOBFUSCATE_FAILED ) <EOL> print "<STR_LIT>" <EOL> print <EOL> print "<STR_LIT>" <EOL> Project . load_obfuscation ( ) <EOL> package_count = <NUM_LIT:0> <EOL> for project in projects : <EOL> print "<STR_LIT>" % project . name <EOL> os . chdir ( BASE ) <EOL> if project . package ( ) : <EOL> package_count += <NUM_LIT:1> <EOL> print "<STR_LIT>" % package_count </s>
