Skip to content

Commit

Permalink
accept excludes via arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
carloe committed Jun 12, 2015
1 parent fa7172b commit 835d632
Showing 1 changed file with 55 additions and 35 deletions.
90 changes: 55 additions & 35 deletions credits.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
them into a Settings.bundle friendly plist. Inspired by JosephH
and Sean's comments on stackoverflow: http://stackoverflow.com/q/6428353
:usage: ./credits.py -d project/ -o project/Settings.bundle/Credits.plist
:usage: ./credits.py -s project/ -o project/Settings.bundle/Credits.plist
:author: Carlo Eugster (http://carlo.io)
:license: MIT, see LICENSE for more details.
Expand All @@ -17,47 +17,65 @@
import plistlib
import re
import codecs
import getopt
from optparse import OptionParser
from optparse import Option, OptionValueError
from copy import deepcopy

EXCLUDE = []
VERSION = '0.3'
PROG = os.path.basename(os.path.splitext(__file__)[0])
DESCRIPTION = '''Recursively searches the input directory for 'LICENSE.*' files and compiles them into a Settings.bundle friendly plist. Inspired by JosephH and Sean's comments on stackoverflow: http://stackoverflow.com/q/6428353'''

class MultipleOption(Option):
ACTIONS = Option.ACTIONS + ("extend",)
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)

def take_action(self, action, dest, opt, value, values, parser):
if action == "extend":
values.ensure_value(dest, []).append(value)
else:
Option.take_action(self, action, dest, opt, value, values, parser)


def main(argv):
outputfile = None
inputdir = None
try:
opts, args = getopt.getopt(argv,"hd:o:",["idir=","ofile="])
except getopt.GetoptError:
printHelp(1)
for opt, arg in opts:
if opt == '-h':
printHelp(0)
elif opt in ("-d", "--dir"):
inputdir = arg
elif opt in ("-o", "--out"):
outputfile = arg

if(outputfile == None or inputdir == None):
printHelp(2)

if(not outputfile.endswith('.plist')):
print "Error: Outputfile must end in .plist"
parser = OptionParser(option_class=MultipleOption,
usage='usage: %prog -s source_path -o output_plist -e [exclude_paths]',
version='%s %s' % (PROG, VERSION),
description=DESCRIPTION)
parser.add_option('-s', '--source',
type="string",
dest='inputpath',
metavar='source_path',
help='source directory to search for licenses')
parser.add_option('-o', '--output-plist',
type="string",
dest='outputfile',
metavar='output_plist',
help='path to the plist to be generated')
parser.add_option('-e', '--exclude',
action="extend", type="string",
dest='excludes',
metavar='path1, ...',
help='comma seperated list of paths to be excluded')
if len(sys.argv) == 1:
parser.parse_args(['--help'])

OPTIONS, args = parser.parse_args()

if(not os.path.isdir(OPTIONS.inputpath)):
print "Error: Invalid source path: %s" % OPTIONS.inputpath
sys.exit(2)

if(not os.path.isdir(inputdir)):
print "Error: Input directory does not exist."
if(not OPTIONS.outputfile.endswith('.plist')):
print "Error: Outputfile must end in .plist"
sys.exit(2)

plist = plistFromDir(inputdir)
plistlib.writePlist(plist,outputfile)
plist = plistFromDir(OPTIONS.inputpath, OPTIONS.excludes)
plistlib.writePlist(plist,OPTIONS.outputfile)
return 0

def printHelp(code):
print 'Suntax:'
print '\tcredits.py -d <sourcepath> -o <outputfile>'
sys.exit(code)

def plistFromDir(dir):
def plistFromDir(dir, excludes):
"""
Recursilvely search 'dir' to generates plist objects from LICENSE files.
"""
Expand All @@ -67,7 +85,7 @@ def plistFromDir(dir):
for file in files:
if file.startswith("LICENSE"):
plistPath = os.path.join(root, file)
if not excludePath(plistPath):
if not excludePath(plistPath, excludes):
license = plistFromFile(plistPath)
plist['PreferenceSpecifiers'].append(license)
return plist
Expand All @@ -90,8 +108,10 @@ def plistFromFile(path):
group['FooterText'] = rchop(body, " \n\n")
return group

def excludePath(path):
for pattern in EXCLUDE:
def excludePath(path, excludes):
if excludes is None:
return False
for pattern in excludes:
if(re.search(pattern, path, re.S) != None):
return True
return False
Expand Down

0 comments on commit 835d632

Please sign in to comment.