Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue for building docker container #91

Merged
merged 10 commits into from
Dec 13, 2024
13 changes: 7 additions & 6 deletions .github/workflows/build-container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,25 @@ jobs:
name: 'Build amc2moodle container'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@main
- id: docker-tag
uses: yuya-takeyama/docker-tag-from-github-ref-action@v1
uses: yuya-takeyama/docker-tag-from-github-ref-action@main
with:
latest-branches: 'main,master'
- name: "Build:checkout"
uses: actions/checkout@v2
uses: actions/checkout@main
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
uses: docker/setup-buildx-action@main
- name: Login to Github Packages
uses: docker/login-action@v1
uses: docker/login-action@main
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.PAT }}
- name: 'Build:dockerimage'
uses: docker/build-push-action@v2
id: docker_build
uses: docker/build-push-action@main
with:
# relative path to the place where source code with Dockerfile is located
context: ./docker
Expand Down
174 changes: 85 additions & 89 deletions .github/workflows/ci-mac-os.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,110 +6,106 @@ name: CI-mac-os
# events but only for the master branch
on:
push:
branches: [ '*' ]
paths-ignore: # Don't trigger on files that are updated by the CI
branches: ["*"]
paths-ignore: # Don't trigger on files that are updated by the CI
- README.md
pull_request:
branches: [ '*' ]
branches: ["*"]
schedule:
# * is a special character in YAML so you have to quote this string
# run at 02:01 on every 15th day-of-month.
- cron: '1 2 */15 * *'

- cron: "1 2 */15 * *"

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
test:
runs-on: macos-latest
strategy:
matrix:
python-version: [3.8, 3.9, '3.10', '3.11', '3.12', '3.13']
python-version: [3.8, 3.9, "3.10", "3.11", "3.12", "3.13"]
env:
# set env variable for Wand
MAGICK_HOME: '/opt/homebrew'
MAGICK_HOME: "/opt/homebrew"
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@main
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@main
with:
python-version: ${{ matrix.python-version }}

- name: Install the LaTeXML dependences
run: |
python -m pip install --upgrade pip

# Install requirements for lateXML
brew update
brew install imagemagick
brew install --cask basictex
brew install latexml
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@main
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@main
with:
python-version: ${{ matrix.python-version }}

- name: Update $PATH
# run: echo ::add-path::/Library/TeX/texbin # depreciated
run: echo "/Library/TeX/texbin" >> $GITHUB_PATH

- name: Add fonts (required by moodle2amc tests)
run: |
# xstring is just required for testing --includestyles
sudo tlmgr update --self
sudo tlmgr install collection-fontsrecommended bophook xstring --verify-repo=none

- name: Test latexml
run: |
# add --strict flag to be more strict in error catching
latexml --noparse --nocomment --strict --path=./amc2moodle/amc2moodle --dest=./out.xml ./amc2moodle/tests/payload_test_amc2moodle/QCM.tex

- name: Install amc2moodle
run: |
# add -e to have write access for test. TODO : change with temp file
pip install -e .

- name: Test amc2moodle
run: |
python -m amc2moodle.tests.test_amc2moodle

- name: Test moodle2amc
run: |
# add automultiplechoice.sty local copy to LaTeX PATH (for this step)
# export TEXINPUTS=.:./tests/payload_test_amc2moodle/:$TEXINPUTS (during tests, automultiplechoice.sty is copied in the temporary test folder )
python -m amc2moodle.tests.test_moodle2amc
- name: Test text parser
run: |
python -m amc2moodle.tests.test_utils_text
- name: Test calculated questions parsers
run: |
python -m amc2moodle.tests.test_utils_calculatedParser


# Store output files
# amc2moodle
- name: Archive XML test output of amc2moodle (without tikz)
if: ${{ always() }}
uses: actions/upload-artifact@main
with:
name: test_notikz_${{ matrix.python-version }}
path: output_tests/test_notikz.xml

- name: Archive XML test output of amc2moodle (with tikz)
if: ${{ always() }}
uses: actions/upload-artifact@main
with:
name: test_tikz_${{ matrix.python-version }}
path: output_tests/test_tikz.xml
# moodle2amc
- name: Move latex output
if: ${{ always() }}
run: |
mkdir moodle-bank-exemple-output
mv output_tests/test_moodle-bank-exemple.* moodle-bank-exemple-output/

- name: Archive LaTeX test output (moodle2amc)
if: ${{ always() }}
uses: actions/upload-artifact@main
with:
name: test_moodle-bank-exemple_${{ matrix.python-version }}
path: moodle-bank-exemple-output
- name: Install the LaTeXML dependences
run: |
python -m pip install --upgrade pip


