Last active
August 29, 2015 14:10
-
-
Save hvkale/bfc47e2ef083198f695e to your computer and use it in GitHub Desktop.
Python script that downloads contents of .gitignore file from https://github.com/github/gitignore for specified language / IDE. The name of the argument has to exactly match with the name on github repo. WARNING - the argument name is CASE-SENSITIVE Python 2.7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 '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 Color.OKGREEN + 'File Found at ' + Color.OKBLUE + location + Color.ENDC | |
global gitignore | |
for line in urllib2.urlopen(location): | |
gitignore += line | |
pass | |
file = open('.gitignore', mode) | |
file.write(gitignore) | |
file.close() | |
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 Color.FAIL | |
print 'ERROR! File not found' | |
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 | |
if __name__ == '__main__': | |
if sys.argv[0].startswith('python'): | |
main(sys.argv[1]) | |
else: | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you thought about using the requests module instead of urllib?