Skip to content

Commit 1b46f55

Browse files
committed
fixes #337
1 parent 694e704 commit 1b46f55

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

fastcore/xtras.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,26 @@ def loads_multi(s:str):
147147
yield obj
148148
s = s[pos:]
149149

150+
# Cell
151+
def _unpack(fname, out):
152+
shutil.unpack_archive(str(fname), str(out))
153+
ls = out.ls()
154+
return ls[0] if len(ls) == 1 else out
155+
150156
# Cell
151157
def untar_dir(fname, dest, rename=False, overwrite=False):
152158
"untar `file` into `dest`, creating a directory if the root contains more than one item"
153159
with tempfile.TemporaryDirectory(dir='.') as d:
154160
out = Path(d)/remove_suffix(Path(fname).stem, '.tar')
155161
out.mkdir()
156-
shutil.unpack_archive(str(fname), str(out))
157-
ls = out.ls()
158-
src = ls[0] if len(ls) == 1 else out
159-
dest = dest/(out if rename else src).name
162+
if rename: dest = dest/out.name
163+
else:
164+
src = _unpack(fname, out)
165+
dest = dest/src.name
160166
if dest.exists():
161167
if overwrite: shutil.rmtree(dest)
162168
else: return dest
169+
if rename: src = _unpack(fname, out)
163170
shutil.move(str(src), dest)
164171
return dest
165172

nbs/03_xtras.ipynb

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@
618618
{
619619
"data": {
620620
"text/plain": [
621-
"['g', 'a', 'b', 'h', 'd', 'f', 'c', 'e']"
621+
"['b', 'a', 'g', 'h', 'f', 'e', 'c', 'd']"
622622
]
623623
},
624624
"execution_count": null,
@@ -1000,6 +1000,19 @@
10001000
"test_eq(list(loads_multi(tst)), [{'a': 1}, {'b': 2}])"
10011001
]
10021002
},
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+
},
10031016
{
10041017
"cell_type": "code",
10051018
"execution_count": null,
@@ -1012,13 +1025,14 @@
10121025
" with tempfile.TemporaryDirectory(dir='.') as d:\n",
10131026
" out = Path(d)/remove_suffix(Path(fname).stem, '.tar')\n",
10141027
" 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",
10191032
" if dest.exists():\n",
10201033
" if overwrite: shutil.rmtree(dest)\n",
10211034
" else: return dest\n",
1035+
" if rename: src = _unpack(fname, out)\n",
10221036
" shutil.move(str(src), dest)\n",
10231037
" return dest"
10241038
]
@@ -1029,13 +1043,13 @@
10291043
"metadata": {},
10301044
"outputs": [],
10311045
"source": [
1032-
"def test_untar(foldername, **kwargs):\n",
1046+
"def test_untar(foldername, rename=False, **kwargs):\n",
10331047
" with tempfile.TemporaryDirectory() as d:\n",
10341048
" nm = os.path.join(d, 'a')\n",
10351049
" shutil.make_archive(nm, 'gztar', **kwargs)\n",
10361050
" with tempfile.TemporaryDirectory() as d2:\n",
10371051
" d2 = Path(d2)\n",
1038-
" untar_dir(nm+'.tar.gz', d2)\n",
1052+
" untar_dir(nm+'.tar.gz', d2, rename=rename)\n",
10391053
" test_eq(d2.ls(), [d2/foldername])"
10401054
]
10411055
},
@@ -1056,6 +1070,22 @@
10561070
"test_untar('images', base_dir='images')"
10571071
]
10581072
},
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+
},
10591089
{
10601090
"cell_type": "markdown",
10611091
"metadata": {},

0 commit comments

Comments
 (0)