I implemented a Python script that generates git commits for the last year making one's Contribution Graph look full of changes. It was my first Python program (I work mostly with Java). Although the program is quite useless I want to use it as a learning playground to build my Python competences. That is why I would appreciate any feedback about the code (both constructive and destructive).
Questions:
- I didn't use types to make the script work with Python 2 and 3. Is it a good practice?
- Does the script follow Python style conventions? I didn't manage to find unified Python style guides, I used what was provided by
flake8. - Do you usually cover scripts like this with Unit tests? How would you do that?
- And generally what would you improve? Any comments are appreciated.
Code: (applied the changes proposed by Graipher)
You can find the full script here.
def main():
args = arguments()
curr_date = datetime.now()
directory = 'repository-' + curr_date.strftime('%Y-%m-%d-%H-%M-%S')
repository = args.repository
if repository is not None:
start = repository.rfind('/') + 1
end = repository.rfind('.')
directory = repository[start:end]
no_weekends = args.no_weekends
frequency = args.frequency
os.mkdir(directory)
os.chdir(directory)
run(['git', 'init'])
start_date = curr_date.replace(hour=20, minute=0) - timedelta(366)
for day in (start_date + timedelta(n) for n in range(366)):
if (not no_weekends or day.weekday() < 5) \
and randint(0, 100) < frequency:
for commit_time in (day + timedelta(minutes=m)
for m in range(contributions_per_day(args))):
contribute(commit_time)
if repository is not None:
run(['git', 'remote', 'add', 'origin', repository])
run(['git', 'push', '-u', 'origin', 'master'])
print('\nRepository generation ' +
'\x1b[6;30;42mcompleted successfully\x1b[0m!')
def contribute(date):
with open(os.path.join(os.getcwd(), 'README.md'), 'a') as file:
file.write(message(date) + '\n\n')
run(['git', 'add', '.'])
run(['git', 'commit', '-m', '"%s"' % message(date),
'--date', date.strftime('"%Y-%m-%d %H:%M:%S"')])
def run(commands):
Popen(commands).wait()
def message(date):
return date.strftime('Contribution: %Y-%m-%d %H:%M')
def contributions_per_day(args):
max_c = args.max_commits
if max_c > 20:
max_c = 20
if max_c < 1:
max_c = 1
return randint(1, max_c)
def arguments():
parser = argparse.ArgumentParser()
parser.add_argument('-nw', '--no_weekends',
required=False, action='store_true', default=False,
help="""do not commit on weekends""")
parser.add_argument('-mc', '--max_commits', type=int, default=10,
required=False, help="""Defines the maximum amount of
commits a day the script can make. Accepts a number
from 1 to 20. If N is specified the script commits
from 1 to N times a day. The exact number of commits
is defined randomly for each day. The default value
is 10.""")
parser.add_argument('-fr', '--frequency', type=int, default=80,
required=False, help="""Percentage of days when the
script performs commits. If N is specified, the script
will commit N%% of days in a year. The default value
is 80.""")
parser.add_argument('-r', '--repository', type=str, required=False,
help="""A link on an empty non-initialized remote git
repository. If specified, the script pushes the changes
to the repository. The link is accepted in SSH or HTTPS
format. For example: [email protected]:user/repo.git or
https://github.com/user/repo.git""")
return parser.parse_args()
if __name__ == "__main__":
main()