本文整理汇总了Python中preupg.utils.FileHelper.write_to_file方法的典型用法代码示例。如果您正苦于以下问题:Python FileHelper.write_to_file方法的具体用法?Python FileHelper.write_to_file怎么用?Python FileHelper.write_to_file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类preupg.utils.FileHelper
的用法示例。
在下文中一共展示了FileHelper.write_to_file方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update_check_script
# 需要导入模块: from preupg.utils import FileHelper [as 别名]
# 或者: from preupg.utils.FileHelper import write_to_file [as 别名]
def update_check_script(self, updates, author=None):
"""
The function updates check script with license file
and with API functions like check_rpm_to and check_applies_to
"""
script_type = FileHelper.get_script_type(self.check_script_path)
if author is None:
author = "<empty_line>"
generated_section, functions = ModuleHelper.generate_common_stuff(
settings.license % author, updates, script_type)
lines = FileHelper.get_file_content(self.check_script_path, "rb",
method=True)
if not [x for x in lines if re.search(r'#END GENERATED SECTION', x)]:
raise MissingHeaderCheckScriptError(self.check_script_path)
for func in functions:
lines = [x for x in lines if func not in x.strip()]
output_text = ""
for line in lines:
if '#END GENERATED SECTION' in line:
new_line = '\n'.join(generated_section)
new_line = new_line.replace('<empty_line>',
'').replace('<new_line>', '')
output_text += new_line + '\n'
output_text += line
FileHelper.write_to_file(self.check_script_path, "wb", output_text)
示例2: write_xml
# 需要导入模块: from preupg.utils import FileHelper [as 别名]
# 或者: from preupg.utils.FileHelper import write_to_file [as 别名]
def write_xml(self):
"""Function writes XML document to file"""
self.target_tree.set('xmlns:xhtml', 'http://www.w3.org/1999/xhtml/')
# we really must set encoding here! and suppress it in write_to_file
data = ElementTree.tostring(self.target_tree, "utf-8")
FileHelper.write_to_file(self.path, 'wb', data, False)
self.target_tree = ElementTree.parse(self.path).getroot()
示例3: update_check_script
# 需要导入模块: from preupg.utils import FileHelper [as 别名]
# 或者: from preupg.utils.FileHelper import write_to_file [as 别名]
def update_check_script(self, updates, author=None):
"""
The function updates check script with license file
and with API functions like check_rpm_to and check_applies_to
"""
script_type = FileHelper.get_script_type(self.full_path_name)
if author is None:
author = "<empty_line>"
generated_section, functions = ModuleHelper.generate_common_stuff(settings.license % author,
updates,
script_type)
lines = FileHelper.get_file_content(self.full_path_name, "rb", method=True)
if not [x for x in lines if re.search(r'#END GENERATED SECTION', x)]:
MessageHelper.print_error_msg("#END GENERATED SECTION is missing in check_script %s" % self.full_path_name)
raise MissingHeaderCheckScriptError
for func in functions:
lines = [x for x in lines if func not in x.strip()]
output_text = ""
for line in lines:
if '#END GENERATED SECTION' in line:
new_line = '\n'.join(generated_section)
new_line = new_line.replace('<empty_line>', '').replace('<new_line>', '')
output_text += new_line+'\n'
if 'check_applies' in updates:
component = updates['check_applies']
else:
component = "distribution"
if script_type == "sh":
output_text += 'COMPONENT="'+component+'"\n'
else:
output_text += 'set_component("'+component+'")\n'
output_text += line
FileHelper.write_to_file(self.full_path_name, "wb", output_text)
示例4: setUp
# 需要导入模块: from preupg.utils import FileHelper [as 别名]
# 或者: from preupg.utils.FileHelper import write_to_file [as 别名]
def setUp(self):
self.root_dir_name = "tests/FOOBAR6_7" + settings.results_postfix
self.dirname = os.path.join(self.root_dir_name, "test")
if os.path.exists(self.dirname):
shutil.rmtree(self.dirname)
os.makedirs(self.dirname)
self.filename = os.path.join(self.dirname, 'test.ini')
self.rule = []
self.loaded_ini = {}
self.test_ini = {'content_title': 'Testing content title',
'content_description': ' some content description',
'author': 'test <[email protected]>',
'config_file': '/etc/named.conf'
}
self.check_sh = """#!/bin/bash
#END GENERATED SECTION
#This is testing check script
"""
check_name = os.path.join(self.dirname, settings.check_script)
FileHelper.write_to_file(check_name, "wb", self.check_sh)
os.chmod(check_name, stat.S_IEXEC | stat.S_IRWXG | stat.S_IRWXU)
self.solution_text = """
A solution text for test suite"
"""
test_solution_name = os.path.join(self.dirname, settings.solution_txt)
FileHelper.write_to_file(test_solution_name, "wb", self.solution_text)
os.chmod(check_name, stat.S_IEXEC | stat.S_IRWXG | stat.S_IRWXU)
示例5: test_secret_check_script
# 需要导入模块: from preupg.utils import FileHelper [as 别名]
# 或者: from preupg.utils.FileHelper import write_to_file [as 别名]
def test_secret_check_script(self):
"""Check occurrence of secret file for check script"""
self.test_ini['check_script'] = '.minicheck'
text = """#!/usr/bin/sh\necho 'ahojky'\n"""
FileHelper.write_to_file(os.path.join(self.dir_name, self.check_script), "wb", text)
self.loaded_ini[self.filename].append(self.test_ini)
self.xml_utils = XmlUtils(self.dir_name, self.loaded_ini)
self.assertRaises(MissingFileInContentError, lambda: list(self.xml_utils.prepare_sections()))
示例6: test_hashes
# 需要导入模块: from preupg.utils import FileHelper [as 别名]
# 或者: from preupg.utils.FileHelper import write_to_file [as 别名]
def test_hashes(self):
text_to_hash="""
This is preupgrade assistant test has string"
"""
self.dir_name = "tests/hashes"
os.mkdir(self.dir_name)
FileHelper.write_to_file(os.path.join(self.dir_name, "post_script"), 'wb', text_to_hash)
PostupgradeHelper.hash_postupgrade_file(False, self.dir_name)
return_value = PostupgradeHelper.hash_postupgrade_file(False, self.dir_name, check=True)
self.assertTrue(return_value)
示例7: test_incorrect_tag
# 需要导入模块: from preupg.utils import FileHelper [as 别名]
# 或者: from preupg.utils.FileHelper import write_to_file [as 别名]
def test_incorrect_tag(self):
"""
Check occurrence of incorrect tag
Tests issue #30
"""
text_ini = '[preupgrade]\n'
text_ini += '\n'.join([key + " = " + self.test_ini[key] for key in self.test_ini])
text_ini += '\n[]\neliskk\n'
FileHelper.write_to_file(self.filename, "wb", text_ini)
oscap = OscapGroupXml(self.root_dir_name, self.dir_name)
self.assertRaises(configparser.ParsingError, oscap.find_all_ini)
示例8: write_profile_xml
# 需要导入模块: from preupg.utils import FileHelper [as 别名]
# 或者: from preupg.utils.FileHelper import write_to_file [as 别名]
def write_profile_xml(self, target_tree):
"""The function stores all-xccdf.xml file into content directory"""
file_name = os.path.join(self.dirname, "all-xccdf.xml")
print ('File which can be used by Preupgrade-Assistant is:\n', ''.join(file_name))
try:
# encoding must be set! otherwise ElementTree return non-ascii characters
# as html entities instead, which are unsusable for us
data = ElementTree.tostring(target_tree, "utf-8")
FileHelper.write_to_file(file_name, "wb", data, False)
except IOError as ioe:
print ('Problem with writing to file ', file_name, ioe.message)
示例9: write_xml
# 需要导入模块: from preupg.utils import FileHelper [as 别名]
# 或者: from preupg.utils.FileHelper import write_to_file [as 别名]
def write_xml(self):
"""The function is used for storing a group.xml file"""
self.find_all_ini()
self.write_list_rules()
xml_utils = XmlUtils(self.dirname, self.loaded)
self.rule = xml_utils.prepare_sections()
file_name = os.path.join(self.dirname, "group.xml")
try:
FileHelper.write_to_file(file_name, "wb", ["%s" % item for item in self.rule])
except IOError as ior:
print ('Problem with write data to the file ', file_name, ior.message)
示例10: write_xccdf_version
# 需要导入模块: from preupg.utils import FileHelper [as 别名]
# 或者: from preupg.utils.FileHelper import write_to_file [as 别名]
def write_xccdf_version(file_name, direction=False):
"""
Function updates XCCDF version because
of separate HTML generation and our own XSL stylesheet
"""
namespace_1 = 'http://checklists.nist.gov/xccdf/1.1'
namespace_2 = 'http://checklists.nist.gov/xccdf/1.2'
content = FileHelper.get_file_content(file_name, "rb")
if direction:
content = re.sub(namespace_2, namespace_1, content)
else:
content = re.sub(namespace_1, namespace_2, content)
FileHelper.write_to_file(file_name, 'wb', content)
示例11: _create_check_script
# 需要导入模块: from preupg.utils import FileHelper [as 别名]
# 或者: from preupg.utils.FileHelper import write_to_file [as 别名]
def _create_check_script(self):
if self.check_script:
if self.script_type == "sh":
content = settings.temp_bash_script
else:
content = settings.temp_python_script
FileHelper.write_to_file(os.path.join(self.get_content_path(),
self.get_check_script()),
'wb',
content)
os.chmod(os.path.join(self.get_content_path(),
self.get_check_script()),
0755)
示例12: update_report
# 需要导入模块: from preupg.utils import FileHelper [as 别名]
# 或者: from preupg.utils.FileHelper import write_to_file [as 别名]
def update_report(self, report_path):
"""Update XML or HTML report with relevant solution texts."""
if not self.solution_texts:
self.load_solution_texts()
orig_file = os.path.join(self.assessment_result_path, report_path)
report_content = FileHelper.get_file_content(orig_file, "rb")
for solution_placeholer, solution_text in self.solution_texts.items():
report_content = report_content.replace(solution_placeholer,
solution_text)
FileHelper.write_to_file(orig_file, "wb", report_content)
示例13: _copy_xccdf_file
# 需要导入模块: from preupg.utils import FileHelper [as 别名]
# 或者: from preupg.utils.FileHelper import write_to_file [as 别名]
def _copy_xccdf_file(self, update_text=None, update_return_value=None):
temp_dir = tempfile.mkdtemp()
xccdf_file = os.path.join(os.getcwd(), "tests", "generated_results", "inplace_risk_test.xml")
temp_file = os.path.join(temp_dir, "all-xccdf.xml")
shutil.copyfile(xccdf_file, temp_file)
content = FileHelper.get_file_content(temp_file, "rb", decode_flag=False)
new_text = b"preupg.risk.%s: Test %s Inplace risk" % (update_text, update_text)
if update_text is not None:
content = content.replace(b"INPLACE_TAG", new_text)
else:
content = content.replace(b"INPLACE_TAG", "")
content = content.replace(b"RESULT_VALUE", update_return_value)
FileHelper.write_to_file(temp_file, "wb", content)
return temp_file
示例14: solution_file
# 需要导入模块: from preupg.utils import FileHelper [as 别名]
# 或者: from preupg.utils.FileHelper import write_to_file [as 别名]
def solution_file(message):
"""
Function appends a message to solution file.
solution file will be created in module directory
:param message: Message - string of list of strings
:return:
"""
if os.path.exists(SOLUTION_FILE):
mod = "a+b"
else:
mod = "wb"
FileHelper.write_to_file(SOLUTION_FILE, mod, message)
示例15: write_list_rules
# 需要导入模块: from preupg.utils import FileHelper [as 别名]
# 或者: from preupg.utils.FileHelper import write_to_file [as 别名]
def write_list_rules(self):
end_point = self.dirname.find(SystemIdentification.get_valid_scenario(self.dirname))
rule_name = '_'.join(self.dirname[end_point:].split('/')[1:])
file_list_rules = os.path.join(settings.UPGRADE_PATH, settings.file_list_rules)
lines = []
if os.path.exists(file_list_rules):
lines = FileHelper.get_file_content(file_list_rules, "rb", method=True)
else:
lines = []
for values in six.itervalues(self.loaded):
check_script = [v for k, v in six.iteritems(values[0]) if k == 'check_script']
if check_script:
check_script = os.path.splitext(''.join(check_script))[0]
lines.append(settings.xccdf_tag + rule_name + '_' + check_script + '\n')
FileHelper.write_to_file(file_list_rules, "wb", lines)