33import subprocess
44import sys
55import os .path
6+ import argparse
67from git import Repo , Actor
78from watchdog .observers import Observer
89from watchdog .events import PatternMatchingEventHandler
@@ -44,8 +45,10 @@ def process(self, event):
4445 print (event .src_path , event .event_type )
4546
4647 # Copy the file to the destination
48+ print ("Copying files..." , end = "" )
4749 for destination in self .destinations :
4850 shutil .copy (event .src_path , destination )
51+ print ("Done" )
4952
5053 # Push to master
5154 self .git_process ()
@@ -55,22 +58,30 @@ def git_process(self):
5558 Push update to git.
5659 """
5760 # Setup Repo.
61+ print ("Getting Index..." , end = "" )
5862 repo = Repo (self .git_directory )
5963 index = repo .index
64+ print ("Done" )
6065
6166 # Add files.
67+ print ("Adding files..." , end = "" )
6268 index .add (self .destinations )
69+ print ("Done" )
6370
6471 # Commit.
72+ print ("Commiting..." , end = "" )
6573 committer = Actor (self .author_name , self .author_email )
6674 index .commit (self .commit_message ,
6775 author = committer , committer = committer )
76+ print ("Done" )
6877
6978 # Push.
79+ print ("Pushing to Git..." , end = "" )
7080 for remote in repo .remotes :
7181 if (remote .name == "origin" ):
7282 remote .push ()
7383 break
84+ print ("Done" )
7485
7586 def on_modified (self , event ):
7687 """
@@ -86,30 +97,34 @@ def on_created(self, event):
8697
8798
8899def main ():
89- # Detect required arguments met.
90- if (len (sys .argv ) < 7 ):
91- print ("[ ERROR ] missing parameters" )
92- print (
93- "FileUpdaterPDF.py author_name author_email commit_message /path/to/git/directory /path/to/watch/directory /path/to/destination/directory..." )
94- return
95-
96- # Parse arguments from command line.
97- author_name = sys .argv [1 ]
98- author_email = sys .argv [2 ]
99- commit_message = sys .argv [3 ]
100- git_directory = sys .argv [4 ]
101- source_path = sys .argv [5 ]
102- destinations = sys .argv [6 :]
100+ # Setup the argument parser.
101+ parser = argparse .ArgumentParser (
102+ description = "Watch a source directory for PDF modifications or creations, move PDF file to the destination directory, and push to Git" )
103+ parser .add_argument ("-n" , "--author_name" ,
104+ help = "author of the commit" , required = True )
105+ parser .add_argument ("-e" , "--author_email" ,
106+ help = "author's email" , required = True )
107+ parser .add_argument ("-m" , "--commit_message" ,
108+ help = "the message associated with the commit" , required = True )
109+ parser .add_argument ("-g" , "--git_directory" ,
110+ help = "the base Git directory of the destination" , required = True )
111+ parser .add_argument ("-s" , "--source_path" ,
112+ help = "the directory to watch" , required = True )
113+ parser .add_argument ("-d" , "--destinations" ,
114+ help = "the destination(s) of the file" , nargs = "+" )
115+
116+ # Parse arguments.
117+ arguments = parser .parse_args ()
103118
104119 # Run WatchDog with FileUpdaterPDF.
105120 observer = Observer ()
106121 observer .schedule (FileUpdaterPDF (
107- author_name , author_email , commit_message , git_directory , destinations ), source_path )
122+ arguments . author_name , arguments . author_email , arguments . commit_message , arguments . git_directory , arguments . destinations ), arguments . source_path )
108123 observer .start ()
109124
110125 # Print some initial information.
111- print ("Observation starting..." )
112126 print ("Ctrl-C to stop" )
127+ print ("Observation starting..." )
113128
114129 # Run indefinitely. Stop if Ctrl-C.
115130 try :
0 commit comments