#!/usr/bin/env python
import logging
import os
import citadel.nodes.node
import citadel.scm
[docs]class Root(citadel.nodes.node.Base):
""":synopsis: Append a small bash header for all citadel scripts.
:requirements: None
:platform: Any
This module will be injected by default and requires no explicit usage to
actually work. It prepends common options to all citadel scripts:
.. code-block:: bash
:linenos:
#!/usr/bin/env bash
set -eu
set -x
"""
def __init__(self, yml, path=[], environment=None):
super(Root, self).__init__(yml, path)
self.output.append(self.generate_header())
if environment:
self.output.append('### CLI ENVIRONMENT args')
for line in environment.split():
if '=' in line:
name, value = line.split('=', 1)
# Periods make it an invalid identifier in bash
# so we ignore them
if '.' in name:
continue
self.output.append('export %s="%s"' % (name, value))
else:
logging.debug('Ignoring invalid export line: %s', line)
self.output.append(self.generate_scm_vars())
def generate_header(self):
return """#!/usr/bin/env bash
### Generated by CITADel ###
set -ue
"""
def generate_scm_vars(self):
scm = citadel.scm.get_client(raiseOnError=False)
return """
export PWD="%s"
ACTIVE_ISSUES="%s"
ACTIVE_BRANCH="%s"
ACTIVE_FILES="%s"
""" % (os.getcwd(), scm.get_active_issues(), scm.get_active_branch(), scm.get_active_files())