Skip to content

Module dcm2bids.version⚓︎

This module take care of the versioning

None

View Source
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# -*- coding: utf-8 -*-

"""This module take care of the versioning"""

# dcm2bids version

__version__ = "2.1.7"

import logging

import shlex

from distutils.version import LooseVersion

from subprocess import check_output, CalledProcessError, TimeoutExpired

from shutil import which

logger = logging.getLogger(__name__)

def is_tool(name):

    """ Check if a program is in PATH

    Args:

        name (string): program name

    Returns:

        boolean

    """

    return which(name) is not None

def check_github_latest(githubRepo, timeout=3):

    """ Check the latest version of a github repository

    Args:

        githubRepo (string): a github repository ("username/repository")

        timeout (int): time in seconds

    Returns:

        A string of the version

    """

    url = "https://github.com/{}/releases/latest".format(githubRepo)

    try:

        output = check_output(shlex.split("curl -L --silent " + url), timeout=timeout)

    except CalledProcessError:

        logger.info(f"Checking latest version of {githubRepo} was not possible")

        logger.debug(f"Error while 'curl --silent {url}'", exc_info=True)

        return

    except TimeoutExpired:

        logger.info(f"Checking latest version of {githubRepo} was not possible")

        logger.debug(f"Command 'curl --silent {url}' timed out after {timeout}s")

        return

    # The output should have this format

    # <html><body>You are being <a href="https://github.com/{gitRepo}/releases/tag/{version}">redirected</a>.</body></html>

    try:

        version = output.decode().split("{}/releases/tag/".format(githubRepo))[1].split('"')[0]

        # Versions are X.X.X

        if len(version) > 5:

            version = version[:5]

        return version

    except:

        logger.debug(

            "Checking latest version of %s was not possible", githubRepo,

            exc_info=True,

        )

        return

def check_latest(name="dcm2bids"):

    """ Check if a new version of a software exists and print some details

    Implemented for dcm2bids, dcm2niix

    Args:

        name (string): name of the software

    Returns:

        None

    """

    data = {

        "dcm2bids": {

            "repo": "unfmontreal/Dcm2Bids",

            "host": "https://github.com",

            "current": __version__,

        },

        "dcm2niix": {

            "repo": "rordenlab/dcm2niix",

            "host": "https://github.com",

            "current": dcm2niix_version,

        },

    }

    if is_tool("curl"):

        host = data.get(name)["host"]

        if host == "https://github.com":

            repo = data.get(name)["repo"]

            latest = check_github_latest(repo)

        else:

            # Not implemented

            return

    else:

        logger.debug("Checking latest version of %s was not possible", name)

        logger.debug("curl: %s", is_tool("curl"))

        return

    current = data.get(name)["current"]

    if callable(current):

        current = current()

    try:

        news = LooseVersion(latest) > LooseVersion(current)

    except:

        news = None

    if news:

        logger.warning("Your using %s version %s", name, current)

        logger.warning("A new version exists : %s", latest)

        logger.warning("Check %s/%s", host, repo)

def dcm2niix_version():

    """

    Returns:

        A string of the version of dcm2niix install on the system

    """

    if not is_tool("dcm2niix"):

        logger.error("dcm2niix is not in your PATH or not installed")

        logger.error("Check https://github.com/rordenlab/dcm2niix")

        return

    try:

        output = check_output(shlex.split("dcm2niix"))

    except:

        logger.error("Running: dcm2niix", exc_info=True)

        return

    try:

        lines = output.decode().split("\n")

    except:

        logger.debug(output, exc_info=True)

        return

    for line in lines:

        try:

            splits = line.split()

            return splits[splits.index("version") + 1]

        except:

            continue

    return

Variables⚓︎

1
logger

Functions⚓︎

check_github_latest⚓︎

1
2
3
4
def check_github_latest(
    githubRepo,
    timeout=3
)

Check the latest version of a github repository

Parameters:

Name Type Description Default
githubRepo string a github repository ("username/repository") None
timeout int time in seconds None

