Skip to content

Commit

Permalink
BUG: Fix temporary folder containing special characters
Browse files Browse the repository at this point in the history
qt.QDateTime().currentDateTime().toString(...) may use Arabic numerals on an operating system set to Arabic language.
Always specify en-US locale when using when using date in file/folder names to avoid having to work with paths that may contain unicode characters
(such paths may cause problems on certain file systems and it may not be always possible to pass them as command-line arguments).

Fixes problem originally reported at:
https://discourse.slicer.org/t/i-tried-everything-to-run-totalsegmentator-pls-help/33023/3

(cherry picked from commit eccb98c)
  • Loading branch information
lassoan authored and jcfr committed Dec 11, 2023
1 parent 6138fcc commit 38e54b9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
5 changes: 4 additions & 1 deletion Base/Python/slicer/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2850,7 +2850,10 @@ def tempDirectory(key="__SlicerTemp__", tempDir=None, includeDateTime=True):
if not tempDir:
tempDir = qt.QDir(slicer.app.temporaryPath)
if includeDateTime:
tempDirName = key + qt.QDateTime().currentDateTime().toString("yyyy-MM-dd_hh+mm+ss.zzz")
# Force using en-US locale, otherwise for example on a computer with
# Egyptian Arabic (ar-EG) locale, Arabic numerals may be used.
enUsLocale = qt.QLocale(qt.QLocale.English, qt.QLocale.UnitedStates)
tempDirName = key + enUsLocale.toString(qt.QDateTime.currentDateTime(), "yyyy-MM-dd_hh+mm+ss.zzz")
else:
tempDirName = key
fileInfo = qt.QFileInfo(qt.QDir(tempDir), tempDirName)
Expand Down
10 changes: 8 additions & 2 deletions Base/QTGUI/qSlicerApplication.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1017,12 +1017,15 @@ void qSlicerApplication::setupFileLogging()

// Add new log file path for the current session
QString tempDir = this->temporaryPath();
// Force using en-US locale, otherwise for example on a computer with
// Egyptian Arabic (ar-EG) locale, Arabic numerals may be used.
QLocale enUsLocale = QLocale(QLocale::English, QLocale::UnitedStates);
QString currentLogFilePath = QString("%1/%2_%3_%4_%5_%6.log")
.arg(tempDir)
.arg(this->applicationName())
.arg(qSlicerApplication::application()->applicationVersion())
.arg(qSlicerApplication::application()->mainApplicationRevision())
.arg(QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss"))
.arg(enUsLocale.toString(QDateTime::currentDateTime(), "yyyyMMdd_hhmmss"))
.arg(QRandomGenerator::global()->generate() % 1000, 3, 10, QLatin1Char('0'));
logFilePaths.prepend(currentLogFilePath);

Expand Down Expand Up @@ -1117,9 +1120,12 @@ void qSlicerApplication::logApplicationInformation() const

int titleIndex = 0;
// Session start time
// Force using en-US locale, otherwise for example on a computer with
// Egyptian Arabic (ar-EG) locale, Arabic numerals may be used.
QLocale enUsLocale = QLocale(QLocale::English, QLocale::UnitedStates);
qDebug("%s: %s",
qPrintable(titles.at(titleIndex++).leftJustified(titleWidth, '.')),
qPrintable(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")));
qPrintable(enUsLocale.toString(QDateTime::currentDateTime(), "yyyyMMdd_hhmmss")));

// Slicer version
qDebug("%s: %s (revision %s / %s) %s - %s %s",
Expand Down
5 changes: 4 additions & 1 deletion Modules/Scripted/DICOMLib/DICOMUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,10 @@ def importFromDICOMWeb(
outputDirectoryBase = slicer.dicomDatabase.databaseDirectory + "/DICOMweb"
if not os.access(outputDirectoryBase, os.F_OK):
os.makedirs(outputDirectoryBase)
outputDirectoryBase += "/" + qt.QDateTime.currentDateTime().toString("yyyyMMdd-hhmmss")
# Force using en-US locale, otherwise for example on a computer with
# Egyptian Arabic (ar-EG) locale, Arabic numerals may be used.
enUsLocale = qt.QLocale(qt.QLocale.English, qt.QLocale.UnitedStates)
outputDirectoryBase += "/" + enUsLocale.toString(qt.QDateTime.currentDateTime(), "yyyyMMdd-hhmmss")
outputDirectory = qt.QTemporaryDir(outputDirectoryBase) # Add unique substring to directory
outputDirectory.setAutoRemove(False)
outputDirectoryPath = outputDirectory.path()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,12 @@ void qSlicerDICOMExportDialog::onExport()
{
// Save to temporary folder and store files in database directory when adding
outputFolder.setPath(qSlicerApplication::application()->temporaryPath());
QString tempSubDirName = QString("DICOMExportTemp_%1").arg(QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss"));

// Force using en-US locale, otherwise for example on a computer with
// Egyptian Arabic (ar-EG) locale, Arabic numerals may be used.
QLocale enUsLocale = QLocale(QLocale::English, QLocale::UnitedStates);
QString tempSubDirName = QString("DICOMExportTemp_%1").arg(enUsLocale.toString(QDateTime::currentDateTime(), "yyyyMMdd_hhmmss"));

outputFolder.mkdir(tempSubDirName);
outputFolder.cd(tempSubDirName);
}
Expand Down

0 comments on commit 38e54b9

Please sign in to comment.