From e40e9e81809c6c49cb012a01c63a3463bfa41f8e Mon Sep 17 00:00:00 2001 From: Vijayakumar Date: Tue, 7 May 2024 14:10:01 +0530 Subject: T6310: updated codeql version T6310: updated codeql version for other calls --- .github/workflows/codeql-analysis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 75f8a1b..7176702 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -41,7 +41,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v2 + uses: github/codeql-action/init@v3 with: languages: ${{matrix.language}} config-file: ${{inputs.codeql-cfg-path}} @@ -50,7 +50,7 @@ jobs: # If this step fails, configure a build command manually using build-command input. This command will be executed in the corresponding step. - name: Autobuild if: ${{!inputs.build-command}} - uses: github/codeql-action/autobuild@v2 + uses: github/codeql-action/autobuild@v3 - name: Manual build if: ${{inputs.build-command}} @@ -58,4 +58,4 @@ jobs: ${{inputs.build-command}} - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 + uses: github/codeql-action/analyze@v3 -- cgit v1.2.3 From d327ae13f5da750e80ebc2497139b7fd94ad6a88 Mon Sep 17 00:00:00 2001 From: Vijayakumar Date: Tue, 7 May 2024 14:32:43 +0530 Subject: T6310: add PR templates --- .github/PULL_REQUEST_TEMPLATE.md | 60 ++++++++++++++++++++++++ .github/labeler.yml | 12 +++++ .github/reviewers.yml | 3 ++ .github/workflows/auto-author-assign.yml | 27 +++++++++++ .github/workflows/mergifyio_backport.yml | 22 +++++++++ .github/workflows/pr-conflicts.yml | 18 +++++++ .github/workflows/pull-request-labels.yml | 20 ++++++++ .github/workflows/pull-request-management.yml | 25 ++++++++++ .github/workflows/pull-request-message-check.yml | 23 +++++++++ .github/workflows/stale.yml | 22 +++++++++ .github/workflows/unused-imports.yml | 22 +++++++++ 11 files changed, 254 insertions(+) create mode 100644 .github/PULL_REQUEST_TEMPLATE.md create mode 100644 .github/labeler.yml create mode 100644 .github/reviewers.yml create mode 100644 .github/workflows/auto-author-assign.yml create mode 100644 .github/workflows/mergifyio_backport.yml create mode 100644 .github/workflows/pr-conflicts.yml create mode 100644 .github/workflows/pull-request-labels.yml create mode 100644 .github/workflows/pull-request-management.yml create mode 100644 .github/workflows/pull-request-message-check.yml create mode 100644 .github/workflows/stale.yml create mode 100644 .github/workflows/unused-imports.yml diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..cd348ea --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,60 @@ + + + +## Change Summary + + +## Types of changes + +- [ ] Bug fix (non-breaking change which fixes an issue) +- [ ] New feature (non-breaking change which adds functionality) +- [ ] Code style update (formatting, renaming) +- [ ] Refactoring (no functional changes) +- [ ] Migration from an old Vyatta component to vyos-1x, please link to related PR inside obsoleted component +- [ ] Other (please describe): + +## Related Task(s) + + + +## Related PR(s) + + +## Component(s) name + + +## Proposed changes + + +## How to test + + +## Smoketest result + + +## Checklist: + + + +- [ ] I have read the [**CONTRIBUTING**](https://github.com/vyos/vyos-1x/blob/current/CONTRIBUTING.md) document +- [ ] I have linked this PR to one or more Phabricator Task(s) +- [ ] I have run the components [**SMOKETESTS**](https://github.com/vyos/vyos-1x/tree/current/smoketest/scripts/cli) if applicable +- [ ] My commit headlines contain a valid Task id +- [ ] My change requires a change to the documentation +- [ ] I have updated the documentation accordingly diff --git a/.github/labeler.yml b/.github/labeler.yml new file mode 100644 index 0000000..e0b9ee4 --- /dev/null +++ b/.github/labeler.yml @@ -0,0 +1,12 @@ +equuleus: + - any: + - base-branch: 'equuleus' +current: + - any: + - base-branch: 'current' +crux: + - any: + - base-branch: 'crux' +sagitta: + - any: + - base-branch: 'sagitta' diff --git a/.github/reviewers.yml b/.github/reviewers.yml new file mode 100644 index 0000000..a1647d2 --- /dev/null +++ b/.github/reviewers.yml @@ -0,0 +1,3 @@ +--- +"**/*": + - team: reviewers diff --git a/.github/workflows/auto-author-assign.yml b/.github/workflows/auto-author-assign.yml new file mode 100644 index 0000000..1a7f8ef --- /dev/null +++ b/.github/workflows/auto-author-assign.yml @@ -0,0 +1,27 @@ +name: "PR Triage" +on: + pull_request_target: + types: [opened, reopened, ready_for_review, locked] + +permissions: + pull-requests: write + +jobs: + # https://github.com/marketplace/actions/auto-author-assign + assign-author: + runs-on: ubuntu-latest + steps: + - name: "Assign Author to PR" + uses: toshimaru/auto-author-assign@v1.6.2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + + # https://github.com/shufo/auto-assign-reviewer-by-files + assign_reviewer: + runs-on: ubuntu-latest + steps: + - name: Request review based on files changes and/or groups the author belongs to + uses: shufo/auto-assign-reviewer-by-files@v1.1.4 + with: + token: ${{ secrets.PR_ACTION_ASSIGN_REVIEWERS }} + config: .github/reviewers.yml diff --git a/.github/workflows/mergifyio_backport.yml b/.github/workflows/mergifyio_backport.yml new file mode 100644 index 0000000..d9f863d --- /dev/null +++ b/.github/workflows/mergifyio_backport.yml @@ -0,0 +1,22 @@ +name: Mergifyio backport + +on: [issue_comment] + +jobs: + mergifyio_backport: + if: github.repository == 'vyos/vyos-1x' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - uses: actions-ecosystem/action-regex-match@v2 + id: regex-match + with: + text: ${{ github.event.comment.body }} + regex: '@[Mm][Ee][Rr][Gg][Ii][Ff][Yy][Ii][Oo] backport ' + + - uses: actions-ecosystem/action-add-labels@v1 + if: ${{ steps.regex-match.outputs.match != '' }} + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + labels: backport diff --git a/.github/workflows/pr-conflicts.yml b/.github/workflows/pr-conflicts.yml new file mode 100644 index 0000000..2fd0bb4 --- /dev/null +++ b/.github/workflows/pr-conflicts.yml @@ -0,0 +1,18 @@ +name: "PR Conflicts checker" +on: + pull_request_target: + types: [synchronize] + +jobs: + Conflict_Check: + name: 'Check PR status: conflicts and resolution' + runs-on: ubuntu-latest + steps: + - name: check if PRs are dirty + uses: eps1lon/actions-label-merge-conflict@v3 + with: + dirtyLabel: "state: conflict" + removeOnDirtyLabel: "state: conflict resolved" + repoToken: "${{ secrets.GITHUB_TOKEN }}" + commentOnDirty: "This pull request has conflicts, please resolve those before we can evaluate the pull request." + commentOnClean: "Conflicts have been resolved. A maintainer will review the pull request shortly." diff --git a/.github/workflows/pull-request-labels.yml b/.github/workflows/pull-request-labels.yml new file mode 100644 index 0000000..43856be --- /dev/null +++ b/.github/workflows/pull-request-labels.yml @@ -0,0 +1,20 @@ +--- +name: Add pull request labels + +on: + pull_request_target: + branches: + - current + - crux + - equuleus + - sagitta + +jobs: + add-pr-label: + name: Add PR Labels + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write + steps: + - uses: actions/labeler@v5 diff --git a/.github/workflows/pull-request-management.yml b/.github/workflows/pull-request-management.yml new file mode 100644 index 0000000..3a855c1 --- /dev/null +++ b/.github/workflows/pull-request-management.yml @@ -0,0 +1,25 @@ +--- +name: Build Pull Request Package + +on: + pull_request: + branches: + - current + - crux + - equuleus + +jobs: + j2lint: + name: Validate j2 files + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + timeout-minutes: 2 + - name: Setup J2Lint + timeout-minutes: 2 + run: | + sudo pip install git+https://github.com/aristanetworks/j2lint.git@341b5d5db86e095b622f09770cb6367a1583620e + - name: Run J2lint + timeout-minutes: 2 + run: | + j2lint $GITHUB_WORKSPACE/data diff --git a/.github/workflows/pull-request-message-check.yml b/.github/workflows/pull-request-message-check.yml new file mode 100644 index 0000000..8c206a5 --- /dev/null +++ b/.github/workflows/pull-request-message-check.yml @@ -0,0 +1,23 @@ +--- +name: Check pull request message format + +on: + pull_request: + branches: + - current + - crux + - equuleus + +jobs: + check-pr-title: + name: Check pull request title + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + timeout-minutes: 2 + - name: Install the requests library + run: pip3 install requests + - name: Check the PR title + timeout-minutes: 2 + run: | + ./scripts/check-pr-title-and-commit-messages.py '${{ github.event.pull_request.url }}' diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml new file mode 100644 index 0000000..d21d151 --- /dev/null +++ b/.github/workflows/stale.yml @@ -0,0 +1,22 @@ +name: "Issue and PR stale management" +on: + schedule: + - cron: "0 0 * * *" + +jobs: + stale: + runs-on: ubuntu-latest + if: github.repository == 'vyos/vyos-1x' + steps: + # Issue stale management + - uses: actions/stale@v6 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + days-before-stale: 90 + days-before-close: -1 + stale-issue-message: 'This issue is stale because it has been open 90 days with no activity. The issue will be reviewed by a maintainer and may be closed' + stale-issue-label: 'state: stale' + exempt-issue-labels: 'state: accepted, state: in-progress' + stale-pr-message: 'This PR is stale because it has been open 30 days with no activity. The PR will be reviewed by a maintainer and may be closed' + stale-pr-label: 'state: stale' + exempt-pr-labels: 'state: accepted, state: in-progress' diff --git a/.github/workflows/unused-imports.yml b/.github/workflows/unused-imports.yml new file mode 100644 index 0000000..da57bd2 --- /dev/null +++ b/.github/workflows/unused-imports.yml @@ -0,0 +1,22 @@ +name: Check for unused imports using Pylint +on: + pull_request_target: + branches: + - current + - sagitta + +jobs: + Check-Unused-Imports: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v3 + with: + python-version: 3.11 + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pylint + - name: Analysing the code with pylint + run: make unused-imports -- cgit v1.2.3 From ffae259e9e09acc27c4be6c748e335f42cd1924e Mon Sep 17 00:00:00 2001 From: kumvijaya Date: Wed, 8 May 2024 14:06:39 +0530 Subject: T6315: added reusable codeql workflow documentation --- README.md | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c2d3879..430987e 100644 --- a/README.md +++ b/README.md @@ -1 +1,66 @@ -# vyos-github-actions \ No newline at end of file +# vyos-github-actions + + + +This keeps all the reusable github action workflows for vyos + +**codeql-analysis**: +This performs codeql analysis on the invoking repo using given inputs. + +This performs below: + +- Checkout the code. +- Initialize codeql. This uses the input languages. Initializes for each language given in input. +- Build the code. Either using autobuild or manual build as per input. +- Analyze with codeql. + +_Usage_: + +```yaml +name: "Perform CodeQL Analysis" + +on: + push: + branches: [ "current", "sagitta", "equuleus" ] + pull_request: + # The branches below must be a subset of the branches above + branches: [ "current" ] + schedule: + - cron: '22 10 * * 0' + +permissions: + actions: read + contents: read + security-events: write + +jobs: + codeql-analysis-call: + uses: vyos/vyos-github-actions/.github/workflows/codeql-analysis.yml@main + secrets: inherit + with: + languages: "['python']" +``` + + +**Inputs**: + +- _languages_: Optional input , 'typescript', 'python', 'ruby'. To set multiple languages, use the same syntax as you can see in the default value. +- _codeql-cfg-path_: Optional input to set path to a CodeQL config file. +- _build-command_: Optional input to specify manual build command. The multiline syntax is supported + + + + + +| **Input** | **Description** | **Default** | **Required** | +| ---------------------- | ---------------------------------------------------------------------------------------------- | ---------------| ------------- | +| **`languages`** | Languages for CodeQL check. Supported values are: 'cpp', 'csharp', 'go', 'java', 'javascript' | **['python']** | **false** | +| **`codeql-cfg-path`** | Path to a CodeQL config file | | **false** | +| **`build-command`** | Manual build command | | **false** | + + +Referenece: +[Codeql Action](https://github.com/github/codeql-action) + + +Also see the [GitHub reusable workflows documentation](https://docs.github.com/en/actions/creating-actions/sharing-actions-and-workflows-from-your-private-repository) -- cgit v1.2.3 From fcdc636e107eb7086db5db60b85b1041b09d1ee6 Mon Sep 17 00:00:00 2001 From: kumvijaya Date: Wed, 8 May 2024 14:12:15 +0530 Subject: T6315: added reusable codeql workflow documentation --- README.md | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/README.md b/README.md index 430987e..74e1b92 100644 --- a/README.md +++ b/README.md @@ -41,14 +41,6 @@ jobs: languages: "['python']" ``` - -**Inputs**: - -- _languages_: Optional input , 'typescript', 'python', 'ruby'. To set multiple languages, use the same syntax as you can see in the default value. -- _codeql-cfg-path_: Optional input to set path to a CodeQL config file. -- _build-command_: Optional input to specify manual build command. The multiline syntax is supported - - @@ -56,7 +48,7 @@ jobs: | ---------------------- | ---------------------------------------------------------------------------------------------- | ---------------| ------------- | | **`languages`** | Languages for CodeQL check. Supported values are: 'cpp', 'csharp', 'go', 'java', 'javascript' | **['python']** | **false** | | **`codeql-cfg-path`** | Path to a CodeQL config file | | **false** | -| **`build-command`** | Manual build command | | **false** | +| **`build-command`** | Manual build command. The multiline syntax is supported | | **false** | Referenece: -- cgit v1.2.3 From 6e6b3310bf5e293ff3879ca2d2cda31dea9c35cd Mon Sep 17 00:00:00 2001 From: kumvijaya Date: Wed, 8 May 2024 14:13:52 +0530 Subject: T6315: added reusable codeql workflow documentation --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 74e1b92..285369a 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,9 @@ This keeps all the reusable github action workflows for vyos -**codeql-analysis**: -This performs codeql analysis on the invoking repo using given inputs. +## codeql-analysis ## + +This reusable workflow performs codeql analysis on the invoking repo using given inputs. This performs below: -- cgit v1.2.3 From 2c31d715f5bf9b44450b15e9a05bc0d070b0c12f Mon Sep 17 00:00:00 2001 From: kumvijaya Date: Wed, 8 May 2024 16:04:25 +0530 Subject: T6315: fixed PR review comments on repo pr template and workflows --- .github/PULL_REQUEST_TEMPLATE.md | 13 ------------- .github/labeler.yml | 3 --- .github/workflows/pull-request-labels.yml | 1 - .github/workflows/pull-request-management.yml | 2 +- .github/workflows/pull-request-message-check.yml | 2 +- 5 files changed, 2 insertions(+), 19 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index cd348ea..4c8266c 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -14,7 +14,6 @@ the box, please use [x] - [ ] New feature (non-breaking change which adds functionality) - [ ] Code style update (formatting, renaming) - [ ] Refactoring (no functional changes) -- [ ] Migration from an old Vyatta component to vyos-1x, please link to related PR inside obsoleted component - [ ] Other (please describe): ## Related Task(s) @@ -24,9 +23,6 @@ the box, please use [x] ## Related PR(s) -## Component(s) name - - ## Proposed changes @@ -40,21 +36,12 @@ like this ``` --> -## Smoketest result - - ## Checklist: - [ ] I have read the [**CONTRIBUTING**](https://github.com/vyos/vyos-1x/blob/current/CONTRIBUTING.md) document - [ ] I have linked this PR to one or more Phabricator Task(s) -- [ ] I have run the components [**SMOKETESTS**](https://github.com/vyos/vyos-1x/tree/current/smoketest/scripts/cli) if applicable - [ ] My commit headlines contain a valid Task id - [ ] My change requires a change to the documentation - [ ] I have updated the documentation accordingly diff --git a/.github/labeler.yml b/.github/labeler.yml index e0b9ee4..3a16d27 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -4,9 +4,6 @@ equuleus: current: - any: - base-branch: 'current' -crux: - - any: - - base-branch: 'crux' sagitta: - any: - base-branch: 'sagitta' diff --git a/.github/workflows/pull-request-labels.yml b/.github/workflows/pull-request-labels.yml index 43856be..31e1bd9 100644 --- a/.github/workflows/pull-request-labels.yml +++ b/.github/workflows/pull-request-labels.yml @@ -5,7 +5,6 @@ on: pull_request_target: branches: - current - - crux - equuleus - sagitta diff --git a/.github/workflows/pull-request-management.yml b/.github/workflows/pull-request-management.yml index 3a855c1..9918649 100644 --- a/.github/workflows/pull-request-management.yml +++ b/.github/workflows/pull-request-management.yml @@ -5,7 +5,7 @@ on: pull_request: branches: - current - - crux + - sagitta - equuleus jobs: diff --git a/.github/workflows/pull-request-message-check.yml b/.github/workflows/pull-request-message-check.yml index 8c206a5..aee2dec 100644 --- a/.github/workflows/pull-request-message-check.yml +++ b/.github/workflows/pull-request-message-check.yml @@ -5,7 +5,7 @@ on: pull_request: branches: - current - - crux + - sagitta - equuleus jobs: -- cgit v1.2.3 From 84627dba07a4c0c7b4f4e791427b7ce108bf3aac Mon Sep 17 00:00:00 2001 From: kumvijaya Date: Wed, 8 May 2024 16:15:02 +0530 Subject: T6315: fixed PR review comments on runner usage --- .github/workflows/pull-request-management.yml | 2 +- .github/workflows/pull-request-message-check.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pull-request-management.yml b/.github/workflows/pull-request-management.yml index 9918649..34c9c45 100644 --- a/.github/workflows/pull-request-management.yml +++ b/.github/workflows/pull-request-management.yml @@ -11,7 +11,7 @@ on: jobs: j2lint: name: Validate j2 files - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 timeout-minutes: 2 diff --git a/.github/workflows/pull-request-message-check.yml b/.github/workflows/pull-request-message-check.yml index aee2dec..2487879 100644 --- a/.github/workflows/pull-request-message-check.yml +++ b/.github/workflows/pull-request-message-check.yml @@ -11,7 +11,7 @@ on: jobs: check-pr-title: name: Check pull request title - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 timeout-minutes: 2 -- cgit v1.2.3 From cae4d78dbf5144a5d6e4a12df39e7e68419e89fc Mon Sep 17 00:00:00 2001 From: kumvijaya Date: Wed, 8 May 2024 16:20:10 +0530 Subject: T6315: added scripts for pr check --- scripts/check-pr-title-and-commit-messages.py | 51 ++++++++++ scripts/override-default | 140 ++++++++++++++++++++++++++ scripts/transclude-template | 50 +++++++++ 3 files changed, 241 insertions(+) create mode 100644 scripts/check-pr-title-and-commit-messages.py create mode 100644 scripts/override-default create mode 100644 scripts/transclude-template diff --git a/scripts/check-pr-title-and-commit-messages.py b/scripts/check-pr-title-and-commit-messages.py new file mode 100644 index 0000000..001f6cf --- /dev/null +++ b/scripts/check-pr-title-and-commit-messages.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +import re +import sys +import time + +import requests + +# Use the same regex for PR title and commit messages for now +title_regex = r'^(([a-zA-Z\-_.]+:\s)?)T\d+:\s+[^\s]+.*' +commit_regex = title_regex + +def check_pr_title(title): + if not re.match(title_regex, title): + print("PR title '{}' does not match the required format!".format(title)) + print("Valid title example: T99999: make IPsec secure") + sys.exit(1) + +def check_commit_message(title): + if not re.match(commit_regex, title): + print("Commit title '{}' does not match the required format!".format(title)) + print("Valid title example: T99999: make IPsec secure") + sys.exit(1) + +if __name__ == '__main__': + if len(sys.argv) < 2: + print("Please specify pull request URL!") + sys.exit(1) + + # There seems to be a race condition that causes this scripts to receive + # an incomplete PR object that is missing certain fields, + # which causes temporary CI failures that require re-running the script + # + # It's probably better to add a small delay to prevent that + time.sleep(5) + + # Get the pull request object + pr = requests.get(sys.argv[1]).json() + if "title" not in pr: + print("The PR object does not have a title field!") + print("Did not receive a valid pull request object, please check the URL!") + sys.exit(1) + + check_pr_title(pr["title"]) + + # Get the list of commits + commits = requests.get(pr["commits_url"]).json() + for c in commits: + # Retrieve every individual commit and check its title + co = requests.get(c["url"]).json() + check_commit_message(co["commit"]["message"]) diff --git a/scripts/override-default b/scripts/override-default new file mode 100644 index 0000000..5058e79 --- /dev/null +++ b/scripts/override-default @@ -0,0 +1,140 @@ +#!/usr/bin/env python3 +# +# override-default: preprocessor for XML interface definitions to interpret +# redundant entries (relative to path) with tag 'defaultValue' as an override +# directive. Must be called before build-command-templates, as the schema +# disallows redundancy. +# +# Copyright (C) 2021 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 or later as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# + +# Use lxml xpath capability to find multiple elements with tag defaultValue +# relative to path; replace and remove to override the value. + +import sys +import glob +import logging +from copy import deepcopy +from lxml import etree + +debug = False + +logger = logging.getLogger(__name__) +logs_handler = logging.StreamHandler() +logger.addHandler(logs_handler) + +if debug: + logger.setLevel(logging.DEBUG) +else: + logger.setLevel(logging.INFO) + +def clear_empty_path(el): + # on the odd chance of interleaved comments + tmp = [l for l in el if isinstance(l.tag, str)] + if not tmp: + p = el.getparent() + p.remove(el) + clear_empty_path(p) + +def override_element(l: list): + """ + Allow multiple override elements; use the final one (in document order). + """ + if len(l) < 2: + logger.debug("passing list of single element to override_element") + return + + # assemble list of leafNodes of overriding defaultValues, for later removal + parents = [] + for el in l[1:]: + parents.append(el.getparent()) + + # replace element with final override + l[0].getparent().replace(l[0], l[-1]) + + # remove all but overridden element + for el in parents: + tmp = el.getparent() + tmp.remove(el) + clear_empty_path(tmp) + +def merge_remaining(l: list, elementtree): + """ + Merge (now) single leaf node containing 'defaultValue' with leaf nodes + of same path and no 'defaultValue'. + """ + for p in l: + p = p.split() + path_str = f'/interfaceDefinition/*' + path_list = [] + for i in range(len(p)): + path_list.append(f'[@name="{p[i]}"]') + path_str += '/children/*'.join(path_list) + rp = elementtree.xpath(path_str) + if len(rp) > 1: + for el in rp[1:]: + # in practice there will only be one child of the path, + # either defaultValue or Properties, since + # override_element() has already run + for child in el: + rp[0].append(deepcopy(child)) + tmp = el.getparent() + tmp.remove(el) + clear_empty_path(tmp) + +def collect_and_override(dir_name): + """ + Collect elements with defaultValue tag into dictionary indexed by name + attributes of ancestor path. + """ + for fname in glob.glob(f'{dir_name}/*.xml'): + tree = etree.parse(fname) + root = tree.getroot() + defv = {} + + xpath_str = '//defaultValue' + xp = tree.xpath(xpath_str) + + for element in xp: + ap = element.xpath('ancestor::*[@name]') + ap_name = [el.get("name") for el in ap] + ap_path_str = ' '.join(ap_name) + defv.setdefault(ap_path_str, []).append(element) + + for k, v in defv.items(): + if len(v) > 1: + logger.info(f"overriding default in path '{k}'") + override_element(v) + + to_merge = list(defv) + merge_remaining(to_merge, tree) + + revised_str = etree.tostring(root, encoding='unicode', pretty_print=True) + + with open(f'{fname}', 'w') as f: + f.write(revised_str) + +def main(): + if len(sys.argv) < 2: + logger.critical('Must specify XML directory!') + sys.exit(1) + + dir_name = sys.argv[1] + + collect_and_override(dir_name) + +if __name__ == '__main__': + main() diff --git a/scripts/transclude-template b/scripts/transclude-template new file mode 100644 index 0000000..5c6668a --- /dev/null +++ b/scripts/transclude-template @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +# +# transclude-template: preprocessor for XML interface definitions to +# interpret #include statements to include nested XML fragments and +# snippets in documents. +# +# Copyright (C) 2021 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 or later as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# + +import os +import re +import sys + +regexp = re.compile(r'^ *#include <(.+)>$') + +def parse_file(filename): + lines = "" + with open(filename, 'r') as f: + while True: + line = f.readline() + if line: + result = regexp.match(line) + if result: + lines += parse_file(os.path.join(directory, result.group(1))) + else: + lines += line + else: + return lines + +if __name__ == '__main__': + if len(sys.argv) < 2: + print('Must specify XML file!', file=sys.stderr) + sys.exit(1) + filename = sys.argv[1] + directory = os.path.dirname(os.path.abspath(filename)) + print(parse_file(filename)) + -- cgit v1.2.3 From d8bc71a13f19258270364dc7b3d54fb7c52a67b7 Mon Sep 17 00:00:00 2001 From: kumvijaya Date: Wed, 8 May 2024 16:25:18 +0530 Subject: T6315: updated scripts permissions --- scripts/check-pr-title-and-commit-messages.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 scripts/check-pr-title-and-commit-messages.py diff --git a/scripts/check-pr-title-and-commit-messages.py b/scripts/check-pr-title-and-commit-messages.py old mode 100644 new mode 100755 -- cgit v1.2.3