Skip to content

Commit

Permalink
[python] Create opentitanlib package that contains tool
Browse files Browse the repository at this point in the history
For now only rom_chip_info is moved, and a copy of version_file.py
is included. An entry point is exported that is then used by our
bazel build system.

Signed-off-by: Amaury Pouly <amaury.pouly@lowrisc.org>
  • Loading branch information
pamaury committed Oct 11, 2024
1 parent d0488f0 commit 734ffd5
Show file tree
Hide file tree
Showing 11 changed files with 429 additions and 313 deletions.
12 changes: 8 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

# If you edit this file, please re-generate the python requirements using
# util/sh/scripts/gen-python-requirements.sh
# See doc/contributing/sw/adding_python_depedencies.md for more details.

[project]
name = "opentitan"
version = "0.0.0"
Expand Down Expand Up @@ -86,12 +90,12 @@ dependencies = [
"chipwhisperer@https://github.com/newaetech/chipwhisperer-minimal/archive/2643131b71e528791446ee1bab7359120288f4ab.zip",

# Test
"hello @ file://${PROJECT_ROOT}/util/hello"
"hello @ file://${PROJECT_ROOT}/util/hello",

# OpenTitan python package
"opentitanlib @ file://${PROJECT_ROOT}/util/python",
]

[tool.setuptools]
# This is actually not a python project, we just use pyproject.toml to manage dependencies.
py-modules = []

[tool.uv.workspace]
members = ["util/hello/hello"]
646 changes: 349 additions & 297 deletions python-requirements.txt

Large diffs are not rendered by default.

14 changes: 4 additions & 10 deletions util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

load("@rules_python//python:defs.bzl", "py_binary")
load("@ot_python_deps//:requirements.bzl", "all_requirements", "requirement")
load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")

package(default_visibility = ["//visibility:public"])

Expand All @@ -20,17 +21,10 @@ py_binary(
],
)

py_binary(
py_console_script_binary(
name = "rom_chip_info",
srcs = ["rom_chip_info.py"],
)

py_test(
name = "rom_chip_info_test",
srcs = [
"rom_chip_info.py",
"rom_chip_info_test.py",
],
pkg = "@ot_python_deps//opentitanlib",
script = "rom_chip_info",
)

py_binary(
Expand Down
1 change: 1 addition & 0 deletions util/hello/hello.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

from termcolor import colored
from opentitanlib.bazel import version_file

def main():
print(colored("hello", 'red'))
Expand Down
1 change: 1 addition & 0 deletions util/hello/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ description = "Add your description here"
requires-python = ">=3.8"
dependencies = [
"termcolor==1.1.0",
"opentitanlib",
]

[project.scripts]
Expand Down
7 changes: 7 additions & 0 deletions util/python/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright lowRISC contributors (OpenTitan project).
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

# This directory is managed as a python package, imported by
# python-requirements.
# This file is here just to prevent bazel from looking into this repository.
7 changes: 7 additions & 0 deletions util/python/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright lowRISC contributors (OpenTitan project).
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

# This directory is managed as a python package, imported by
# python-requirements.
# This file is here just to prevent bazel from looking into this repository.
17 changes: 17 additions & 0 deletions util/python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[project]
name = "opentitanlib"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.8"
dependencies = []

[project.scripts]
rom_chip_info = "opentitanlib.scripts.rom_chip_info:main"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["src/opentitanlib"]
34 changes: 34 additions & 0 deletions util/python/src/opentitanlib/bazel/version_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright lowRISC contributors (OpenTitan project).
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

import sys
from typing import Union


class VersionInformation():
def __init__(self, path: str):
"""
Parse a bazel version file and store a dictionary of the values.
If `path` is None, an empty dictionary will be created.
If an error occurs during parsing, an exception is raised.
"""
self.version_stamp = {}
if path is None:
return
try:
with open(path, 'rt') as f:
for line in f:
k, v = line.strip().split(' ', 1)
self.version_stamp[k] = v
except ValueError:
raise SystemExit(sys.exc_info()[1])

def scm_version(self, default: Union[str, None] = None) -> Union[str, None]:
return self.version_stamp.get('BUILD_GIT_VERSION', default)

def scm_revision(self, default: Union[str, None] = None) -> Union[str, None]:
return self.version_stamp.get('BUILD_SCM_REVISION', default)

def scm_status(self, default: Union[str, None] = None) -> Union[str, None]:
return self.version_stamp.get('BUILD_SCM_STATUS', default)
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python3
# Copyright lowRISC contributors (OpenTitan project).
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
Expand All @@ -8,7 +7,7 @@
import logging as log
from pathlib import Path

import version_file
from opentitanlib.bazel import version_file


def generate_chip_info_c_source(scm_revision: int) -> str:
Expand Down
File renamed without changes.

0 comments on commit 734ffd5

Please sign in to comment.