Skip to content

Add script for converting Zephyr repository #394

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ RUN \
libapache2-mod-wsgi-py3 \
libjansson4 \
libyaml-0-2 \
wget
wget \
yq \
rsync

COPY ./requirements.txt /usr/local/elixir/

Expand Down
15 changes: 15 additions & 0 deletions projects/zephyr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
# Enable DT bindings compatible strings support
dts_comp_support=1

version_dir()
{
grep "^elixir-" |
sed -e 's/elixir-//';
}

version_rev()
{
sed -e 's/^/elixir-/';
}

list_tags()
{
echo "$tags" |
Expand All @@ -21,3 +32,7 @@ get_latest_tags()
{
git tag | grep -v '^zephyr-v' | version_dir | grep -v '\-rc' | sort -Vr
}

fetch_hook() {
$script_dir/utils/zephyr-converter.sh $opt1
}
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Jinja2~=3.1.5
Pygments~=2.18.0
Falcon~=4.0.2
pytest==7.2.1
west~=1.3.0

# NOTE binary wheels of berkeleydb are not distributed - on Debian this may
# require installing build-essentials, python3-dev and libdb-dev
Expand Down
11 changes: 11 additions & 0 deletions script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ script_dir=`pwd`
cd "$cur_dir"
dts_comp_support=0 # DT bindings compatible strings support (disable by default)

# Converts git repository tag to name visible in Elixir
version_dir()
{
cat;
}

# Converts tag name visible in Elixir to git repository tag
version_rev()
{
cat;
Expand Down Expand Up @@ -214,6 +216,11 @@ dts_comp()
echo $dts_comp_support
}

# Ran from utils/index to allow projects modify the repository after fetch
fetch_hook() {
return;
}

project=$(basename `dirname $LXR_REPO_DIR`)

plugin=$script_dir/projects/$project.sh
Expand Down Expand Up @@ -291,6 +298,10 @@ case $cmd in
dts_comp
;;

fetch-hook)
fetch_hook
;;

