comparison env/lib/python3.9/site-packages/pip/_vendor/pep517/dirtools.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 import os
2 import io
3 import contextlib
4 import tempfile
5 import shutil
6 import errno
7 import zipfile
8
9
10 @contextlib.contextmanager
11 def tempdir():
12 """Create a temporary directory in a context manager."""
13 td = tempfile.mkdtemp()
14 try:
15 yield td
16 finally:
17 shutil.rmtree(td)
18
19
20 def mkdir_p(*args, **kwargs):
21 """Like `mkdir`, but does not raise an exception if the
22 directory already exists.
23 """
24 try:
25 return os.mkdir(*args, **kwargs)
26 except OSError as exc:
27 if exc.errno != errno.EEXIST:
28 raise
29
30
31 def dir_to_zipfile(root):
32 """Construct an in-memory zip file for a directory."""
33 buffer = io.BytesIO()
34 zip_file = zipfile.ZipFile(buffer, 'w')
35 for root, dirs, files in os.walk(root):
36 for path in dirs:
37 fs_path = os.path.join(root, path)
38 rel_path = os.path.relpath(fs_path, root)
39 zip_file.writestr(rel_path + '/', '')
40 for path in files:
41 fs_path = os.path.join(root, path)
42 rel_path = os.path.relpath(fs_path, root)
43 zip_file.write(fs_path, rel_path)
44 return zip_file