Skip to content

Commit

Permalink
allow running the GUI w/o pixi install
Browse files Browse the repository at this point in the history
  • Loading branch information
briantoby committed Jan 20, 2025
1 parent b4dbfcc commit 76f3ef0
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 83 deletions.
5 changes: 5 additions & 0 deletions GSASII.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# script to start GSAS-II when not installed via pixi
import os
os.environ["GSASII_YOLO_PATH"] = "True"
from GSASII.GSASII import main
main()
103 changes: 21 additions & 82 deletions GSASII/GSASIIpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -1927,22 +1927,20 @@ def _old_TestSPG(fpth):
sys.path = savpath
return True

def SetBinaryPath(printInfo=False, loadBinary=False):
def SetBinaryPath(showConfigMsg=False):
'''
Add location of GSAS-II shared libraries (binaries: .so or
.pyd files) to path
.pyd files) to path (when needed). When GSAS-II is installed by
pixi, no change in the path is needed.
This routine must be executed after GSASIIpath is imported
and before any other GSAS-II imports are done, since
they assume binary files are in path
they may assume binary files are in path
:param bool printInfo: When True, information is printed to show
has happened (default is False)
:param bool loadBinary: no longer in use. This is now done in
:func:`GSASIIdataGUI.ShowVersions`.
:param bool showConfigMsg: When True, config info is shown (default is False)
'''
# run this routine only once no matter how many times it is called
# using the following globals to check for repeated calls
# cache the results of this routine so that repeated calls
# only search for binary routines once
global BinaryPathLoaded,binaryPath,BinaryPathFailed
if BinaryPathLoaded or BinaryPathFailed: return
try:
Expand All @@ -1951,83 +1949,24 @@ def SetBinaryPath(printInfo=False, loadBinary=False):
return
try:
from GSASII import pypowder
binaryPath = str(pathlib.Path(pypowder.__file__).parent)
binaryPath = None # special value to indicate that binaries have been installed into package
if showConfigMsg:
print(f'GSAS-II binaries co-located with GSAS-II: {os.path.dirname(__file__)}')
BinaryPathLoaded = True
LoadConfig(showConfigMsg)
return
except ImportError:
_old_path_discovery(printInfo, loadbinary)

if binaryPath:
LoadConfig(printInfo)
pass

def _old_path_discovery(printInfo=False, loadBinary=False):
global BinaryPathLoaded,binaryPath,BinaryPathFailed
if path2GSAS2 not in sys.path:
sys.path.insert(0,path2GSAS2) # make sure current path is used
binpath = None
binprfx = GetBinaryPrefix()
# places to look for the GSAS-II binary directory
binseapath = [os.path.abspath(sys.path[0])] # where Python is installed
binseapath += [os.path.abspath(os.path.dirname(__file__))] # directory where this file is found
binseapath += [os.path.dirname(binseapath[-1])] # parent of above directory
binseapath += [os.path.expanduser('~/.GSASII')] # directory in user's home
def appendIfExists(searchpathlist,loc,subdir):
newpath = os.path.join(loc,subdir)
if os.path.exists(newpath):
if newpath in searchpathlist: return
searchpathlist.append(newpath)
searched = []
for loc in binseapath:
if loc in searched: continue
searched.append(loc)
# Look at bin directory (created by a local compile) before looking for standard dist files
searchpathlist = []
appendIfExists(searchpathlist,loc,'bin')
appendIfExists(searchpathlist,loc,'bindist')
appendIfExists(searchpathlist,loc,'GSASII-bin')
# also look for directories named by platform etc in loc/AllBinaries or loc
versions = {}
namedpath = glob.glob(os.path.join(loc,'AllBinaries',binprfx+'*'))
namedpath += glob.glob(os.path.join(loc,'GSASII-bin',binprfx+'*'))
for d in namedpath:
d = os.path.realpath(d)
v = intver(d.rstrip('/').split('_')[-1].lstrip('n'))
versions[v] = d
vmin = None
vmax = None
# try to order the search in a way that makes sense
for v in sorted(versions.keys()):
if v <= inpver:
vmin = v
elif v > inpver:
vmax = v
break
if vmin in versions and versions[vmin] not in searchpathlist:
searchpathlist.append(versions[vmin])
if vmax in versions and versions[vmax] not in searchpathlist:
searchpathlist.append(versions[vmax])
for fpth in searchpathlist:
if TestSPG(fpth):
binpath = fpth # got one that works, look no farther!
break
else:
continue
break
if binpath: # were GSAS-II binaries found?
binaryPath = binpath
BinaryPathLoaded = True
else:
print('*** ERROR: Unable to find GSAS-II binaries. Much of GSAS-II cannot function')
try:
from . import pathHacking
except ImportError:
print('Binary load failed and module pathHacking not present')
BinaryPathFailed = True
return None

