Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f2796cd

Browse files
committed
build_parameters.py: use subprocess for git commands
1 parent af688fc commit f2796cd

1 file changed

Lines changed: 66 additions & 22 deletions

File tree

build_parameters.py

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@
4646
parser.add_argument('--vehicle', dest='single_vehicle', help="If you just want to copy to one vehicle, you can do this. Otherwise it will work for all vehicles (Copter, Plane, Rover, AntennaTracker, Sub, Blimp)") # noqa: E501
4747
args = parser.parse_args()
4848

49+
50+
# Parameters
51+
COMMITFILE = "git-version.txt"
52+
BASEURL = "https://firmware.ardupilot.org/"
53+
ALLVEHICLES = ["AntennaTracker", "Copter", "Plane", "Rover", "Sub", "Blimp"]
54+
VEHICLES = ALLVEHICLES
55+
# Filter out versions below this semantic version threshold.
56+
PARAM_PARSE_MINIMUM_VERSION = (3, 9, 0)
57+
58+
BASEPATH = ""
4959
error_count = 0
5060

5161

@@ -83,16 +93,50 @@ def format(self, record):
8393
'Connection': 'keep-alive'
8494
})
8595

86-
# Parameters
87-
COMMITFILE = "git-version.txt"
88-
BASEURL = "https://firmware.ardupilot.org/"
89-
ALLVEHICLES = ["AntennaTracker", "Copter", "Plane", "Rover", "Sub", "Blimp"]
90-
VEHICLES = ALLVEHICLES
9196

92-
# Filter out versions below this semantic version threshold.
93-
PARAM_PARSE_MINIMUM_VERSION = (3, 9, 0)
97+
def run_git(cmd, cwd=None, check=True, max_retries=3):
98+
"""Run git command with retry logic for lock conflicts"""
99+
if cwd is None:
100+
cwd = os.getcwd()
94101

95-
BASEPATH = ""
102+
for attempt in range(max_retries):
103+
try:
104+
debug(f"Running git command (attempt {attempt + 1}): {cmd}")
105+
result = subprocess.run(
106+
cmd.split(),
107+
cwd=cwd,
108+
capture_output=True,
109+
text=True,
110+
check=check,
111+
timeout=300 # 5 minute timeout
112+
)
113+
if result.stderr:
114+
debug(f"Git stderr: {result.stderr}")
115+
return result.stdout
116+
117+
except subprocess.CalledProcessError as e:
118+
# Check if it's a lock file issue
119+
if 'index.lock' in str(e.stderr) or 'Unable to create' in str(e.stderr):
120+
debug(f"Git lock detected on attempt {attempt + 1}, waiting git process to complete...")
121+
if attempt < max_retries - 1:
122+
import time
123+
time.sleep(3) # Wait a second before retry
124+
continue
125+
126+
error(f"Git command failed: {cmd}")
127+
error(f"Error: {e.stderr}")
128+
if check:
129+
raise
130+
131+
except subprocess.TimeoutExpired:
132+
error(f"Git command timed out: {cmd}")
133+
if check:
134+
raise
135+
136+
# If we get here, all retries failed
137+
error(f"Git command failed after {max_retries} attempts: {cmd}")
138+
if check:
139+
raise subprocess.CalledProcessError(1, cmd)
96140

97141

98142
def rst_has_duplicate_labels(filepath: str) -> bool:
@@ -315,19 +359,20 @@ def setup():
315359

316360
try:
317361
# Goes to ardupilot folder and clean it and update to make sure that is the most recent one.
318-
debug("Recovering from a previous run...")
319-
os.chdir(args.gitFolder)
320-
os.system("git reset --hard HEAD")
321-
os.system("git clean -f -d")
322-
os.system("git checkout -f master")
323-
os.system("git fetch origin master")
324-
os.system("git reset --hard origin/master")
325-
os.system("git pull")
362+
repo_path = os.path.abspath(args.gitFolder)
326363
global BASEPATH
327-
BASEPATH = os.getcwd()
364+
BASEPATH = repo_path
365+
debug(f"Recovering from a previous run in {repo_path}")
366+
367+
run_git("git reset --hard HEAD", cwd=repo_path)
368+
run_git("git clean -f -d", cwd=repo_path)
369+
run_git("git checkout -f master", cwd=repo_path)
370+
run_git("git fetch origin master", cwd=repo_path)
371+
run_git("git reset --hard origin/master", cwd=repo_path)
372+
run_git("git pull", cwd=repo_path)
373+
328374
check_temp_folders()
329-
os.chdir(BASEPATH) # Need to call git command correctly
330-
except Exception as e:
375+
except (subprocess.CalledProcessError, OSError) as e:
331376
error(f"ArduPilot Repo folder not found (cd {args.gitFolder} failed)")
332377
error(e)
333378
sys.exit(1)
@@ -573,9 +618,8 @@ def replace_anchors(source_file, dest_file, version_tag):
573618
# Checkout an Commit ID in order to get its parameters
574619
try:
575620
debug(f"Git checkout on {vehicle} version {version} id {commit_id}")
576-
os.system(f"git checkout --force {commit_id}")
577-
578-
except Exception as e:
621+
run_git(f"git checkout --force {commit_id}", cwd=BASEPATH, check=True)
622+
except subprocess.CalledProcessError as e:
579623
error(f"GIT checkout error: {e}")
580624
sys.exit(1)
581625
debug("")

0 commit comments

Comments
 (0)