Last active
September 9, 2021 11:24
-
-
Save shravanasati/9e84b72e17e094661ce412017822f477 to your computer and use it in GitHub Desktop.
A simple python script to sync the wallpapers in the ~/.iris/wallpapers dir with a remote repo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import os | |
from pathlib import Path | |
from typing import List | |
from datetime import datetime | |
import sys | |
import shlex | |
def run(command: str) -> None: | |
subprocess.run( | |
shlex.split(command), | |
stdout=sys.stdout, | |
stderr=sys.stderr, | |
check=True) | |
def get_pushed_wallpapers() -> List[str]: | |
wp = Path(os.path.expanduser("~")) / ".wallpaper_pusher.txt" | |
if wp.exists(): | |
with wp.open(mode="r") as f: | |
return [i for i in f.read().split("\n") if i.strip() != ""] | |
else: | |
wp.open(mode="w").close() | |
return [] | |
def get_wallpaper_dir() -> str: | |
return os.path.join(os.path.expanduser("~"), ".iris", "wallpapers") | |
def get_all_wallpapers() -> List[str]: | |
wallpaper_dir = get_wallpaper_dir() | |
all_wallpapers = [] | |
for i in os.listdir(wallpaper_dir): | |
if i.endswith(".jpg"): | |
all_wallpapers.append(os.path.join(get_wallpaper_dir(), i)) | |
return all_wallpapers | |
def write_new_wallpapers(all_wallpapers: List[str], pushed_wallpapers: List[str]) -> None: | |
wp = Path(os.path.expanduser("~")) / ".wallpaper_pusher.txt" | |
new_wallpapers = [w for w in all_wallpapers if w not in pushed_wallpapers] | |
with wp.open(mode="a") as f: | |
f.write(f"\n".join(new_wallpapers)) | |
def perform_git_operations() -> None: | |
cwd = os.getcwd() | |
os.chdir(get_wallpaper_dir()) | |
current_time = datetime.now().strftime("%d-%m-%Y %H:%M") | |
run("git add -A") | |
run(f"git commit -m '(automated) {current_time}: new wallpapers'") | |
run("git push") | |
os.chdir(cwd) | |
if __name__ == "__main__": | |
all_wallpapers = get_all_wallpapers() | |
pushed_wallpapers = get_pushed_wallpapers() | |
push = False | |
count = 0 | |
for i in all_wallpapers: | |
if i not in pushed_wallpapers: | |
count += 1 | |
if count > 0: | |
push = True | |
if push: | |
print( | |
f"{count} new wallpapers detected, pushing them.") | |
perform_git_operations() | |
write_new_wallpapers(all_wallpapers, pushed_wallpapers) | |
print("task executed successfully!") | |
else: | |
print("No new wallpapers detected.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment