-
Notifications
You must be signed in to change notification settings - Fork 615
feat(python): support toml file in versioning step #5492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
dbd0cdc
refactor
CCFenner 7550012
use defer
CCFenner 091e4ae
fix typo in parameter
CCFenner 99e03f7
install wheel dependency
CCFenner b759b6e
fix test cases
CCFenner ed35c2d
add sonar config
CCFenner 416feb3
Revert "add sonar config"
CCFenner 1baca48
add build function
CCFenner 602b154
move install function
CCFenner a116c85
add feature flag pkg
CCFenner 03f19fd
add support for toml file
CCFenner ebbafeb
update docs
CCFenner 9ca556c
rename
CCFenner 211a3a8
add toml flag
CCFenner d58b7be
Revert "add toml flag"
CCFenner 43b6432
remove feature flag
CCFenner ac6c0e7
Revert "add feature flag pkg"
CCFenner d7b65dc
adjust log messages
CCFenner cebce21
install build module
CCFenner f4edbcd
add functions to install
CCFenner 5faf70a
update
CCFenner 4f5dbc5
use other pip
CCFenner bcd6d14
debug
CCFenner 6b0a242
debug
CCFenner 8655d57
debug
CCFenner 07f7eaf
fix test case
CCFenner 8fa38ce
remove commented code
CCFenner 305cd1e
switch param order
CCFenner 216731c
rename param
CCFenner 6161ef9
add test cases
CCFenner 6b0ba9a
move venv handling to pip pkg
CCFenner 8022ad7
move venv handling to pip pkg
CCFenner b8b8f0f
move venv handling to build file
CCFenner ef75950
move publish to python pkg
CCFenner 50727b8
fix test
CCFenner 1561451
fix test
CCFenner 516d92d
fix test
CCFenner ac5d1fe
fix tests
CCFenner 2e78dc1
fix test cases
CCFenner 5518724
Merge branch 'master' into python
CCFenner f66f97c
cleanup
CCFenner 4372f9c
add error message
CCFenner a5e03bc
cleanup
CCFenner c06abb2
fix test case
CCFenner 4a61ae9
handle toml file
CCFenner 355acb9
add toml handling
CCFenner cd56093
cleanup
CCFenner 16939fb
add test cases
CCFenner bf5bf03
update tests
CCFenner e0ffd5c
use toml pkg
CCFenner e7e9100
Added toml unit tests
petkodimitrov24 4ba437b
Merge branch 'master' into python-version
CCFenner 672e8e9
Merge branch 'master' into python-version
petkodimitrov24 5db3ed1
Merge branch 'master' into python-version
petkodimitrov24 73961cd
Merge branch 'master' into python-version
petkodimitrov24 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package versioning | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/BurntSushi/toml" | ||
| ) | ||
|
|
||
| const ( | ||
| TomlBuildDescriptor = "pyproject.toml" | ||
| ) | ||
|
|
||
| // Pip utility to interact with Python specific versioning | ||
| type Toml struct { | ||
| Pip | ||
| coordinates tomlCoordinates | ||
| } | ||
|
|
||
| type tomlCoordinates struct { | ||
| Project struct { | ||
| Name string `toml:"name"` | ||
| Version string `toml:"version"` | ||
| } `toml:"project"` | ||
| } | ||
|
|
||
| func (p *Toml) init() error { | ||
| var coordinates tomlCoordinates | ||
|
|
||
| if !strings.Contains(p.Pip.path, TomlBuildDescriptor) { | ||
| return fmt.Errorf("file '%v' is not a %s", p.Pip.path, TomlBuildDescriptor) | ||
| } | ||
|
|
||
| if err := p.Pip.init(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if _, err := toml.Decode(p.Pip.buildDescriptorContent, &coordinates); err != nil { | ||
| return err | ||
| } | ||
| p.coordinates = coordinates | ||
| return nil | ||
| } | ||
|
|
||
| // GetName returns the name from the build descriptor | ||
| func (p *Toml) GetName() (string, error) { | ||
| if err := p.init(); err != nil { | ||
| return "", fmt.Errorf("failed to read file '%v': %w", p.Pip.path, err) | ||
| } | ||
| if len(p.coordinates.Project.Name) == 0 { | ||
| return "", fmt.Errorf("no name information found in file '%v'", p.Pip.path) | ||
| } | ||
| return p.coordinates.Project.Name, nil | ||
| } | ||
|
|
||
| // // GetVersion returns the current version from the build descriptor | ||
| func (p *Toml) GetVersion() (string, error) { | ||
| if err := p.init(); err != nil { | ||
| return "", fmt.Errorf("failed to read file '%v': %w", p.Pip.path, err) | ||
| } | ||
| if len(p.coordinates.Project.Version) == 0 { | ||
| return "", fmt.Errorf("no version information found in file '%v'", p.Pip.path) | ||
| } | ||
| return p.coordinates.Project.Version, nil | ||
| } | ||
|
|
||
| // SetVersion updates the version in the build descriptor | ||
| func (p *Toml) SetVersion(new string) error { | ||
| if current, err := p.GetVersion(); err != nil { | ||
CCFenner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return err | ||
| } else { | ||
| // replace with single quotes | ||
| p.Pip.buildDescriptorContent = strings.ReplaceAll( | ||
| p.Pip.buildDescriptorContent, | ||
| fmt.Sprintf("version = '%v'", current), | ||
| fmt.Sprintf("version = '%v'", new)) | ||
| // replace with double quotes as well | ||
| p.Pip.buildDescriptorContent = strings.ReplaceAll( | ||
| p.Pip.buildDescriptorContent, | ||
| fmt.Sprintf("version = \"%v\"", current), | ||
| fmt.Sprintf("version = \"%v\"", new)) | ||
| err = p.Pip.writeFile(p.Pip.path, []byte(p.Pip.buildDescriptorContent), 0600) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to write file '%v': %w", p.Pip.path, err) | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // GetCoordinates returns the build descriptor coordinates | ||
| func (p *Toml) GetCoordinates() (Coordinates, error) { | ||
| result := Coordinates{} | ||
| // get name | ||
| if name, err := p.GetName(); err != nil { | ||
| return result, fmt.Errorf("failed to retrieve coordinates: %w", err) | ||
| } else { | ||
| result.ArtifactID = name | ||
| } | ||
| // get version | ||
| if version, err := p.GetVersion(); err != nil { | ||
| return result, fmt.Errorf("failed to retrieve coordinates: %w", err) | ||
| } else { | ||
| result.Version = version | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To parse TOML it's better to use BurntSushi/toml, less fragile and provides robust parsing: