|
| 1 | +import time |
| 2 | +import shutil |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +import os.path |
| 6 | +from git import Repo, Actor |
| 7 | +from watchdog.observers import Observer |
| 8 | +from watchdog.events import PatternMatchingEventHandler |
| 9 | + |
| 10 | +""" |
| 11 | + HerokuFileUpdaterPDF watches for creations and modifications of pdf files at the scheduled source and moves them to the destination. |
| 12 | +
|
| 13 | + Note: The destination should contain the new name Example: /path/some-new-name.pdf |
| 14 | +""" |
| 15 | + |
| 16 | + |
| 17 | +class HerokuFileUpdaterPDF(PatternMatchingEventHandler): |
| 18 | + patterns = ["*.pdf"] |
| 19 | + |
| 20 | + def __init__(self, author_name, author_email, message, git_directory, destinations): |
| 21 | + """ |
| 22 | + Construct a HerokuFileUpdaterPDF. |
| 23 | + """ |
| 24 | + super(HerokuFileUpdaterPDF, self).__init__() |
| 25 | + self.git_directory = git_directory |
| 26 | + self.destinations = destinations |
| 27 | + self.author_name = author_name |
| 28 | + self.author_email = author_email |
| 29 | + self.message = message |
| 30 | + |
| 31 | + def process(self, event): |
| 32 | + """ |
| 33 | + Process files modified or created. |
| 34 | +
|
| 35 | + event.event_type |
| 36 | + 'modified' | 'created' | 'moved' | 'deleted' |
| 37 | + event.is_directory |
| 38 | + True | False |
| 39 | + event.src_path |
| 40 | + path/to/observed/file |
| 41 | + """ |
| 42 | + # Print path and event type |
| 43 | + print(event.src_path, event.event_type) |
| 44 | + |
| 45 | + # Copy the file to the destination |
| 46 | + for destination in self.destinations: |
| 47 | + shutil.copy(event.src_path, destination) |
| 48 | + |
| 49 | + # Push to master |
| 50 | + self.git_process() |
| 51 | + |
| 52 | + def git_process(self): |
| 53 | + """ |
| 54 | + Push update to git. |
| 55 | + """ |
| 56 | + # Setup Repo. |
| 57 | + repo = Repo(self.git_directory) |
| 58 | + index = repo.index |
| 59 | + |
| 60 | + # Add files. |
| 61 | + index.add(self.destinations) |
| 62 | + |
| 63 | + # Commit. |
| 64 | + committer = Actor(self.author_name, self.author_email) |
| 65 | + index.commit(self.message, author=committer, committer=committer) |
| 66 | + |
| 67 | + # Push. |
| 68 | + for remote in repo.remotes: |
| 69 | + if (remote.name == "origin"): |
| 70 | + remote.push() |
| 71 | + break |
| 72 | + |
| 73 | + def on_modified(self, event): |
| 74 | + """ |
| 75 | + If a file following the patterns defined is modified, execute the following code. |
| 76 | + """ |
| 77 | + self.process(event) |
| 78 | + |
| 79 | + def on_created(self, event): |
| 80 | + """ |
| 81 | + If a file following the patterns defined is created, execute the following code. |
| 82 | + """ |
| 83 | + self.process(event) |
| 84 | + |
| 85 | + |
| 86 | +def main(): |
| 87 | + # Detect required arguments met. |
| 88 | + if (len(sys.argv) < 7): |
| 89 | + print("[ ERROR ] missing parameters") |
| 90 | + print( |
| 91 | + "HerokuFileUpdaterPDF.py author_name author_email /path/to/git/directory /path/to/watch/directory /path/to/destination/directory...") |
| 92 | + return |
| 93 | + |
| 94 | + # Parse arguments from command line. |
| 95 | + author_name = sys.argv[1] |
| 96 | + author_email = sys.argv[2] |
| 97 | + message = sys.argv[3] |
| 98 | + git_directory = sys.argv[4] |
| 99 | + source_path = sys.argv[5] |
| 100 | + destinations = sys.argv[6:] |
| 101 | + |
| 102 | + # Run WatchDog with HerokuFileUpdaterPDF. |
| 103 | + observer = Observer() |
| 104 | + observer.schedule(HerokuFileUpdaterPDF( |
| 105 | + author_name, author_email, message, git_directory, destinations), source_path) |
| 106 | + observer.start() |
| 107 | + |
| 108 | + # Print some initial information. |
| 109 | + print("Observation starting...") |
| 110 | + print("Ctrl-C to stop") |
| 111 | + |
| 112 | + # Run indefinitely. Stop if Ctrl-C. |
| 113 | + try: |
| 114 | + while(True): |
| 115 | + time.sleep(1) |
| 116 | + except KeyboardInterrupt: |
| 117 | + # Print that it has started quitting information. |
| 118 | + print("Quitting...") |
| 119 | + observer.stop() |
| 120 | + |
| 121 | + observer.join() |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + main() |
0 commit comments