(source, target=None, interpreter=None, main=None,
                   filter=None, compressed=False)
Create an application archive from SOURCE.

    The SOURCE can be the name of a directory, or a filename or a file-like
    object referring to an existing archive.

    The content of SOURCE is packed into an application archive in TARGET,
    which can be a filename or a file-like object.  If SOURCE is a directory,
    TARGET can be omitted and will default to the name of SOURCE with .pyz
    appended.

    The created application archive will have a shebang line specifying
    that it should run with INTERPRETER (there will be no shebang line if
    INTERPRETER is None), and a __main__.py which runs MAIN (if MAIN is
    not specified, an existing __main__.py will be used).  It is an error
    to specify MAIN for anything other than a directory source with no
    __main__.py, and it is an error to omit MAIN if the directory has no
    __main__.py.
Create an application archive from SOURCE.
Create an application archive from SOURCE .
def create_archive(source, target=None, interpreter=None, main=None,
                   filter=None, compressed=False):
    """Create an application archive from SOURCE.

    The SOURCE can be the name of a directory, or a filename or a file-like
    object referring to an existing archive.

    The content of SOURCE is packed into an application archive in TARGET,
    which can be a filename or a file-like object.  If SOURCE is a directory,
    TARGET can be omitted and will default to the name of SOURCE with .pyz
    appended.

    The created application archive will have a shebang line specifying
    that it should run with INTERPRETER (there will be no shebang line if
    INTERPRETER is None), and a __main__.py which runs MAIN (if MAIN is
    not specified, an existing __main__.py will be used).  It is an error
    to specify MAIN for anything other than a directory source with no
    __main__.py, and it is an error to omit MAIN if the directory has no
    __main__.py.
    """
    # Are we copying an existing archive?
    source_is_file = False
    if hasattr(source, 'read') and hasattr(source, 'readline'):
        source_is_file = True
    else:
        source = pathlib.Path(source)
        if source.is_file():
            source_is_file = True

    if source_is_file:
        _copy_archive(source, target, interpreter)
        return

    # We are creating a new archive from a directory.
    if not source.exists():
        raise ZipAppError("Source does not exist")
    has_main = (source / '__main__.py').is_file()
    if main and has_main:
        raise ZipAppError(
            "Cannot specify entry point if the source has __main__.py")
    if not (main or has_main):
        raise ZipAppError("Archive has no entry point")

    main_py = None
    if main:
        # Check that main has the right format.
        mod, sep, fn = main.partition(':')
        mod_ok = all(part.isidentifier() for part in mod.split('.'))
        fn_ok = all(part.isidentifier() for part in fn.split('.'))
        if not (sep == ':' and mod_ok and fn_ok):
            raise ZipAppError("Invalid entry point: " + main)
        main_py = MAIN_TEMPLATE.format(module=mod, fn=fn)

    if target is None:
        target = source.with_suffix('.pyz')
    elif not hasattr(target, 'write'):
        target = pathlib.Path(target)

    with _maybe_open(target, 'wb') as fd:
        _write_file_prefix(fd, interpreter)
        compression = (zipfile.ZIP_DEFLATED if compressed else
                       zipfile.ZIP_STORED)
        with zipfile.ZipFile(fd, 'w', compression=compression) as z:
            for child in source.rglob('*'):
                arcname = child.relative_to(source)
                if filter is None or filter(arcname):
                    z.write(child, arcname.as_posix())
            if main_py:
                z.writestr('__main__.py', main_py.encode('utf-8'))

    if interpreter and not hasattr(target, 'write'):
        target.chmod(target.stat().st_mode | stat.S_IEXEC)
def create_archive ( source , target = None , interpreter = None , main = None , filter = None , compressed = False ) : # Are we copying an existing archive? source_is_file = False if hasattr ( source , 'read' ) and hasattr ( source , 'readline' ) : source_is_file = True else : source = pathlib . Path ( source ) if source . is_file ( ) : source_is_file = True if source_is_file : _copy_archive ( source , target , interpreter ) return # We are creating a new archive from a directory. if not source . exists ( ) : raise ZipAppError ( "Source does not exist" ) has_main = ( source / '__main__.py' ) . is_file ( ) if main and has_main : raise ZipAppError ( "Cannot specify entry point if the source has __main__.py" ) if not ( main or has_main ) : raise ZipAppError ( "Archive has no entry point" ) main_py = None if main : # Check that main has the right format. mod , sep , fn = main . partition ( ':' ) mod_ok = all ( part . isidentifier ( ) for part in mod . split ( '.' ) ) fn_ok = all ( part . isidentifier ( ) for part in fn . split ( '.' ) ) if not ( sep == ':' and mod_ok and fn_ok ) : raise ZipAppError ( "Invalid entry point: " + main ) main_py = MAIN_TEMPLATE . format ( module = mod , fn = fn ) if target is None : target = source . with_suffix ( '.pyz' ) elif not hasattr ( target , 'write' ) : target = pathlib . Path ( target ) with _maybe_open ( target , 'wb' ) as fd : _write_file_prefix ( fd , interpreter ) compression = ( zipfile . ZIP_DEFLATED if compressed else zipfile . ZIP_STORED ) with zipfile . ZipFile ( fd , 'w' , compression = compression ) as z : for child in source . rglob ( '*' ) : arcname = child . relative_to ( source ) if filter is None or filter ( arcname ) : z . write ( child , arcname . as_posix ( ) ) if main_py : z . writestr ( '__main__.py' , main_py . encode ( 'utf-8' ) ) if interpreter and not hasattr ( target , 'write' ) : target . chmod ( target . stat ( ) . st_mode | stat . S_IEXEC )