Skip to content

Commit b993170

Browse files
committed
windows
1 parent 961618f commit b993170

File tree

4 files changed

+134
-10
lines changed

4 files changed

+134
-10
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
@echo off
2+
REM Usage:
3+
REM _pytest.bat urls.txt
4+
REM _pytest.bat https://host/a.tar.gz https://host/b.tgz
5+
REM From ChatGPT
6+
7+
setlocal EnableExtensions EnableDelayedExpansion
8+
9+
REM ---- temp workspace under %TEMP% ----
10+
set "TMPROOT=%TEMP%\remote_tests_%RANDOM%%RANDOM%"
11+
md "%TMPROOT%" || (echo Failed to create TMPROOT & exit /b 1)
12+
13+
REM ---- resolve args -> URL list file ----
14+
if "%~2"=="" (
15+
if exist "%~1" (
16+
set "URLS_FILE=%~f1"
17+
) else (
18+
set "URLS_FILE=%TMPROOT%\urls.txt"
19+
> "%URLS_FILE%" echo %~1
20+
)
21+
) else (
22+
set "URLS_FILE=%TMPROOT%\urls.txt"
23+
(for %%U in (%*) do @echo %%~U) > "%URLS_FILE%"
24+
)
25+
26+
pushd "%TMPROOT%" >nul || (echo Failed to enter TMPROOT & exit /b 1)
27+
28+
set /a i=0
29+
set /a overall_ec=0
30+
31+
REM read URL file line-by-line; hand off each line to a subroutine
32+
for /f "usebackq delims=" %%L in ("%URLS_FILE%") do call :process_one "%%L"
33+
34+
popd >nul
35+
rmdir /s /q "%TMPROOT%" >nul 2>&1
36+
exit /b %overall_ec%
37+
38+
REM ===================== subroutine =====================
39+
:process_one
40+
setlocal EnableExtensions EnableDelayedExpansion
41+
42+
REM grab the raw line and trim leading spaces
43+
set "url=%~1"
44+
if "%url%"=="" (endlocal & goto :eof)
45+
:trim
46+
if not "%url:~0,1%"==" " goto :trim_done
47+
set "url=%url:~1%"
48+
goto trim
49+
:trim_done
50+
51+
REM skip comments
52+
if "%url:~0,1%"=="#" (endlocal & goto :eof)
53+
54+
REM ----- do the work for this URL -----
55+
endlocal & set /a i+=1 & set "URL=%url%"
56+
echo(
57+
echo ==> [%i%]
58+
59+
set "PKGDIR=%TMPROOT%\pkg_%i%"
60+
md "%PKGDIR%"
61+
pushd "%PKGDIR%" >nul || goto :after
62+
63+
REM download archive into PKGDIR
64+
curl -L --fail -o "archive.tar.gz" "%URL%"
65+
if errorlevel 1 (
66+
echo curl failed
67+
set /a overall_ec=1
68+
popd >nul & goto :after
69+
)
70+
71+
REM extract (try gzip flags, then plain)
72+
tar -xzf "archive.tar.gz" >nul 2>&1
73+
if errorlevel 1 tar -xf "archive.tar.gz" >nul 2>&1
74+
if errorlevel 1 (
75+
echo tar extract failed
76+
set /a overall_ec=1
77+
popd >nul & goto :after
78+
)
79+
80+
REM get first entry (try -tzf, then -tf)
81+
set "FIRST="
82+
for /f "delims=" %%F in ('tar -tzf "archive.tar.gz" 2^>nul') do set "FIRST=%%F" & goto got_first
83+
for /f "delims=" %%F in ('tar -tf "archive.tar.gz" 2^>nul') do set "FIRST=%%F" & goto got_first
84+
:got_first
85+
86+
REM choose project root (top dir if present)
87+
set "PROJROOT=%CD%"
88+
if defined FIRST for /f "tokens=1 delims=/" %%T in ("%FIRST%") do if exist ".\%%T\" set "PROJROOT=%CD%\%%T"
89+
90+
REM mirror original: drop src\ if present
91+
if exist "%PROJROOT%\src\" rmdir /s /q "%PROJROOT%\src" >nul 2>&1
92+
93+
REM run pytest from repo root (with tests on PYTHONPATH if exists)
94+
pushd "%PROJROOT%" >nul
95+
echo Running pytest in: "%CD%"
96+
set "OLD_PYTHONPATH=%PYTHONPATH%"
97+
if exist "tests\" (
98+
if defined OLD_PYTHONPATH (
99+
set "PYTHONPATH=%CD%;tests;%OLD_PYTHONPATH%"
100+
) else (
101+
set "PYTHONPATH=%CD%;tests"
102+
)
103+
) else (
104+
if defined OLD_PYTHONPATH (
105+
set "PYTHONPATH=%CD%;%OLD_PYTHONPATH%"
106+
) else (
107+
set "PYTHONPATH=%CD%"
108+
)
109+
)
110+
pytest
111+
if errorlevel 1 set /a overall_ec=1
112+
set "PYTHONPATH=%OLD_PYTHONPATH%"
113+
popd >nul
114+
115+
popd >nul
116+
:after
117+
rmdir /s /q "%PKGDIR%" >nul 2>&1
118+
goto :eof

