Recipe: All Linode StackScripts

Lately I’ve been playing around with Linode. One of the neat (but incomplete, hard to use, and not as good as it could be) features are Linode Stack Scripts. I wanted a full set of them to work with in constructing my own. Browsing through them graphically is a hassle, making it hard to grep them, copy from them (since they have line numbers, and the way to remove the line numbers is javascript), etc. This Python script downloads all of them to the cwd.

Season to taste, public domain license!

import re
import urllib
import htmlentitydefs

exp = re.compile(
   '''\[CDATA\[\n\n'''  + \
   '(.*?)' + \
   '\n\]\]>\\</div>',
   re.DOTALL | re.VERBOSE)


myrx = re.compile('&(' + '|'.join(htmlentitydefs.entitydefs.keys()) + ');')
def dehtml(s):
    return re.sub(
    myrx,
    lambda m: htmlentitydefs.entitydefs[m.group(1)],
    s
    )

last_seen = 0
ii = 0
while 1:
    ii += 1
    if ii - last_seen > 100:  
        print ("stopping at %i" % ii)
        break # we've probably seen them all
    try:
        text = urllib.urlopen("http://www.linode.com/stackscripts/view/?StackScriptID=%i#viewSource" % ii).read()
    except:
        continue
    
    try:
        ans = exp.search(text).groups()[0][1:]
    except:
        continue 
    
    ans = dehtml(ans)
    last_seen = ii
    print ("creating stackscript_%i.sh" % ii)
    ofh = file("stackscript_%i.sh" % ii,'w')
    ofh.write(ans)
    ofh.close()

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s