Skip to content

Instantly share code, notes, and snippets.

@staydecent
Last active December 15, 2015 19:09
Show Gist options
  • Save staydecent/5308748 to your computer and use it in GitHub Desktop.
Save staydecent/5308748 to your computer and use it in GitHub Desktop.
Concatenates files in a single dir. Supports before ordering and nice things.
#!/bin/bash
# Concat our shit
declare -a libsBefore
declare -a helperFiles
libsBefore=(
'jquery.min.js'
'underscore-min.js'
'backbone-min.js'
)
helperFiles=(
"ecquire/helpers/softmatch.js"
"ecquire/helpers/softmatch_tokenized.js"
"ecquire/helpers/background_access.js"
"ecquire/helpers/notification.js"
"ecquire/helpers/polling.js"
"ecquire/helpers/records.js"
)
libsJoined+=$(printf ",%s" "${libsBefore[@]}")
libsJoined=${libsJoined:1}
helpersJoined+=$(printf ",%s" "${helperFiles[@]}")
helpersJoined=${helpersJoined:1}
python ./concat.py 'ecquire/libs' 'ecquire/public/libs.js' ${libsJoined}
python ./concat.py ${helpersJoined} 'ecquire/public/frontend_helpers.js'
python ./concat.py 'ecquire/models' 'ecquire/public/app_models.js'
python ./concat.py 'ecquire/view' 'ecquire/public/app_views.js'
from glob import glob
import shutil
import os
import sys
CWD = os.path.join(os.getcwd() + '/')
def concat(look, dest, before=[], reg='*.js'):
# type safety
if ',' in look:
look = look.split(',')
if ',' in before:
before = before.split(',')
if type(look) == list and len(before) > 0:
print('Both `look` and `before` cannot be lists.')
return
destination = open(os.path.join(CWD + dest), 'wb')
before = map(lambda x: os.path.join(CWD, look, x), before)
# set array
if type(look) == list:
iterateFiles = map(lambda x: os.path.join(CWD, x), look)
# look in dir
else:
iterateFiles = glob(os.path.join(CWD, look + '/') + reg)
# ordering?
if len(before) > 0:
iterateFiles = before + iterateFiles
# remove dupes
seen = set()
seen_add = seen.add
iterateFiles = [ x for x in iterateFiles if x not in seen and not seen_add(x)]
for filename in iterateFiles:
shutil.copyfileobj(open(filename, 'rb'), destination)
destination.close()
print "Concatenated all {0} files in {1} to {2}".format(reg, look, dest)
def main(argv):
pass
# need atleast 2 args
if len(argv) < 2:
print("Concat takes at least 2 arguments.")
print("`concat.py look (String or List) dest (String) before (List)`")
return
concat(*argv)
if __name__ == '__main__':
main(sys.argv[1:]) # Skips program name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment