Last active
September 25, 2015 04:57
-
-
Save ptrv/866760 to your computer and use it in GitHub Desktop.
Script for adding ignore rules to gitignore files.
This file contains hidden or 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/python | |
# Script for adding ignore rules to gitignore files. | |
# | |
# Peter Vasil | |
# Date: 2011-03-11 | |
import sys | |
from optparse import OptionParser | |
import urllib | |
import json | |
import string | |
import pickle | |
import os | |
from time import time | |
p_path = os.getenv('HOME') + '/.gitignore-rules-adder-cache' | |
templates = [] | |
templates_global = [] | |
GITIGNORE_REPO = "https://github.com/github/gitignore/raw/master/" | |
GITIGNORE_EXT = ".gitignore" | |
GITIGNORE_GLOBAL = "Global/" | |
def get_all_templates(): | |
""" | |
""" | |
global templates | |
global templates_global | |
repo_url = "https://api.github.com/repos/github/gitignore/contents" | |
repo_url2 = "https://api.github.com/repos/github/gitignore/contents/Global" | |
# content = json.loads(urllib.urlopen(repo_url).read()) | |
# print json.dumps(content, indent=4) | |
for chunk in json.JSONDecoder().decode(urllib.urlopen(repo_url).read()): | |
for c, i in chunk.iteritems(): | |
if c == "name": | |
sidx = string.find(i, ".gitignore") | |
if not sidx == -1: | |
templates.append(i[:sidx]) | |
for chunk in json.JSONDecoder().decode(urllib.urlopen(repo_url2).read()): | |
for c, i in chunk.iteritems(): | |
if c == "name": | |
sidx = string.find(i, ".gitignore") | |
if not sidx == -1: | |
templates_global.append(i[:sidx]) | |
f = open(p_path, 'w') | |
pickle.dump(templates, f) | |
pickle.dump(templates_global, f) | |
f.close() | |
def print_template_names(): | |
""" | |
""" | |
print "-" * 20 | |
print "Valid global template names:" | |
print "-" * 20 | |
print templates_global | |
print "-" * 20 | |
print "Valid template names:" | |
print "-" * 20 | |
print templates | |
def get_template(template_name, get_global): | |
""" | |
""" | |
repo_url = GITIGNORE_REPO | |
if get_global: | |
repo_url += GITIGNORE_GLOBAL | |
url = repo_url + template_name + GITIGNORE_EXT | |
templates_site = urllib.urlopen(url) | |
template = templates_site.read() | |
return template | |
def template_exists(template_name, get_global): | |
""" | |
""" | |
template_exists = False | |
if get_global: | |
if template_name in templates_global: | |
template_exists = True | |
else: | |
if template_name in templates: | |
template_exists = True | |
return template_exists | |
def print_template(template_name, get_global): | |
""" | |
""" | |
if template_exists(template_name, get_global): | |
template = get_template(template_name, get_global) | |
print template | |
def load_templates_from_pickle(f): | |
""" | |
""" | |
global templates | |
global templates_global | |
templates = pickle.load(f) | |
templates_global = pickle.load(f) | |
def main(): | |
""" | |
""" | |
usage = "usage: %prog [OPTIONS] ignore-file template-name" | |
parser = OptionParser(usage, version="%prog 0.1") | |
parser.add_option("-l", "--list-templates", | |
action="store_true", | |
dest="list_template_names", | |
help="lists template names") | |
parser.add_option("-g", "--global", | |
action="store_true", | |
dest="global_templates", | |
help="templates for global ignore files") | |
parser.add_option("-p", "--print-template", | |
action="store", | |
dest="print_template", | |
help="prints template to console") | |
(options, args) = parser.parse_args() | |
try: | |
with open(p_path, 'r') as f: | |
info = os.stat(p_path) | |
time_d = time() - info.st_mtime | |
if time_d > (24 * 24 * 60): | |
f.close() | |
get_all_templates() | |
else: | |
load_templates_from_pickle(f) | |
f.close() | |
except IOError: | |
get_all_templates() | |
if options.list_template_names: | |
print_template_names() | |
sys.exit() | |
get_global = False | |
if options.global_templates: | |
get_global = True | |
if options.print_template: | |
print "print template\n" | |
print_template(options.print_template, get_global) | |
sys.exit() | |
if len(args) != 2: | |
parser_text = """ | |
please enter the path to the gitignore file | |
and the template you want to add. | |
""" | |
parser.error(parser_text) | |
gitignore_file, template_name = args | |
if template_exists(template_name, get_global): | |
print "Getting template from web..." | |
template = get_template(template_name, get_global) | |
print "Adding template to '%s'..." % gitignore_file | |
with open(gitignore_file, "a") as f: | |
f.write("#" * 20) | |
f.write("\n# " + template_name + "\n") | |
f.write("#" * 20 + "\n\n") | |
f.write(template + "\n") | |
print "Done!" | |
else: | |
print "'%s' is not a valid template name." % template_name | |
print "Nothing added to '%s'" % gitignore_file | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment