|
618 | 618 | { |
619 | 619 | "data": { |
620 | 620 | "text/plain": [ |
621 | | - "['g', 'a', 'b', 'h', 'd', 'f', 'c', 'e']" |
| 621 | + "['b', 'a', 'g', 'h', 'f', 'e', 'c', 'd']" |
622 | 622 | ] |
623 | 623 | }, |
624 | 624 | "execution_count": null, |
|
1000 | 1000 | "test_eq(list(loads_multi(tst)), [{'a': 1}, {'b': 2}])" |
1001 | 1001 | ] |
1002 | 1002 | }, |
| 1003 | + { |
| 1004 | + "cell_type": "code", |
| 1005 | + "execution_count": null, |
| 1006 | + "metadata": {}, |
| 1007 | + "outputs": [], |
| 1008 | + "source": [ |
| 1009 | + "#export\n", |
| 1010 | + "def _unpack(fname, out):\n", |
| 1011 | + " shutil.unpack_archive(str(fname), str(out))\n", |
| 1012 | + " ls = out.ls()\n", |
| 1013 | + " return ls[0] if len(ls) == 1 else out" |
| 1014 | + ] |
| 1015 | + }, |
1003 | 1016 | { |
1004 | 1017 | "cell_type": "code", |
1005 | 1018 | "execution_count": null, |
|
1012 | 1025 | " with tempfile.TemporaryDirectory(dir='.') as d:\n", |
1013 | 1026 | " out = Path(d)/remove_suffix(Path(fname).stem, '.tar')\n", |
1014 | 1027 | " out.mkdir()\n", |
1015 | | - " shutil.unpack_archive(str(fname), str(out))\n", |
1016 | | - " ls = out.ls()\n", |
1017 | | - " src = ls[0] if len(ls) == 1 else out\n", |
1018 | | - " dest = dest/(out if rename else src).name\n", |
| 1028 | + " if rename: dest = dest/out.name\n", |
| 1029 | + " else:\n", |
| 1030 | + " src = _unpack(fname, out)\n", |
| 1031 | + " dest = dest/src.name\n", |
1019 | 1032 | " if dest.exists():\n", |
1020 | 1033 | " if overwrite: shutil.rmtree(dest)\n", |
1021 | 1034 | " else: return dest\n", |
| 1035 | + " if rename: src = _unpack(fname, out)\n", |
1022 | 1036 | " shutil.move(str(src), dest)\n", |
1023 | 1037 | " return dest" |
1024 | 1038 | ] |
|
1029 | 1043 | "metadata": {}, |
1030 | 1044 | "outputs": [], |
1031 | 1045 | "source": [ |
1032 | | - "def test_untar(foldername, **kwargs):\n", |
| 1046 | + "def test_untar(foldername, rename=False, **kwargs):\n", |
1033 | 1047 | " with tempfile.TemporaryDirectory() as d:\n", |
1034 | 1048 | " nm = os.path.join(d, 'a')\n", |
1035 | 1049 | " shutil.make_archive(nm, 'gztar', **kwargs)\n", |
1036 | 1050 | " with tempfile.TemporaryDirectory() as d2:\n", |
1037 | 1051 | " d2 = Path(d2)\n", |
1038 | | - " untar_dir(nm+'.tar.gz', d2)\n", |
| 1052 | + " untar_dir(nm+'.tar.gz', d2, rename=rename)\n", |
1039 | 1053 | " test_eq(d2.ls(), [d2/foldername])" |
1040 | 1054 | ] |
1041 | 1055 | }, |
|
1056 | 1070 | "test_untar('images', base_dir='images')" |
1057 | 1071 | ] |
1058 | 1072 | }, |
| 1073 | + { |
| 1074 | + "cell_type": "markdown", |
| 1075 | + "metadata": {}, |
| 1076 | + "source": [ |
| 1077 | + "If `rename` then the directory created is named based on the archive, without extension:" |
| 1078 | + ] |
| 1079 | + }, |
| 1080 | + { |
| 1081 | + "cell_type": "code", |
| 1082 | + "execution_count": null, |
| 1083 | + "metadata": {}, |
| 1084 | + "outputs": [], |
| 1085 | + "source": [ |
| 1086 | + "test_untar('a', base_dir='images', rename=True)" |
| 1087 | + ] |
| 1088 | + }, |
1059 | 1089 | { |
1060 | 1090 | "cell_type": "markdown", |
1061 | 1091 | "metadata": {}, |
|
0 commit comments