| OLD | NEW |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package python | 5 package python |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "regexp" | 9 "regexp" |
| 10 "strconv" | 10 "strconv" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 | 38 |
| 39 // ParseVersion parses a Python version from a version string (e.g., "1.2.3"). | 39 // ParseVersion parses a Python version from a version string (e.g., "1.2.3"). |
| 40 func ParseVersion(s string) (Version, error) { | 40 func ParseVersion(s string) (Version, error) { |
| 41 var v Version | 41 var v Version |
| 42 if s == "" { | 42 if s == "" { |
| 43 return v, nil | 43 return v, nil |
| 44 } | 44 } |
| 45 | 45 |
| 46 match := canonicalVersionRE.FindStringSubmatch(s) | 46 match := canonicalVersionRE.FindStringSubmatch(s) |
| 47 if match == nil { | 47 if match == nil { |
| 48 » » return v, errors.Reason("non-canonical Python version string: %(
value)q"). | 48 » » return v, errors.Reason("non-canonical Python version string: %q
", s).Err() |
| 49 » » » D("value", s). | |
| 50 » » » Err() | |
| 51 } | 49 } |
| 52 parts := strings.Split(match[2], ".") | 50 parts := strings.Split(match[2], ".") |
| 53 | 51 |
| 54 parseVersion := func(value string) (int, error) { | 52 parseVersion := func(value string) (int, error) { |
| 55 version, err := strconv.Atoi(value) | 53 version, err := strconv.Atoi(value) |
| 56 if err != nil { | 54 if err != nil { |
| 57 » » » return 0, errors.Annotate(err).Reason("invalid number va
lue: %(value)q"). | 55 » » » return 0, errors.Annotate(err, "invalid number value: %q
", value).Err() |
| 58 » » » » D("value", value). | |
| 59 » » » » Err() | |
| 60 } | 56 } |
| 61 return version, nil | 57 return version, nil |
| 62 } | 58 } |
| 63 | 59 |
| 64 // Regexp match guarantees that "parts" will have at least one component
, and | 60 // Regexp match guarantees that "parts" will have at least one component
, and |
| 65 // that all components are well-formed numbers. | 61 // that all components are well-formed numbers. |
| 66 var err error | 62 var err error |
| 67 if len(parts) >= 3 { | 63 if len(parts) >= 3 { |
| 68 if v.Patch, err = parseVersion(parts[2]); err != nil { | 64 if v.Patch, err = parseVersion(parts[2]); err != nil { |
| 69 » » » return v, errors.Annotate(err).Reason("invalid patch val
ue").Err() | 65 » » » return v, errors.Annotate(err, "invalid patch value").Er
r() |
| 70 } | 66 } |
| 71 } | 67 } |
| 72 if len(parts) >= 2 { | 68 if len(parts) >= 2 { |
| 73 if v.Minor, err = parseVersion(parts[1]); err != nil { | 69 if v.Minor, err = parseVersion(parts[1]); err != nil { |
| 74 » » » return v, errors.Annotate(err).Reason("invalid minor val
ue").Err() | 70 » » » return v, errors.Annotate(err, "invalid minor value").Er
r() |
| 75 } | 71 } |
| 76 } | 72 } |
| 77 if v.Major, err = parseVersion(parts[0]); err != nil { | 73 if v.Major, err = parseVersion(parts[0]); err != nil { |
| 78 » » return v, errors.Annotate(err).Reason("invalid major value").Err
() | 74 » » return v, errors.Annotate(err, "invalid major value").Err() |
| 79 } | 75 } |
| 80 if v.IsZero() { | 76 if v.IsZero() { |
| 81 return v, errors.Reason("version is incomplete").Err() | 77 return v, errors.Reason("version is incomplete").Err() |
| 82 } | 78 } |
| 83 return v, nil | 79 return v, nil |
| 84 } | 80 } |
| 85 | 81 |
| 86 func (v Version) String() string { | 82 func (v Version) String() string { |
| 87 if v.IsZero() { | 83 if v.IsZero() { |
| 88 return "" | 84 return "" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 case v.Major > other.Major: | 142 case v.Major > other.Major: |
| 147 return false | 143 return false |
| 148 case v.Minor < other.Minor: | 144 case v.Minor < other.Minor: |
| 149 return true | 145 return true |
| 150 case v.Minor > other.Minor: | 146 case v.Minor > other.Minor: |
| 151 return false | 147 return false |
| 152 default: | 148 default: |
| 153 return (v.Patch < other.Patch) | 149 return (v.Patch < other.Patch) |
| 154 } | 150 } |
| 155 } | 151 } |
| OLD | NEW |