|
22 | 22 | "from functools import wraps\n", |
23 | 23 | "\n", |
24 | 24 | "import mimetypes,pickle,random,json,subprocess,shlex,bz2,gzip,zipfile,tarfile\n", |
25 | | - "import imghdr,struct,distutils.util,tempfile,time,string,collections\n", |
| 25 | + "import imghdr,struct,distutils.util,tempfile,time,string,collections,shutil\n", |
26 | 26 | "from contextlib import contextmanager,ExitStack\n", |
27 | 27 | "from pdb import set_trace\n", |
28 | 28 | "from datetime import datetime, timezone\n", |
|
618 | 618 | { |
619 | 619 | "data": { |
620 | 620 | "text/plain": [ |
621 | | - "['c', 'b', 'e', 'f', 'g', 'a', 'd', 'h']" |
| 621 | + "['g', 'b', 'c', 'e', 'a', 'h', 'd', 'f']" |
622 | 622 | ] |
623 | 623 | }, |
624 | 624 | "execution_count": null, |
|
1007 | 1007 | "outputs": [], |
1008 | 1008 | "source": [ |
1009 | 1009 | "#export\n", |
1010 | | - "def untar_dir(fname, dest):\n", |
1011 | | - " \"untar `file` into `dest`\"\n", |
| 1010 | + "def untar_dir(fname, dest, rename=False, overwrite=False):\n", |
| 1011 | + " \"untar `file` into `dest`, creating a directory if the root contains more than one item\"\n", |
1012 | 1012 | " with tempfile.TemporaryDirectory(dir='.') as d:\n", |
1013 | | - " out = Path(d)/'out'\n", |
| 1013 | + " out = Path(d)/remove_suffix(Path(fname).stem, '.tar')\n", |
1014 | 1014 | " out.mkdir()\n", |
1015 | 1015 | " shutil.unpack_archive(fname, out)\n", |
1016 | 1016 | " ls = out.ls()\n", |
1017 | | - " if len(ls) == 1: shutil.move(str(ls[0]), dest)\n", |
1018 | | - " else: shutil.move(str(out), dest/remove_suffix(Path(fname).stem, '.tar'))" |
1019 | | - ] |
1020 | | - }, |
1021 | | - { |
1022 | | - "cell_type": "markdown", |
1023 | | - "metadata": {}, |
1024 | | - "source": [ |
1025 | | - "If the contents of `fname` contain just one file or directory, it is placed directly in `dest`:" |
| 1017 | + " src = ls[0] if len(ls) == 1 else out\n", |
| 1018 | + " dest = dest/(out if rename else src).name\n", |
| 1019 | + " if overwrite: shutil.rmtree(dest, ignore_errors=True)\n", |
| 1020 | + " shutil.move(str(src), dest)\n", |
| 1021 | + " return dest" |
1026 | 1022 | ] |
1027 | 1023 | }, |
1028 | 1024 | { |
|
1041 | 1037 | " test_eq(d2.ls(), [d2/foldername])" |
1042 | 1038 | ] |
1043 | 1039 | }, |
| 1040 | + { |
| 1041 | + "cell_type": "markdown", |
| 1042 | + "metadata": {}, |
| 1043 | + "source": [ |
| 1044 | + "If the contents of `fname` contain just one file or directory, it is placed directly in `dest`:" |
| 1045 | + ] |
| 1046 | + }, |
1044 | 1047 | { |
1045 | 1048 | "cell_type": "code", |
1046 | 1049 | "execution_count": null, |
|
1134 | 1137 | "execution_count": null, |
1135 | 1138 | "metadata": {}, |
1136 | 1139 | "outputs": [ |
1137 | | - { |
1138 | | - "name": "stdout", |
1139 | | - "output_type": "stream", |
1140 | | - "text": [ |
1141 | | - "popenargs (['cmd', '/c', 'echo'],)\n", |
1142 | | - "kwargs {'stdout': -1, 'stderr': -1}\n", |
1143 | | - "popenargs (('cmd', '/c', 'pip', '--version'),)\n", |
1144 | | - "kwargs {'stdout': -1, 'stderr': -1}\n", |
1145 | | - "popenargs (['cmd', '/c', 'pip', '--version'],)\n", |
1146 | | - "kwargs {'stdout': -1, 'stderr': -1}\n" |
1147 | | - ] |
1148 | | - }, |
1149 | 1140 | { |
1150 | 1141 | "data": { |
1151 | 1142 | "text/plain": [ |
1152 | | - "'pip 21.0.1 from D:\\\\programs\\\\envs\\\\nbdev\\\\lib\\\\site-packages\\\\pip (python 3.8)'" |
| 1143 | + "'pip 21.1.3 from /home/jhoward/miniconda3/lib/python3.8/site-packages/pip (python 3.8)'" |
1153 | 1144 | ] |
1154 | 1145 | }, |
1155 | 1146 | "execution_count": null, |
|
1167 | 1158 | "cell_type": "code", |
1168 | 1159 | "execution_count": null, |
1169 | 1160 | "metadata": {}, |
1170 | | - "outputs": [ |
1171 | | - { |
1172 | | - "name": "stdout", |
1173 | | - "output_type": "stream", |
1174 | | - "text": [ |
1175 | | - "popenargs (['cmd', '/c', 'dir', '/p'],)\n", |
1176 | | - "kwargs {'stdout': -1, 'stderr': -1}\n", |
1177 | | - "popenargs (['cmd', '/c', 'dir', '/p'],)\n", |
1178 | | - "kwargs {'stdout': -1, 'stderr': -1}\n", |
1179 | | - "popenargs (('cmd', '/c', 'dir', '/p'),)\n", |
1180 | | - "kwargs {'stdout': -1, 'stderr': -1}\n" |
1181 | | - ] |
1182 | | - } |
1183 | | - ], |
| 1161 | + "outputs": [], |
1184 | 1162 | "source": [ |
1185 | 1163 | "if sys.platform == 'win32':\n", |
1186 | 1164 | " assert 'ipynb' in run('cmd /c dir /p')\n", |
|
1203 | 1181 | "cell_type": "code", |
1204 | 1182 | "execution_count": null, |
1205 | 1183 | "metadata": {}, |
1206 | | - "outputs": [ |
1207 | | - { |
1208 | | - "name": "stdout", |
1209 | | - "output_type": "stream", |
1210 | | - "text": [ |
1211 | | - "popenargs (['cmd', '/c', 'findstr', 'asdfds', '00_test.ipynb'],)\n", |
1212 | | - "kwargs {'stdout': -1, 'stderr': -1}\n" |
1213 | | - ] |
1214 | | - } |
1215 | | - ], |
| 1184 | + "outputs": [], |
1216 | 1185 | "source": [ |
1217 | 1186 | "if sys.platform == 'win32':\n", |
1218 | 1187 | " test_eq(run('cmd /c findstr asdfds 00_test.ipynb', ignore_ex=True)[0], 1)\n", |
|
1231 | 1200 | "cell_type": "code", |
1232 | 1201 | "execution_count": null, |
1233 | 1202 | "metadata": {}, |
1234 | | - "outputs": [ |
1235 | | - { |
1236 | | - "name": "stdout", |
1237 | | - "output_type": "stream", |
1238 | | - "text": [ |
1239 | | - "popenargs (['cmd', '/c', 'echo', 'hi'],)\n", |
1240 | | - "kwargs {'stdout': -1, 'stderr': -1}\n" |
1241 | | - ] |
1242 | | - } |
1243 | | - ], |
| 1203 | + "outputs": [], |
1244 | 1204 | "source": [ |
1245 | 1205 | "if sys.platform == 'win32':\n", |
1246 | 1206 | " # why I ignore as_types, because every time nbdev_clean_nbs will update \\n to \\r\\n\n", |
|
1394 | 1354 | { |
1395 | 1355 | "data": { |
1396 | 1356 | "text/plain": [ |
1397 | | - "Path('.gitattributes')" |
| 1357 | + "Path('05_transform.ipynb')" |
1398 | 1358 | ] |
1399 | 1359 | }, |
1400 | 1360 | "execution_count": null, |
|
1428 | 1388 | { |
1429 | 1389 | "data": { |
1430 | 1390 | "text/plain": [ |
1431 | | - "(Path('../fastcore/all.py'), Path('00_test.ipynb'))" |
| 1391 | + "(Path('../fastcore/all.py'), Path('05_transform.ipynb'))" |
1432 | 1392 | ] |
1433 | 1393 | }, |
1434 | 1394 | "execution_count": null, |
|
1494 | 1454 | "finally: Path.BASE_PATH = None" |
1495 | 1455 | ] |
1496 | 1456 | }, |
| 1457 | + { |
| 1458 | + "cell_type": "code", |
| 1459 | + "execution_count": null, |
| 1460 | + "metadata": {}, |
| 1461 | + "outputs": [], |
| 1462 | + "source": [ |
| 1463 | + "#export\n", |
| 1464 | + "@patch\n", |
| 1465 | + "def delete(self:Path):\n", |
| 1466 | + " \"Delete a file, symlink, or directory tree\"\n", |
| 1467 | + " if not self.exists(): return\n", |
| 1468 | + " if self.is_dir(): shutil.rmtree(self)\n", |
| 1469 | + " else: self.unlink()" |
| 1470 | + ] |
| 1471 | + }, |
1497 | 1472 | { |
1498 | 1473 | "cell_type": "markdown", |
1499 | 1474 | "metadata": {}, |
|
0 commit comments