os.makedirs(os.path.dirname(udisk2_etc), exist_ok=True)
xml_data = etree.ElementTree(etree.Element('policyconfig'))
if os.path.exists(udisk2):
- data = open(udisk2, 'rb').read()
+ with open(udisk2, 'rb') as f:
+ data = f.read()
existing_xml = etree.ElementTree(etree.fromstring(data))
root = xml_data.getroot()
root.append(existing_xml.find('vendor'))
raise ValueError('"%s" is not a message attribute' % attribute)
data = self.parse_value(value)
mfile = os.path.join(cdir, attribute)
- current = open(mfile, 'r').read() if os.path.exists(mfile) else ''
+ if os.path.exists(mfile):
+ with open(mfile, 'r') as f:
+ current = f.read()
+ else:
+ current = ''
# Only overwrite the msg if it hasn't been modified. It may have been
# modified by another GPO.
if 'new_val' not in data or current.strip() == data['new_val'].strip():
e.valuename)
mfile = os.path.join(cdir, e.valuename)
if os.path.exists(mfile):
- old_val = open(mfile, 'r').read()
+ with open(mfile, 'r') as f:
+ old_val = f.read()
else:
old_val = ''
# If policy is already applied, skip application
w.write(contents)
if visudo is None:
raise FileNotFoundError('visudo not found, please install it')
- sudo_validation = \
- Popen([visudo, '-c', '-f', f.name],
- stdout=PIPE, stderr=PIPE).wait()
+ with Popen([visudo, '-c', '-f', f.name],
+ stdout=PIPE, stderr=PIPE) as proc:
+ sudo_validation = proc.wait()
if sudo_validation == 0:
with NamedTemporaryFile(prefix='gp_',
delete=False,
class gp_inf_ext(gp_ext):
def read(self, data_file):
- policy = open(data_file, 'rb').read()
+ with open(data_file, 'rb') as f:
+ policy = f.read()
inf_conf = ConfigParser(interpolation=None)
inf_conf.optionxform = str
try:
class gp_pol_ext(gp_ext):
def read(self, data_file):
- raw = open(data_file, 'rb').read()
+ with open(data_file, 'rb') as f:
+ raw = f.read()
return ndr_unpack(preg.file, raw)
class gp_xml_ext(gp_ext):
def read(self, data_file):
- raw = open(data_file, 'rb').read()
+ with open(data_file, 'rb') as f:
+ raw = f.read()
try:
return etree.fromstring(raw.decode())
except UnicodeDecodeError:
raise ValueError('"%s" is not a message attribute' % attribute)
msg = value
data = self.parse_value(value)
- current = open(issue, 'r').read() if os.path.exists(issue) else ''
+ if os.path.exists(issue):
+ with open(issue, 'r') as f:
+ current = f.read()
+ else:
+ current = ''
# Only overwrite the msg if it hasn't been modified. It may have been
# modified by another GPO.
if 'new_val' not in data or current.strip() == data['new_val'].strip():
self.cache_remove_attribute(guid, attribute)
def apply(self, guid, issue, text):
- current = open(issue, 'r').read() if os.path.exists(issue) else ''
+ if os.path.exists(issue):
+ with open(issue, 'r') as f:
+ current = f.read()
+ else:
+ current = ''
if current != text.text:
with open(issue, 'w') as w:
w.write(text.text)
raise ValueError('"%s" is not a message attribute' % attribute)
msg = value
data = self.parse_value(value)
- current = open(motd, 'r').read() if os.path.exists(motd) else ''
+ if os.path.exists(motd):
+ with open(motd, 'r') as f:
+ current = f.read()
+ else:
+ current = ''
# Only overwrite the msg if it hasn't been modified. It may have been
# modified by another GPO.
if 'new_val' not in data or current.strip() == data['new_val'].strip():
self.cache_remove_attribute(guid, attribute)
def apply(self, guid, motd, text):
- current = open(motd, 'r').read() if os.path.exists(motd) else ''
+ if os.path.exists(motd):
+ with open(motd, 'r') as f:
+ current = f.read()
+ else:
+ current = ''
if current != text.text:
with open(motd, 'w') as w:
w.write(text.text)