Skip to content

[CI] Add ruff check #538

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 7 commits into
base: dev
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
16 changes: 16 additions & 0 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Lint and format

on:
push:
branches: [ '**' ]
workflow_dispatch:

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/ruff-action@v1
with:
args: check
src: "."
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ riscv-isac/riscv-env

__pycache__
riscof_work

# ignore ruff cache
.ruff_cache
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.7.0
hooks:
# Run the linter.
- id: ruff
# Run the formatter. (disabled until format PR merged)
# - id: ruff-format
# args: [ --check ]
30 changes: 28 additions & 2 deletions CONTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,33 @@ Your inputs are welcome and greatly appreciated! We want to make contributing to
- Becoming a maintainer

## We develop with Github

We use github to host code, to track issues and feature requests, as well as accept pull requests.

## Prepare

We use [ruff](https://docs.astral.sh/ruff/) as our linter and formatter, all the changes you made will be checked by out CI pipeline. So you should:

- install ruff: `pip install ruff` or [more install methods](https://docs.astral.sh/ruff/installation/)

To run CI locally, you can use [pre-commit](https://pre-commit.com/), it runs the same check as CI pipeline:

- install pre-commit: `pip install pre-commit`

then you can run check directly by: `pre-commit run`

> notice that pre-commit will only run on the staged files, if you want to check all, please use `pre-commit run -a`

If you use git, you can set [git hooks](https://git-scm.com/book/ms/v2/Customizing-Git-Git-Hooks) automatically by:

- set git hooks: `pre-commit install`

then pre-commit check will be triggered automatically when you run `git commit`

> if your think some changes are necessary even though they won't pass the check, you can use `git commit --no-verify` which will skip the pre-commit check.

If you are using vscode, you can also use [Offical Ruff Extension for vscode](https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff) to automatically format/lint your code on save.

## We use a simple git flow where all code changes happen through Pull Requests

Pull requests are the best way to propose changes to the codebase. We actively welcome your pull requests:
Expand Down Expand Up @@ -54,9 +79,11 @@ Note: You can have either a patch or minor or major update.
Note: In case of a conflict, the maintainers will decide the final version to be assigned.

## Any contributions you make will be under the permissive open-source License

In short, when you submit code changes, your submissions are understood to be under a permissive open source license like BSD-3, Apache-2.0 and CC, etc that covers the project. Feel free to contact the maintainers if that's a concern.

## Report bugs using Github's [issues](https://github.com/riscv/riscv-arch-test/issues)

We use GitHub issues to track public bugs. Report a bug by [opening a new issue](https://github.com/riscv/riscv-arch-test/issues/new); it's that easy!

## Write bug reports with detail, background, and sample code
Expand All @@ -66,12 +93,11 @@ We use GitHub issues to track public bugs. Report a bug by [opening a new issue]
- A quick summary and/or background
- Steps to reproduce
- Be specific!
- Give sample code if you can.
- Give sample code if you can.
- What you expected would happen
- What actually happens
- Notes (possibly including why you think this might be happening, or stuff you tried that didn't work)

## License
By contributing, you agree that your contributions will be licensed under its permissive open source
licenses.

7 changes: 0 additions & 7 deletions riscof-plugins/rv32/makeplugin/riscof_makeplugin.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import os
import re
import shutil
import subprocess
import shlex
import logging
import random
import string
from string import Template

import riscof.utils as utils
from riscof.pluginTemplate import pluginTemplate
import riscof.constants as constants
from riscv_isac.isac import isac

logger = logging.getLogger()

Expand Down
8 changes: 0 additions & 8 deletions riscof-plugins/rv32/sail_cSim/riscof_sail_cSim.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import os
import re
import shutil
import subprocess
import shlex
import logging
import random
import string
from string import Template
import distutils

import riscof.utils as utils
from riscof.pluginTemplate import pluginTemplate
import riscof.constants as constants
from riscv_isac.isac import isac

logger = logging.getLogger()

Expand Down
9 changes: 0 additions & 9 deletions riscof-plugins/rv32/spike_simple/riscof_spike_simple.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import os
import re
import shutil
import subprocess
import shlex
import logging
import random
import string
from string import Template
import sys

import riscof.utils as utils
import riscof.constants as constants
from riscof.pluginTemplate import pluginTemplate

logger = logging.getLogger()
Expand Down
7 changes: 0 additions & 7 deletions riscof-plugins/rv64/makeplugin/riscof_makeplugin.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import os
import re
import shutil
import subprocess
import shlex
import logging
import random
import string
from string import Template

import riscof.utils as utils
from riscof.pluginTemplate import pluginTemplate
import riscof.constants as constants
from riscv_isac.isac import isac

logger = logging.getLogger()

Expand Down
8 changes: 0 additions & 8 deletions riscof-plugins/rv64/sail_cSim/riscof_sail_cSim.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import os
import re
import shutil
import subprocess
import shlex
import logging
import random
import string
from string import Template

import riscof.utils as utils
from riscof.pluginTemplate import pluginTemplate
import riscof.constants as constants
from riscv_isac.isac import isac

logger = logging.getLogger()

Expand Down
9 changes: 0 additions & 9 deletions riscof-plugins/rv64/spike_simple/riscof_spike_simple.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import os
import re
import shutil
import subprocess
import shlex
import logging
import random
import string
from string import Template
import sys

import riscof.utils as utils
import riscof.constants as constants
from riscof.pluginTemplate import pluginTemplate

logger = logging.getLogger()
Expand Down
5 changes: 2 additions & 3 deletions riscv-ctg/docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

import sys
import os
import shlex
import re

import sphinx_rtd_theme

def get_version():
changelog = open('../../CHANGELOG.md','r').read()
x = re.findall(r'## \[(.*?)\] -',changelog)[0]
Expand Down Expand Up @@ -108,13 +109,11 @@ def setup(app):
#
#html_theme = 'bootstrap'
#html_theme = 'alabaster'
import sphinx_rtd_theme
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
html_theme = 'sphinx_rtd_theme'
html_theme_options = {
'prev_next_buttons_location': 'both',
'display_version': True,
'includehidden': False,
'collapse_navigation':True,
'sticky_navigation': True,
'navigation_depth': 4,
Expand Down
3 changes: 0 additions & 3 deletions riscv-ctg/docs/sphinxext/cairosvgconverter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# -*- coding: utf-8 -*-
"""
"""
from sphinx.errors import ExtensionError
from sphinx.locale import _
from sphinx.transforms.post_transforms.images import ImageConverter
from sphinx.util import logging
from sphinx.util.osutil import ENOENT, EPIPE, EINVAL
from cairosvg import svg2pdf

logger = logging.getLogger(__name__)
Expand Down
16 changes: 10 additions & 6 deletions riscv-ctg/riscv_ctg/constants.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# See LICENSE.incore for details

import os
from math import *
from math import sqrt
from math import * # noqa: F403
from string import Template
from riscv_isac.fp_dataset import *

root = os.path.abspath(os.path.dirname(__file__))

Expand Down Expand Up @@ -80,13 +80,15 @@ def gen_sp_dataset(bit_width,sign=True):
:return: a list of integers
'''
if sign:
conv_func = lambda x: twos(x,bit_width)
def conv_func(x):
return twos(x, bit_width)
sqrt_min = int(-sqrt(2**(bit_width-1)))
sqrt_max = int(sqrt((2**(bit_width-1)-1)))
else:
sqrt_min = 0
sqrt_max = int(sqrt((2**bit_width)-1))
conv_func = lambda x:(int(x,16) if '0x' in x else int(x,2)) if isinstance(x,str) else x
def conv_func(x):
return (int(x, 16) if "0x" in x else int(x, 2)) if isinstance(x, str) else x

dataset = [3, "0x"+"".join(["5"]*int(bit_width/4)), "0x"+"".join(["a"]*int(bit_width/4)), 5, "0x"+"".join(["3"]*int(bit_width/4)), "0x"+"".join(["6"]*int(bit_width/4))]
dataset = list(map(conv_func,dataset)) + [int(sqrt(abs(conv_func("0x8"+"".join(["0"]*int((bit_width/4)-1)))))*(-1 if sign else 1))] + [sqrt_min,sqrt_max]
Expand Down Expand Up @@ -173,9 +175,11 @@ def gen_bitmanip_dataset(bit_width,sign=True):
:return: a list of integers
'''
if sign:
conv_func = lambda x: twos(x,bit_width)
def conv_func(x):
return twos(x, bit_width)
else:
conv_func = lambda x:(int(x,16) if '0x' in x else int(x,2)) if isinstance(x,str) else x
def conv_func(x):
return (int(x, 16) if "0x" in x else int(x, 2)) if isinstance(x, str) else x

# dataset for 0x5, 0xa, 0x3, 0xc, 0x6, 0x9 patterns

Expand Down
8 changes: 3 additions & 5 deletions riscv-ctg/riscv_ctg/cross_comb.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# See LICENSE.incore for details
import random
from constraint import *
from constraint import Problem
from string import Template

import riscv_isac.utils as isac_utils
from riscv_ctg import constants

import riscv_ctg.utils as utils
import riscv_ctg.constants as const
from riscv_ctg.constants import *
from riscv_ctg.constants import case_template, part_template, signode_template
from riscv_ctg.log import logger
from riscv_ctg.__init__ import __version__
from riscv_ctg.generator import OPS
from riscv_ctg.dsp_function import *

INSTR_FORMAT = {
'rformat' : '$instr $rd, $rs1, $rs2',
Expand Down
9 changes: 5 additions & 4 deletions riscv-ctg/riscv_ctg/csr_comb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import functools

from riscv_ctg.log import logger
from riscv_ctg.constants import *
from riscv_ctg.constants import case_template, part_template, signode_template, csr_reg_read_and_sig_upd_template, csr_reg_restore_template, csr_comb_test_template, csr_reg_write_to_field_template

import tokenize as tkn
from io import BytesIO
Expand Down Expand Up @@ -281,7 +281,7 @@ def csr_comb(self, cgf_node):
try:
bool_expr = parse_csr_covpt(covpt)
sols = bool_expr.SAT()
except:
except Exception:
logger.error(f'Invalid csr_comb coverpoint: {covpt}')
continue

Expand All @@ -295,12 +295,13 @@ def csr_comb(self, cgf_node):
logger.error(f'Skipping invalid csr_comb coverpoint condition clause: {clause}')
continue
if mod is not None:
if reg_with_mod is None: reg_with_mod = csr_reg
if reg_with_mod is None:
reg_with_mod = csr_reg
elif reg_with_mod != csr_reg:
logger.error(f'Skipping invalid csr_comb solution with modifiers on more than one registers for the coverpoint: {covpt}')
continue

if not csr_reg in reg_mask_val_mod_dict:
if csr_reg not in reg_mask_val_mod_dict:
if mod == 'old':
reg_mask_val_mod_dict[csr_reg] = [0, 0, 0, 0, mask, val]
elif mod == 'write':
Expand Down
6 changes: 2 additions & 4 deletions riscv-ctg/riscv_ctg/ctg.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# See LICENSE.incore file for details

import copy
import os,re
import os
import multiprocessing as mp

import time
Expand All @@ -13,11 +13,9 @@
from riscv_ctg.generator import Generator
from riscv_ctg.cross_comb import cross
from riscv_ctg.csr_comb import GeneratorCSRComb
from math import *
from riscv_ctg.__init__ import __version__

def create_test(usage_str, node,label,base_isa,max_inst, op_template, randomize, out_dir, xlen, flen, inxFlag):
iflen = 0
if 'mnemonics' not in node and 'csr_comb' not in node:
logger.warning("Neither mnemonics nor csr_comb node not found in covergroup: " + str(label))
return
Expand Down Expand Up @@ -134,6 +132,6 @@ def ctg(verbose, out, random ,xlen_arg,flen_arg, cgf_file,num_procs,base_isa, ma
op_template = utils.load_yaml(const.template_files)
cgf = expand_cgf(cgf_file,xlen,flen)
pool = mp.Pool(num_procs)
results = pool.starmap(create_test, [(usage_str, node,label,base_isa,max_inst, op_template,
pool.starmap(create_test, [(usage_str, node,label,base_isa,max_inst, op_template,
randomize, out_dir, xlen, flen, inxFlag) for label,node in cgf.items()])
pool.close()
Loading