install_wfm_stitching.py 3.63 KB
Newer Older
Owen Arnold's avatar
Owen Arnold committed
1
2
3
4
5
6
7
8
9
10
11
12
13
# -*- coding: utf-8 -*-
""" Script to correctly install wfm-stitching alongside Mantid.
Can either be used to install a specific wfm-stitching package version, or without modification,
will install the latest version.

You should run this script inside MantidPlot from the script window, to ensure that you are using
the same python interpreter that Mantid uses. The installation must use the same python interpreter
that mantid uses.

You can install from the command line using the lauch_mantid_plot executable with the xq
(execute and quit) option set, you can also pass a version to installation script, if not
provided it will fetch the latest wfm_stitching package.

Owen Arnold's avatar
Owen Arnold committed
14
Example on windows:
Owen Arnold's avatar
Owen Arnold committed
15
16
17
18
19
20
21
22
23
24
25

C:\\MantidInstall\\bin\\launch_mantidplot -x install_wfm_stitching.py

or

C:\\MantidInstall\\bin\\launch_mantidplot -x install_wfm_stitching.py -v v0.1.0
"""
import subprocess
from argparse import ArgumentParser
import os
import sys
Owen Arnold's avatar
Owen Arnold committed
26
import re
Owen Arnold's avatar
Owen Arnold committed
27

Owen Arnold's avatar
Owen Arnold committed
28
29
30
31
32
def _run_subprocess(cmd):
    print('attempting to run {0}'.format(' '.join(cmd)))
    subprocess.check_call(cmd)

def _extract_upload_url(python, required_version=None):
Owen Arnold's avatar
Owen Arnold committed
33
34
35
36
37
    """ From gitlab project releases fetch the download url for the release matching the tag
    """
    try: 
        import gitlab
    except ImportError:
Owen Arnold's avatar
Owen Arnold committed
38
        _run_subprocess([python, '-m', 'pip', 'install', '--upgrade', '--user', 'python-gitlab'])
Owen Arnold's avatar
Owen Arnold committed
39
40
41
42
        import gitlab
        
    gl = gitlab.Gitlab('https://git.esss.dk')
    project = gl.projects.list(search='wfm_stitching')[0]
Owen Arnold's avatar
Owen Arnold committed
43
    if required_version:
Owen Arnold's avatar
Owen Arnold committed
44
        tag = project.tags.get(required_version)
Owen Arnold's avatar
Owen Arnold committed
45
46
47
48
49
50
51
52
53
54
55
    else:
        # Get the latest tag
        tag = project.tags.list(order_by='updated', sort='desc')[0]
    description = tag.attributes['release']['description']
    # Search for mark-down style upload in the form [link-text](upload-url) and extract upload-url
    upload = re.search('\[.*\]\((.*)\)', description).group(1)
    # Join with the project web url to obtain the full address
    upload_url = project.attributes['web_url'] + upload
    return upload_url


Owen Arnold's avatar
Owen Arnold committed
56
57
58
def _do_wfm_stitching_install(python, required_version):
    url = _extract_upload_url(python, required_version)
    _run_subprocess([python, '-m', 'pip', 'install', '--user', '--upgrade', url])
Owen Arnold's avatar
Owen Arnold committed
59

Owen Arnold's avatar
Owen Arnold committed
60
def _do_wfm_stitching_uninstall(python):
Owen Arnold's avatar
Owen Arnold committed
61
    """Convenience function to allow the removal of the wfm-stitching package"""
Owen Arnold's avatar
Owen Arnold committed
62
63
    _run_subprocess([python, '-m', 'pip', 'uninstall', '-y', 'wfm_stitching'])
    _run_subprocess([python, '-m', 'pip', 'uninstall', '-y', 'python-gitlab'])
Owen Arnold's avatar
Owen Arnold committed
64
65
66

if __name__ == "__main__":
    parser = ArgumentParser(description='Install WFM package http://git.esss.dk/wedel/wfm_sitching')
67
68
    parser.add_argument('-v', '--version', default=None,
                        help='Specify the version you wish to use i.e. v0.1.2.')
Owen Arnold's avatar
Owen Arnold committed
69
70
    parser.add_argument('-u', '--uninstall', action='store_true', default=False,  help='uninstall any existing wfm-stitching package')
    args = parser.parse_args()
71
    interpreter = sys.executable
72
73
74
75
76
77
78
79
    opt1 = sys.executable
    if os.path.exists(opt1):
        interpreter = opt1
    else:  
        opt2 = os.path.join(os.path.dirname(sys.executable), 'python')
        interpreter = opt2 
    if not os.path.exists(interpreter):
        raise RuntimeError("Cannot find interpreter {0} or {1}".format(opt1, opt2))
80
81
    if sys.version_info < (3, 5):
        raise RuntimeError("Python version too old. Needs to be >= 3.5, but found {}".format(repr(sys.version_info)))
Owen Arnold's avatar
Owen Arnold committed
82
83
84
    if args.uninstall:
        if args.version:
            print('Warning: version argument is ignored during uninstall')
Owen Arnold's avatar
Owen Arnold committed
85
        _do_wfm_stitching_uninstall(interpreter)
Owen Arnold's avatar
Owen Arnold committed
86
    else:
Owen Arnold's avatar
Owen Arnold committed
87
        _do_wfm_stitching_install(interpreter, required_version=args.version)