Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Learn Renku
Teaching on Renku
Advanced teaching automation
Commits
fa96f295
Commit
fa96f295
authored
Jul 20, 2022
by
Cyril Matthey-Doret
Browse files
rewrite clone_forks in python
parent
6939a25c
Pipeline
#387698
passed with stage
in 13 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
teach_utils/clone_forks.py
0 → 100644
View file @
fa96f295
#!/usr/bin/env python
# Reads JSON output from collect-forks (either as a file or in stdin)
# and clone all target forks into provided directory at the target commit.
# usage ./clone_forks_from_json.sh OUT_DIR forks.json
# ./collect_forks.py --token token.asc URL | ./clone_forks_from_json.sh OUT_DIR
import
sys
from
pathlib
import
Path
import
json
import
click
import
git
def
clone_fork
(
url
:
str
,
out_path
:
Path
,
commit
:
str
):
"""
Clone input repo in target directory
and checkout desired commit."""
cloned
=
git
.
Repo
.
clone_from
(
url
,
out_path
)
cloned
.
git
.
checkout
(
commit
)
@
click
.
command
()
@
click
.
argument
(
"out_dir"
,
type
=
click
.
Path
(
file_okay
=
False
,
exists
=
False
))
@
click
.
argument
(
"forks"
,
type
=
click
.
File
(
mode
=
"r"
),
default
=
sys
.
stdin
)
def
main
(
out_dir
,
forks
,
token
):
forks
=
json
.
load
(
forks
)
for
fork
in
forks
:
url
,
group
,
commit
=
[
fork
.
get
(
key
)
for
key
in
[
"url"
,
"group"
,
"commit"
]]
path
=
Path
(
out_dir
)
/
group
clone_fork
(
url
,
path
,
commit
)
teach_utils/clone_forks_from_json.sh
deleted
100644 → 0
View file @
6939a25c
#!/usr/bin/env bash
# Reads JSON output from collect_forks.py (either as a file or in stdin)
# and clone all target forks into provided directory at the target commit.
# usage ./clone_forks_from_json.sh OUT_DIR forks.json
# ./collect_forks.py --token token.asc URL | ./clone_forks_from_json.sh OUT_DIR
# Help message
function
usage
()
{
cat
<<
EOF
Usage:
./
$(
basename
$0
)
[outdir] [in_file.json]
./collect_forks.py --token token.asc URL | ./
$(
basename
$0
)
[outdir]
Reads json output from collect_forks.py and clone all target forks into provided directory
at the target commit. Repositories are cloned into outdir/namespace.
Arguments:
outdir: Directory where all forks will be cloned [default: .]
in_file.json: JSON output of collect_forks.py containing fork metadata [default: stdin]
EOF
exit
0
}
# Parsing CL arguments
OUT_DIR
=
${
1
:-
.
}
JSON
=
${
2
:-
/dev/stdin
}
if
[[
$#
-gt
2
]]
||
[[
$1
==
'-h'
]]
||
[[
$1
==
'--help'
]]
;
then
usage
fi
mkdir
-p
"
${
OUT_DIR
}
"
jq
".[] |
\"
git clone
\(
.url)
$OUT_DIR
/
\(
.group) && cd
${
OUT_DIR
}
/
\(
.group) && git checkout
\(
.commit)
\"
"
\
<
"
${
JSON
}
"
\
| xargs
-L
1
-I
{}
sh
-c
"{}"
teach_utils/collect_forks.py
View file @
fa96f295
...
...
@@ -49,7 +49,7 @@ def collect_forks(project_url: str, header=Dict[str, str]) -> List[Dict]:
def
filter_group_forks
(
forks
:
List
[
Dict
])
->
List
[
Dict
]:
"""Given a list of forks' metadata, only keep those that belong to a group
Examples
--------
>>> d1 = {'id': 1, 'namespace': {'kind': 'user'}}
...
...
@@ -93,9 +93,9 @@ def get_last_commit_hash(
def
format_fork_metadata
(
fork
:
Dict
,
header
:
Dict
[
str
,
str
],
deadline
:
Optional
[
str
]
=
None
)
->
Dict
:
"""Format and add fields to a fork's metadata. The
"""Format and add fields to a fork's metadata. The
resulting metadata will have the following fields:
id,
http_url_to_repo
, autostart_url, commit, members, visibility, group"""
id,
url
, autostart_url, commit, members, visibility, group"""
meta
=
{
"id"
:
fork
[
"id"
],
"url"
:
fork
[
"http_url_to_repo"
],
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment