Skip to content

Instantly share code, notes, and snippets.

@nsfmc
Last active October 1, 2015 23:57
Show Gist options
  • Save nsfmc/2127455 to your computer and use it in GitHub Desktop.
Save nsfmc/2127455 to your computer and use it in GitHub Desktop.
commit successive changes to illutrator files
#!env python
"""
artboard.py - (c) 2012 marcos.a.ojeda <[email protected]>
--"Quis leget haec?"
requires:
graphicsmagick - brew install graphicsmagick
ghostscript - brew install ghostscript
macfsevents - pip install MacFSEvents
"""
import sys
import os
import subprocess
import fsevents
import re
from collections import defaultdict
def popen_results(args):
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
return proc.communicate()[0]
def popen_return_code(args, input=None):
proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
proc.communicate(input)
return proc.returncode
FS_ITEM_CREATED = 0x00000100 #256
FS_ITEM_REMOVED = 0x00000200 #512
FS_ITEM_RENAMED = 0x00000800 #2048
FS_ITEM_MODIFIED = 0x00001000 #4096
# deal with observing the filesystem
def run_observer(observer):
try:
observer.start()
print 'Artboard is now looking for changes in %s' % os.getcwd()
while True:
pass
except KeyboardInterrupt:
observer.stop()
def whatsnew():
newfiles = popen_results("hg st -m -a -u -n".split(' ')).strip()
if newfiles:
return newfiles.split('\n')
return []
def extracto(files):
"""groups and bins changes to files"""
bs = re.compile('(?P<filename>.*?)-(?P<artboard>\d{2}).png')
coll = defaultdict(list)
for f in files:
fn = os.path.split(f)[1]
m = bs.search(fn)
if m:
coll[m.group('filename')].append(int(m.group('artboard')))
return ['"%s.ai" boards:(%s)' % (f, ','.join([str(ab) for ab in coll[f]])) for f in coll]
# iterate over illustrator files
def renderFiles( illustrations ):
"""feeds files through gm"""
for ai in illustrations:
print "+ Rendering %s" % ai
base = ai.split(".")[0]
newfile = 'repo/%s-%%02d.png' % base
cmds = ["gm", "convert", "+adjoin", ai, newfile]
popen_results(cmds)
# commit the files
concise = extracto(whatsnew())
print ' New/Modified files:'
print '\n'.join([' - %s' % x for x in concise])
addresults = popen_results(["hg", "add", "."])
commit_msg = popen_results(["hg", "ci", "-m", "; ".join(concise)])
def callback(event, *args, **kwargs):
"""docstring for callback"""
fname = os.path.split(event.name)[-1]
if fname.endswith("ai"):
print '\n%s has changed' % fname
fileschanged = whatsnew()
if fileschanged:
renderFiles(fileschanged)
observer = fsevents.Observer()
stream = fsevents.Stream(callback, os.getcwd(), file_events=True)
observer.schedule(stream)
run_observer(observer)
@nsfmc
Copy link
Author

nsfmc commented Jan 11, 2013

this is broken at least for me in that the only way i can terminate the script is by killing the window.

also, at some point gm added the +adjoin option (or made it necessary), so it may break with older versions of gm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment