Skip to content

Commit

Permalink
Merge pull request #35 from opensafely-core/convert-images
Browse files Browse the repository at this point in the history
Add convert utility for output images.
  • Loading branch information
bloodearnest authored Dec 19, 2024
2 parents 6b08b44 + 98effbf commit fdf1d6a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ RUN --mount=type=cache,target=/var/cache/apt \
echo "deb https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy main" > /etc/apt/sources.list.d/deadsnakes-ppa.list &&\
/usr/lib/apt/apt-helper download-file 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xf23c5a6cf475977595c89f51ba6932366a755776' /etc/apt/trusted.gpg.d/deadsnakes.asc

# stata needs libpng16, install python dependencies
# stata needs libpng16, install python dependencies, and imagemagick
# edit imagemagick policy to allow eps conversion, which is disabled by default
COPY packages.txt /root/packages.txt
RUN --mount=type=cache,target=/var/cache/apt \
/root/docker-apt-install.sh /root/packages.txt
/root/docker-apt-install.sh /root/packages.txt &&\
sed -i -z 's#<!-- disable ghostscript.*</policymap>#</policymap>#g' /etc/ImageMagick-6/policy.xml

# set PYTHONUSERBASE for installing user packages
ENV PYTHONUSERBASE=/usr/local
Expand Down
7 changes: 5 additions & 2 deletions packages.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# needed by stata
libpng16-16
libtinfo5
libpng16-16
libtinfo5
libncurses5
# python
libpython3.11
python3.11
python3.11-distutils
python3-pip
python3.11-venv
# to generate more formats, as stata on linux can only do eps/svg/tiff
ghostscript
imagemagick
13 changes: 13 additions & 0 deletions tests/analysis/convert.do
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
* Create a simple dataset
set obs 10 // Create 10 observations
gen x = _n // Create variable x as the observation number
gen y = x^2 // Create variable y as x squared

* Generate a scatterplot of y vs. x
graph twoway scatter y x, title("Test Graph") xlabel(, grid) ylabel(, grid)

* Save the graph as a file
graph export "test.eps", replace

* convert to png using convert utility
! convert test.eps test.png
9 changes: 6 additions & 3 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ def sanitize(string):
return RE_WHITESPACE.sub(" ", string).strip()


def run_stata(command):
def run_stata(command, workspace=None):
if workspace is None:
workspace = TESTS_PATH.resolve()

command_list = command.split()
filestem = Path(command_list[0]).stem
uid = os.getuid()
Expand All @@ -36,13 +39,13 @@ def run_stata(command):
"-e",
"STATA_LICENSE",
"-v",
f"{TESTS_PATH.resolve()}:/workspace",
f"{workspace}:/workspace",
IMAGE,
*command_list,
],
capture_output=True,
)
log_file = TESTS_PATH / f"{filestem}.log"
log_file = workspace / f"{filestem}.log"
# The log file should always exist, even if the .do file failed to load.
# If it doesn't, something probably went wrong with running the container itself,
# so fail and report the process stderr.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import shutil

from .helpers import run_stata


Expand All @@ -15,3 +17,11 @@ def test_basic_stata_fails():
assert return_code == 1
for content in [output, log_content]:
assert "badstring" in content


def test_convert_image(tmp_path):
shutil.copy("tests/analysis/convert.do", tmp_path)

return_code, output, log_content = run_stata("convert.do", workspace=tmp_path)
assert return_code == 0
assert (tmp_path / "test.png").exists()

0 comments on commit fdf1d6a

Please sign in to comment.