Skip to content

Commit 4d3b248

Browse files
authored
Merge pull request #305 from mszhanyi/master
windows compatibility for run
2 parents 861fd14 + d077b6c commit 4d3b248

File tree

2 files changed

+101
-16
lines changed

2 files changed

+101
-16
lines changed

fastcore/xtras.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,20 @@ def repo_details(url):
162162
return res.split('/')[-2:]
163163

164164
# Cell
165-
def run(cmd, *rest, ignore_ex=False, as_bytes=False, stderr=False):
165+
def run(cmd, *rest, same_in_win=False, ignore_ex=False, as_bytes=False, stderr=False):
166166
"Pass `cmd` (splitting with `shlex` if string) to `subprocess.run`; return `stdout`; raise `IOError` if fails"
167-
if rest: cmd = (cmd,)+rest
168-
elif isinstance(cmd,str): cmd = shlex.split(cmd)
167+
# Even the command is same on Windows, we have to add `cmd /c `"
168+
import logging
169+
if rest:
170+
if sys.platform == 'win32' and same_in_win:
171+
cmd = ('cmd', '/c', cmd, *rest)
172+
else:
173+
cmd = (cmd,)+rest
174+
elif isinstance(cmd, str):
175+
if sys.platform == 'win32' and same_in_win: cmd = 'cmd /c ' + cmd
176+
cmd = shlex.split(cmd)
177+
elif isinstance(cmd, list):
178+
if sys.platform == 'win32' and same_in_win: cmd = ['cmd', '/c'] + cmd
169179
res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
170180
stdout = res.stdout
171181
if stderr and res.stderr: stdout += b' ;; ' + res.stderr

nbs/03_xtras.ipynb

