Skip to content

Ticket 7914 update script #241

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 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
2e8fe94
First draft of update_scripts.py
Feb 16, 2024
280ed72
rework to the script
Feb 19, 2024
c7c30f9
Change update script to create branch if doesn't exist, fetch and mer…
FreddieAkeroyd Feb 21, 2024
0592705
Merging
FreddieAkeroyd Feb 21, 2024
929b22a
Merge if name == main call
FreddieAkeroyd Feb 21, 2024
069be72
fix path in pythonwrap
FreddieAkeroyd Feb 21, 2024
694b790
Change push to set upstream origin
FreddieAkeroyd Feb 22, 2024
209e8a5
Add Jenkinsfile, bat file, and change user prompt behaviour to defaul…
FreddieAkeroyd Feb 22, 2024
f09efdd
Change path to call_update_scripts.bat
FreddieAkeroyd Feb 22, 2024
71bad20
Change path to pythonwrap.bat for Jenkins pipeline
FreddieAkeroyd Feb 22, 2024
4f0d6e7
Change path to update_scripts.py for Jenkins pipeline
FreddieAkeroyd Feb 22, 2024
c5812e9
Change path to root for Jenkins pipeline
FreddieAkeroyd Feb 22, 2024
bdfff5f
change merge master to merge origin/master
esmith1729 Feb 27, 2024
d2eb0fa
Add /d after cd, change update_scripts.bat structure
FreddieAkeroyd Feb 28, 2024
ebf96d1
revert update_scripts.bat to previous version
FreddieAkeroyd Feb 28, 2024
d2d3023
delete unnecessary file
esmith1729 Feb 29, 2024
a1af076
Merge branch 'ticket_7914_update_script' of https://github.com/ISISCo…
esmith1729 Feb 29, 2024
2299de4
Created general method for automatic merge in git_tasks.py; implement…
esmith1729 Feb 29, 2024
11c5696
Add try/except clause to body of update_scripts.py
esmith1729 Feb 29, 2024
ddb20b0
Add shell=True
esmith1729 Feb 29, 2024
09a9288
Add empty args to GitTasks
esmith1729 Feb 29, 2024
0248bf4
Add @task decorator to automatic merge function, in order to give it …
esmith1729 Mar 1, 2024
0f11479
Fixed typo of "confirm_steps"
esmith1729 Mar 1, 2024
13db87a
Changed "confirm_step" to "confirm_steps" in task.py
esmith1729 Mar 1, 2024
1a92c92
Changed confirm_steps back to confirm_step. Added self.prompt to GitT…
esmith1729 Mar 1, 2024
b088a85
Added git.Repo instance to git_tasks.py
esmith1729 Mar 1, 2024
ae045ad
removed {} from Scripts base dir call
esmith1729 Mar 1, 2024
252d972
Removed comments, update manual prompt message of automatic_merge method
esmith1729 Mar 1, 2024
69fc533
Remove extra ), put %COMPUTERNAME% into fstring
esmith1729 Mar 1, 2024
f9b848d
Add print statement to test COMPUTERNAME
esmith1729 Mar 1, 2024
a8345b0
Created global for COMPUTERNAME from BaseTasks' get_machine_name() me…
esmith1729 Mar 1, 2024
e9af826
Put manual prompt into right spot
esmith1729 Mar 1, 2024
df7cbf6
Made task prompt message more clear
esmith1729 Mar 1, 2024
e9ab25a
Added cd /d and git checkout call in git_tasks steps of automatic merge
esmith1729 Mar 1, 2024
bba76d9
Merge remote-tracking branch 'origin/master' into ticket_7914_update_…
FreddieAkeroyd Mar 9, 2025
a1c8871
ruff
FreddieAkeroyd Mar 9, 2025
5fba955
Merge remote-tracking branch 'origin/master' into ticket_7914_update_…
FreddieAkeroyd May 20, 2025
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
56 changes: 56 additions & 0 deletions installation_and_upgrade/Jenkinsfile_update_scripts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!groovy

pipeline {
// using same agnert as ConfigCheck job
agent {
label {
label 'ConfigCheck'
}
}

triggers {
cron('H 8 * * *')
}

environment {
SSH_CREDENTIALS = credentials('SSH')
TEST_INSTRUMENT_LIST = "${TEST_INSTRUMENT_LIST}"
USE_TEST_INSTRUMENT_LIST = "${USE_TEST_INSTRUMENT_LIST}"
DEBUG_MODE = "${DEBUG_MODE}"
}

stages {
stage('Checkout') {
steps {
timeout(time: 2, unit: 'HOURS') {
retry(5) {
checkout scm
}
}
}
}

stage('Check Instrument/Scripts can have master merged into it') {
steps {
echo 'Check Instrument/Scripts can have a clean merge master, and then merge master into it'
timeout(time: 1, unit: 'HOURS') {
bat '''
call installation_and_upgrade/call_update_scripts.bat
'''
}
}
}
}

post {
always {
logParser([
projectRulePath: 'parse_rules',
parsingRulesPath: '',
showGraphs: true,
unstableOnWarning: true,
useProjectRule: true,
])
}
}
}
5 changes: 5 additions & 0 deletions installation_and_upgrade/call_update_scripts.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
setlocal

