From d3c97b5b7cc5635dc1feb46da00953d86a21be33 Mon Sep 17 00:00:00 2001 From: Spockuto Date: Thu, 28 Jan 2016 12:28:45 +0530 Subject: [PATCH] Added Argparse and PEP8ified the code --- README.md | 24 +++++++++++++++++---- greenhat.py | 61 +++++++++++++++++++++++++++++++---------------------- 2 files changed, 56 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index d1e58fb1ff..657c2b07f0 100644 --- a/README.md +++ b/README.md @@ -2,16 +2,32 @@ greenhat is a quick hack for decorating your GitHub contribution calendar with commits for the past `n` days. It uses the `GIT_AUTHOR_DATE` and `GIT_COMMITTER_DATE` environmental variables to make commits appear in the past. Be warned that greenhat will clobber your repository's commit history. ### How to Use -Place `greenhat.py` in your Git repository. Make sure your [remote repository URL is set](https://help.github.com/articles/adding-a-remote/), and that you have a [public SSH key set up](https://help.github.com/articles/generating-ssh-keys/). Then run the script with the python interpreter, with an integer specifying `n` number of days before today to generate commits for. E.g., +Place `greenhat.py` in your Git repository. Make sure your [remote repository URL is set](https://help.github.com/articles/adding-a-remote/), and that you have a [public SSH key set up](https://help.github.com/articles/generating-ssh-keys/). Then run the script with the python interpreter, with an integer specifying `days` number of days before today to generate commits for. E.g., - python greenhat.py + python greenhat.py --days It might take a while to generate all the commits. If greenhat stops before it finishes, you can resume where you last left off by specifying a date before today when you want it to resume, like so: - python greenhat.py + python greenhat.py --days --date -`n` is the remaining days you want to generate commits for, and `date` is a date string in the form `yyyy-mm-dd` (e.g., 2013-04-05). +`days` is the remaining days you want to generate commits for, and `date` is a date string in the form `yyyy-mm-dd` (e.g., 2013-04-05). +Usage +===== +```sh + +usage: greenhat.py [-h] [-dy DAYS] [-da DATE] + +Quick hack for making real work happen. + +optional arguments: + -h, --help show this help message and exit + -dy DAYS, --days DAYS + Number of days before to generate commits + -da DATE, --date DATE + Specify a date(Default: Today) + +``` #### An Example The following calendar is the result of running `python greenhat.py 365`: diff --git a/greenhat.py b/greenhat.py index 48a11116e6..add922ca57 100755 --- a/greenhat.py +++ b/greenhat.py @@ -1,38 +1,49 @@ # Copyright (c) 2015 Angus H. (4148) # Distributed under the GNU General Public License v3.0 (GPLv3). +'''define utf-8 encoding''' -from datetime import date, timedelta +from datetime import date, timedelta, datetime from random import randint from time import sleep import sys import subprocess -import os +from argparse import ArgumentParser + + -# returns a date string for the date that is N days before STARTDATE def get_date_string(n, startdate): - d = startdate - timedelta(days=n) - rtn = d.strftime("%a %b %d %X %Y %z -0400") - return rtn + '''# returns a date string for the date that is N days before STARTDATE''' + d = startdate - timedelta(days=n) + rtn = d.strftime("%a %b %d %X %Y %z -0400") + return rtn # main app -def main(argv): - if len(argv) < 1 or len(argv) > 2: - print "Error: Bad input." - sys.exit(1) - n = int(argv[0]) - if len(argv) == 1: - startdate = date.today() - if len(argv) == 2: - startdate = date(int(argv[1][0:4]), int(argv[1][5:7]), int(argv[1][8:10])) - i = 0 - while i <= n: - curdate = get_date_string(i, startdate) - num_commits = randint(1, 10) - for commit in range(0, num_commits): - subprocess.call("echo '" + curdate + str(randint(0, 1000000)) +"' > realwork.txt; git add realwork.txt; GIT_AUTHOR_DATE='" + curdate + "' GIT_COMMITTER_DATE='" + curdate + "' git commit -m 'update'; git push;", shell=True) - sleep(.5) - i += 1 - subprocess.call("git rm realwork.txt; git commit -m 'delete'; git push;", shell=True) +def main(): + '''Main function''' + parser = ArgumentParser(description="Quick hack for making real work happen.") + parser.add_argument("-dy", "--days", help="Number of days before to generate commits", type=int) + parser.add_argument("-da", "--date", help="Specify a date(Default: Today)", type=str) + + if len(sys.argv) == 1: + parser.print_help() + return + + args = parser.parse_args() + + n = args.days + if args.date: + startdate = datetime.strptime(str(args.date), "%Y-%m-%d").date() + else: + startdate = date.today() + i = 0 + while i <= n: + curdate = get_date_string(i, startdate) + num_commits = randint(1, 10) + for commit in range(0, num_commits): + subprocess.call("echo '" + curdate + str(randint(0, 1000000)) +"' > realwork.txt; git add realwork.txt; GIT_AUTHOR_DATE='" + curdate + "' GIT_COMMITTER_DATE='" + curdate + "' git commit -m 'update'; git push;", shell=True) + sleep(.5) + i += 1 + subprocess.call("git rm realwork.txt; git commit -m 'delete'; git push;", shell=True) if __name__ == "__main__": - main(sys.argv[1:]) + main()