Mercurial > repos > guerler > hhblits
comparison lib/python3.8/site-packages/pip/_vendor/pep517/dirtools.py @ 0:9e54283cc701 draft
"planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
author | guerler |
---|---|
date | Mon, 27 Jul 2020 03:47:31 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9e54283cc701 |
---|---|
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 |