diff -r 65a2b468fb3c Lib/zipapp.py --- a/Lib/zipapp.py Fri Mar 13 10:42:08 2015 -0400 +++ b/Lib/zipapp.py Fri Mar 13 11:10:18 2015 -0400 @@ -36,6 +36,8 @@ @contextlib.contextmanager def _maybe_open(archive, mode): + if isinstance(archive, pathlib.Path): + archive = str(archive) if isinstance(archive, str): with open(archive, mode) as f: yield f @@ -92,12 +94,20 @@ is an error to omit MAIN if the directory has no __main__.py. """ # Are we copying an existing archive? - if not (isinstance(source, str) and os.path.isdir(source)): + source_exists = False + if hasattr(source, 'read') and hasattr(source, 'readline'): + source_exists = True + else: + source = pathlib.Path(source) + if source.is_file(): + source_exists = True + + if source_exists: _copy_archive(source, target, interpreter) return - # We are creating a new archive from a directory - has_main = os.path.exists(os.path.join(source, '__main__.py')) + # We are creating a new archive from a directory. + has_main = (source / '__main__.py').exists() if main and has_main: raise ZipAppError( "Cannot specify entry point if the source has __main__.py") @@ -106,7 +116,7 @@ main_py = None if main: - # Check that main has the right format + # 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('.')) @@ -115,7 +125,9 @@ main_py = MAIN_TEMPLATE.format(module=mod, fn=fn) if target is None: - target = source + '.pyz' + 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) @@ -127,8 +139,8 @@ if main_py: z.writestr('__main__.py', main_py.encode('utf-8')) - if interpreter and isinstance(target, str): - os.chmod(target, os.stat(target).st_mode | stat.S_IEXEC) + if interpreter and not hasattr(target, 'write'): + target.chmod(target.stat().st_mode | stat.S_IEXEC) def get_interpreter(archive):