help)
echo "Usage: $0 subcommand [args]..."
exit 1
Expand Down
3 changes: 3 additions & 0 deletions utils/index
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ project_fetch() {

$git fetch --all --tags -j4

LXR_REPO_DIR=$1/repo LXR_DATA_DIR=$1/data \
./script.sh fetch-hook "$(realpath $1)" || exit 1

# A gc.log file implies a garbage collect failed in the past.
# Also, create a hidden flag which could be useful to trigger GCs manually.
if test -e $1/repo/gc.log -o "$ELIXIR_GC"; then
Expand Down
118 changes: 118 additions & 0 deletions utils/zephyr-converter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/bin/bash

set -e

clean_git_worktree() {
find $1 -mindepth 1 -maxdepth 1 -type d -not -path "$1/.git*" -exec rm -r {} \; || true
find $1 -mindepth 1 -maxdepth 1 -type f -not -path "$1/.git*" -exec rm -r {} \; || true
}

copy_git_worktree() {
rsync -q -av $1 --exclude .git $2
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reasoning behind two Git directories? Could you use a single one instead and generate tags on it which have a prefix/suffix?

So maybe upstream pushes v1.0 and we add a new local tag called elixir-v1.0 (or whatever). Then the upper layers only pick those tags up?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seemed trickier to do it with a single repository, and I don't think modifying the original source in any way is a good idea.

But then I realized, that the repo directory has to be the conversion target, not source. And utils/index already fetches new tags into hardcoded repo directory. So if I want to have conversion target and source separate, I would have to modify utils/index (and possibly more) to fetch tags into a different directory. I don't really see a good way of changing that.
And then there is west wanting to have zephyr repository in the base directory/"top dir"...

The next (current) iteration uses worktrees, and adds tags prefixed with elixir- to the main repo.


resolve_path() {
project_path_resolved=`readlink -m $1`
echo ./`realpath -m --relative-to=$(pwd) $project_path_resolved`
}

print_readme() {
echo "This directory contains projects specified in west.yaml."
echo "It was generated automatically and is not a part of the upstream $PROJECT_NAME repository."
echo "You can report bugs (ex. missing west.yaml projects) at https://github.com/bootlin/elixir"
}

for dep in "west" "rsync" "yq"; do
if [[ ! "$(command -v $dep)" ]]; then
echo "$dep is not installed"
exit 1
fi
done

if [[ "$#" -ne 1 ]]; then
echo "usage: $0 west-project-dir"
exit 1
fi

if ! command -v west 2>&1 >/dev/null; then
echo "west command is not available"
exit 1
fi

cd $1

PROJECT_NAME=zephyr
TOP_DIR=./west-topdir
REPO_DIR=./repo
TMP_DIR=./tmp
MAIN_BRANCH=main
export ZEPHYR_BASE=$TOP_DIR

if [[ ! -d $REPO_DIR ]]; then
echo "$REPO_DIR does not exist. Please clone project repository to $REPO first."
exit 1
fi

git -C $REPO_DIR config user.email [email protected]
git -C $REPO_DIR config user.name "west repository converter for $PROJECT_NAME"

if [[ ! -f $TOP_DIR/$PROJECT_NAME/.git ]]; then
git -C $REPO_DIR worktree add -f ../$TOP_DIR/$PROJECT_NAME $MAIN_BRANCH
fi

if [[ ! -f $TMP_DIR/.git ]]; then
git -C $REPO_DIR worktree add -f ../$TMP_DIR $MAIN_BRANCH
git -C $TMP_DIR checkout --orphan elixir
git -C $TMP_DIR commit --allow-empty -m "initial commit"
fi

if [[ ! -d $TOP_DIR/.west ]]; then
west init $TOP_DIR/zephyr -l
fi

project_tags=`git -C $REPO_DIR tag | grep -v "^elixir" | grep -v "^$PROJECT_NAME"`
local_tags=`git -C $REPO_DIR tag | { grep "^elixir" || true; } | sed 's/^elixir-//'`
new_tags=`echo $project_tags $local_tags | tr ' ' '\n' | sort -n | uniq -u`

for tag in $new_tags; do
echo "found missing tag $tag"
git -C $TOP_DIR/$PROJECT_NAME checkout -f $tag
clean_git_worktree $TMP_DIR

west_manifest=$TOP_DIR/$PROJECT_NAME/west.yml
if [[ -f $west_manifest ]]; then
# Find disabled groups
extra_group_names=`cat west-topdir/$PROJECT_NAME/west.yml | yq -r '(.manifest."group-filter" // [])[]'`
# Take only disabled groups (start with '-'), enable them (replace - with +),
# concatenate lines to group-a,group-b,...
extra_groups=`echo $extra_group_names | tr ' ' '\n' | grep '^-' | sed 's/^-/+/' | paste -s -d,`
west update $([[ ! -z $extra_groups ]] && echo --group-filter "$extra_groups")
# Get module paths to copy
module_paths=`cat $west_manifest | yq -r '.manifest.projects | map(.path)[] | select(. != null)'`

mkdir -p $TMP_DIR/west_projects
for top_path in $module_paths; do
# Check if project_path does not traverse outside west_projects
project_path=`resolve_path $TMP_DIR/west_projects/$top_path`
if [[ $project_path =~ ^$TMP_DIR/west_projects/.* ]]; then
echo "copying $top_path project directory"
mkdir -p $project_path
copy_git_worktree $TOP_DIR/$top_path/ $project_path
else
echo "found suspicious path $project_path, not copying"
fi
done

print_readme > $TMP_DIR/west_projects/README.txt
fi

echo "copying $PROJECT_NAME directory"
git -C $TMP_DIR checkout -q elixir
copy_git_worktree $TOP_DIR/$PROJECT_NAME/ $TMP_DIR

echo "commiting $tag"
git -C $TMP_DIR add '*'
git -C $TMP_DIR commit -q -a -m $tag
git -C $TMP_DIR tag elixir-$tag
done