requirements/profiles/_tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ packs:
99

1010
extras:
1111
- _pytest.sh tar_url.txt
12+
- _pytest.bat tar_url.txt

src/diffpy/cmi/conda.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import json
1717
import subprocess
1818
import sys
19+
import os
1920
from dataclasses import dataclass
2021
from pathlib import Path
2122
from typing import List, Optional, Sequence, Tuple
@@ -97,10 +98,18 @@ def run(
9798
do_capture = (not dbg) if capture is None else bool(capture)
9899
qcmd = " ".join(str(x) for x in cmd)
99100

101+
win = (os.name == "nt")
102+
prog = str(cmd[0]).lower() if cmd else ""
103+
needs_cmd = win and (
104+
prog.endswith("\\conda.bat") or prog.endswith("\\mamba.bat")
105+
or prog in ("conda", "conda.bat", "mamba", "mamba.bat")
106+
)
107+
argv = (["cmd", "/c"] + list(cmd)) if needs_cmd else list(cmd)
108+
100109
try:
101110
if do_capture:
102111
cp = subprocess.run(
103-
list(cmd),
112+
argv,
104113
cwd=str(cwd) if cwd else None,
105114
capture_output=True,
106115
text=True,
@@ -119,7 +128,7 @@ def run(
119128
sys.stderr.flush()
120129
else:
121130
cp = subprocess.run(
122-
list(cmd),
131+
argv,
123132
cwd=str(cwd) if cwd else None,
124133
text=True,
125134
check=False,

src/diffpy/cmi/installer.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,6 @@ def _script_supported_on_platform(ext: str) -> bool:
200200
"""
201201
ext = (ext or "").lower()
202202
if os.name == "nt":
203-
if ext == ".sh":
204-
return shutil.which("bash") is not None
205203
return ext == ".bat"
206204
return ext == ".sh"
207205

@@ -262,11 +260,6 @@ def _script_exec_cmd(path: Path, args: List[str]) -> List[str]:
262260
"""
263261
ext = path.suffix.lower()
264262
if os.name == "nt":
265-
if ext == ".sh":
266-
bash = shutil.which("bash")
267-
if bash:
268-
return [bash, str(path)] + args
269-
return [str(path)] + args
270263
if ext != ".bat":
271264
return [str(path)] + args
272265
return ["cmd", "/c", str(path)] + args
@@ -338,7 +331,10 @@ def install_requirements(
338331

339332
# Execute scripts
340333
for r in reqs:
341-
if r.kind != "script":
334+
if r.kind != "script" or not r.name:
335+
continue
336+
if not r.name.startswith("_") and _is_installed(r.name):
337+
plog.info("Skipping script (already satisfied): %s", r.raw.strip())
342338
continue
343339
first_token = shlex.split(r.raw.strip(), posix=(os.name != "nt"))[0]
344340
ext = Path(first_token).suffix.lower()

0 commit comments

Comments
 (0)