# add the data import and export directory to the search path
if binpath not in sys.path: sys.path.insert(0,binpath)
if printInfo: print(f'GSAS-II binary directory: {binpath}')
newpath = os.path.join(path2GSAS2,'imports')
if newpath not in sys.path: sys.path.append(newpath)
newpath = os.path.join(path2GSAS2,'exports')
if newpath not in sys.path: sys.path.append(newpath)

return

LoadConfig(showConfigMsg)
BinaryPathFailed = pathHacking._path_discovery(showConfigMsg)

def LoadConfig(printInfo=True):
# setup read of config.py, if present
Expand Down
7 changes: 6 additions & 1 deletion GSASII/GSASIIspc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
import sys
import copy
import os.path as ospath
import GSASII.pyspg as pyspg

from . import GSASIIpath
if GSASIIpath.binaryPath:
import pyspg
else:
import GSASII.pyspg as pyspg

npsind = lambda x: np.sin(x*np.pi/180.)
npcosd = lambda x: np.cos(x*np.pi/180.)
nxs = np.newaxis
Expand Down
89 changes: 89 additions & 0 deletions GSASII/pathHacking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# modules for use where GSAS-II binaries are not co-located with
# the main GSAS-II files and the path is modified (I can hear Tom
# saying "Shame, Shame!").

import glob
import os
import sys
import numpy as np
from . import GSASIIpath

def _path_discovery(printInfo=False):
def appendIfExists(searchpathlist,loc,subdir):
newpath = os.path.join(loc,subdir)
if os.path.exists(newpath):
if newpath in searchpathlist: return False
searchpathlist.append(newpath)
inpver = GSASIIpath.intver(np.__version__)

if GSASIIpath.path2GSAS2 not in sys.path:
sys.path.insert(0,GSASIIpath.path2GSAS2) # make sure current path is used
binpath = None
binprfx = GSASIIpath.GetBinaryPrefix()
# places to look for the GSAS-II binary directory
binseapath = [os.path.abspath(sys.path[0])] # where Python is installed
binseapath += [os.path.abspath(os.path.dirname(__file__))] # directory where this file is found
binseapath += [os.path.dirname(binseapath[-1])] # parent of above directory
binseapath += [os.path.expanduser('~/.GSASII')] # directory in user's home
searched = []
for loc in binseapath:
if loc in searched: continue
if not os.path.exists(loc): continue
searched.append(loc)
# Look at bin directory (created by a local compile) before looking for standard dist files
searchpathlist = []
appendIfExists(searchpathlist,loc,'bin')
appendIfExists(searchpathlist,loc,'bindist')
appendIfExists(searchpathlist,loc,'GSASII-bin')
# also look for directories named by platform etc in loc/AllBinaries or loc
versions = {}
namedpath = glob.glob(os.path.join(loc,'AllBinaries',binprfx+'*'))
namedpath += glob.glob(os.path.join(loc,'GSASII-bin',binprfx+'*'))
for d in namedpath:
d = os.path.realpath(d)
v = GSASIIpath.intver(d.rstrip('/').split('_')[-1].lstrip('n'))
versions[v] = d
vmin = None
vmax = None
# try to order the search in a way that makes sense
for v in sorted(versions.keys()):
if v <= inpver:
vmin = v
elif v > inpver:
vmax = v
break
if vmin in versions and versions[vmin] not in searchpathlist:
searchpathlist.append(versions[vmin])
if vmax in versions and versions[vmax] not in searchpathlist:
searchpathlist.append(versions[vmax])
for fpth in searchpathlist:
if not glob.glob(os.path.join(fpth,'pyspg.*')): continue
if GSASIIpath.TestSPG(fpth):
binpath = fpth # got one that works, look no farther!
break
else:
continue
break
if binpath: # were GSAS-II binaries found?
GSASIIpath.binaryPath = binpath
GSASIIpath.BinaryPathLoaded = True
else:
print('*** ERROR: Unable to find GSAS-II binaries. Much of GSAS-II cannot function')
if GSASIIpath.GetConfigValue('debug'):
print('Searched directories:')
for i in searched: print(f'\t{i}')
print(f'''for subdirectories named .../bin, .../bindist, .../GSASII-bin,
.../AllBinaries/{binprfx}* and .../GSASII-bin/{binprfx}*''')
return True

# add the data import and export directory to the search path
if binpath not in sys.path: sys.path.insert(0,binpath)
if printInfo: print(f'GSAS-II binary directory: {binpath}')

# *** Thanks to work by Tom, imports and exports are now found directly
# *** and the code below is no longer needed.
# ***
#newpath = os.path.join(path2GSAS2,'imports')
#if newpath not in sys.path: sys.path.append(newpath)
#newpath = os.path.join(path2GSAS2,'exports')
#if newpath not in sys.path: sys.path.append(newpath)

0 comments on commit 76f3ef0

Please sign in to comment.