Skip to content

Commit 2ccd4e4

Browse files
author
Idan Miara
committed
LatestPythonVersionFromPath - a platform independent version of PythonVersionFromPath with an extra optional flag
1 parent 27563a4 commit 2ccd4e4

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

PythonForDelphi/Components/Sources/Core/PythonVersions.pas

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ TPythonVersion = record
7474
function GetLatestRegisteredPythonVersion(out PythonVersion: TPythonVersion): Boolean;
7575
function PythonVersionFromPath(const Path: string; out PythonVersion: TPythonVersion): Boolean;
7676
{$ENDIF}
77+
function LatestPythonVersionFromPath(const Path: string; out PythonVersion: TPythonVersion; FindLatestVersion: Boolean = True): Boolean;
7778

7879
implementation
7980

@@ -478,4 +479,63 @@ function PythonVersionFromPath(const Path: string; out PythonVersion: TPythonVer
478479

479480
{$ENDIF}
480481

482+
function LatestPythonVersionFromPath(const Path: string; out PythonVersion: TPythonVersion; FindLatestVersion: Boolean): Boolean;
483+
function FindPythonDLL(APath : string): string;
484+
Var
485+
DLLFileName: string;
486+
I, LastI, IncI: integer;
487+
begin
488+
Result := '';
489+
if FindLatestVersion then begin
490+
I := High(PYTHON_KNOWN_VERSIONS);
491+
LastI := COMPILED_FOR_PYTHON_VERSION_INDEX;
492+
IncI := -1;
493+
end else begin
494+
I := COMPILED_FOR_PYTHON_VERSION_INDEX;
495+
LastI := High(PYTHON_KNOWN_VERSIONS);
496+
IncI := +1;
497+
end;
498+
APath := IncludeTrailingPathDelimiter(APath);
499+
while i<>LastI+IncI do begin
500+
DLLFileName := PYTHON_KNOWN_VERSIONS[I].DLLName;
501+
if FileExists(APath+DLLFileName) then begin
502+
Result := DLLFileName;
503+
exit;
504+
end;
505+
I := I+IncI;
506+
end;
507+
end;
508+
509+
Var
510+
DLLFileName: string;
511+
DLLPath: string;
512+
begin
513+
Result := False;
514+
Finalize(PythonVersion);
515+
FillChar(PythonVersion, SizeOf(TPythonVersion), 0);
516+
DLLPath := ExcludeTrailingPathDelimiter(Path);
517+
518+
PythonVersion.InstallPath := DLLPath;
519+
520+
DLLFileName := FindPythonDLL(DLLPath);
521+
if DLLFileName = '' then begin
522+
DLLPath := IncludeTrailingPathDelimiter(DLLPath) + 'Scripts';
523+
DLLFileName := FindPythonDLL(DLLPath);
524+
end;
525+
if DLLFileName = '' then Exit;
526+
527+
{$IFDEF MSWINDOWS}
528+
// check if same platform
529+
try
530+
if {$IFDEF CPUX64}not {$ENDIF}IsEXEx64(DLLPath+'\python.exe') then Exit;
531+
except
532+
Exit;
533+
end;
534+
PythonVersion.fSysArchitecture := PythonVersion.ExpectedArchitecture;
535+
{$ENDIF}
536+
537+
PythonVersion.DLLPath := DLLPath;
538+
PythonVersion.SysVersion := GetPythonVersionFromDLLName(DLLFileName);
539+
end;
540+
481541
end.

0 commit comments

Comments
 (0)