-
Notifications
You must be signed in to change notification settings - Fork 114
/
VersionSplitter.py
63 lines (53 loc) · 2.14 KB
/
VersionSplitter.py
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
# -*- coding: utf-8 -*-
#
# Copyright 2015-2020 Elliot Jordan
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from autopkglib import Processor # noqa: F401
__all__ = ["VersionSplitter"]
class VersionSplitter(Processor): # pylint: disable=invalid-name
"""This processor splits version numbers and returns the specified index.
By default, it splits using a space, and returns the first item. Default
behavior example: "3.0.8 :074a131:" --> "3.0.8".
More examples (with "split_on" in parentheses and "index" in brackets):
- "8.3.1.1 (154179)" --> (" ")[0] -->"8.3.1.1"
- "1.3.1-stable" --> ("-")[0] -->"1.3.1"
- "macosx_64bit_3.0" --> ("_")[2] -->"3.0"
"""
input_variables = {
"version": {
"required": True,
"description": "The version string that needs splitting.",
},
"split_on": {
"required": False,
"description": "The character(s) to use for splitting the "
"version. (Defaults to a space.)",
},
"index": {
"required": False,
"description": "The index of the version string to be "
"returned. (Defaults to 0.)",
},
}
output_variables = {"version": {"description": "The cleaned up version string."}}
description = __doc__
def main(self):
"""Main process."""
split_on = self.env.get("split_on", " ")
index = self.env.get("index", 0)
self.env["version"] = self.env["version"].split(split_on)[index]
self.output("Split version: {}".format(self.env["version"]))
if __name__ == "__main__":
PROCESSOR = VersionSplitter()
PROCESSOR.execute_shell()