(args)
return 0
Executor for the p8tool build command.

    Args:
        args: The argparse.Namespace arguments object.
Executor for the p8tool build command.
Executor for the p8tool build command .
def do_build(args):
    """Executor for the p8tool build command.

    Args:
        args: The argparse.Namespace arguments object.
    """
    if (not args.filename.endswith('.p8') and
            not args.filename.endswith('.p8.png')):
        util.error('Output filename must end with .p8 or .p8.png.')
        return 1

    empty_source = game.Game.make_empty_game(filename=args.filename)

    # Determine whether output file exists and if so load it, otherwise
    # create an empty cart.
    if os.path.exists(args.filename):
        result = file.from_file(args.filename)
    else:
        result = game.Game.make_empty_game(filename=args.filename)

    for section in ('lua', 'gfx', 'gff', 'map', 'sfx', 'music'):
        if getattr(args, section, None) is not None:

            # Verify "empty" overrides don't conflict with provided sources.
            if getattr(args, 'empty_' + section, False):
                util.error('Cannot specify --%s and --empty-%s args '
                           'together.' % (section, section))
                return 1

            # Verify source files exist and are of supported types.
            fn = getattr(args, section)
            if not os.path.exists(fn):
                util.error('File "%s" given for --%s arg does not exist.' %
                           (fn, section))
                return 1
            if (not fn.endswith('.p8') and
                    not fn.endswith('.p8.png') and
                    not (section == 'lua' and fn.endswith('.lua'))):
                util.error(
                    'Unsupported file type for --%s arg.' % (section,))
                return 1

            # Load section from source and store it in the result.
            if section == 'lua' and fn.endswith('.lua'):
                with open(fn, 'rb') as infh:
                    result.lua = lua.Lua.from_lines(
                        infh, version=game.DEFAULT_VERSION)
                    package_lua = {}
                    _evaluate_require(
                        result.lua, file_path=fn, package_lua=package_lua,
                        lua_path=getattr(args, 'lua_path', None))

                    if getattr(args, 'optimize_tokens', False):
                        # TODO: perform const subst, dead code elim, taking
                        # package_lua into account
                        raise NotImplementedError(
                            '--optimize_tokens not yet implemented, sorry')

                    result.lua = _prepend_package_lua(result.lua, package_lua)
                    _remove_global_return(result.lua)
            else:
                source = file.from_file(fn)
                setattr(result, section, getattr(source, section))

        elif getattr(args, 'empty_' + section, False):
            setattr(result, section, getattr(empty_source, section))

    # TODO: allow overriding the label source for .p8.png

    # Save result as args.filename.
    lua_writer_cls = None
    lua_writer_args = None
    if getattr(args, 'lua_format', False):
        lua_writer_cls = lua.LuaFormatterWriter,
        lua_writer_args = {
            'indentwidth': args.indentwidth,
            # 'keep_property_names': args.keep_property_names,
            'keep_all_names': args.keep_all_names,
            'keep_names_from_file': args.keep_names_from_file}
    elif getattr(args, 'lua_minify', False):
        lua_writer_cls = lua.LuaMinifyTokenWriter
    file.to_file(
        result, filename=args.filename,
        lua_writer_cls=lua_writer_cls,
        lua_writer_args=lua_writer_args)

    return 0
def do_build ( args ) : if ( not args . filename . endswith ( '.p8' ) and not args . filename . endswith ( '.p8.png' ) ) : util . error ( 'Output filename must end with .p8 or .p8.png.' ) return 1 empty_source = game . Game . make_empty_game ( filename = args . filename ) # Determine whether output file exists and if so load it, otherwise # create an empty cart. if os . path . exists ( args . filename ) : result = file . from_file ( args . filename ) else : result = game . Game . make_empty_game ( filename = args . filename ) for section in ( 'lua' , 'gfx' , 'gff' , 'map' , 'sfx' , 'music' ) : if getattr ( args , section , None ) is not None : # Verify "empty" overrides don't conflict with provided sources. if getattr ( args , 'empty_' + section , False ) : util . error ( 'Cannot specify --%s and --empty-%s args ' 'together.' % ( section , section ) ) return 1 # Verify source files exist and are of supported types. fn = getattr ( args , section ) if not os . path . exists ( fn ) : util . error ( 'File "%s" given for --%s arg does not exist.' % ( fn , section ) ) return 1 if ( not fn . endswith ( '.p8' ) and not fn . endswith ( '.p8.png' ) and not ( section == 'lua' and fn . endswith ( '.lua' ) ) ) : util . error ( 'Unsupported file type for --%s arg.' % ( section , ) ) return 1 # Load section from source and store it in the result. if section == 'lua' and fn . endswith ( '.lua' ) : with open ( fn , 'rb' ) as infh : result . lua = lua . Lua . from_lines ( infh , version = game . DEFAULT_VERSION ) package_lua = { } _evaluate_require ( result . lua , file_path = fn , package_lua = package_lua , lua_path = getattr ( args , 'lua_path' , None ) ) if getattr ( args , 'optimize_tokens' , False ) : # TODO: perform const subst, dead code elim, taking # package_lua into account raise NotImplementedError ( '--optimize_tokens not yet implemented, sorry' ) result . lua = _prepend_package_lua ( result . lua , package_lua ) _remove_global_return ( result . lua ) else : source = file . from_file ( fn ) setattr ( result , section , getattr ( source , section ) ) elif getattr ( args , 'empty_' + section , False ) : setattr ( result , section , getattr ( empty_source , section ) ) # TODO: allow overriding the label source for .p8.png # Save result as args.filename. lua_writer_cls = None lua_writer_args = None if getattr ( args , 'lua_format' , False ) : lua_writer_cls = lua . LuaFormatterWriter , lua_writer_args = { 'indentwidth' : args . indentwidth , # 'keep_property_names': args.keep_property_names, 'keep_all_names' : args . keep_all_names , 'keep_names_from_file' : args . keep_names_from_file } elif getattr ( args , 'lua_minify' , False ) : lua_writer_cls = lua . LuaMinifyTokenWriter file . to_file ( result , filename = args . filename , lua_writer_cls = lua_writer_cls , lua_writer_args = lua_writer_args ) return 0