1# Copyright (C) 2012 Canonical Ltd.
2# Copyright (C) 2012 Hewlett-Packard Development Company, L.P.
3# Copyright (C) 2012 Yahoo! Inc.
4#
5# Author: Scott Moser <scott.moser@canonical.com>
6# Author: Juerg Haefliger <juerg.haefliger@hp.com>
7# Author: Joshua Harlow <harlowja@yahoo-inc.com>
8#
9# This file is part of cloud-init. See LICENSE file for license information.
10
11import os
12
13from cloudinit import handlers
14from cloudinit import util
15
16from cloudinit.settings import (PER_ALWAYS)
17
18
19class ShellScriptPartHandler(handlers.Handler):
20
21    prefixes = ['#!']
22
23    def __init__(self, paths, **_kwargs):
24        handlers.Handler.__init__(self, PER_ALWAYS)
25        self.script_dir = paths.get_ipath_cur('scripts')
26        if 'script_path' in _kwargs:
27            self.script_dir = paths.get_ipath_cur(_kwargs['script_path'])
28
29    def handle_part(self, data, ctype, filename, payload, frequency):
30        if ctype in handlers.CONTENT_SIGNALS:
31            # TODO(harlowja): maybe delete existing things here
32            return
33
34        filename = util.clean_filename(filename)
35        payload = util.dos2unix(payload)
36        path = os.path.join(self.script_dir, filename)
37        util.write_file(path, payload, 0o700)
38
39# vi: ts=4 expandtab
40