<s> """<STR_LIT>""" <EOL> __revision__ = "<STR_LIT>" <EOL> import os <EOL> import SCons <EOL> from SCons . Action import Action <EOL> from SCons . Builder import Builder <EOL> from xml . dom . minidom import * <EOL> from xml . sax . saxutils import escape <EOL> from SCons . Tool . packaging import stripinstallbuilder <EOL> def convert_to_id ( s , id_set ) : <EOL> """<STR_LIT>""" <EOL> charset = '<STR_LIT>' <EOL> if s [ <NUM_LIT:0> ] in '<STR_LIT>' : <EOL> s += '<STR_LIT:_>' + s <EOL> id = [ c for c in s if c in charset ] <EOL> try : <EOL> return id_set [ id ] [ s ] <EOL> except KeyError : <EOL> if id not in id_set : id_set [ id ] = { s : id } <EOL> else : id_set [ id ] [ s ] = id + str ( len ( id_set [ id ] ) ) <EOL> return id_set [ id ] [ s ] <EOL> def is_dos_short_file_name ( file ) : <EOL> """<STR_LIT>""" <EOL> fname , ext = os . path . splitext ( file ) <EOL> proper_ext = len ( ext ) == <NUM_LIT:0> or ( <NUM_LIT:2> <= len ( ext ) <= <NUM_LIT:4> ) <EOL> proper_fname = file . isupper ( ) and len ( fname ) <= <NUM_LIT:8> <EOL> return proper_ext and proper_fname <EOL> def gen_dos_short_file_name ( file , filename_set ) : <EOL> """<STR_LIT>""" <EOL> if is_dos_short_file_name ( file ) : <EOL> return file <EOL> fname , ext = os . path . splitext ( file ) <EOL> file = file . upper ( ) <EOL> if is_dos_short_file_name ( file ) : <EOL> return file <EOL> forbidden = '<STR_LIT>' <EOL> fname = [ c for c in fname if c not in forbidden ] <EOL> duplicate , num = not None , <NUM_LIT:1> <EOL> while duplicate : <EOL> shortname = "<STR_LIT>" % ( fname [ : <NUM_LIT:8> - len ( str ( num ) ) ] . upper ( ) , str ( num ) ) <EOL> if len ( ext ) >= <NUM_LIT:2> : <EOL> shortname = "<STR_LIT>" % ( shortname , ext [ : <NUM_LIT:4> ] . upper ( ) ) <EOL> duplicate , num = shortname in filename_set , num + <NUM_LIT:1> <EOL> assert ( is_dos_short_file_name ( shortname ) ) , '<STR_LIT>' % ( shortname , file ) <EOL> filename_set . append ( shortname ) <EOL> return shortname <EOL> def create_feature_dict ( files ) : <EOL> """<STR_LIT>""" <EOL> dict = { } <EOL> def add_to_dict ( feature , file ) : <EOL> if not SCons . Util . is_List ( feature ) : <EOL> feature = [ feature ] <EOL> for f in feature : <EOL> if f not in dict : <EOL> dict [ f ] = [ file ] <EOL> else : <EOL> dict [ f ] . append ( file ) <EOL> for file in files : <EOL> if hasattr ( file , '<STR_LIT>' ) : <EOL> add_to_dict ( file . PACKAGING_X_MSI_FEATURE , file ) <EOL> elif hasattr ( file , '<STR_LIT>' ) : <EOL> add_to_dict ( '<STR_LIT>' , file ) <EOL> else : <EOL> add_to_dict ( '<STR_LIT:default>' , file ) <EOL> return dict <EOL> def generate_guids ( root ) : <EOL> """<STR_LIT>""" <EOL> from hashlib import md5 <EOL> needs_id = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> for ( key , value ) in needs_id . items ( ) : <EOL> node_list = root . getElementsByTagName ( key ) <EOL> attribute = value <EOL> for node in node_list : <EOL> hash = md5 ( node . toxml ( ) ) . hexdigest ( ) <EOL> hash_str = '<STR_LIT>' % ( hash [ : <NUM_LIT:8> ] , hash [ <NUM_LIT:8> : <NUM_LIT:12> ] , hash [ <NUM_LIT:12> : <NUM_LIT:16> ] , hash [ <NUM_LIT:16> : <NUM_LIT:20> ] , hash [ <NUM_LIT:20> : ] ) <EOL> node . attributes [ attribute ] = hash_str <EOL> def string_wxsfile ( target , source , env ) : <EOL> return "<STR_LIT>" % ( target [ <NUM_LIT:0> ] . path ) <EOL> def build_wxsfile ( target , source , env ) : <EOL> """<STR_LIT>""" <EOL> file = open ( target [ <NUM_LIT:0> ] . abspath , '<STR_LIT:w>' ) <EOL> try : <EOL> doc = Document ( ) <EOL> root = doc . createElement ( '<STR_LIT>' ) <EOL> root . attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> doc . appendChild ( root ) <EOL> filename_set = [ ] <EOL> id_set = { } <EOL> build_wxsfile_header_section ( root , env ) <EOL> build_wxsfile_file_section ( root , source , env [ '<STR_LIT>' ] , env [ '<STR_LIT>' ] , env [ '<STR_LIT>' ] , filename_set , id_set ) <EOL> generate_guids ( root ) <EOL> build_wxsfile_features_section ( root , source , env [ '<STR_LIT>' ] , env [ '<STR_LIT>' ] , env [ '<STR_LIT>' ] , id_set ) <EOL> build_wxsfile_default_gui ( root ) <EOL> build_license_file ( target [ <NUM_LIT:0> ] . get_dir ( ) , env ) <EOL> file . write ( doc . toprettyxml ( ) ) <EOL> if '<STR_LIT>' in env : <EOL> env [ '<STR_LIT>' ] ( target , source ) <EOL> except KeyError , e : <EOL> raise SCons . Errors . UserError ( '<STR_LIT>' % e . args [ <NUM_LIT:0> ] ) <EOL> def create_default_directory_layout ( root , NAME , VERSION , VENDOR , filename_set ) : <EOL> """<STR_LIT>""" <EOL> doc = Document ( ) <EOL> d1 = doc . createElement ( '<STR_LIT>' ) <EOL> d1 . attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> d1 . attributes [ '<STR_LIT:Name>' ] = '<STR_LIT>' <EOL> d2 = doc . createElement ( '<STR_LIT>' ) <EOL> d2 . attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> d2 . attributes [ '<STR_LIT:Name>' ] = '<STR_LIT>' <EOL> d3 = doc . createElement ( '<STR_LIT>' ) <EOL> d3 . attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> d3 . attributes [ '<STR_LIT:Name>' ] = escape ( gen_dos_short_file_name ( VENDOR , filename_set ) ) <EOL> d3 . attributes [ '<STR_LIT>' ] = escape ( VENDOR ) <EOL> d4 = doc . createElement ( '<STR_LIT>' ) <EOL> project_folder = "<STR_LIT>" % ( NAME , VERSION ) <EOL> d4 . attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> d4 . attributes [ '<STR_LIT:Name>' ] = escape ( gen_dos_short_file_name ( project_folder , filename_set ) ) <EOL> d4 . attributes [ '<STR_LIT>' ] = escape ( project_folder ) <EOL> d1 . childNodes . append ( d2 ) <EOL> d2 . childNodes . append ( d3 ) <EOL> d3 . childNodes . append ( d4 ) <EOL> root . getElementsByTagName ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . childNodes . append ( d1 ) <EOL> return d4 <EOL> def build_wxsfile_file_section ( root , files , NAME , VERSION , VENDOR , filename_set , id_set ) : <EOL> """<STR_LIT>""" <EOL> root = create_default_directory_layout ( root , NAME , VERSION , VENDOR , filename_set ) <EOL> components = create_feature_dict ( files ) <EOL> factory = Document ( ) <EOL> def get_directory ( node , dir ) : <EOL> """<STR_LIT>""" <EOL> if dir == '<STR_LIT>' or not dir : <EOL> return node <EOL> Directory = node <EOL> dir_parts = dir . split ( os . path . sep ) <EOL> upper_dir = '<STR_LIT>' <EOL> dir_parts = [ d for d in dir_parts if d != '<STR_LIT>' ] <EOL> for d in dir_parts [ : ] : <EOL> already_created = [ c for c in Directory . childNodes <EOL> if c . nodeName == '<STR_LIT>' <EOL> and c . attributes [ '<STR_LIT>' ] . value == escape ( d ) ] <EOL> if already_created != [ ] : <EOL> Directory = already_created [ <NUM_LIT:0> ] <EOL> dir_parts . remove ( d ) <EOL> upper_dir += d <EOL> else : <EOL> break <EOL> for d in dir_parts : <EOL> nDirectory = factory . createElement ( '<STR_LIT>' ) <EOL> nDirectory . attributes [ '<STR_LIT>' ] = escape ( d ) <EOL> nDirectory . attributes [ '<STR_LIT:Name>' ] = escape ( gen_dos_short_file_name ( d , filename_set ) ) <EOL> upper_dir += d <EOL> nDirectory . attributes [ '<STR_LIT>' ] = convert_to_id ( upper_dir , id_set ) <EOL> Directory . childNodes . append ( nDirectory ) <EOL> Directory = nDirectory <EOL> return Directory <EOL> for file in files : <EOL> drive , path = os . path . splitdrive ( file . PACKAGING_INSTALL_LOCATION ) <EOL> filename = os . path . basename ( path ) <EOL> dirname = os . path . dirname ( path ) <EOL> h = { <EOL> '<STR_LIT>' : '<STR_LIT:yes>' , <EOL> '<STR_LIT>' : convert_to_id ( filename , id_set ) , <EOL> '<STR_LIT>' : filename , <EOL> '<STR_LIT>' : gen_dos_short_file_name ( filename , filename_set ) , <EOL> '<STR_LIT>' : file . get_path ( ) , <EOL> } <EOL> for k , v in [ ( k , v ) for ( k , v ) in h . items ( ) if not hasattr ( file , k ) ] : <EOL> setattr ( file , k , v ) <EOL> File = factory . createElement ( '<STR_LIT>' ) <EOL> File . attributes [ '<STR_LIT>' ] = escape ( file . PACKAGING_X_MSI_LONGNAME ) <EOL> File . attributes [ '<STR_LIT:Name>' ] = escape ( file . PACKAGING_X_MSI_SHORTNAME ) <EOL> File . attributes [ '<STR_LIT>' ] = escape ( file . PACKAGING_X_MSI_SOURCE ) <EOL> File . attributes [ '<STR_LIT>' ] = escape ( file . PACKAGING_X_MSI_FILEID ) <EOL> File . attributes [ '<STR_LIT>' ] = escape ( file . PACKAGING_X_MSI_VITAL ) <EOL> Component = factory . createElement ( '<STR_LIT>' ) <EOL> Component . attributes [ '<STR_LIT>' ] = '<STR_LIT:1>' <EOL> Component . attributes [ '<STR_LIT>' ] = convert_to_id ( filename , id_set ) <EOL> Directory = get_directory ( root , dirname ) <EOL> Directory . childNodes . append ( Component ) <EOL> Component . childNodes . append ( File ) <EOL> def build_wxsfile_features_section ( root , files , NAME , VERSION , SUMMARY , id_set ) : <EOL> """<STR_LIT>""" <EOL> factory = Document ( ) <EOL> Feature = factory . createElement ( '<STR_LIT>' ) <EOL> Feature . attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> Feature . attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> Feature . attributes [ '<STR_LIT>' ] = '<STR_LIT:1>' <EOL> Feature . attributes [ '<STR_LIT>' ] = escape ( '<STR_LIT>' % ( NAME , VERSION ) ) <EOL> Feature . attributes [ '<STR_LIT>' ] = escape ( SUMMARY ) <EOL> Feature . attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> for ( feature , files ) in create_feature_dict ( files ) . items ( ) : <EOL> SubFeature = factory . createElement ( '<STR_LIT>' ) <EOL> SubFeature . attributes [ '<STR_LIT>' ] = '<STR_LIT:1>' <EOL> if SCons . Util . is_Tuple ( feature ) : <EOL> SubFeature . attributes [ '<STR_LIT>' ] = convert_to_id ( feature [ <NUM_LIT:0> ] , id_set ) <EOL> SubFeature . attributes [ '<STR_LIT>' ] = escape ( feature [ <NUM_LIT:0> ] ) <EOL> SubFeature . attributes [ '<STR_LIT>' ] = escape ( feature [ <NUM_LIT:1> ] ) <EOL> else : <EOL> SubFeature . attributes [ '<STR_LIT>' ] = convert_to_id ( feature , id_set ) <EOL> if feature == '<STR_LIT:default>' : <EOL> SubFeature . attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> SubFeature . attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif feature == '<STR_LIT>' : <EOL> SubFeature . attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> SubFeature . attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> SubFeature . attributes [ '<STR_LIT>' ] = escape ( feature ) <EOL> SubFeature . attributes [ '<STR_LIT>' ] = escape ( feature ) <EOL> for f in files : <EOL> ComponentRef = factory . createElement ( '<STR_LIT>' ) <EOL> ComponentRef . attributes [ '<STR_LIT>' ] = convert_to_id ( os . path . basename ( f . get_path ( ) ) , id_set ) <EOL> SubFeature . childNodes . append ( ComponentRef ) <EOL> Feature . childNodes . append ( SubFeature ) <EOL> root . getElementsByTagName ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . childNodes . append ( Feature ) <EOL> def build_wxsfile_default_gui ( root ) : <EOL> """<STR_LIT>""" <EOL> factory = Document ( ) <EOL> Product = root . getElementsByTagName ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> UIRef = factory . createElement ( '<STR_LIT>' ) <EOL> UIRef . attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> Product . childNodes . append ( UIRef ) <EOL> UIRef = factory . createElement ( '<STR_LIT>' ) <EOL> UIRef . attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> Product . childNodes . append ( UIRef ) <EOL> def build_license_file ( directory , spec ) : <EOL> """<STR_LIT>""" <EOL> name , text = '<STR_LIT>' , '<STR_LIT>' <EOL> try : <EOL> name = spec [ '<STR_LIT>' ] <EOL> text = spec [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> if name != '<STR_LIT>' or text != '<STR_LIT>' : <EOL> file = open ( os . path . join ( directory . get_path ( ) , '<STR_LIT>' ) , '<STR_LIT:w>' ) <EOL> file . write ( '<STR_LIT>' ) <EOL> if text != '<STR_LIT>' : <EOL> file . write ( text . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) ) <EOL> else : <EOL> file . write ( name + '<STR_LIT>' ) <EOL> file . write ( '<STR_LIT:}>' ) <EOL> file . close ( ) <EOL> def build_wxsfile_header_section ( root , spec ) : <EOL> """<STR_LIT>""" <EOL> factory = Document ( ) <EOL> Product = factory . createElement ( '<STR_LIT>' ) <EOL> Package = factory . createElement ( '<STR_LIT>' ) <EOL> root . childNodes . append ( Product ) <EOL> Product . childNodes . append ( Package ) <EOL> if '<STR_LIT>' not in spec : <EOL> spec [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> Product . attributes [ '<STR_LIT:Name>' ] = escape ( spec [ '<STR_LIT>' ] ) <EOL> Product . attributes [ '<STR_LIT>' ] = escape ( spec [ '<STR_LIT>' ] ) <EOL> Product . attributes [ '<STR_LIT>' ] = escape ( spec [ '<STR_LIT>' ] ) <EOL> Product . attributes [ '<STR_LIT>' ] = escape ( spec [ '<STR_LIT>' ] ) <EOL> Package . attributes [ '<STR_LIT>' ] = escape ( spec [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in spec : <EOL> Package . attributes [ '<STR_LIT>' ] = escape ( spec [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in spec : <EOL> Package . attributes [ '<STR_LIT>' ] = escape ( spec [ '<STR_LIT>' ] ) <EOL> Media = factory . createElement ( '<STR_LIT>' ) <EOL> Media . attributes [ '<STR_LIT>' ] = '<STR_LIT:1>' <EOL> Media . attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> Media . attributes [ '<STR_LIT>' ] = '<STR_LIT:yes>' <EOL> root . getElementsByTagName ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . childNodes . append ( Media ) <EOL> wxs_builder = Builder ( <EOL> action = Action ( build_wxsfile , string_wxsfile ) , <EOL> ensure_suffix = '<STR_LIT>' ) <EOL> def package ( env , target , source , PACKAGEROOT , NAME , VERSION , <EOL> DESCRIPTION , SUMMARY , VENDOR , X_MSI_LANGUAGE , ** kw ) : <EOL> SCons . Tool . Tool ( '<STR_LIT>' ) . generate ( env ) <EOL> loc = locals ( ) <EOL> del loc [ '<STR_LIT>' ] <EOL> kw . update ( loc ) <EOL> del kw [ '<STR_LIT:source>' ] , kw [ '<STR_LIT:target>' ] , kw [ '<STR_LIT>' ] <EOL> target , source = stripinstallbuilder ( target , source , env ) <EOL> env [ '<STR_LIT>' ] = kw <EOL> specfile = wxs_builder ( * [ env , target , source ] , ** kw ) <EOL> msifile = env . WiX ( target , specfile ) <EOL> return ( msifile , source + [ specfile ] ) </s>
