Changeset 5285 for trunk/GSASIIpath.py


Ignore:
Timestamp:
May 20, 2022 12:35:20 PM (8 months ago)
Author:
toby
Message:

multiple PDFfit2 changes; seems to work well on Windows w/3.7, 3.8 & 3.9

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIpath.py

    r5264 r5285  
    12521252'''.format(script)
    12531253    subprocess.Popen(["osascript","-e",osascript])
    1254    
     1254
     1255#======================================================================
     1256# conda/pip routines
    12551257def findConda():
    12561258    '''Determines if GSAS-II has been installed as g2conda or gsas2full
     
    13241326        if sys.platform != "win32": proc.wait()
    13251327        sys.exit()
     1328       
     1329def condaTest():
     1330    '''Returns True if it appears that Python is being run under Anaconda
     1331    Python with conda present. Tests for conda environment vars and that
     1332    the conda package is installed in the current environment.
     1333
     1334    :returns: True, if running under Conda
     1335    '''   
     1336    if not all([(i in os.environ) for i in ('CONDA_DEFAULT_ENV','CONDA_EXE', 'CONDA_PREFIX', 'CONDA_PYTHON_EXE')]): return False
     1337    # is the conda package available?
     1338    try:
     1339        import conda.cli.python_api
     1340    except:
     1341        print('You do not have the conda package installed in this environment',
     1342                  '\nConsider using the "conda install conda" command')
     1343        return False
     1344
     1345    # There is no foolproof way to check if someone activates conda
     1346    # but then calls a different Python using its path...
     1347    # ...If we are in the base environment then the conda Python
     1348    # should be the same path as the one currently being run:
     1349    if os.environ['CONDA_DEFAULT_ENV'] == 'base':
     1350        try:
     1351            if os.path.samefile(os.environ['CONDA_PYTHON_EXE'],
     1352                                sys.executable): return True
     1353        except:
     1354            return False
     1355
     1356    # ...If not in the base environment, what we can do is check if the
     1357    # python we are running in shares the beginning part of its path with
     1358    # the one in the base installation:
     1359    return commonPath(os.environ['CONDA_PYTHON_EXE'],sys.executable)
     1360
     1361def condaInstall(packageList):
     1362    '''Installs one or more packages using the anaconda conda package
     1363    manager. Can be used to install multiple packages and optionally
     1364    use channels.
     1365
     1366    :param list packageList: a list of strings with name(s) of packages
     1367      and optionally conda options.
     1368      Examples::
     1369
     1370       packageList=['gsl']
     1371       packageList=['-c','conda-forge','wxpython']
     1372       packageList=['numpy','scipy','matplotlib']
     1373
     1374    :returns: None if the the command ran normally, or an error message
     1375      if it did not.
     1376    '''
     1377    import conda.cli.python_api
     1378    try:
     1379        (out, err, rc) = conda.cli.python_api.run_command(
     1380            conda.cli.python_api.Commands.INSTALL,packageList
     1381#    use_exception_handler=True#, stdout=sys.stdout, stderr=sys.stderr)
     1382            )
     1383        #print('rc=',rc)
     1384        print('Ran conda. output follows...')
     1385        print(70*'='+'\n'+out+'\n'+70*'=')
     1386        #print('err=',err)
     1387        if rc != 0: return str(out)
     1388    except Exception as msg:
     1389        print("Error occurred, see below\n",msg) 
     1390        return "error occurred"
     1391    return None
     1392   
     1393def fullsplit(fil,prev=None):
     1394    '''recursive routine to split all levels of directory names
     1395    '''
     1396    if prev is None: # first call: normalize and drop file name
     1397        fil = os.path.normcase(os.path.abspath(os.path.dirname(fil)))
     1398        prev = []
     1399    i,j = os.path.split(fil)
     1400    if j:
     1401        prev.insert(0,j)
     1402        out = fullsplit(i,prev)
     1403    else:
     1404        return [i]+prev
     1405    return out
     1406
     1407def commonPath(file1,file2):
     1408    '''Check if two files share the same path. Note that paths
     1409    are considered the same if either file is in a subdirectory
     1410    of the other, but not if they are in different subdirectories
     1411    /a/b/c/x.x and  /a/b/c/y.y share a path, as does  /a/b/c/d/y.y
     1412    but  /a/b/c/d/x.x and  /a/b/c/e/x.x do not.
     1413
     1414    :returns: True if the paths are common
     1415    '''
     1416
     1417    for i,j in zip(fullsplit(os.environ['CONDA_PYTHON_EXE']),
     1418               fullsplit(sys.executable)):
     1419        if i != j: return False
     1420    return True
     1421
     1422def pipInstall(packageList):
     1423    '''Installs one or more packages using the pip package installer.
     1424    Use of this should be avoided if conda can be used (see :func:`condaTest`
     1425    to test for conda). Can be used to install multiple packages together.
     1426    One can use pip options, but this is probably not needed.
     1427
     1428    :param list packageList: a list of strings with name(s) of packages
     1429      Examples::
     1430
     1431       packageList=['gsl']
     1432       packageList=['wxpython','matplotlib','scipy']
     1433       packageList=[r'\\Mac\Home\Scratch\wheels\pygsl-2.3.3-py3-none-any.whl']
     1434       packageList=['z:/Scratch/wheels/pygsl-2.3.3-py3-none-any.whl']
     1435
     1436    :returns: None if the the command ran normally, or an error message
     1437      if it did not.
     1438    '''
     1439    try:
     1440        subprocess.check_call([sys.executable, '-m', 'pip', 'install']+packageList)
     1441    except Exception as msg:
     1442        return msg
     1443    return None
    13261444   
    13271445if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.