Skip to content

Commit

Permalink
Allow multiple patch directories per environment
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Jan 6, 2024
1 parent 2608080 commit 6556268
Show file tree
Hide file tree
Showing 2 changed files with 272 additions and 266 deletions.
62 changes: 33 additions & 29 deletions pio-scripts/patch_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
Import("env")

def getPatchPath(env):
return os.path.join(env["PROJECT_DIR"], "patches", env.GetProjectOption('custom_patches'))
patchList = []
for patch in env.GetProjectOption('custom_patches').split(","):
patchList.append(os.path.join(env["PROJECT_DIR"], "patches", patch))
return patchList

def is_tool(name):
"""Check whether `name` is on PATH and marked as executable."""
Expand Down Expand Up @@ -44,35 +47,36 @@ def main():
print('Git not found. Will not apply custom patches!')
return

directory = getPatchPath(env)
if (not os.path.isdir(directory)):
print('Patch directory not found: ' + directory)
return

for file in os.listdir(directory):
if (not file.endswith('.patch')):
continue

fullPath = os.path.join(directory, file)
preparePath = fullPath + '.prepare'
replaceInFile(fullPath, preparePath, '$$$env$$$', env['PIOENV'])
print('Working on patch: ' + fullPath + '... ', end='')
directories = getPatchPath(env)
for directory in directories:
if (not os.path.isdir(directory)):
print('Patch directory not found: ' + directory)
return

for file in os.listdir(directory):
if (not file.endswith('.patch')):
continue

fullPath = os.path.join(directory, file)
preparePath = fullPath + '.prepare'
replaceInFile(fullPath, preparePath, '$$$env$$$', env['PIOENV'])
print('Working on patch: ' + fullPath + '... ', end='')

# Check if patch was already applied
process = subprocess.run(['git', 'apply', '--reverse', '--check', preparePath], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if (process.returncode == 0):
print('already applied')
os.remove(preparePath)
continue

# Apply patch
process = subprocess.run(['git', 'apply', preparePath])
if (process.returncode == 0):
print('applied')
else:
print('failed')

# Check if patch was already applied
process = subprocess.run(['git', 'apply', '--reverse', '--check', preparePath], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if (process.returncode == 0):
print('already applied')
os.remove(preparePath)
continue

# Apply patch
process = subprocess.run(['git', 'apply', preparePath])
if (process.returncode == 0):
print('applied')
else:
print('failed')

os.remove(preparePath)


main()
main()
Loading

0 comments on commit 6556268

Please sign in to comment.