Skip to content

Instantly share code, notes, and snippets.

@marcoceppi
Last active October 2, 2015 20:21
Show Gist options
  • Save marcoceppi/7bb0607021b85477283a to your computer and use it in GitHub Desktop.
Save marcoceppi/7bb0607021b85477283a to your computer and use it in GitHub Desktop.
Charm fetcher
#!/usr/bin/env python
import os
import yaml
import shutil
import urllib
import zipfile
from theblues.charmstore import CharmStore
cs = CharmStore('https://api.jujucharms.com/v4')
class Charm(object):
def __init__(self, url):
if not url.startswith('cs:'):
raise Exception('Not a charm I care about')
if '~' in url:
raise Exception('Do not care about personal urls')
self.id = url.replace('cs:', '')
self.url = url
self.series, self.name, self.version = [self.id.split('/')[0]] + self.id.split('/')[1].rsplit('-', 1)
self.version = int(self.version)
self.archive = cs.archive_url(self.id)
def download(self, to):
filename, _ = urllib.urlretrieve(self.archive)
z = zipfile.ZipFile(filename)
z.extractall(os.path.join(to, self.name))
os.unlink(filename)
def load_manifest(manifest):
contents = {}
if os.path.isfile(manifest):
with open(manifest, 'r') as m:
contents = yaml.safe_load(m.read())
return contents
def save_manifest(manifest, manifest_file):
with open(manifest_file, 'w') as f:
f.write(yaml.safe_dump(manifest, default_flow_style=False))
def list_charms(series='trusty'):
return [Charm(c['Id']) for c in cs.search('', series=series, limit=300, promulgated_only=True)]
def refresh(series, output):
if not os.path.exists(output):
os.makedirs(output)
manifest_file = os.path.join(output, 'manifest.yaml')
manifest = load_manifest(manifest_file)
charms = list_charms(series)
for charm in charms:
current_version = manifest.get(charm.name, -1)
if charm.version <= current_version:
print('SKIP %s' % charm.id)
continue
if os.path.exists(os.path.join(output, charm.name)):
shutil.rmtree(os.path.join(output, charm.name))
print('FETCH %s' % charm.id)
charm.download(output)
manifest[charm.name] = charm.version
save_manifest(manifest, manifest_file)
def main():
cwd = os.getcwd()
for dir in ['precise', 'trusty']:
refresh(dir, os.path.join(cwd, dir))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment