Changeset 5288 for trunk/GSASIIpath.py


Ignore:
Timestamp:
May 25, 2022 6:12:19 PM (7 months ago)
Author:
toby
Message:

add routines to install gsl and/or diffpy.pdffit2, possibly in a separate environment; add more binaries to the libraries

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIpath.py

    r5285 r5288  
    4848   
    4949    :param str key: a value to be found in the configuration (config.py) file
    50     :param default: a value to be supplied is none is in the config file or
     50    :param default: a value to be supplied if none is in the config file or
    5151      the config file is not found. Defaults to None
    5252    :returns: the value found or the default.
     
    13271327        sys.exit()
    13281328       
    1329 def condaTest():
     1329def condaTest(requireAPI=False):
    13301330    '''Returns True if it appears that Python is being run under Anaconda
    13311331    Python with conda present. Tests for conda environment vars and that
     
    13351335    '''   
    13361336    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',
     1337    if requireAPI:
     1338        # is the conda package available?
     1339        try:
     1340            import conda.cli.python_api
     1341        except:
     1342            print('You do not have the conda package installed in this environment',
    13421343                  '\nConsider using the "conda install conda" command')
    1343         return False
     1344            return False
    13441345
    13451346    # There is no foolproof way to check if someone activates conda
     
    13571358    # python we are running in shares the beginning part of its path with
    13581359    # the one in the base installation:
    1359     return commonPath(os.environ['CONDA_PYTHON_EXE'],sys.executable)
     1360    dir1 = os.path.dirname(os.environ['CONDA_PYTHON_EXE'])
     1361    dir2 = os.path.dirname(sys.executable)
     1362    if sys.platform != "win32": # python in .../bin/..
     1363        dir1 = os.path.dirname(dir1)
     1364        dir2 = os.path.dirname(dir2)   
     1365    return commonPath(dir1,dir2)
    13601366
    13611367def condaInstall(packageList):
     
    13751381      if it did not.
    13761382    '''
    1377     import conda.cli.python_api
     1383    try:
     1384        import conda.cli.python_api
     1385    except:
     1386        print('You do not have the conda package installed in this environment',
     1387                  '\nConsider using the "conda install conda" command')
     1388        return None
    13781389    try:
    13791390        (out, err, rc) = conda.cli.python_api.run_command(
     
    14051416    return out
    14061417
    1407 def 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
     1418def commonPath(dir1,dir2):
     1419    '''Check if two directories share a path. Note that paths
     1420    are considered the same if either directory is a subdirectory
    14101421    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.
     1422    /a/b/c shares a path with /a/b/c/d but /a/b/c/d and /a/b/c/e do not.
    14131423
    14141424    :returns: True if the paths are common
    14151425    '''
    14161426
    1417     for i,j in zip(fullsplit(os.environ['CONDA_PYTHON_EXE']),
    1418                fullsplit(sys.executable)):
     1427    for i,j in zip(fullsplit(dir1),fullsplit(dir2)):
    14191428        if i != j: return False
    14201429    return True
     
    14431452    return None
    14441453   
     1454def condaEnvCreate(envname, packageList, force=False):
     1455    '''Create a Python interpreter in a new conda environment. Use this
     1456    when there is a potential conflict between packages and it would
     1457    be better to keep the packages separate (which is one of the reasons
     1458    conda supports environments). Note that conda should be run from the
     1459    case environment; this attempts to deal with issues if it is not.
     1460
     1461    :param str envname: the name of the environment to be created.
     1462      If the environment exists, it will be overwritten only if force is True.
     1463    :param list packageList: a list of conda install create command
     1464      options, such as::
     1465
     1466            ['python=3.7', 'conda', 'gsl', 'diffpy.pdffit2',
     1467                '-c', 'conda-forge', '-c', 'diffpy']
     1468
     1469    :param bool force: if False (default) an error will be generated
     1470      if an environment exists
     1471
     1472    :returns: (status,msg) where status is True if an error occurs and
     1473      msg is a string with error information if status is True or the
     1474      location of the newly-created Python interpreter.
     1475    '''
     1476    if not all([(i in os.environ) for i in ('CONDA_DEFAULT_ENV',
     1477                            'CONDA_EXE', 'CONDA_PREFIX', 'CONDA_PYTHON_EXE')]):
     1478        return True,'not running under conda?'
     1479    try:
     1480        import conda.cli.python_api
     1481    except:
     1482        return True,'conda package not available (in environment)'
     1483    # workaround for bug that avoids nesting packages if running from an
     1484    # environment (see https://github.com/conda/conda/issues/11493)
     1485    p = os.path.dirname(os.path.dirname(os.environ['CONDA_EXE']))
     1486    if not os.path.exists(os.path.join(p,'envs')):
     1487        msg = ('Error derived installation path not found: '+
     1488                  os.path.join(p,'envs'))
     1489        print(msg)
     1490        return True,msg
     1491    newenv = os.path.join(p,'envs',envname)
     1492    if os.path.exists(newenv) and not force:
     1493        msg = 'path '+newenv+' already exists and force is not set, aborting'
     1494        print(msg)
     1495        return True,msg
     1496    pathList = ['-p',newenv]
     1497    try:
     1498        (out, err, rc) = conda.cli.python_api.run_command(
     1499            conda.cli.python_api.Commands.CREATE,
     1500            packageList + pathList,
     1501    use_exception_handler=True, stdout=sys.stdout, stderr=sys.stderr
     1502            )
     1503        #print('rc=',rc)
     1504        #print('out=',out)
     1505        #print('err=',err)
     1506        if rc != 0: return True,str(out)
     1507        if sys.platform == "win32":
     1508            newpython = os.path.join(newenv,'python.exe')
     1509        else:
     1510            newpython = os.path.join(newenv,'bin','python')
     1511        if os.path.exists(newpython):
     1512            return False,newpython
     1513        return True,'Unexpected, '+newpython+' not found'
     1514    except Exception as msg:
     1515        print("Error occurred, see below\n",msg)
     1516        return True,'Error: '+str(msg)
     1517   
     1518def addCondaPkg():
     1519    '''Install the conda API into the current conda environment using the
     1520    command line, so that the API can be used in the current Python interpreter
     1521
     1522    Attempts to do this without a shell failed on the Mac because it seems that
     1523    the environment was inherited; seems to work w/o shell on Windows.
     1524    '''
     1525    if not all([(i in os.environ) for i in ('CONDA_DEFAULT_ENV','CONDA_EXE',
     1526                        'CONDA_PREFIX', 'CONDA_PYTHON_EXE')]):
     1527        return None
     1528    condaexe = os.environ['CONDA_EXE']
     1529    currenv = os.environ['CONDA_DEFAULT_ENV']
     1530    if sys.platform == "win32":
     1531        cmd = [os.environ['CONDA_EXE'],'install','conda','-n',currenv,'-y']
     1532        p = subprocess.Popen(cmd,
     1533                         #stdout=subprocess.PIPE,
     1534                         stderr=subprocess.PIPE)
     1535    else:
     1536        script = 'source ' + os.path.join(
     1537            os.path.dirname(os.environ['CONDA_PYTHON_EXE']),
     1538            'activate') + ' base; '
     1539        script += 'conda install conda -n '+currenv+' -y'
     1540        p = subprocess.Popen(script,shell=True,env={},
     1541                         #stdout=subprocess.PIPE,
     1542                         stderr=subprocess.PIPE)
     1543    out,err = MakeByte2str(p.communicate())
     1544    #print('Output from adding conda:\n',out)
     1545    if err:
     1546        print('Note error/warning:')
     1547        print(err)
     1548    if currenv == "base":
     1549        print('\nUnexpected action: adding conda to base environment???')
     1550
    14451551if __name__ == '__main__':
    14461552    '''What follows is called to update (or downdate) GSAS-II in a separate process.
Note: See TracChangeset for help on using the changeset viewer.