#!/usr/bin/env python
"""
gitignore.py:
This program searches for specified module on GitHub/gitignore repository.
If the file is found on github, it is downloaded and created in the current working directory.
This program creates .gitignore if it does not exist or overwrites if it does exist.
"""
__author__ = "Harshad Kale"
__license__ = "MIT"
import sys
import urllib2
subject = ''
location = ''
gitignore = ''
class Color:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def file_exists(subject, searchGlobal=False):
gitignore_url = 'https://raw.githubusercontent.com/github/gitignore/master/'
global location
if not searchGlobal:
location = gitignore_url + subject + '.gitignore'
else:
location = gitignore_url + 'Global/' + subject + '.gitignore'
print
print 'Checking at ' + Color.OKBLUE + location + Color.ENDC
request = urllib2.Request(location)
request.get_method = lambda : 'HEAD'
try:
response = urllib2.urlopen(request)
return True
except urllib2.HTTPError:
return False
def create_gitignore(mode='w+'):
print
print Color.OKGREEN + 'File Found at ' + Color.OKBLUE + location + Color.ENDC
print
global gitignore
for line in urllib2.urlopen(location):
gitignore += line
pass
file = open('.gitignore', mode)
file.write(gitignore)
file.close()
print
print Color.OKGREEN + 'Your .gitignore is created successfully' + Color.ENDC
def main(argv):
print argv
subject = argv[1]
print Color.HEADER + 'Searching gitignore for - ' + subject + Color.ENDC
if (file_exists(subject) or file_exists(subject.capitalize()) or
file_exists(subject, True) or file_exists(subject.capitalize(), True)):
if len(argv) > 2:
mode = argv[2]
if (mode == 'a' or mode == '-a' or
mode == 'append' or mode == '-append'):
create_gitignore('a')
else:
print 'Wrong argument.'
print '[-a] to append to existing .gitignore file'
else:
create_gitignore('w+')
else:
print
print
print Color.FAIL
print 'ERROR! File not found'
print
print 'NOTE - '
print 'GitHub URLs are ' + Color.WARNING + 'case sensitive.' + Color.FAIL + ' If not found, this program tries to match for it by capitalizing.'
print 'There is a chance that the gitignore file that you are looking for has camalized name for exmaple JetBrains, CakePHP'
print 'Please check at ' + Color.OKBLUE + 'https://github.com/github/gitignore' + Color.FAIL + ' for exact location and name of your file'
print Color.ENDC
print
print
if __name__ == '__main__':
if sys.argv[0].startswith('python'):
main(sys.argv[1])
else:
main(sys.argv)