# Install requirements for lateXML
brew update
brew install imagemagick
brew install --cask basictex
brew install latexml

- name: Update $PATH
# run: echo ::add-path::/Library/TeX/texbin # depreciated
run: echo "/Library/TeX/texbin" >> $GITHUB_PATH

- name: Add fonts (required by moodle2amc tests)
run: |
# xstring is just required for testing --includestyles
sudo tlmgr update --self
sudo tlmgr install --verify-repo=none collection-fontsrecommended bophook xstring

- name: Test latexml
run: |
# add --strict flag to be more strict in error catching
latexml --noparse --nocomment --strict --path=./amc2moodle/amc2moodle --dest=./out.xml ./amc2moodle/tests/payload_test_amc2moodle/QCM.tex

- name: Install amc2moodle
run: |
# add -e to have write access for test. TODO : change with temp file
pip install -e .

- name: Test amc2moodle
run: |
python -m amc2moodle.tests.test_amc2moodle

- name: Test moodle2amc
run: |
# add automultiplechoice.sty local copy to LaTeX PATH (for this step)
# export TEXINPUTS=.:./tests/payload_test_amc2moodle/:$TEXINPUTS (during tests, automultiplechoice.sty is copied in the temporary test folder )
python -m amc2moodle.tests.test_moodle2amc
- name: Test text parser
run: |
python -m amc2moodle.tests.test_utils_text
- name: Test calculated questions parsers
run: |
python -m amc2moodle.tests.test_utils_calculatedParser

# Store output files
# amc2moodle
- name: Archive XML test output of amc2moodle (without tikz)
if: ${{ always() }}
uses: actions/upload-artifact@main
with:
name: test_notikz_${{ matrix.python-version }}
path: output_tests/test_notikz.xml

- name: Archive XML test output of amc2moodle (with tikz)
if: ${{ always() }}
uses: actions/upload-artifact@main
with:
name: test_tikz_${{ matrix.python-version }}
path: output_tests/test_tikz.xml
# moodle2amc
- name: Move latex output
if: ${{ always() }}
run: |
mkdir moodle-bank-exemple-output
mv output_tests/test_moodle-bank-exemple.* moodle-bank-exemple-output/

