Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
gitlab-ci
Manage
Activity
Members
Labels
Plan
Jira
Code
Merge requests
4
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SECO Northern Europe
Yocto
infrastructure
gitlab-ci
Commits
3e491ba0
Commit
3e491ba0
authored
3 years ago
by
Jonas Höppner
Browse files
Options
Downloads
Patches
Plain Diff
CI: Add new file to query the revision for a project from the manifest.
parent
633e40df
No related branches found
Branches containing commit
Tags
v4.1-rc1
Tags containing commit
1 merge request
!107
CI: deploy_gitlab_ci: Move one-time code to own function as reference
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
get_current_revision_from_manifest.py
+157
-0
157 additions, 0 deletions
get_current_revision_from_manifest.py
with
157 additions
and
0 deletions
get_current_revision_from_manifest.py
0 → 100755
+
157
−
0
View file @
3e491ba0
#!/usr/bin/env python3
import
common
import
argparse
import
logging
import
re
from
gitlab
import
Gitlab
from
gitlab.v4.objects
import
Project
from
lxml
import
etree
def
get_current_revision_from_manifest
(
manifest_project
:
Project
,
manifest_branch
,
project
:
Project
,
recipe_name
=
None
,
srcrev_file
=
common
.
srcrev_file
,
):
"""
Reads the xml manifest an the SRC_REVC file in the given manifest project
at given branch and returns the current revision of the given project.
Returns a dict containing the file referencing the project as key and the revision
"""
if
manifest_branch
is
None
:
manifest_branch
=
manifest_project
.
default_branch
logging
.
debug
(
"
Using default branch %s
"
,
manifest_project
)
# Get all manifest xml
repository_tree
=
manifest_project
.
repository_tree
(
ref
=
manifest_branch
,
all
=
True
,
retry_transient_errors
=
True
)
logging
.
debug
(
repository_tree
)
manifest_files
=
[
f
for
f
in
repository_tree
if
(
f
[
"
name
"
].
endswith
(
"
.xml
"
)
or
f
[
"
name
"
]
==
srcrev_file
)
and
f
[
"
type
"
]
==
"
blob
"
]
results
=
{}
for
f
in
manifest_files
:
file_name
=
f
[
"
name
"
]
logging
.
debug
(
file_name
)
content
=
common
.
get_repository_file_raw
(
manifest_project
,
file_name
,
ref
=
manifest_branch
)
if
content
is
None
:
logging
.
error
(
"
Failed to read %s.
"
,
file_name
)
continue
logging
.
debug
(
content
)
revision
=
None
if
file_name
.
endswith
(
"
.xml
"
):
manifest
=
etree
.
fromstring
(
content
.
encode
())
for
node
in
manifest
.
findall
(
"
project
"
):
project_name
=
node
.
get
(
"
name
"
)
if
project_name
is
not
None
and
project_name
.
endswith
(
project
.
path
):
logging
.
debug
(
node
)
revision
=
node
.
get
(
"
revision
"
)
elif
recipe_name
is
not
None
:
# read from SRCREV
project_line
=
None
# Match "...RECIPE_NAME ="
pattern
=
re
.
compile
(
"
{}[ ,
\t
]{{0,}}?=
"
.
format
(
recipe_name
))
for
line
in
content
.
splitlines
():
if
pattern
.
search
(
line
):
project_line
=
line
if
project_line
is
None
:
continue
# Get current project revision from SRCREV file
# Assuming notation: <project> = "<hash>"
revision
=
project_line
.
split
(
'"'
)[
1
]
if
revision
is
not
None
:
results
[
file_name
]
=
revision
return
results
def
main
():
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"
--gitlab-url
"
,
help
=
"""
URL to the GitLab instance
"""
,
dest
=
"
gitlab_url
"
,
required
=
True
,
)
parser
.
add_argument
(
"
--token
"
,
help
=
"""
GitLab REST API private access token
"""
,
dest
=
"
token
"
,
required
=
True
,
)
parser
.
add_argument
(
"
--manifest-project
"
,
help
=
"""
name of the manifest project
"""
,
dest
=
"
manifest_project
"
,
required
=
True
,
)
parser
.
add_argument
(
"
--manifest-branch
"
,
help
=
"""
the branch in the manifest repo to read, use project
'
s default if not set
"""
,
dest
=
"
manifest_branch
"
,
default
=
None
,
)
parser
.
add_argument
(
"
--project
"
,
help
=
"""
name of the project get the current revision for
"""
,
dest
=
"
project
"
,
required
=
True
,
)
parser
.
add_argument
(
"
--srcrev-file
"
,
help
=
"""
source revision file name (default:
'
SRCREV.conf
'
)
"""
,
dest
=
"
srcrev_file
"
,
default
=
common
.
srcrev_file
,
required
=
False
,
)
parser
.
add_argument
(
"
--recipe-name
"
,
help
=
"""
recipe name to resolve the project in
'
SRCREV.conf
'"""
,
dest
=
"
recipe_name
"
,
default
=
None
,
required
=
False
,
)
parser
.
add_argument
(
"
-v
"
,
"
--verbose
"
,
action
=
"
store_true
"
,
help
=
"""
Increase verbosity.
"""
,
)
args
,
_
=
parser
.
parse_known_args
()
if
args
.
verbose
:
logging
.
basicConfig
(
level
=
logging
.
DEBUG
)
gitlab
=
Gitlab
(
args
.
gitlab_url
,
private_token
=
args
.
token
)
project
=
common
.
get_project
(
gitlab
,
args
.
project
)
manifest_project
=
common
.
get_project
(
gitlab
,
args
.
manifest_project
)
current_revisions
=
get_current_revision_from_manifest
(
manifest_project
=
manifest_project
,
manifest_branch
=
args
.
manifest_branch
,
project
=
project
,
recipe_name
=
args
.
recipe_name
,
srcrev_file
=
args
.
srcrev_file
,
)
print
(
"
The manifest repo {} includes the project {} at revision {}
"
.
format
(
manifest_project
.
name
,
project
.
name
,
current_revisions
)
)
if
__name__
==
"
__main__
"
:
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment