Skip to content

Commit

Permalink
call subprocess correctly, use tmp path
Browse files Browse the repository at this point in the history
  • Loading branch information
talos committed Feb 26, 2015
1 parent 91eb432 commit c7aedd8
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions images/build/scripts/build.py
Expand Up @@ -6,21 +6,22 @@

import requests
import sys
import os
import subprocess


def shell(cmd):
"""
Run a shell command convenience function.
"""
return subprocess.call(cmd, shell=True)
return subprocess.check_call(cmd, shell=True)


def run_postgres_script(script_path):
"""
Run some psql code.
"""
return shell(['gosu', 'postgres', 'psql', '<', script_path])
return shell('gosu postgres psql < {}'.format(script_path))


def run_remote_script(desc):
Expand All @@ -37,7 +38,7 @@ def run_remote_script(desc):
if script_type == 'postgres':
run_postgres_script(script_path)
elif script_type in ('bash', ):
shell([script_type, script_path])
shell('{} {}'.format(script_type, script_path))
else:
raise Exception("Script type '{}' not supported".format(script_type))

Expand All @@ -46,16 +47,16 @@ def wget_download(url, name):
"""
Download a URL and save it in file called 'name'.
"""
outfile_path = "/{}".format(name)
shell(["wget", "-q", "-O", outfile_path, url])
outfile_path = "tmp/{}".format(name)
shell("wget -q -O {} {}".format(outfile_path, url))
return outfile_path


def pgload_import(dataset_name, data_path, load_format):
"""
Import a dataset via pgload.
"""
pgload_path = '/scripts/pgloader.load'
pgload_path = 'tmp/pgloader.load'
format_type = load_format.get('type', 'csv')
default_sep = '\t' if format_type == 'tsv' else ','
separator = load_format.get('separator', default_sep)
Expand All @@ -67,17 +68,20 @@ def pgload_import(dataset_name, data_path, load_format):
fields terminated by '{}';
'''.format(dataset_name, separator))

script = ['gosu', 'postgres', 'tail', '-n', '+2', data_path, '|']
script = 'gosu postgres tail -n +2 {} | '.format(data_path)
if bool(load_format.get('unique', False)):
script += ['sort', '|', 'uniq', '|']
script += ['pgloader', pgload_path]
script += 'sort | uniq | '
script += 'pgloader {}'.format(pgload_path)
shell(script)


def build(url):
"""
Main function. Takes the URL of the data.json spec.
"""
if not os.path.exists('tmp'):
os.mkdir('tmp')

resp = requests.get(url).json()
dataset_name = resp[u'name']

Expand Down

0 comments on commit c7aedd8

Please sign in to comment.