I've got hundreds of Arduino libraries that we maintain in my 'libraries' folder, and it can very quickly get difficult to maintain them. There's 4 issues I spent an inordinate amount of time on!
First issue is that whenever a library is updated, the IDE does not delete the previous version, but keeps both around...after a few years this ends in a lot of cruft.
Secondly, libraries downloaded via the IDE are placed into a folder in the format arduino_nnnnn where n is a number, so you don't really know what library is inside it, making the first issue hard to clean up
Thirdly - for Adafruit libraries, they are git clone'd into the library folder which means I sometimes end up in a branch that I'm working on, and then submit a PR. Days later I've forgotten to change back into my main branch, losing out on any merged updates!
Finally - there's constant updates to our libraries, and the IDE does alert me when this happens - which means I need to do a git pull - but in hundreds of libraries this takes a long time to go to each folder and run the git pull command.
THUS a script! This python code does a number of clean up tasks, I only have to run it a few times a week and it will keep my arduino libraries folder nice n tidy. Thanks to ChatGPT4 Sept 25 for the assist, it made the scripting faster (note I did make some changes beyond what GPT4 wrote)
SSH_KEY_PATH = "C:\\\\Users\\\\ladyada\\\\github.ppk" # change path to your ppk or other pubkey file!
import os
import re
import subprocess
def sanitize_name(name):
return re.sub(r'[^a-zA-Z0-9]', '_', name)
def git_actions(folder_path):
os.environ['GIT_SSH_COMMAND'] = f'plink -i {SSH_KEY_PATH}'
git_folder = os.path.join(folder_path, ".git")
if os.path.exists(git_folder):
current_branch = subprocess.getoutput(f"git -C {folder_path} rev-parse --abbrev-ref HEAD").strip()
if current_branch not in ['main', 'master']:
print(f"Alert: {folder_path} is on a non-main/master branch: {current_branch}")
else:
result = subprocess.run(["git", "-C", folder_path, "pull", "origin", current_branch], capture_output=True, text=True)
if "Already up to date." in result.stdout:
pass # print(f"{os.path.basename(folder_path)}: Up to date")
elif "Fast-forward" in result.stdout:
print(f"{os.path.basename(folder_path)}: Updated")
else:
print(folder_path, result.stdout, result.stderr)
def rename_arduino_folders(root_folder):
for folder in os.listdir(root_folder):
folder_path = os.path.join(root_folder, folder)
# Handle Git actions on every folder
git_actions(folder_path)
if folder.startswith("arduino_"):
properties_file = os.path.join(folder_path, "library.properties")
if os.path.exists(properties_file):
with open(properties_file, 'r') as f:
lines = f.readlines()
properties = {}
for line in lines:
try:
key, value = line.strip().split('=', 1)
properties[key] = value
except ValueError:
continue
if 'name' in properties and 'version' in properties:
new_name = f"{sanitize_name(properties['name'])}_{sanitize_name(properties['version'])}"
new_folder_path = os.path.join(root_folder, new_name)
if folder_path != new_folder_path:
confirmation = input(f"About to rename {folder} to {new_name}. Press return to continue.")
if confirmation == '':
os.rename(folder_path, new_folder_path)
print(f"Renamed {folder} to {new_name}")
if __name__ == "__main__":
arduino_lib_folder = '.'
rename_arduino_folders(arduino_lib_folder)
This page (Arduino Library Folder Cleanup) was last updated on March 08, 2024.