Skip to content

Commit

Permalink
#1350 - Apply Ruff format
Browse files Browse the repository at this point in the history
Signed-off-by: Chin Yeung Li <[email protected]>
  • Loading branch information
chinyeungli committed Aug 14, 2024
1 parent ab1c989 commit bdf96d9
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 114 deletions.
9 changes: 3 additions & 6 deletions scanpipe/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ class ProjectAdmin(ScanPipeBaseAdmin):
ordering = ["-created_date"]
fieldsets = [
("", {"fields": ("name", "slug", "notes", "extra_data", "settings", "uuid")}),
("Links", {"fields": ("packages_link",
"dependencies_link", "resources_link")}),
("Links", {"fields": ("packages_link", "dependencies_link", "resources_link")}),
]
readonly_fields = ["packages_link",
"dependencies_link", "resources_link", "uuid"]
readonly_fields = ["packages_link", "dependencies_link", "resources_link", "uuid"]

def get_queryset(self, request):
return (
Expand Down Expand Up @@ -118,8 +116,7 @@ class CodebaseResourceAdmin(ScanPipeBaseAdmin):
search_fields = [
"path",
]
list_filter = ["project", "type",
"programming_language", "compliance_alert"]
list_filter = ["project", "type", "programming_language", "compliance_alert"]
ordering = ["project", "path"]


Expand Down
15 changes: 5 additions & 10 deletions scanpipe/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,10 @@ def load_pipelines(self):
pipelines Python files found at `SCANCODEIO_PIPELINES_DIRS` locations.
"""
entry_points = importlib_metadata.entry_points()
pipeline_entry_points = set(
entry_points.select(group="scancodeio_pipelines"))
pipeline_entry_points = set(entry_points.select(group="scancodeio_pipelines"))

for entry_point in sorted(pipeline_entry_points):
self.register_pipeline(name=entry_point.name,
cls=entry_point.load())
self.register_pipeline(name=entry_point.name, cls=entry_point.load())

pipelines_dirs = getattr(settings, "SCANCODEIO_PIPELINES_DIRS", [])
logger.debug(f"Load user provided pipelines from {pipelines_dirs}")
Expand Down Expand Up @@ -224,15 +222,13 @@ def set_policies(self):
include the proper content, we want to raise an exception while the app
is loading to warn system admins about the issue.
"""
policies_file_location = getattr(
settings, "SCANCODEIO_POLICIES_FILE", None)
policies_file_location = getattr(settings, "SCANCODEIO_POLICIES_FILE", None)

if policies_file_location:
policies_file = Path(policies_file_location).expanduser()

if policies_file.exists():
logger.debug(style.SUCCESS(
f"Load policies from {policies_file}"))
logger.debug(style.SUCCESS(f"Load policies from {policies_file}"))
policies = saneyaml.load(policies_file.read_text())
license_policies = policies.get("license_policies", [])
self.license_policies_index = self.get_policies_index(
Expand All @@ -255,8 +251,7 @@ def policies_enabled(self):

def sync_runs_and_jobs(self):
"""Synchronize ``QUEUED`` and ``RUNNING`` Run with their related Jobs."""
logger.info(
"Synchronizing QUEUED and RUNNING Run with their related Jobs...")
logger.info("Synchronizing QUEUED and RUNNING Run with their related Jobs...")

run_model = self.get_model("Run")
queued_or_running = run_model.objects.queued_or_running()
Expand Down
30 changes: 10 additions & 20 deletions scanpipe/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ def field(self):

# Retrieve distinct values for the specified field.
field_values = (
qs.order_by(field_name).values_list(
field_name, flat=True).distinct()
qs.order_by(field_name).values_list(field_name, flat=True).distinct()
)
value_choices = [
(value, value) for value in field_values if value not in EMPTY_VALUES
Expand Down Expand Up @@ -248,11 +247,9 @@ def filter_queryset(self, queryset):
for name, value in self.form.cleaned_data.items():
field_name = self.filters[name].field_name
if value == self.empty_value:
queryset = queryset.filter(
**{f"{field_name}__in": EMPTY_VALUES})
queryset = queryset.filter(**{f"{field_name}__in": EMPTY_VALUES})
elif value == self.any_value:
queryset = queryset.filter(
~Q(**{f"{field_name}__in": EMPTY_VALUES}))
queryset = queryset.filter(~Q(**{f"{field_name}__in": EMPTY_VALUES}))
elif value == self.other_value and hasattr(queryset, "less_common"):
return queryset.less_common(name)
else:
Expand Down Expand Up @@ -302,8 +299,7 @@ def parse_query_string_to_lookups(query_string, default_lookup_expr, default_fie
search_value = term
field_name = default_field

lookups &= Q(
**{f"{field_name}__{lookup_expr}": search_value}, _negated=negated)
lookups &= Q(**{f"{field_name}__{lookup_expr}": search_value}, _negated=negated)

return lookups

Expand Down Expand Up @@ -420,8 +416,7 @@ def __init__(self, data=None, *args, **kwargs):
archived_count = Project.objects.filter(is_archived=True).count()
self.filters["is_archived"].extra["widget"] = BulmaLinkWidget(
choices=[
("",
f'<i class="fa-solid fa-seedling"></i> {active_count} Active'),
("", f'<i class="fa-solid fa-seedling"></i> {active_count} Active'),
(
"true",
f'<i class="fa-solid fa-dice-d6"></i> {archived_count} Archived',
Expand Down Expand Up @@ -685,16 +680,14 @@ class PackageFilterSet(FilterSetUtilsMixin, django_filters.FilterSet):
"tag",
"resources_count",
],
grouped_fields={"package_url": [
"type", "namespace", "name", "version"]},
grouped_fields={"package_url": ["type", "namespace", "name", "version"]},
)
purl = PackageURLFilter(label="Package URL")
is_vulnerable = IsVulnerable(field_name="affected_by_vulnerabilities")
compliance_alert = django_filters.ChoiceFilter(
choices=[(EMPTY_VAR, "None")] + CodebaseResource.Compliance.choices,
)
copyright = django_filters.filters.CharFilter(
widget=HasValueDropdownWidget)
copyright = django_filters.filters.CharFilter(widget=HasValueDropdownWidget)
declared_license_expression = django_filters.filters.CharFilter(
widget=HasValueDropdownWidget
)
Expand Down Expand Up @@ -774,8 +767,7 @@ class DependencyFilterSet(FilterSetUtilsMixin, django_filters.FilterSet):
"datafile_resource",
"datasource_id",
],
grouped_fields={"package_url": [
"type", "namespace", "name", "version"]},
grouped_fields={"package_url": ["type", "namespace", "name", "version"]},
)
purl = PackageURLFilter(label="Package URL")
type = ModelFieldValuesFilter()
Expand Down Expand Up @@ -851,8 +843,7 @@ def get_status_choices(qs, include_any=False):
status_values = (
qs.order_by("status").values_list("status", flat=True).distinct()
)
value_choices = [(status, status)
for status in status_values if status]
value_choices = [(status, status) for status in status_values if status]
return default_choices + value_choices


Expand Down Expand Up @@ -894,5 +885,4 @@ def __init__(self, *args, **kwargs):
if project:
qs = CodebaseResource.objects.filter(project=project)
status_filter = self.filters["status"]
status_filter.extra["choices"] = status_filter.get_status_choices(
qs)
status_filter.extra["choices"] = status_filter.get_status_choices(qs)
24 changes: 8 additions & 16 deletions scanpipe/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class MultipleFileInput(forms.ClearableFileInput):

class MultipleFileField(forms.FileField):
def __init__(self, *args, **kwargs):
kwargs.setdefault("widget", MultipleFileInput(
attrs={"class": "file-input"}))
kwargs.setdefault("widget", MultipleFileInput(attrs={"class": "file-input"}))
super().__init__(*args, **kwargs)

def clean(self, data, initial=None):
Expand Down Expand Up @@ -171,8 +170,7 @@ def __init__(self, *args, **kwargs):
name_field.help_text = "The unique name of your project."

# Do not include "add-on" pipelines in the context of the create Project form
pipeline_choices = scanpipe_app.get_pipeline_choices(
include_addon=False)
pipeline_choices = scanpipe_app.get_pipeline_choices(include_addon=False)
self.fields["pipeline"].choices = pipeline_choices

def clean_name(self):
Expand Down Expand Up @@ -207,8 +205,7 @@ class AddLabelsForm(forms.Form):
labels = TagField(
label="Add labels to this project:",
widget=TagWidget(
attrs={"class": "input",
"placeholder": "Comma-separated list of labels"}
attrs={"class": "input", "placeholder": "Comma-separated list of labels"}
),
)

Expand Down Expand Up @@ -400,8 +397,7 @@ class ProjectSettingsForm(forms.ModelForm):
ignored_dependency_scopes = KeyValueListField(
label="Ignored dependency scopes",
required=False,
help_text=convert_markdown_to_html(
ignored_dependency_scopes_help.strip()),
help_text=convert_markdown_to_html(ignored_dependency_scopes_help.strip()),
widget=forms.Textarea(
attrs={
"class": "textarea is-dynamic",
Expand All @@ -415,8 +411,7 @@ class ProjectSettingsForm(forms.ModelForm):
ignored_vulnerabilities = ListTextarea(
label="Ignored vulnerabilities",
required=False,
help_text=convert_markdown_to_html(
ignored_vulnerabilities_help.strip()),
help_text=convert_markdown_to_html(ignored_vulnerabilities_help.strip()),
widget=forms.Textarea(
attrs={
"class": "textarea is-dynamic",
Expand All @@ -437,8 +432,7 @@ class ProjectSettingsForm(forms.ModelForm):
"\nFeel free to modify its content according to your preferences and paste "
"the entire HTML code into this field."
),
widget=forms.Textarea(
attrs={"class": "textarea is-dynamic", "rows": 3}),
widget=forms.Textarea(attrs={"class": "textarea is-dynamic", "rows": 3}),
)
product_name = forms.CharField(
label="Product name",
Expand Down Expand Up @@ -495,8 +489,7 @@ def update_project_settings(self, project):


class ProjectCloneForm(forms.Form):
clone_name = forms.CharField(
widget=forms.TextInput(attrs={"class": "input"}))
clone_name = forms.CharField(widget=forms.TextInput(attrs={"class": "input"}))
copy_inputs = forms.BooleanField(
initial=True,
required=False,
Expand Down Expand Up @@ -554,8 +547,7 @@ class Meta:

def __init__(self, *args, **kwargs):
if not kwargs.get("instance"):
raise ValueError(
"An Run object is required to instantiate this form.")
raise ValueError("An Run object is required to instantiate this form.")

super().__init__(*args, **kwargs)
pipeline_class = self.instance.pipeline_class
Expand Down
18 changes: 6 additions & 12 deletions scanpipe/pipes/rootfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ class RootFs:
"""A root filesystem."""

location = attr.attrib(
metadata=dict(
doc="The root directory location where this rootfs lives.")
metadata=dict(doc="The root directory location where this rootfs lives.")
)

distro = attr.attrib(
Expand All @@ -97,8 +96,7 @@ def from_project_codebase(cls, project):
Return RootFs objects collected from the project's "codebase" directory.
Each directory in the input/ is considered as the root of a root filesystem.
"""
subdirs = [path for path in project.codebase_path.glob(
"*/") if path.is_dir()]
subdirs = [path for path in project.codebase_path.glob("*/") if path.is_dir()]
for subdir in subdirs:
rootfs_location = str(subdir.absolute())
yield RootFs(location=rootfs_location)
Expand Down Expand Up @@ -172,8 +170,7 @@ def has_hash_diff(install_file, codebase_resource):
# Find a suitable hash type that is present on both install_file and
# codebase_resource, skip otherwise.
share_hash_type = all(
[hasattr(install_file, hash_type), hasattr(
codebase_resource, hash_type)]
[hasattr(install_file, hash_type), hasattr(codebase_resource, hash_type)]
)

if not share_hash_type:
Expand Down Expand Up @@ -203,8 +200,7 @@ def package_getter(root_dir, **kwargs):

def _create_system_package(project, purl, package):
"""Create system package and related resources."""
created_package = pipes.update_or_create_package(
project, package.to_dict())
created_package = pipes.update_or_create_package(project, package.to_dict())

installed_files = []
if hasattr(package, "resources"):
Expand Down Expand Up @@ -320,8 +316,7 @@ def match_not_analyzed(
3. Relate each matched CodebaseResource to the matching DiscoveredPackage and
set its status.
"""
known_resources = get_resource_with_md5(
project=project, status=reference_status)
known_resources = get_resource_with_md5(project=project, status=reference_status)
known_resources_by_md5_size = {
(
r.md5,
Expand All @@ -330,8 +325,7 @@ def match_not_analyzed(
for r in known_resources
}
count = 0
matchables = get_resource_with_md5(
project=project, status=not_analyzed_status)
matchables = get_resource_with_md5(project=project, status=not_analyzed_status)
for matchable in matchables:
key = (matchable.md5, matchable.size)
matched = known_resources_by_md5_size.get(key)
Expand Down
6 changes: 2 additions & 4 deletions scanpipe/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ def report_failure(job, connection, type, value, traceback):
try:
run = get_run_instance(run_pk=job.id)
except Run.DoesNotExist:
info(
f"FAILURE to get the Run instance with job.id={job.id}", "Unknown")
info(f"FAILURE to get the Run instance with job.id={job.id}", "Unknown")
return

run.set_task_ended(exitcode=1, output=f"value={value} trace={traceback}")
Expand All @@ -63,8 +62,7 @@ def execute_pipeline_task(run_pk):
run.set_scancodeio_version()
run.set_task_started(run_pk)

info(
f'Run pipeline: "{run.pipeline_name}" on project: "{project.name}"', run_pk)
info(f'Run pipeline: "{run.pipeline_name}" on project: "{project.name}"', run_pk)

pipeline = run.make_pipeline_instance()
exitcode, output = pipeline.execute()
Expand Down
Loading

0 comments on commit bdf96d9

Please sign in to comment.