tw2.lesscss / setup_ext.py

The default branch has multiple heads

import os

from distutils.core import setup, Command
from BeautifulSoup import BeautifulSoup
import urllib2

from posixpath import basename
import re

class SetupBuildCommand(Command):
    """
    Master setup build command to subclass from.
    """

    user_options = []

    def initialize_options(self):
        """
        Setup the current dir.
        """
        self._dir = os.getcwd()

    def finalize_options(self):
        """
        Set final values for all the options that this command supports.
        """
        pass

class UpdateLessCSSCommand(SetupBuildCommand):
    '''
    Updates the Less CSS Runtime library
    '''
    
    description = "updates LessCSS with latest version"

    def run(self):
        page = urllib2.urlopen('http://lesscss.org')
        soup = BeautifulSoup(page)

        download_link = soup.findAll(id='download')
        try:
            link_obj = download_link[0]
        except IndexError:
            raise Exception, "Can't find the download link. The page must\
                have changed. File a bug-report and we will fix it!"

        # Get the download link and extract the version number
        dl_link = link_obj['href']
        file_name = basename(dl_link)
        r = re.compile('less-([\d.]+)')
        ver_num = r.match(file_name).groups()[0][:-1]

        # Check to see if the directory already exists, if not make it
        if not os.path.exists('tw2/lesscss/static/lesscss/%s' % ver_num):
            os.mkdir('tw2/lesscss/static/lesscss/%s' % ver_num)

        # Download LessCSS
        less_js = urllib2.urlopen(dl_link)
        with open('tw2/lesscss/static/lesscss/%s/less.min.js'%ver_num, 'w') as js:
            print "Downloading %s to static/%s" % (file_name, ver_num)
            js.write(less_js.read())
       
        # Update version.py
        vs_file = "_version_num_ = '{ver_num}'".format(ver_num=ver_num)
        
        with open('tw2/lesscss/version.py', 'w') as vs_py:
            print "Writing new version.py"
            vs_py.write(vs_file)

        # Done!
        print 'Updated LessCSS to %s...' % ver_num
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.