"fd" is apparently not pythonic

This commit is contained in:
Disassembler 2018-10-02 22:09:34 +02:00
parent 30254eb527
commit ab78d18491
Signed by: Disassembler
GPG Key ID: 524BD33A0EE29499
4 changed files with 34 additions and 34 deletions

View File

@ -115,8 +115,8 @@ def get_unused_ip():
# This is a poor man's DHCP server which uses /etc/hosts as lease database
# Leases the first unused IP from range 172.17.0.0/16
leased = []
with open('/etc/hosts', 'r') as fd:
for line in fd.read().splitlines():
with open('/etc/hosts', 'r') as f:
for line in f.read().splitlines():
if line.startswith('172.17'):
ip = line.split()[0].split('.')
leased.append(int(ip[2]) * 256 + int(ip[3]))
@ -127,14 +127,14 @@ def get_unused_ip():
def update_hosts_lease(ip, app):
hosts = []
with open('/etc/hosts', 'r') as fd:
for line in fd:
with open('/etc/hosts', 'r') as f:
for line in f:
if not line.strip().endswith(' {}'.format(app)):
hosts.append(line)
if ip:
hosts.append('{} {}\n'.format(ip, app))
with open('/etc/hosts', 'w') as fd:
fd.writelines(hosts)
with open('/etc/hosts', 'w') as f:
f.writelines(hosts)
def set_container_ip(pid, ip):
# Set IP in container based on PID given via lxc.hook.start-host hook

View File

@ -7,9 +7,9 @@ def fix_installed(layers):
installed = []
for layer in layers[:-1]:
try:
with open(os.path.join(layer, 'lib/apk/db/installed'), 'r') as fd:
with open(os.path.join(layer, 'lib/apk/db/installed'), 'r') as f:
buffer = []
for line in fd:
for line in f:
if line.startswith('C:'):
buffer = ''.join(buffer)
if buffer not in installed:
@ -22,22 +22,22 @@ def fix_installed(layers):
except:
continue
os.makedirs(os.path.join(layers[-1], 'lib/apk/db'), 0o755, True)
with open(os.path.join(layers[-1], 'lib/apk/db/installed'), 'w') as fd:
fd.writelines(installed)
with open(os.path.join(layers[-1], 'lib/apk/db/installed'), 'w') as f:
f.writelines(installed)
def fix_world(layers):
world = []
for layer in layers[:-1]:
try:
with open(os.path.join(layer, 'etc/apk/world'), 'r') as fd:
for line in fd:
with open(os.path.join(layer, 'etc/apk/world'), 'r') as f:
for line in f:
if line not in world:
world.append(line)
except:
continue
os.makedirs(os.path.join(layers[-1], 'etc/apk'), 0o755, True)
with open(os.path.join(layers[-1], 'etc/apk/world'), 'w') as fd:
fd.writelines(world)
with open(os.path.join(layers[-1], 'etc/apk/world'), 'w') as f:
f.writelines(world)
fix_installed(sys.argv[1:])
fix_world(sys.argv[1:])

View File

@ -69,8 +69,8 @@ class LXCImage:
self.lxcfile = os.path.join(self.build_dir, 'lxcfile')
def build(self):
with open(self.lxcfile, 'r') as fd:
lxcfile = [l.strip() for l in fd.readlines()]
with open(self.lxcfile, 'r') as f:
lxcfile = [l.strip() for l in f.readlines()]
script = []
script_eof = None
@ -120,16 +120,16 @@ class LXCImage:
rootfs = 'overlay:{}:{}'.format(':'.join(self.layers[:-1][::-1]), self.layers[-1])
mounts = '\n'.join(self.mounts)
env = '\n'.join(self.env)
with open(os.path.join(LXC_ROOT, self.name, 'config'), 'w') as fd:
fd.write(CONFIG_TEMPLATE.format(name=self.name,
with open(os.path.join(LXC_ROOT, self.name, 'config'), 'w') as f:
f.write(CONFIG_TEMPLATE.format(name=self.name,
rootfs=rootfs, mounts=mounts, env=env,
uid=self.uid, gid=self.gid,
cmd=self.cmd, cwd=self.cwd, halt=self.halt))
def run_script(self, script):
sh = os.path.join(self.layers[-1], 'run.sh')
with open(sh, 'w') as fd:
fd.write('#!/bin/sh\nset -ev\n\n{}\n'.format('\n'.join(script)))
with open(sh, 'w') as f:
f.write('#!/bin/sh\nset -ev\n\n{}\n'.format('\n'.join(script)))
os.chmod(sh, 0o700)
subprocess.run(['lxc-execute', '-n', self.name, '--', '/bin/sh', '-lc', '/run.sh'], check=True)
os.unlink(sh)

View File

@ -19,8 +19,8 @@ def pack(pkg_file):
pkg_file = os.path.join(pkg_file, 'pkg')
# Prepare metadata
meta = {}
with open(pkg_file) as fd:
for line in fd:
with open(pkg_file) as f:
for line in f:
line = [l.strip() for l in line.split(':', 1)]
meta[line[0]] = line[1]
meta['deps'] = meta['deps'].split()
@ -51,26 +51,26 @@ def pack(pkg_file):
packages = {}
packages_file = os.path.join(BUILD_ROOT, 'packages')
if os.path.exists(packages_file):
with open(packages_file, 'r') as fd:
packages = json.load(fd)
with open(packages_file, 'r') as f:
packages = json.load(f)
packages[pkg_name] = meta
with open(packages_file, 'w') as fd:
json.dump(packages, fd, sort_keys=True, indent=4)
with open(packages_file, 'w') as f:
json.dump(packages, f, sort_keys=True, indent=4)
# Sign packages
print('Signing packages')
with open(os.path.join(BUILD_ROOT, 'packages.key'), 'rb') as fd:
priv_key = load_pem_private_key(fd.read(), None, default_backend())
with open(os.path.join(BUILD_ROOT, 'packages'), 'rb') as fd:
data = fd.read()
with open(os.path.join(BUILD_ROOT, 'packages.sha512'), 'wb') as fd:
fd.write(priv_key.sign(data, ec.ECDSA(hashes.SHA512())))
with open(os.path.join(BUILD_ROOT, 'packages.key'), 'rb') as f:
priv_key = load_pem_private_key(f.read(), None, default_backend())
with open(os.path.join(BUILD_ROOT, 'packages'), 'rb') as f:
data = f.read()
with open(os.path.join(BUILD_ROOT, 'packages.sig'), 'wb') as f:
f.write(priv_key.sign(data, ec.ECDSA(hashes.SHA512())))
def hash_file(file_path):
sha512 = hashlib.sha512()
with open(file_path, 'rb') as fd:
with open(file_path, 'rb') as f:
while True:
data = fd.read(65536)
data = f.read(65536)
if not data:
break
sha512.update(data)