Skip to content

Commit

Permalink
support for python 2.7 (pytrip#653)
Browse files Browse the repository at this point in the history
* pytest for 2.7

* typo fixed

* fixes

* fix no nonlocal in py2.7

* python -m for pytest

* missing dep added

* missing init added

* fix no newline in py2.7

* fix write in py2.7

* fix write in py2.7 with decode

* add install of vc++9.0 when py2.7

* add windows check in py 2.7

* vcredist2008

* echo GITHUB_WORKSPACE

* wget and msiexec

* add comments

* [build]

* add vc++9.0 in full_tests [build]

* fix reqs in setup.py [build]

* deepsource fixes [build]

* fixes

* add building wheel for py2.7 [build]

* changes

* [build]

* change name slice

* wget tests

* remove msiexec logs

* wget changes

* deepsource fixes

* appveyor py2.7

* fixes

* fixes [build]

Co-authored-by: michal367 <[email protected]>
  • Loading branch information
grzanka and michal367 committed Dec 15, 2021
1 parent c841bb3 commit 7ebd134
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 98 deletions.
22 changes: 17 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ jobs:
needs: [initialise]
strategy:
matrix:
python-version: ['3.5', '3.9', '3.10']
python-version: ['2.7', '3.5', '3.9', '3.10']
platform: [ubuntu-latest, windows-latest]
exclude:
# skip this config, because it is slow
- python-version: '2.7'
platform: windows-latest
steps:
- uses: actions/checkout@v2

Expand All @@ -69,7 +73,7 @@ jobs:
- name: Smoke tests
run: |
pytest -k "smoke" tests/
python -m pytest -k "smoke" tests/
full_tests:
# these full tests, being slow are triggered only in following cases:
Expand All @@ -83,7 +87,7 @@ jobs:
needs: [initialise, smoke_test]
strategy:
matrix:
python-version: ['3.5', '3.6', '3.7', '3.8', '3.9', '3.10']
python-version: ['2.7', '3.5', '3.6', '3.7', '3.8', '3.9', '3.10']
platform: [ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v2
Expand All @@ -92,6 +96,14 @@ jobs:
with:
python-version: ${{ matrix.python-version }}

- name: Install Visual C++ 9.0 for Python 2.7
# we download from web.archive.org, because it is not accessible on official microsoft site
# https://stackoverflow.com/questions/43645519/microsoft-visual-c-9-0-is-required
if: matrix.platform == 'windows-latest' && matrix.python-version == '2.7'
run: |
C:\msys64\usr\bin\wget.exe --progress=dot:mega --tries=50 'https://web.archive.org/web/20200709160228if_/https://download.microsoft.com/download/7/9/6/796EF2E4-801B-4FC4-AB28-B59FBF6D907B/VCForPython27.msi'
msiexec /i VCForPython27.msi /qn
- name: Install dependencies
run: |
python --version
Expand All @@ -103,7 +115,7 @@ jobs:
- name: Regular tests
run: |
pytest -k "not slow" tests/
python -m pytest -k "not slow" tests/
build_wheels_manylinux1:
# these full tests, being slow are triggered only in following cases:
Expand All @@ -117,7 +129,7 @@ jobs:
needs: [initialise, full_tests]
strategy:
matrix:
python-versions: ["cp35-cp35m", "cp36-cp36m", "cp37-cp37m","cp38-cp38", "cp39-cp39"]
python-versions: ["cp27-cp27m", "cp35-cp35m", "cp36-cp36m", "cp37-cp37m","cp38-cp38", "cp39-cp39"]

steps:
- uses: actions/checkout@v2
Expand Down
3 changes: 3 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ environment:
secure: GgEHENWgbfFj3Y3ESLvQqfHXKyp2BPy65U8B21ACBOw57Yv/epOgh8bp5tsi4PUa9ZkAIRW8m4GZx5HHm4q36zkXZZaZUZYV3qMRNpoGe/EweEdg8dqjW5wAGQ/bDQgVgfbWzg9o2KwuSFd+j1wLcKEc4ZMcKZvgOQlQQXqVKNB3VpeZftjI14ss1QPFgY4TLpa+bfaH1xHVCT/BKAy7y8wKBfZBvpb4xWOyOKaRKDa1+NOxS8TGYRBThWK6R9/gvXvh9ySzFwUbjCbHdcpK/w==
PYPIUSER: __token__
matrix:
- platform: x64
PYTHON: "C:\\Python27-x64"

- platform: x64
PYTHON: "C:\\Python35-x64"

Expand Down
13 changes: 10 additions & 3 deletions pytrip/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
A cube is a 3D object holding data, such as CT Hounsfield units, Dose- or LET values.
"""
import os
import io
import re
import sys
import logging
Expand Down Expand Up @@ -96,7 +97,7 @@ def __init__(self, cube=None):
# unique for each CT slice
self._ct_sop_instance_uid = cube._ct_sop_instance_uid

self.cube = np.zeros((self.dimz, self.dimy, self.dimx), dtype=cube.pydata_type)
self.cube = np.zeros((self.dimz, self.dimy, self.dimx), dtype=cube.pydata_type) # skipcq PTC-W0052

else:
import getpass
Expand Down Expand Up @@ -745,8 +746,14 @@ def _write_trip_header(self, path):
else:
output_str += "z_table no\n"

with open(path, "w+", newline='\n') as f:
f.write(output_str)
# for compatibility with python 2.7 we need to use `io.open` instead of `open`,
# as `open` function in python 2.7 cannot handle `newline` argument.
# This needs to be followed by `decode()`d string being written
with io.open(path, "w+", newline='\n') as f:
try:
f.write(output_str)
except TypeError:
f.write(output_str.decode())

def _write_trip_data(self, path):
""" Writes the binary data cube in TRiP98 format to a file.
Expand Down
4 changes: 2 additions & 2 deletions pytrip/ddd.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ def load_ddd(self, directory):
data = f.read()
lines = data.split('\n')
for n, line in enumerate(data.split("\n")):
if line.find("energy") is not -1:
if line.find("energy") != -1:
energy = float(line.split()[1])
if line.find('!') is -1 and line.find('#') is -1:
if line.find('!') == -1 and line.find('#') == -1:
break
for i in range(n, len(lines)):
if len(lines[i]) < 3:
Expand Down
24 changes: 14 additions & 10 deletions pytrip/tripexecuter/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,21 +467,23 @@ def _compress_files(self, source_dir):
logger.debug("Compressing files in {:s} to {:s}".format(source_dir, target_path))
self._info("Compressing files in {:s} to {:s}".format(source_dir, target_path))

total_size = get_size(source_dir)
sum_size = 0
# trick for no nonlocal in python 2.7
track_progress_info = {
"total_size": get_size(source_dir),
"sum_size": 0
}

def track_progress(tarinfo):
nonlocal total_size, sum_size
if tarinfo.isfile():
sum_size += tarinfo.size
percentage = int(sum_size / total_size * 100)
track_progress_info["sum_size"] += tarinfo.size
percentage = int(track_progress_info["sum_size"] / track_progress_info["total_size"] * 100)
self._log("Compressing file {} with size {} ({}%)".format(tarinfo.name,
human_readable_size(tarinfo.size),
percentage))
return tarinfo

with tarfile.open(target_path, "w:gz") as tar:
self._log("Size to compress: {:s}".format(human_readable_size(total_size)))
self._log("Size to compress: {:s}".format(human_readable_size(track_progress_info["total_size"])))
tar.add(source_dir, arcname=dirname, filter=track_progress)
self._log("Compressing done\n")

Expand Down Expand Up @@ -510,15 +512,17 @@ def _extract_tarball(self, tgz_path, target_dirpath):
if os.path.exists(out_dirpath):
shutil.rmtree(out_dirpath)

sum_size = 0
# trick for no nonlocal in python 2.7
track_progress_info = {
"sum_size": 0
}

def track_progress(members, files_total_size):
nonlocal sum_size
for file in members:
yield file
if file.isfile():
sum_size += file.size
percentage = int(sum_size / files_total_size * 100)
track_progress_info["sum_size"] += file.size
percentage = int(track_progress_info["sum_size"] / files_total_size * 100)
self._log("Extracting file {} with size {} ({}%)".format(file.name,
human_readable_size(file.size),
percentage))
Expand Down
Loading

0 comments on commit 7ebd134

Please sign in to comment.