Skip to content
GitLab
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Primary navigation
Search or go to…
Project
buildgrid
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Privacy statement
Keyboard shortcuts
?
What's new
3
Snippets
Groups
Projects
Show more breadcrumbs
BuildGrid
buildgrid
Commits
2c0d9b59
Commit
2c0d9b59
authored
6 years ago
by
Martin Blanchard
Browse files
Options
Downloads
Patches
Plain Diff
utils.py: New OutputDirectory maker helper
#61
parent
4a3f2c7c
No related branches found
No related tags found
Loading
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
buildgrid/utils.py
+143
-0
143 additions, 0 deletions
buildgrid/utils.py
with
143 additions
and
0 deletions
buildgrid/utils.py
+
143
−
0
View file @
2c0d9b59
...
...
@@ -145,6 +145,117 @@ def file_maker(file_path, file_digest):
is_executable
=
os
.
access
(
file_path
,
os
.
X_OK
))
def
directory_maker
(
directory_path
,
child_directories
=
None
,
cas
=
None
,
upload_directories
=
True
):
"""
Creates a :obj:`Directory` from a local directory and possibly upload it.
Args:
directory_path (str): absolute or relative path to a local directory.
child_directories (list): output list of of children :obj:`Directory`
objects.
cas (:obj:`Uploader`): a CAS client uploader.
upload_directories (bool): wheter or not to upload the :obj:`Directory`
objects along with the files.
Returns:
:obj:`Directory`, :obj:`Digest`: Tuple of a new gRPC :obj:`Directory`
for the local directory pointed by `directory_path` and the digest
for that object.
"""
if
not
os
.
path
.
isabs
(
directory_path
):
directory_path
=
os
.
path
.
abspath
(
directory_path
)
files
,
directories
,
symlinks
=
list
(),
list
(),
list
()
for
directory_entry
in
os
.
scandir
(
directory_path
):
# Create a FileNode and corresponding BatchUpdateBlobsRequest:
if
directory_entry
.
is_file
(
follow_symlinks
=
False
):
if
cas
is
not
None
:
node_digest
=
cas
.
upload_file
(
directory_entry
.
path
)
else
:
node_digest
=
create_digest
(
read_file
(
directory_entry
.
path
))
node
=
remote_execution_pb2
.
FileNode
()
node
.
name
=
directory_entry
.
name
node
.
digest
.
CopyFrom
(
node_digest
)
node
.
is_executable
=
os
.
access
(
directory_entry
.
path
,
os
.
X_OK
)
files
.
append
(
node
)
# Create a DirectoryNode and corresponding BatchUpdateBlobsRequest:
elif
directory_entry
.
is_dir
(
follow_symlinks
=
False
):
node_directory
,
node_digest
=
directory_maker
(
directory_entry
.
path
,
child_directories
=
child_directories
,
upload_directories
=
upload_directories
,
cas
=
cas
)
node
=
remote_execution_pb2
.
DirectoryNode
()
node
.
name
=
directory_entry
.
name
node
.
digest
.
CopyFrom
(
node_digest
)
directories
.
append
(
node
)
# Create a SymlinkNode if necessary;
elif
os
.
path
.
islink
(
directory_entry
.
path
):
node_target
=
os
.
readlink
(
directory_entry
.
path
)
node
=
remote_execution_pb2
.
SymlinkNode
()
node
.
name
=
directory_entry
.
name
node
.
target
=
node_target
symlinks
.
append
(
node
)
files
.
sort
(
key
=
attrgetter
(
'
name
'
))
directories
.
sort
(
key
=
attrgetter
(
'
name
'
))
symlinks
.
sort
(
key
=
attrgetter
(
'
name
'
))
directory
=
remote_execution_pb2
.
Directory
()
directory
.
files
.
extend
(
files
)
directory
.
directories
.
extend
(
directories
)
directory
.
symlinks
.
extend
(
symlinks
)
if
child_directories
is
not
None
:
child_directories
.
append
(
directory
)
if
cas
is
not
None
and
upload_directories
:
directory_digest
=
cas
.
upload_directory
(
directory
)
else
:
directory_digest
=
create_digest
(
directory
.
SerializeToString
())
return
directory
,
directory_digest
def
tree_maker
(
directory_path
,
cas
=
None
):
"""
Creates a :obj:`Tree` from a local directory and possibly upload it.
If `cas` is specified, the local directory content will be uploded/stored
in remote CAS (the :obj:`Tree` message won
'
t).
Args:
directory_path (str): absolute or relative path to a local directory.
cas (:obj:`Uploader`): a CAS client uploader.
Returns:
:obj:`Tree`, :obj:`Digest`: Tuple of a new gRPC :obj:`Tree` for the
local directory pointed by `directory_path` and the digest for that
object.
"""
if
not
os
.
path
.
isabs
(
directory_path
):
directory_path
=
os
.
path
.
abspath
(
directory_path
)
child_directories
=
list
()
directory
,
_
=
directory_maker
(
directory_path
,
children
=
child_directories
,
cas
=
cas
)
tree
=
remote_execution_pb2
.
Tree
()
tree
.
children
.
extend
(
child_directories
)
tree
.
root
.
CopyFrom
(
directory
)
if
cas
is
not
None
:
tree_digest
=
cas
.
send_message
(
tree
)
else
:
tree_digest
=
create_digest
(
tree
.
SerializeToString
())
return
tree
,
tree_digest
def
read_file
(
file_path
):
"""
Loads raw file content in memory.
...
...
@@ -196,3 +307,35 @@ def output_file_maker(file_path, input_path, cas=None):
output_file
.
is_executable
=
os
.
access
(
file_path
,
os
.
X_OK
)
return
output_file
def
output_directory_maker
(
directory_path
,
working_path
,
cas
=
None
):
"""
Creates an :obj:`OutputDirectory` from a local directory.
If `cas` is specified, the local directory content will be uploded/stored
in remote CAS (the :obj:`OutputDirectory` message won
'
t).
Note:
`directory_path` **must** point inside or be relative to `input_path`.
Args:
directory_path (str): absolute or relative path to a local directory.
working_path (str): absolute or relative path to the working directory.
cas (:obj:`Uploader`): a CAS client uploader.
Returns:
:obj:`OutputDirectory`: a new gRPC :obj:`OutputDirectory` for the
directory pointed by `directory_path`.
"""
if
not
os
.
path
.
isabs
(
directory_path
):
directory_path
=
os
.
path
.
abspath
(
directory_path
)
if
not
os
.
path
.
isabs
(
working_path
):
working_path
=
os
.
path
.
abspath
(
working_path
)
_
,
tree_digest
=
tree_maker
(
directory_path
,
cas
=
cas
)
output_directory
=
remote_execution_pb2
.
OutputDirectory
()
output_directory
.
tree_digest
.
CopyFrom
(
tree_digest
)
output_directory
.
path
=
os
.
path
.
relpath
(
directory_path
,
start
=
working_path
)
return
output_directory
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