Returns:

Type Description
None A string of the version
View Source
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def check_github_latest(githubRepo, timeout=3):

    """ Check the latest version of a github repository

    Args:

        githubRepo (string): a github repository ("username/repository")

        timeout (int): time in seconds

    Returns:

        A string of the version

    """

    url = "https://github.com/{}/releases/latest".format(githubRepo)

    try:

        output = check_output(shlex.split("curl -L --silent " + url), timeout=timeout)

    except CalledProcessError:

        logger.info(f"Checking latest version of {githubRepo} was not possible")

        logger.debug(f"Error while 'curl --silent {url}'", exc_info=True)

        return

    except TimeoutExpired:

        logger.info(f"Checking latest version of {githubRepo} was not possible")

        logger.debug(f"Command 'curl --silent {url}' timed out after {timeout}s")

        return

    # The output should have this format

    # <html><body>You are being <a href="https://github.com/{gitRepo}/releases/tag/{version}">redirected</a>.</body></html>

    try:

        version = output.decode().split("{}/releases/tag/".format(githubRepo))[1].split('"')[0]

        # Versions are X.X.X

        if len(version) > 5:

            version = version[:5]

        return version

    except:

        logger.debug(

            "Checking latest version of %s was not possible", githubRepo,

            exc_info=True,

        )

        return

check_latest⚓︎

1
2
3
def check_latest(
    name='dcm2bids'
)

Check if a new version of a software exists and print some details

Implemented for dcm2bids, dcm2niix

Parameters:

Name Type Description Default
name string name of the software None

Returns:

Type Description
None None
View Source
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def check_latest(name="dcm2bids"):

    """ Check if a new version of a software exists and print some details

    Implemented for dcm2bids, dcm2niix

    Args:

        name (string): name of the software

    Returns:

        None

    """

    data = {

        "dcm2bids": {

            "repo": "unfmontreal/Dcm2Bids",

            "host": "https://github.com",

            "current": __version__,

        },

        "dcm2niix": {

            "repo": "rordenlab/dcm2niix",

            "host": "https://github.com",

            "current": dcm2niix_version,

        },

    }

    if is_tool("curl"):

        host = data.get(name)["host"]

        if host == "https://github.com":

            repo = data.get(name)["repo"]

            latest = check_github_latest(repo)

        else:

            # Not implemented

            return

    else:

        logger.debug("Checking latest version of %s was not possible", name)

        logger.debug("curl: %s", is_tool("curl"))

        return

    current = data.get(name)["current"]

    if callable(current):

        current = current()

    try:

        news = LooseVersion(latest) > LooseVersion(current)

    except:

        news = None

    if news:

        logger.warning("Your using %s version %s", name, current)

        logger.warning("A new version exists : %s", latest)

        logger.warning("Check %s/%s", host, repo)

dcm2niix_version⚓︎

1
2
3
def dcm2niix_version(

)

Returns:

A string of the version of dcm2niix install on the system

View Source
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def dcm2niix_version():

    """

    Returns:

        A string of the version of dcm2niix install on the system

    """

    if not is_tool("dcm2niix"):

        logger.error("dcm2niix is not in your PATH or not installed")

        logger.error("Check https://github.com/rordenlab/dcm2niix")

        return

    try:

        output = check_output(shlex.split("dcm2niix"))

    except:

        logger.error("Running: dcm2niix", exc_info=True)

        return

    try:

        lines = output.decode().split("\n")

    except:

        logger.debug(output, exc_info=True)

        return

    for line in lines:

        try:

            splits = line.split()

            return splits[splits.index("version") + 1]

        except:

            continue

    return

is_tool⚓︎

1
2
3
def is_tool(
    name
)

Check if a program is in PATH

Parameters:

Name Type Description Default
name string program name None

Returns:

Type Description
None boolean
View Source
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def is_tool(name):

    """ Check if a program is in PATH

    Args:

        name (string): program name

    Returns:

        boolean

    """

    return which(name) is not None

Last update: 2022-06-17
Back to top