Lines changed: 88 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@
618618
{
619619
"data": {
620620
"text/plain": [
621-
"['c', 'f', 'e', 'g', 'h', 'b', 'd', 'a']"
621+
"['c', 'h', 'f', 'a', 'g', 'd', 'b', 'e']"
622622
]
623623
},
624624
"execution_count": null,
@@ -1045,10 +1045,20 @@
10451045
"outputs": [],
10461046
"source": [
10471047
"#export\n",
1048-
"def run(cmd, *rest, ignore_ex=False, as_bytes=False, stderr=False):\n",
1048+
"def run(cmd, *rest, same_in_win=False, ignore_ex=False, as_bytes=False, stderr=False):\n",
10491049
" \"Pass `cmd` (splitting with `shlex` if string) to `subprocess.run`; return `stdout`; raise `IOError` if fails\"\n",
1050-
" if rest: cmd = (cmd,)+rest\n",
1051-
" elif isinstance(cmd,str): cmd = shlex.split(cmd)\n",
1050+
" # Even the command is same on Windows, we have to add `cmd /c `\"\n",
1051+
" import logging\n",
1052+
" if rest: \n",
1053+
" if sys.platform == 'win32' and same_in_win: \n",
1054+
" cmd = ('cmd', '/c', cmd, *rest)\n",
1055+
" else:\n",
1056+
" cmd = (cmd,)+rest\n",
1057+
" elif isinstance(cmd, str):\n",
1058+
" if sys.platform == 'win32' and same_in_win: cmd = 'cmd /c ' + cmd\n",
1059+
" cmd = shlex.split(cmd)\n",
1060+
" elif isinstance(cmd, list):\n",
1061+
" if sys.platform == 'win32' and same_in_win: cmd = ['cmd', '/c'] + cmd\n",
10521062
" res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n",
10531063
" stdout = res.stdout\n",
10541064
" if stderr and res.stderr: stdout += b' ;; ' + res.stderr\n",
@@ -1069,7 +1079,54 @@
10691079
"cell_type": "code",
10701080
"execution_count": null,
10711081
"metadata": {},
1072-
"outputs": [],
1082+
"outputs": [
1083+
{
1084+
"name": "stdout",
1085+
"output_type": "stream",
1086+
"text": [
1087+
"popenargs (['cmd', '/c', 'echo'],)\n",
1088+
"kwargs {'stdout': -1, 'stderr': -1}\n",
1089+
"popenargs (('cmd', '/c', 'pip', '--version'),)\n",
1090+
"kwargs {'stdout': -1, 'stderr': -1}\n",
1091+
"popenargs (['cmd', '/c', 'pip', '--version'],)\n",
1092+
"kwargs {'stdout': -1, 'stderr': -1}\n"
1093+
]
1094+
},
1095+
{
1096+
"data": {
1097+
"text/plain": [
1098+
"'pip 21.0.1 from D:\\\\programs\\\\envs\\\\nbdev\\\\lib\\\\site-packages\\\\pip (python 3.8)'"
1099+
]
1100+
},
1101+
"execution_count": null,
1102+
"metadata": {},
1103+
"output_type": "execute_result"
1104+
}
1105+
],
1106+
"source": [
1107+
"run('echo', same_in_win=True)\n",
1108+
"run('pip', '--version', same_in_win=True)\n",
1109+
"run(['pip', '--version'], same_in_win=True)"
1110+
]
1111+
},
1112+
{
1113+
"cell_type": "code",
1114+
"execution_count": null,
1115+
"metadata": {},
1116+
"outputs": [
1117+
{
1118+
"name": "stdout",
1119+
"output_type": "stream",
1120+
"text": [
1121+
"popenargs (['cmd', '/c', 'dir', '/p'],)\n",
1122+
"kwargs {'stdout': -1, 'stderr': -1}\n",
1123+
"popenargs (['cmd', '/c', 'dir', '/p'],)\n",
1124+
"kwargs {'stdout': -1, 'stderr': -1}\n",
1125+
"popenargs (('cmd', '/c', 'dir', '/p'),)\n",
1126+
"kwargs {'stdout': -1, 'stderr': -1}\n"
1127+
]
1128+
}
1129+
],
10731130
"source": [
10741131
"if sys.platform == 'win32':\n",
10751132
" assert 'ipynb' in run('cmd /c dir /p')\n",
@@ -1092,7 +1149,16 @@
10921149
"cell_type": "code",
10931150
"execution_count": null,
10941151
"metadata": {},
1095-
"outputs": [],
1152+
"outputs": [
1153+
{
1154+
"name": "stdout",
1155+
"output_type": "stream",
1156+
"text": [
1157+
"popenargs (['cmd', '/c', 'findstr', 'asdfds', '00_test.ipynb'],)\n",
1158+
"kwargs {'stdout': -1, 'stderr': -1}\n"
1159+
]
1160+
}
1161+
],
10961162
"source": [
10971163
"if sys.platform == 'win32':\n",
10981164
" test_eq(run('cmd /c findstr asdfds 00_test.ipynb', ignore_ex=True)[0], 1)\n",
@@ -1111,10 +1177,19 @@
11111177
"cell_type": "code",
11121178
"execution_count": null,
11131179
"metadata": {},
1114-
"outputs": [],
1180+
"outputs": [
1181+
{
1182+
"name": "stdout",
1183+
"output_type": "stream",
1184+
"text": [
1185+
"popenargs (['cmd', '/c', 'echo', 'hi'],)\n",
1186+
"kwargs {'stdout': -1, 'stderr': -1}\n"
1187+
]
1188+
}
1189+
],
11151190
"source": [
11161191
"if sys.platform == 'win32':\n",
1117-
" # why I ignore as_types, because every time nbdev_clean_nbs will update \\n to \\nn\n",
1192+
" # why I ignore as_types, because every time nbdev_clean_nbs will update \\n to \\r\\n\n",
11181193
" test_eq(run('cmd /c echo hi'), 'hi')\n",
11191194
"else:\n",
11201195
" test_eq(run('echo hi', as_bytes=True), b'hi\\n')"
@@ -1298,7 +1373,7 @@
12981373
{
12991374
"data": {
13001375
"text/plain": [
1301-
"(Path('../fastcore/__init__.py'), Path('01_basics.ipynb'))"
1376+
"(Path('../fastcore/all.py'), Path('00_test.ipynb'))"
13021377
]
13031378
},
13041379
"execution_count": null,
@@ -1575,8 +1650,8 @@
15751650
"name": "stdout",
15761651
"output_type": "stream",
15771652
"text": [
1578-
"Num Events: 8, Freq/sec: 423.0\n",
1579-
"Most recent: ▂▂▁▁▇ 318.5 319.0 266.9 275.6 427.7\n"
1653+
"Num Events: 1, Freq/sec: 62.9\n",
1654+
"Most recent: ▃▇▃▁▃ 31.3 31.9 31.4 30.8 31.3\n"
15801655
]
15811656
}
15821657
],
@@ -1725,7 +1800,7 @@
17251800
"name": "stdout",
17261801
"output_type": "stream",
17271802
"text": [
1728-
"2000-01-01 12:00:00 UTC is 2000-01-01 12:00:00+00:00 local time\n"
1803+
"2000-01-01 12:00:00 UTC is 2000-01-01 04:00:00-08:00 local time\n"
17291804
]
17301805
}
17311806
],
@@ -1755,7 +1830,7 @@
17551830
"name": "stdout",
17561831
"output_type": "stream",
17571832
"text": [
1758-
"2000-01-01 12:00:00 local is 2000-01-01 12:00:00+00:00 UTC time\n"
1833+
"2000-01-01 12:00:00 local is 2000-01-01 20:00:00+00:00 UTC time\n"
17591834
]
17601835
}
17611836
],

0 commit comments

Comments
 (0)