Re: [yocto] [PATCH yocto-autobuilder-helper 1/2] Rewrite metrics-gathering scripts

2023-10-31 Thread Yoann Congal

Le 31/10/2023 à 15:49, Ross Burton a écrit :
> From: Ross Burton 
> 
> Rewrite the scripts that gather the metrics to be more generic.
> 
> Extract the metrics repository cloning out so that we don't have to
> repeatedly clone it.
> 
> Make the scripts parse their arguments using getopt and be more specific
> about what they're passed.  In particular, this means that for the patch
> review run we pass the _repository_ that we're scanning so we can do git
> operations on it, and the base of the _layers_ (either a layer, or a
> directory containing layers) so we know what to scan.
> 
> Be more clever when identifying what commits we need to analyse for
> patch review: instead of iterating through a set randomly, we can keep
> the revision list sorted and the checkout operations are a lot faster.
> 
> Remove the commit/file count metric addition as patchreview itself does
> that now.
> 
> Add an explicit --push option so it's easy to test the scripts in
> isolation without pushing.
> 
> Signed-off-by: Ross Burton 

Thanks Ross!
Reviewed-by: Yoann Congal 

> ---
>  config.json|  14 +++--
>  scripts/cve-generate-chartdata |   8 ++-
>  scripts/patchmetrics-update| 109 +---
>  scripts/run-cvecheck   | 106 +++
>  scripts/run-patchmetrics   | 110 -
>  5 files changed, 251 insertions(+), 96 deletions(-)
> 
> diff --git a/config.json b/config.json
> index 918cf08..f3a4ee7 100644
> --- a/config.json
> +++ b/config.json
> @@ -1208,12 +1208,18 @@
>  "BB_SERVER_TIMEOUT = '0'"
>  ],
>  "step1" : {
> -"shortname" : "Generating patch metrics",
> -"EXTRACMDS" : 
> ["../../yocto-autobuilder-helper/scripts/run-patchmetrics ../ ../meta/ 
> ${HELPERRESULTSDIR}/../../patchmetrics . ${HELPERBRANCHNAME}"]
> +"shortname" : "Fetching metrics repositories",
> +"EXTRAPLAINCMDS" : [
> +"git clone 
> ssh://g...@push.yoctoproject.org/yocto-metrics"
> +]
>  },
>  "step2" : {
> -"shortname" : "Running CVE checks",
> -"EXTRACMDS" : 
> ["../../yocto-autobuilder-helper/scripts/run-cvecheck ../ ../meta/ 
> ${HELPERRESULTSDIR}/../../patchmetrics . ${HELPERBRANCHNAME}"]
> +"shortname" : "Generating patch metrics for Poky",
> +"EXTRACMDS" : ["${SCRIPTSDIR}/run-patchmetrics --poky ../ 
> --metrics ../yocto-metrics --repo ../ --layer ../meta --branch 
> ${HELPERBRANCHNAME} --results ${HELPERRESULTSDIR}/../../patchmetrics --push"]
> +},
> +"step3" : {
> +"shortname" : "Running CVE checks for Poky",
> +"EXTRACMDS" : ["${SCRIPTSDIR}/run-cvecheck --metrics 
> ../yocto-metrics --branch ${HELPERBRANCHNAME} --results 
> ${HELPERRESULTSDIR}/../../patchmetrics --push"]
>  }
>  
>  },
> diff --git a/scripts/cve-generate-chartdata b/scripts/cve-generate-chartdata
> index 95d12ff..dbbbe82 100755
> --- a/scripts/cve-generate-chartdata
> +++ b/scripts/cve-generate-chartdata
> @@ -11,8 +11,12 @@ args.add_argument("-j", "--json", help="JSON data file to 
> use")
>  args.add_argument("-r", "--resultsdir", help="results directory to parse")
>  args = args.parse_args()
>  
> -with open(args.json) as f:
> -counts = json.load(f)
> +try:
> +with open(args.json) as f:
> +counts = json.load(f)
> +except FileNotFoundError:
> +# if the file does not exist, start with an empty database.
> +counts = {}
>  
>  lastyear = {}
>  
> diff --git a/scripts/patchmetrics-update b/scripts/patchmetrics-update
> index 65d351e..2fa7e4a 100755
> --- a/scripts/patchmetrics-update
> +++ b/scripts/patchmetrics-update
> @@ -1,65 +1,76 @@
>  #!/usr/bin/env python3
> -import json, os.path, collections
> -import sys
> +
> +import json
> +import pathlib
>  import argparse
>  import subprocess
>  import tempfile
> +import sys
> +
> +# Given a git repository and a base to search for layers (either a layer
> +# directly, or a directory containing layers), run the patchscript and update
> +# the specified JSON file.
>  
>  args = argparse.ArgumentParser(description="Update Patch Metrics")
> -args.add_argument("-j", "--json", help="update JSON")
> -args.add_argument("-m", "--metadata", help="metadata directry to scan")
> -args.add_argument("-s", "--patchscript", help="review script to use")
> -args.add_argument("-r", "--repo", help="repository to use")
> +args.add_argument("-j", "--json", required=True, type=pathlib.Path, 
> help="update specified JSON file")
> +args.add_argument("-s", "--patchscript", required=True, type=pathlib.Path, 
> help="patchreview script to run")
> +args.add_argument("-r", "--repo", required=True, type=pathlib.Path, 
> help="repository to use (e.g. path/to/poky)")
> +args.add_argument("-l", "--layer", type=pathlib.Path, 

Re: [yocto] [PATCH yocto-autobuilder-helper 1/2] Rewrite metrics-gathering scripts

2023-10-31 Thread Ross Burton
Props to Yoann Congal for an early version of this work, review, and fixes.

Ross

> On 31 Oct 2023, at 14:49, Ross Burton via lists.yoctoproject.org 
>  wrote:
> 
> From: Ross Burton 
> 
> Rewrite the scripts that gather the metrics to be more generic.
> 
> Extract the metrics repository cloning out so that we don't have to
> repeatedly clone it.
> 
> Make the scripts parse their arguments using getopt and be more specific
> about what they're passed.  In particular, this means that for the patch
> review run we pass the _repository_ that we're scanning so we can do git
> operations on it, and the base of the _layers_ (either a layer, or a
> directory containing layers) so we know what to scan.
> 
> Be more clever when identifying what commits we need to analyse for
> patch review: instead of iterating through a set randomly, we can keep
> the revision list sorted and the checkout operations are a lot faster.
> 
> Remove the commit/file count metric addition as patchreview itself does
> that now.
> 
> Add an explicit --push option so it's easy to test the scripts in
> isolation without pushing.
> 
> Signed-off-by: Ross Burton 
> ---
> config.json|  14 +++--
> scripts/cve-generate-chartdata |   8 ++-
> scripts/patchmetrics-update| 109 +---
> scripts/run-cvecheck   | 106 +++
> scripts/run-patchmetrics   | 110 -
> 5 files changed, 251 insertions(+), 96 deletions(-)
> 
> diff --git a/config.json b/config.json
> index 918cf08..f3a4ee7 100644
> --- a/config.json
> +++ b/config.json
> @@ -1208,12 +1208,18 @@
> "BB_SERVER_TIMEOUT = '0'"
> ],
> "step1" : {
> -"shortname" : "Generating patch metrics",
> -"EXTRACMDS" : 
> ["../../yocto-autobuilder-helper/scripts/run-patchmetrics ../ ../meta/ 
> ${HELPERRESULTSDIR}/../../patchmetrics . ${HELPERBRANCHNAME}"]
> +"shortname" : "Fetching metrics repositories",
> +"EXTRAPLAINCMDS" : [
> +"git clone 
> ssh://g...@push.yoctoproject.org/yocto-metrics"
> +]
> },
> "step2" : {
> -"shortname" : "Running CVE checks",
> -"EXTRACMDS" : 
> ["../../yocto-autobuilder-helper/scripts/run-cvecheck ../ ../meta/ 
> ${HELPERRESULTSDIR}/../../patchmetrics . ${HELPERBRANCHNAME}"]
> +"shortname" : "Generating patch metrics for Poky",
> +"EXTRACMDS" : ["${SCRIPTSDIR}/run-patchmetrics --poky ../ 
> --metrics ../yocto-metrics --repo ../ --layer ../meta --branch 
> ${HELPERBRANCHNAME} --results ${HELPERRESULTSDIR}/../../patchmetrics --push"]
> +},
> +"step3" : {
> +"shortname" : "Running CVE checks for Poky",
> +"EXTRACMDS" : ["${SCRIPTSDIR}/run-cvecheck --metrics 
> ../yocto-metrics --branch ${HELPERBRANCHNAME} --results 
> ${HELPERRESULTSDIR}/../../patchmetrics --push"]
> }
> 
> },
> diff --git a/scripts/cve-generate-chartdata b/scripts/cve-generate-chartdata
> index 95d12ff..dbbbe82 100755
> --- a/scripts/cve-generate-chartdata
> +++ b/scripts/cve-generate-chartdata
> @@ -11,8 +11,12 @@ args.add_argument("-j", "--json", help="JSON data file to 
> use")
> args.add_argument("-r", "--resultsdir", help="results directory to parse")
> args = args.parse_args()
> 
> -with open(args.json) as f:
> -counts = json.load(f)
> +try:
> +with open(args.json) as f:
> +counts = json.load(f)
> +except FileNotFoundError:
> +# if the file does not exist, start with an empty database.
> +counts = {}
> 
> lastyear = {}
> 
> diff --git a/scripts/patchmetrics-update b/scripts/patchmetrics-update
> index 65d351e..2fa7e4a 100755
> --- a/scripts/patchmetrics-update
> +++ b/scripts/patchmetrics-update
> @@ -1,65 +1,76 @@
> #!/usr/bin/env python3
> -import json, os.path, collections
> -import sys
> +
> +import json
> +import pathlib
> import argparse
> import subprocess
> import tempfile
> +import sys
> +
> +# Given a git repository and a base to search for layers (either a layer
> +# directly, or a directory containing layers), run the patchscript and update
> +# the specified JSON file.
> 
> args = argparse.ArgumentParser(description="Update Patch Metrics")
> -args.add_argument("-j", "--json", help="update JSON")
> -args.add_argument("-m", "--metadata", help="metadata directry to scan")
> -args.add_argument("-s", "--patchscript", help="review script to use")
> -args.add_argument("-r", "--repo", help="repository to use")
> +args.add_argument("-j", "--json", required=True, type=pathlib.Path, 
> help="update specified JSON file")
> +args.add_argument("-s", "--patchscript", required=True, type=pathlib.Path, 
> help="patchreview script to run")
> +args.add_argument("-r", "--repo", required=True, type=pathlib.Path, 
> help="repository to use (e.g. path/to/poky)")
>