Snippets

Adam Labadorf python git integration

Created by Adam Labadorf last modified Adam Labadorf
# this file has the shortened URL of 'http://bit.do/git_integr'
# to load on the fly:
"""
import urllib.request as ur
exec(ur.urlopen('http://bit.do/git_integr').read())
"""
import importlib
import inspect
import glob
import json
import os
from pprint import pprint
import re
import subprocess
import urllib.request as ur

from IPython.core.magic import register_line_magic
 
git_repo_dir = '.git_repos'
not os.path.exists(git_repo_dir) and os.mkdir(git_repo_dir)

@register_line_magic
def bitbucket_snippet(line) :
    line = line.split()
    if len(line) != 2 :
        print('Usage:\n'
              '%bitbucket_snippet <username> <snippet id>\n'
              'e.g. %bitbucket_snippet adamlabadorf x684a')
        return
    owner, snippet_id = line
    get_bitbucket_snippet(owner,snippet_id)
    
def get_bitbucket_snippet(owner, snip_id) :
    snippet_url = 'https://api.bitbucket.org/2.0/snippets/{owner}/{snip_id}'

    params = dict(
        owner = owner,
        snip_id = snip_id
    )

    # fetch json obj
    req = ur.urlopen(snippet_url.format(**params)).read()
    req_json = json.loads(req.decode('utf-8'))

    if not req_json.get('links') or not req_json.get('links').get('clone') :
        raise Exception('unexpected JSON structure: {}'.format(
                json.dumps(req_json,indent=2))
        )

    href = [_['href'] for _ in req_json['links']['clone'] if _['name'] == 'https']
    if not href :
        raise Exception('No https href was found for snippet')
    href = href[0]

    snip_id = req_json.get('id',params['snip_id'])
    repo_dir = '{}/{}'.format(git_repo_dir,snip_id)

    git_clone_or_pull(href,repo_dir)

    module_load_dir(repo_dir)

    list_coffeescripts(repo_dir)

def git_clone_or_pull(href,repo_dir) :
    if os.path.exists(repo_dir) :
        git_cmd = ('git','pull')
        p = subprocess.Popen(git_cmd
                             ,stderr=subprocess.PIPE
                             ,stdout=subprocess.PIPE
                             ,cwd=repo_dir
                            )
    else :
        git_cmd = ('git','clone',href,repo_dir)
        p = subprocess.Popen(git_cmd
                             ,stderr=subprocess.PIPE
                             ,stdout=subprocess.PIPE
                            )
    stdout, stderr = p.communicate()
    if stderr.strip() :
        print('git command: {}'.format(' '.join(git_cmd)))
        print('Stderr: {}'.format(stderr))
    return

def module_load_dir(py_dir) :
    # exec all of the python code in the dir
    # you'd better trust that code!
    for pyfn in glob.glob('{}/*.py'.format(py_dir)) :
        # http://stackoverflow.com/questions/436198/what-is-an-alternative-to-execfile-in-python-3-0
        print('Sourcing {}'.format(pyfn))
        modname = os.path.basename(pyfn).replace('.py','')
        print('    import: {}'.format(modname))
        with open(pyfn) as f :
            mod = importlib.machinery.SourceFileLoader(modname,pyfn)
            mod.load_module()
            cobj = compile(f.read(),pyfn,'exec')
            # things declared/defined in the module show up in co_consts like follows
            #<code object (?P<varname>[^ ]+) at 0x7f4cd408a6f0, file "(?P<path_to>[^"]*)?{pyfn}", line 6>
            src_defines = []
            for const in cobj.co_consts :
                const = repr(const)
                if pyfn in const :
                    patt = '<code object ([^ ]+).*'
                    src_defines.append('{}.{}'.format(modname,re.match(patt,const).group(1)))
            src_defines_str = ''.join(['\n        {}'.format(_) for _ in src_defines])
            print('    defined:{}'.format(src_defines_str))

def list_coffeescripts(repo_dir) :
    coffeefns = glob.glob('{}/*.coffee'.format(repo_dir))
    if coffeefns :
        print('Available coffeescripts')
        for coffeefn in coffeefns :
            print('    {}'.format(coffeefn))
bitbucket_snippet('')

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.