call
installation_and_upgrade\pythonwrap.bat .\installation_and_upgrade installation_and_upgrade\ibex_install_utils\tasks\update_scripts.py
if %errorlevel% neq 0 exit /b %errorlevel%
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

INSTRUMENT_BASE_DIR = os.path.join("C:\\", "Instrument")
SCRIPTS_BASE_DIR = os.path.join(INSTRUMENT_BASE_DIR, "Scripts")
APPS_BASE_DIR = os.path.join(INSTRUMENT_BASE_DIR, "Apps")
VAR_DIR = os.path.join(INSTRUMENT_BASE_DIR, "var")
INST_SHARE_AREA = os.path.join(r"\\isis.cclrc.ac.uk", "inst$")
Expand Down
89 changes: 88 additions & 1 deletion installation_and_upgrade/ibex_install_utils/tasks/git_tasks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import os
import re
import subprocess

import git

from ibex_install_utils.exceptions import ErrorInTask
from ibex_install_utils.task import task
from ibex_install_utils.tasks import BaseTasks
from ibex_install_utils.tasks.common_paths import EPICS_PATH
from ibex_install_utils.tasks.common_paths import EPICS_PATH, SETTINGS_CONFIG_PATH
from ibex_install_utils.user_prompt import UserPrompt

Check failure on line 11 in installation_and_upgrade/ibex_install_utils/tasks/git_tasks.py

View workflow job for this annotation

GitHub Actions / call-workflow / ruff

Ruff (I001)

installation_and_upgrade/ibex_install_utils/tasks/git_tasks.py:1:1: I001 Import block is un-sorted or un-formatted


class GitTasks(BaseTasks):
Expand Down Expand Up @@ -86,3 +91,85 @@
except subprocess.CalledProcessError as e:
print(f"Error checking out to new release branch and push: {e}")
print("Branch may previously exist either locally or remotely - intervention required")

# something for the future in case creating new branch fails
# maybe one exists we want to use?
# try:
# subprocess.check_call(f"cd {EPICS_PATH} && git checkout %COMPUTERNAME%",
# shell=True)
# print("Switched to existing release branch")
# except subprocess.CalledProcessError as e:
# print(f"Error switching to existing release branch and push: {e}")

# Method to check that the machine name matches a git branch
def inst_name_matches_branch(self) -> None:
repo = git.Repo(os.path.join(SETTINGS_CONFIG_PATH, BaseTasks._get_machine_name()))
if repo.active_branch.name != BaseTasks._get_machine_name():
print(
f"Git branch, '{repo.active_branch}', is not the same as machine name ,"
f"'{BaseTasks._get_machine_name()}' "
)
raise ErrorInTask("Git branch is not the same as machine name")

@task("Attempt automatic merge of one branch into another")
def automatic_merge_of_git_remote(
self, branch_to_merge_from: str, branch_to_merge_to: str, dir: str
) -> None:
f"""
Attempt an automatic merge of one branch
{branch_to_merge_from} to another, {branch_to_merge_to} in {dir}
"""
Comment on lines +118 to +121

Check notice

Code scanning / CodeQL

Statement has no effect Note

This statement has no effect.

Copilot Autofix

AI 18 days ago

To fix the issue, the multi-line f-string should be converted into a proper docstring for the automatic_merge_of_git_remote method. This involves moving the string to immediately follow the def line of the method. Since the string is intended to describe the method's purpose and parameters, it should be formatted as a standard docstring (triple-quoted string) without the f prefix, as no variable interpolation is required.


Suggested changeset 1
installation_and_upgrade/ibex_install_utils/tasks/git_tasks.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/installation_and_upgrade/ibex_install_utils/tasks/git_tasks.py b/installation_and_upgrade/ibex_install_utils/tasks/git_tasks.py
--- a/installation_and_upgrade/ibex_install_utils/tasks/git_tasks.py
+++ b/installation_and_upgrade/ibex_install_utils/tasks/git_tasks.py
@@ -117,5 +117,9 @@
     ) -> None:
-        f"""
-        Attempt an automatic merge of one branch
-           {branch_to_merge_from} to another, {branch_to_merge_to} in {dir}
+        """
+        Attempt an automatic merge of one branch to another in a specified directory.
+        
+        Args:
+            branch_to_merge_from (str): The name of the branch to merge from.
+            branch_to_merge_to (str): The name of the branch to merge to.
+            dir (str): The directory where the git repository is located.
         """
EOF
@@ -117,5 +117,9 @@
) -> None:
f"""
Attempt an automatic merge of one branch
{branch_to_merge_from} to another, {branch_to_merge_to} in {dir}
"""
Attempt an automatic merge of one branch to another in a specified directory.

Args:
branch_to_merge_from (str): The name of the branch to merge from.
branch_to_merge_to (str): The name of the branch to merge to.
dir (str): The directory where the git repository is located.
"""
Copilot is powered by AI and may make mistakes. Always verify output.
manual_prompt = (
f"Merge the {branch_to_merge_from} branch into the {branch_to_merge_to} branch. "
f"From {dir} run:\n"
" 0. Clean up any in progress merge (e.g. git merge --abort)\n"
f" 1. git checkout {branch_to_merge_from}\n"
" 2. git pull\n"
f" 3. git checkout {branch_to_merge_to}\n"
f" 4. git merge {branch_to_merge_from}\n"
" 5. Resolve any merge conflicts\n"
" 6. git push\n"
)

