Repository: pyodide/pyodide

Issue:
buildpkg doesn't clean up after failed or interrupted downloads
![image](https://user-images.githubusercontent.com/31370756/54251214-8078aa00-4581-11e9-98d3-d9db60b486c6.png)

when I run `make` command in docker environment, it shows this bug. I think it's the network that cause this error. 

We can use `rm -rf /src/packages/numpy/build` to solve this problem 


Discussion:
slow network also cause another problem. When I only delete the file instead of the directory, the shell will show another problem as below. May be this will be helpful to others. After deleting the directory, the problem fixed

`shutil.ReadError: /src/packages/python-dateutil/build/python-dateutil-2.7.2.tar.gz is not a compressed or uncompressed tar file`
`Makefile:5: recipe for target 'all' failed`
`make[1]: *** [all] Error 1`
`make[1]: Leaving directory '/src/packages'`
`Makefile:225: recipe for target 'build/packages.json' failed`
`make: *** [build/packages.json] Error 2`
`root@0c055853209e:/src# rm /src/packages/python-dateutil/build/python-dateutil-2.7.2.tar.gz`
`root@0c055853209e:/src# make`
`make -C packages`
`make[1]: Entering directory '/src/packages'`
`# Install build dependencies`
`/src/cpython/build/3.7.0/host/bin/python3 -m pip install Cython Tempita`
`Requirement already satisfied: Cython in /src/cpython/build/3.7.0/host/lib/python3.7/site-packages (0.29.6)`
`Requirement already satisfied: Tempita in /src/cpython/build/3.7.0/host/lib/python3.7/site-packages (0.5.2)`
`You are using pip version 10.0.1, however version 19.0.3 is available.`
`You should consider upgrading via the 'pip install --upgrade pip' command.`
`../bin/pyodide buildall . ../build \`
`	--package_abi=1 --ldflags="-O3 -s "BINARYEN_METHOD='native-wasm'" -Werror -s EMULATED_FUNCTION_POINTERS=1 -s EMULATE_FUNCTION_POINTER_CASTS=1 -s SIDE_MODULE=1 -s WASM=1 -s "BINARYEN_TRAP_MODE='clamp'" --memory-init-file 0 -s LINKABLE=1 -s EXPORT_ALL=1" --host=/src/cpython/build/3.7.0/host --target=/src/cpython/installs/python-3.7.0`
`BUILDING PACKAGE: python-dateutil`
`patching file dateutil/tz/tz.py`
`patching file dateutil/rrule.py`
`patch unexpectedly ends in middle of line`
`Hunk #2 succeeded at 107 with fuzz 1.`
`Download error on https://pypi.python.org/simple/setuptools_scm/: [Errno -3] Temporary failure in name resolution -- Some packages may not be found!`
`Download error on https://pypi.python.org/simple/setuptools-scm/: [Errno -3] Temporary failure in name resolution -- Some packages may not be found!`
`Couldn't find index page for 'setuptools_scm' (maybe misspelled?)`
`No local packages or working download links found for setuptools_scm`
`Traceback (most recent call last):`
 ` File "setup.py", line 86, in <module>`
 `   "test": Unsupported`
  `File "/src/cpython/build/3.7.0/host/lib/python3.7/site-packages/setuptools/__init__.py", line 128, in setup`
  `  _install_setup_requires(attrs)`
  `File "/src/cpython/build/3.7.0/host/lib/python3.7/site-packages/setuptools/__init__.py", line 123, in _install_setup_requires`
   ` dist.fetch_build_eggs(dist.setup_requires)`
  `File "/src/cpython/build/3.7.0/host/lib/python3.7/site-packages/setuptools/dist.py", line 513, in fetch_build_eggs`
 `   replace_conflicting=True,`
 ` File "/src/cpython/build/3.7.0/host/lib/python3.7/site-packages/pkg_resources/__init__.py", line 774, in resolve`
 `   replace_conflicting=replace_conflicting`
 ` File "/src/cpython/build/3.7.0/host/lib/python3.7/site-packages/pkg_resources/__init__.py", line 1057, in best_match`
 `   return self.obtain(req, installer)`
`  File "/src/cpython/build/3.7.0/host/lib/python3.7/site-packages/pkg_resources/__init__.py", line 1069, in obtain`
`    return installer(requirement)`
`  File "/src/cpython/build/3.7.0/host/lib/python3.7/site-packages/setuptools/dist.py", line 580, in fetch_build_egg`
`    return cmd.easy_install(req)`
`  File "/src/cpython/build/3.7.0/host/lib/python3.7/site-packages/setuptools/command/easy_install.py", line 667, in easy_install`
`    raise DistutilsError(msg)`
`distutils.errors.DistutilsError: Could not find suitable distribution for Requirement.parse('setuptools_scm')`
`Traceback (most recent call last):`
`  File "/usr/local/lib/python3.7/runpy.py", line 193, in _run_module_as_main`
`    "__main__", mod_spec)`
`  File "/usr/local/lib/python3.7/runpy.py", line 85, in _run_code`
`    exec(code, run_globals)`
`  File "/src/pyodide_build/__main__.py", line 30, in <module>`
`    main()`
`  File "/src/pyodide_build/__main__.py", line 24, in main`
`    args.func(args)`
`  File "/src/pyodide_build/buildall.py", line 93, in main`
`    build_packages(packagesdir, outputdir, args)`
`  File "/src/pyodide_build/buildall.py", line 50, in build_packages`
`    build_package(pkgname, dependencies, packagesdir, outputdir, args)`
`  File "/src/pyodide_build/buildall.py", line 21, in build_package`
`    build_package(req, dependencies, packagesdir, outputdir, args)`
`  File "/src/pyodide_build/buildall.py", line 21, in build_package
    build_package(req, dependencies, packagesdir, outputdir, args)`
`  File "/src/pyodide_build/buildall.py", line 24, in build_package`
`    buildpkg.build_package(packagesdir / pkgname / 'meta.yaml', args)`
`  File "/src/pyodide_build/buildpkg.py", line 162, in build_package`
`    compile(path, srcpath, pkg, args)`
`  File "/src/pyodide_build/buildpkg.py", line 100, in compile`
`    '--target', args.target], env=env, check=True)`
`  File "/usr/local/lib/python3.7/subprocess.py", line 468, in run`
`    output=stdout, stderr=stderr)`
`subprocess.CalledProcessError: Command '['/src/cpython/build/3.7.0/host/bin/python3', '-m', 'pyodide_build', 'pywasmcross', '--cflags', ' ', '--ldflags', '-O3 -s BINARYEN_METHOD=native-wasm -Werror -s EMULATED_FUNCTION_POINTERS=1 -s EMULATE_FUNCTION_POINTER_CASTS=1 -s SIDE_MODULE=1 -s WASM=1 -s BINARYEN_TRAP_MODE=clamp --memory-init-file 0 -s LINKABLE=1 -s EXPORT_ALL=1 ', '--host', '/src/cpython/build/3.7.0/host', '--target', '/src/cpython/installs/python-3.7.0']' returned non-zero exit status 1.`
`Makefile:5: recipe for target 'all' failed`
`make[1]: *** [all] Error 1`
`make[1]: Leaving directory '/src/packages'`
`Makefile:225: recipe for target 'build/packages.json' failed`
`make: *** [build/packages.json] Error 2`

Resolving patch:
diff --git a/pyodide_build/buildpkg.py b/pyodide_build/buildpkg.py
--- a/pyodide_build/buildpkg.py
+++ b/pyodide_build/buildpkg.py
@@ -42,10 +42,14 @@ def check_checksum(path, pkg):
 def download_and_extract(buildpath, packagedir, pkg, args):
     tarballpath = buildpath / Path(pkg['source']['url']).name
     if not tarballpath.is_file():
-        subprocess.run([
-            'wget', '-q', '-O', str(tarballpath), pkg['source']['url']
-        ], check=True)
-        check_checksum(tarballpath, pkg)
+        try:
+            subprocess.run([
+                'wget', '-q', '-O', str(tarballpath), pkg['source']['url']
+            ], check=True)
+            check_checksum(tarballpath, pkg)
+        except Exception:
+            tarballpath.unlink()
+            raise
     srcpath = buildpath / packagedir
     if not srcpath.is_dir():
         shutil.unpack_archive(str(tarballpath), str(buildpath))


Test patch: