Skip to content

Commit

Permalink
Merge pull request #11 from NiftyPET/devel
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl authored Dec 31, 2020
2 parents f25e6ac + c5062be commit c0e6c3c
Showing 1 changed file with 34 additions and 46 deletions.
80 changes: 34 additions & 46 deletions niftypet/ninst/install_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,12 @@ def askdir(title, initialdir):

# source and build folder names
dirsrc = "_src"
dirbld = Path("_bld")
dirbld = "_bld"

# number of threads
ncpu = multiprocessing.cpu_count()

LOG_FORMAT = "%(levelname)s:%(asctime)s:%(name)s:%(funcName)s\n> %(message)s"
CHUNK_SIZE = 2 ** 15 # 32 kiB


class LogHandler(logging.StreamHandler):
Expand Down Expand Up @@ -130,36 +129,30 @@ def urlopen_cached(url, outdir, fname=None, mode="rb"):
fout = outdir / fname
cache = outdir / f"{fname}.url"
if not fout.is_file() or not cache.is_file() or cache.read_text().strip() != url:
req = request.Request(url=url)
with request.urlopen(req) as raw:
with tqdm.wrapattr(raw, "read", total=getattr(raw, "length", None)) as fd:
with fout.open("wb") as fo:
i = fd.read(CHUNK_SIZE)
while i:
fo.write(i)
i = fd.read(CHUNK_SIZE)
fi = request.urlopen(url)
with fout.open("wb") as raw:
with tqdm.wrapattr(raw, "write", total=getattr(fi, "length", None)) as fo:
copyfileobj(fi, fo)
cache.write_text(url)
return fout.open(mode)


def extractall(fzip, dest, desc="Extracting"):
"""zipfile.Zipfile(fzip).extractall(dest) with progress"""
dest = Path(dest).expanduser()
with ZipFile(fzip) as zipf:
with tqdm(
desc=desc,
unit="B",
unit_scale=True,
unit_divisor=1024,
total=sum(getattr(i, "file_size", 0) for i in zipf.infolist()),
) as pbar:
for i in zipf.infolist():
if not getattr(i, "file_size", 0): # directory
zipf.extract(i, fspath(dest))
else:
with zipf.open(i) as fi:
with open(fspath(dest / i.filename), "wb") as fo:
copyfileobj(CallbackIOWrapper(pbar.update, fi), fo)
with ZipFile(fzip) as zipf, tqdm(
desc=desc,
unit="B",
unit_scale=True,
unit_divisor=1024,
total=sum(getattr(i, "file_size", 0) for i in zipf.infolist()),
) as pbar:
for i in zipf.infolist():
if not getattr(i, "file_size", 0): # directory
zipf.extract(i, fspath(dest))
else:
with zipf.open(i) as fi, open(fspath(dest / i.filename), "wb") as fo:
copyfileobj(CallbackIOWrapper(pbar.update, fi), fo)


def query_yesno(question):
Expand Down Expand Up @@ -372,9 +365,6 @@ def install_tool(app, Cnt):
Install the requested software from the git 'repo'
and check out the version given by 'sha1'.
"""
# get the current working directory
cwd = os.getcwd()

# pick the target installation folder for tools
if Cnt.get("PATHTOOLS", None):
path_tools = Path(Cnt["PATHTOOLS"])
Expand Down Expand Up @@ -405,11 +395,11 @@ def install_tool(app, Cnt):
if app == "niftyreg":
repo = repo_reg
sha1 = sha1_reg
dest = path_tools / "niftyreg"
dest = path_tools.resolve() / "niftyreg"
elif app == "dcm2niix":
repo = repo_dcm
sha1 = sha1_dcm
dest = path_tools / "dcm2niix"
dest = path_tools.resolve() / "dcm2niix"

if not Cnt["CMPL_DCM2NIIX"]:
# avoid installing from source, instead download the full version:
Expand All @@ -422,40 +412,40 @@ def install_tool(app, Cnt):
if dest.is_dir():
rmtree(fspath(dest))
dest.mkdir()
os.chdir(dest)

# clone the git repository
run(["git", "clone", repo, dirsrc])
os.chdir(dirsrc)
run(["git", "clone", repo, fspath(dest / dirsrc)])
log.info("checking out the specific git version of the software...")
run(["git", "checkout", sha1])
os.chdir(cwd)
run(["git", "-C", fspath(dest / dirsrc), "checkout", sha1])

dirbld.mkdir(exist_ok=True)
os.chdir(dirbld)
(dest / dirbld).mkdir(exist_ok=True)
# run cmake with arguments
if platform.system() == "Windows":
run(
[
"cmake",
"../" + dirsrc,
fspath(dest / dirsrc),
"-DBUILD_ALL_DEP=ON",
f"-DCMAKE_INSTALL_PREFIX={dest}",
"-G",
Cnt["MSVC_VRSN"],
]
],
cwd=fspath(dest / dirbld),
)
run(
["cmake", "--build", "./", "--config", "Release", "--target", "install"],
cwd=fspath(dest / dirbld),
)
run(["cmake", "--build", "./", "--config", "Release", "--target", "install"])
elif platform.system() in ["Linux", "Darwin"]:
cmd = [
"cmake",
"../" + dirsrc,
fspath(dest / dirsrc),
"-DBUILD_ALL_DEP=ON",
f"-DCMAKE_INSTALL_PREFIX={dest}",
]
if Cnt["CMAKE_TLS_PAR"] != "":
cmd.append(Cnt["CMAKE_TLS_PAR"])
run(cmd)
run(cmd, cwd=fspath(dest / dirbld))
run(
[
"cmake",
Expand All @@ -468,12 +458,10 @@ def install_tool(app, Cnt):
"--",
"-j",
str(ncpu),
]
],
cwd=fspath(dest / dirbld),
)

# restore the current working directory
os.chdir(cwd)

if app == "niftyreg":
try:
Cnt["RESPATH"] = fspath(next((dest / "bin").glob("reg_resample*")))
Expand Down

0 comments on commit c0e6c3c

Please sign in to comment.