Skip to content

Commit

Permalink
chore(magicgen): migrate script to Python
Browse files Browse the repository at this point in the history
The old script was in Go, and naively split the lines by whitespace to
identify the magic names and values. The new Python script uses a proper
regex rule to parse the input and generate the output.

Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
  • Loading branch information
snprajwal committed Jul 5, 2024
1 parent 3b91d77 commit 9f98736
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 234 deletions.
2 changes: 0 additions & 2 deletions magic/magic.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions scripts/magic-gen/Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
GO ?= go
PY ?= python3

MAGIC_DEST ?= ../../magic/magic.go

magic-gen: clean magic.h
$(GO) run magicgen.go magic.h $(MAGIC_DEST)
@$(PY) magicgen.py magic.h $(MAGIC_DEST)

magic.h:
curl -s https://raw.githubusercontent.com/checkpoint-restore/criu/criu-dev/criu/include/magic.h -o magic.h

test:
@echo "Running unit test"
$(GO) test -v
@echo "Running E2E test"
@./magicgen-test.sh
@rm -f input.h output.go expected.go

Expand Down
10 changes: 3 additions & 7 deletions scripts/magic-gen/magicgen-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,14 @@ func LoadMagic() MagicMap {
}
EOF

if [ -n "$GOCOVERDIR" ]; then
export LOCALFLAGS="-cover"
fi

go run $LOCALFLAGS magicgen.go input.h output.go
python3 magicgen.py input.h output.go
cmp output.go expected.go
if [[ $? -eq 0 ]]
then
echo "PASS"
echo "---PASS---"
exit 0
else
echo "FAIL"
echo "---FAIL---"
diff output.go expected.go
exit 1
fi
110 changes: 0 additions & 110 deletions scripts/magic-gen/magicgen.go

This file was deleted.

77 changes: 77 additions & 0 deletions scripts/magic-gen/magicgen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import sys
import re
import argparse

def main():
parser = argparse.ArgumentParser(
description='A script to generate Go bindings for the CRIU magic values provided by magic.h')
parser.add_argument(
'src', help='Input path (magic.h)', type=str)
parser.add_argument(
'dest', help='Output path (magic.go)', type=str)

args = parser.parse_args()

try:
with open(args.src, 'r') as src:
magics = read_magics(src)
except IOError:
sys.exit("Failed to open magic.h")

try:
with open(args.dest, 'w') as dest:
write_magics(dest, magics)
except IOError:
sys.exit("Failed to open magic.py")

def read_magics(src):
magics = {}
pattern = re.compile(r"#define\s+(\S+)\s+0x([0-9A-Fa-f]+)")

for line in src:
match = pattern.match(line)
if match:
name = match.group(1)
value_str = match.group(2)
# If the magic is defined as another magic, skip it
if value_str in magics:
continue
try:
value = int(value_str, 16)
magics[name] = value
except ValueError:
print(f"Failed to parse magic {name}")
continue

return magics

def write_magics(dest, magics):
dest.write(
'''// Code generated by magicgen. DO NOT EDIT.
package magic
type MagicMap struct {
\tByName map[string]uint64
\tByValue map[uint64]string
}
func LoadMagic() MagicMap {
\tmagicMap := MagicMap{
\t\tByName: make(map[string]uint64),
\t\tByValue: make(map[uint64]string),
\t}''')

for name in sorted(magics.keys()):
value = magics[name]
# Ignore RAW_IMAGE_MAGIC and V1 magic
if value == 0 or value == 1:
continue
name = name.replace("_MAGIC", "")
dest.write(f'\n\tmagicMap.ByName["{name}"] = {value}')
dest.write(f'\n\tmagicMap.ByValue[{value}] = "{name}"')

dest.write('\n\treturn magicMap\n}\n')

if __name__ == "__main__":
main()
110 changes: 0 additions & 110 deletions scripts/magic-gen/magicgen_test.go

This file was deleted.

0 comments on commit 9f98736

Please sign in to comment.