- name: Archive LaTeX test output (moodle2amc)
if: ${{ always() }}
uses: actions/upload-artifact@main
with:
name: test_moodle-bank-exemple_${{ matrix.python-version }}
path: moodle-bank-exemple-output
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Help and options can be obtained using
amc2moodle -h
```
Then on moodle, go to the course `administration\question bank\import` and choose 'moodle XML format' and tick: **If your grade are not conform to that you must use: 'Nearest grade if not listed' in import option in the moodle question bank** (see below for details).
Examples of the `amc2moodle` possibilities are given at [QCM.pdf](./amc2moodle/amc2moodle/test/QCM.pdf)
Examples of the `amc2moodle` possibilities are given at [QCM.pdf](./amc2moodle/tests/payload_test_amc2moodle/QCM.pdf)

If your original exam uses [AMC-TXT syntax](https://www.auto-multiple-choice.net/auto-multiple-choice.en/AMC-TXT.shtml), you must first convert it to LaTeX before feeding it to `amc2moodle`. To convert an AMC-TXT file to LaTeX, generate the exam documents with AMC graphical interface as usual. AMC will generate a LaTeX version of your exam called `DOC-filtered.tex` inside the project directory, which you can pass to `amc2moodle`.

Expand All @@ -82,7 +82,7 @@ Help and options can be obtained using
```
moodle2amc -h
```
Then the output LaTeX can be edited and included for creating amc exams. Examples of the `moodle2amc` possibilities are given [here](./amc2moodle/moodle2amc/test/moodle-bank-exemple.pdf).
Then the output LaTeX can be edited and included for creating amc exams. Examples of the `moodle2amc` possibilities are given [here](./amc2moodle/tests/payload_test_moodle2amc/moodle-bank-exemple.pdf).



Expand Down Expand Up @@ -110,8 +110,8 @@ If you want to contribute to `amc2moodle`, your are welcomed! Don't hesitate to
- add support for other language (French and English are present) in AMC command
- ...

To ensure code homogeneity among contributors, we use a source-code analyzer (e.g. `pylint`).
Before submitting a PR, run the tests suite.
To ensure code homogeneity among contributors, we use [`ruff`](https://docs.astral.sh/ruff/) as source-code analyzer included in [`hatch`](https://hatch.pypa.io/1.9/config/static-analysis/) (e.g. `hatch fmt --check`).
Before submitting a PR, run the tests suite using `hatch test` (or `hatch test -c` to run test suite with covering report).

## License
This file is part of amc2moodle, a tool to convert automultiplechoice quizzes to moodle questions.
Expand Down
5 changes: 4 additions & 1 deletion amc2moodle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

from amc2moodle._version import __version__
import logging

from amc2moodle._version import __version__

from .utils.customLogging import CountLogger as _CountLogger

# Define the new default logger class. It has to be here to ensure that all
# logger instances will have the good class
# TODO check for possible side effect
Expand Down
35 changes: 18 additions & 17 deletions amc2moodle/amc2moodle/amc2moodle_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

from typing import Callable
from ..amc2moodle import convert
from ..utils.flatex import Flatex
import subprocess
import sys
import logging
import os
import shutil
from importlib import util # python 3.x
import subprocess
import sys
import tempfile
from shutil import copytree
import logging
from concurrent.futures import ThreadPoolExecutor
from importlib import util # python 3.x
from shutil import copytree
from typing import Callable

from ..amc2moodle import convert
from ..utils.flatex import Flatex

# activate logger
Logger = logging.getLogger(__name__)
Expand All @@ -49,7 +50,7 @@ def checkTools(show=True):
Logger.critical("Please install lxml's Python module")
# LaTeXML
latexMLwhich = subprocess.run(['which', 'latexml'],
stdout=subprocess.DEVNULL)
stdout=subprocess.DEVNULL, check=False)
latexmlOk = latexMLwhich.returncode == 0
if not latexmlOk:
Logger.critical("Please install LaTeXML software (see https://dlmf.nist.gov/LaTeXML/)")
Expand Down Expand Up @@ -271,22 +272,22 @@ def runLaTeXML(self):
def runXMLindent(self):
"""Run XML indentation with subprocess."""
# check for xmlindent
xmlindentwhich = subprocess.run(['which', 'xmlindent'])
xmlindentwhich = subprocess.run(['which', 'xmlindent'], check=False)
xmlindentOk = xmlindentwhich.returncode == 0
# check for xmllint (Macos)
xmllintwhich = subprocess.run(['which', 'xmllint'])
xmllintwhich = subprocess.run(['which', 'xmllint'], check=False)
xmllintOk = xmllintwhich.returncode == 0

# linux
if xmlindentOk:
Logger.debug(' > Indenting XML output...')
subprocess.run(['xmlindent', self.output, '-o', self.output],
stdout=subprocess.DEVNULL)
stdout=subprocess.DEVNULL, check=False)
# MacOS
if xmllintOk and not xmlindentOk:
Logger.debug(' > Indenting XML output...')
subprocess.run(['xmllint', self.output, '--format', '--output', self.output],
stdout=subprocess.DEVNULL)
stdout=subprocess.DEVNULL, check=False)

def runCleanXML(self):
"""Clean final XML file remove "%" added by LaTeXML at end of lines (EXPERIMENTAL)."""
Expand All @@ -295,8 +296,8 @@ def runCleanXML(self):
# copy output file and remove '%\n' (a temporary file will be used and deleted)
fdTemp, pathTemp = tempfile.mkstemp(dir=getPathFile(self.inputtex),
prefix='xmlclean')
Logger.debug(" > Cleaning: create {} ".format(pathTemp))
with open(self.output, 'r') as fin, open(pathTemp, 'w') as fout:
Logger.debug(f" > Cleaning: create {pathTemp} ")
with open(self.output) as fin, open(pathTemp, 'w') as fout:
# set replacement counter for patern occurence
nreplacement = 0
for lno, line in enumerate(fin):
Expand All @@ -305,12 +306,12 @@ def runCleanXML(self):
# remove '%' at ends of lines
line = line.replace(pattern, "\n")
if count > 0:
Logger.debug(" Remove pattern at (line {} of {})".format(lno+1, self.output))
Logger.debug(f" Remove pattern at (line {lno+1} of {self.output})")
fout.write(line)
# copy temporary file to the output
os.close(fdTemp)
shutil.copy(pathTemp, self.output)
Logger.info(" > Cleaning: done, with {} replacements.".format(nreplacement))
Logger.info(f" > Cleaning: done, with {nreplacement} replacements.")

def runBuilding(self):
"""Build the xml file for Moodle quizz."""
Expand Down
Loading
Loading