def test_compile_content(): def boo(): return 'foo' # callable. will extract result assert compile_content(boo) == 'foo' # string, just return assert compile_content('boo') == 'boo'
def to_xml(self): el = BeautifulSoup().new_tag('resource') if self.is_dir: el['class'] = 'directory' else: el['class'] = 'file' el['content-length'] = compile_content(self.content) el['path'] = self.path return el
def exec_bash_script(script): script = compile_content(script) if "\n" in script: f = NamedTemporaryFile(delete=False) f.write(script) f.close() if user: subprocess.call(['chown', user, f.name]) command = 'bash %s' % f.name if user: command = 'su -c "%s" - %s ' % (command, user) debug('bash$ %s' % command) debug('\n\n%s\n' % script) run(command) os.unlink(f.name) else: if user: script = 'su -c "%s" - %s ' % (script, user) run(script, log_output=True)
def apply(self): """ Creates file or update if it's needed """ content_compiled = compile_content(self.content) if os.path.exists(self.path): if self.is_content_update_needed(): def update_content(): # s.file_write(self.path, content_compiled, self.mode, self.owner, self.group) with open(self.path, 'w') as f: f.write(content_compiled) event(self.on_update) return [ChangeSetItem('Update file content %s' % self.path, update_content)] else: debug('File with identical content already exist: %s' % self.path) else: if self.is_dir: def create_dir(): os.makedirs(self.path) event(self.on_create) return [ChangeSetItem('Create dir %s' % self.path, create_dir)] else: def create_file(): with open(self.path, 'w') as f: f.write(content_compiled) event(self.on_create) return [ChangeSetItem('Create file %s' % self.path, create_file)]
def exec_script(script): script = compile_content(script) run(script, log_output=True)
def _content_hash(self): content_compiled = compile_content(self.content) md5 = hashlib.md5() md5.update(content_compiled) content_hash = md5.hexdigest() return content_hash