automatic_prompt = "Attempt automatic merge?"
repo = git.Repo(dir)
if self.prompt.confirm_step(automatic_prompt):
try:
try:
subprocess.check_call(f"cd /d {dir}", shell=True)
print(f" cd: {dir}")
print(f" checkout: {repo.git.checkout(f'{branch_to_merge_to}')}")
print(f" fetch: {repo.git.fetch()}")
print(f" merge: {repo.git.merge(f'{branch_to_merge_from}')}")
except git.GitCommandError as e:
# do gc and prune to remove issues with stale references
# this does a pack that takes a while, hence not do every time
print(f"Retrying git operations after a prune due to {e}")
print(f" gc: {repo.git.gc(prune='now')}")
print(f" prune: {repo.git.remote('prune', 'origin')}")
print(f" fetch: {repo.git.fetch()}")
print(f" merge: {repo.git.merge(f'{branch_to_merge_from}')}")
# no longer push let the instrument do that on start up if needed
except git.GitCommandError as e:
print(f"Error doing automatic merge, please perform the merge manually: {e}")
self.prompt.prompt_and_raise_if_not_yes(manual_prompt)
else:
self.prompt.prompt_and_raise_if_not_yes(manual_prompt)


if __name__ == "__main__":
"""For running task standalone
Must be called with pythonpath set to `<exact path on your pc>/installation_and_upgrade`
as that is the root of this module and all our imports work that way.

This effectively means to call
`set PYTHONPATH=. && python ibex_install_utils/tasks/backup_tasks.py`
from the installation_and_upgrade directory in terminal.
"""
print("")

#! Copying older backups to share will likely fail on developer machines
prompt = UserPrompt(False, True)

git_instance = GitTasks(prompt, "", "", "", "")
git_instance.automatic_merge_of_git_remote("branch1", "branch2", "C:/test")
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import subprocess

from ibex_install_utils.task import task
from ibex_install_utils.tasks import BaseTasks
from ibex_install_utils.tasks.git_tasks import GitTasks
from ibex_install_utils.tasks.common_paths import SCRIPTS_BASE_DIR
from ibex_install_utils.user_prompt import UserPrompt

Check failure on line 7 in installation_and_upgrade/ibex_install_utils/tasks/update_scripts.py

View workflow job for this annotation

GitHub Actions / call-workflow / ruff

Ruff (I001)

installation_and_upgrade/ibex_install_utils/tasks/update_scripts.py:1:1: I001 Import block is un-sorted or un-formatted

COMPUTERNAME = BaseTasks._get_machine_name()

class UpdateScripts(BaseTasks):

@task(f"Update Instrument/Scripts repo by merging 'origin/master' branch into {COMPUTERNAME} branch?")

Check failure on line 13 in installation_and_upgrade/ibex_install_utils/tasks/update_scripts.py

View workflow job for this annotation

GitHub Actions / call-workflow / ruff

Ruff (E501)

installation_and_upgrade/ibex_install_utils/tasks/update_scripts.py:13:101: E501 Line too long (106 > 100)
def update_scripts(self):

Check failure on line 14 in installation_and_upgrade/ibex_install_utils/tasks/update_scripts.py

View workflow job for this annotation

GitHub Actions / call-workflow / ruff

Ruff (ANN201)

installation_and_upgrade/ibex_install_utils/tasks/update_scripts.py:14:9: ANN201 Missing return type annotation for public function `update_scripts`
try:
subprocess.check_call(f"cd /d {SCRIPTS_BASE_DIR}", shell=True)
git_instance = GitTasks(self.prompt,'','','','')
git_instance.automatic_merge_of_git_remote("origin/master", COMPUTERNAME, SCRIPTS_BASE_DIR)

Check failure on line 18 in installation_and_upgrade/ibex_install_utils/tasks/update_scripts.py

View workflow job for this annotation

GitHub Actions / call-workflow / ruff

Ruff (E501)

installation_and_upgrade/ibex_install_utils/tasks/update_scripts.py:18:101: E501 Line too long (103 > 100)
except subprocess.CalledProcessError as e:
print(f"{e}")

if __name__ == "__main__":
prompt = UserPrompt(True,False)
print(COMPUTERNAME)
UpdateScripts(prompt, "", "", "", "", "").update_scripts()
5 changes: 5 additions & 0 deletions installation_and_upgrade/pythonwrap.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@ECHO OFF
setlocal
set PYTHONPATH=%1
C:/Instrument/Apps/Python3/python3.exe %2 %3
endlocal
Loading