|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -import filecmp |
16 | 15 | import io |
17 | | -import os |
18 | 16 | import re |
19 | | -import shutil |
20 | 17 | import subprocess |
21 | | -import sys |
22 | | -import tempfile |
23 | | - |
24 | | - |
25 | | -def _open_archive(tarfile, tmp_dir): |
26 | | - with tempfile.TemporaryFile(mode='w+') as f: |
27 | | - try: |
28 | | - subprocess.check_call(['tar', '-xvf', tarfile], cwd=tmp_dir, stdout=f) |
29 | | - except Exception: |
30 | | - f.seek(0) |
31 | | - sys.stderr.write(f.read()) |
32 | | - raise |
33 | | - return os.listdir(tmp_dir) |
34 | | - |
35 | | - |
36 | | -def _files_same(dir1, dir2, basenames): |
37 | | - diff = filecmp.cmpfiles(dir1, dir2, basenames) |
38 | | - return 0 == len(diff[1] + diff[2]) |
39 | | - |
40 | | - |
41 | | -def _dirs_same(dir1, dir2, basenames): |
42 | | - for d in basenames: |
43 | | - left = os.path.join(dir1, d) |
44 | | - right = os.path.join(dir2, d) |
45 | | - if not (os.path.isdir(left) and os.path.isdir(right)): |
46 | | - return False |
47 | | - diff = filecmp.dircmp(right, right) |
48 | | - if 0 != len(diff.left_only + diff.right_only + diff.diff_files + |
49 | | - diff.common_funny + diff.funny_files): |
50 | | - return False |
51 | | - return True |
52 | | - |
53 | | - |
54 | | -def _move_files(dirfrom, dirto, basenames): |
55 | | - for f in basenames: |
56 | | - from_file = os.path.join(dirfrom, f) |
57 | | - to_file = os.path.join(dirto, f) |
58 | | - if os.path.isfile(to_file): |
59 | | - os.path.remove(to_file) |
60 | | - shutil.move(from_file, to_file) |
61 | | - |
62 | | - |
63 | | -def _move_dirs(dirfrom, dirto, basenames): |
64 | | - for d in basenames: |
65 | | - from_dir = os.path.join(dirfrom, d) |
66 | | - to_dir = os.path.join(dirto, d) |
67 | | - if os.path.isdir(to_dir): |
68 | | - shutil.rmtree(to_dir) |
69 | | - shutil.move(from_dir, to_dir) |
70 | | - |
71 | | - |
72 | | -def untar(tarfile, outdir): |
73 | | - """Returns True if untar content differs from pre-existing outdir content.""" |
74 | | - tmpdir = tempfile.mkdtemp() |
75 | | - try: |
76 | | - untared = _open_archive(tarfile, tmpdir) |
77 | | - files = [f for f in untared if os.path.isfile(os.path.join(tmpdir, f))] |
78 | | - dirs = [d for d in untared if os.path.isdir(os.path.join(tmpdir, d))] |
79 | | - assert len(files) + len(dirs) == len(untared), 'Only files and directories' |
80 | | - if _files_same(tmpdir, outdir, files) and _dirs_same(tmpdir, outdir, dirs): |
81 | | - # Nothing new or different in the tarfile. |
82 | | - return False |
83 | | - # Some or all of the files / directories are new. |
84 | | - _move_files(tmpdir, outdir, files) |
85 | | - _move_dirs(tmpdir, outdir, dirs) |
86 | | - return True |
87 | | - finally: |
88 | | - if os.path.isdir(tmpdir): |
89 | | - shutil.rmtree(tmpdir) |
90 | | - |
91 | 18 |
|
92 | 19 | QUOTED = re.compile(r'\(module\s*(\$\S*)?\s+(quote|binary)') |
93 | 20 |
|
|
0 commit comments