"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 # 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 # Leases the first unused IP from range 172.17.0.0/16
leased = [] leased = []
with open('/etc/hosts', 'r') as fd: with open('/etc/hosts', 'r') as f:
for line in fd.read().splitlines(): for line in f.read().splitlines():
if line.startswith('172.17'): if line.startswith('172.17'):
ip = line.split()[0].split('.') ip = line.split()[0].split('.')
leased.append(int(ip[2]) * 256 + int(ip[3])) leased.append(int(ip[2]) * 256 + int(ip[3]))
@ -127,14 +127,14 @@ def get_unused_ip():
def update_hosts_lease(ip, app): def update_hosts_lease(ip, app):
hosts = [] hosts = []
with open('/etc/hosts', 'r') as fd: with open('/etc/hosts', 'r') as f:
for line in fd: for line in f:
if not line.strip().endswith(' {}'.format(app)): if not line.strip().endswith(' {}'.format(app)):
hosts.append(line) hosts.append(line)
if ip: if ip:
hosts.append('{} {}\n'.format(ip, app)) hosts.append('{} {}\n'.format(ip, app))
with open('/etc/hosts', 'w') as fd: with open('/etc/hosts', 'w') as f:
fd.writelines(hosts) f.writelines(hosts)
def set_container_ip(pid, ip): def set_container_ip(pid, ip):
# Set IP in container based on PID given via lxc.hook.start-host hook # 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 = [] installed = []
for layer in layers[:-1]: for layer in layers[:-1]:
try: 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 = [] buffer = []
for line in fd: for line in f:
if line.startswith('C:'): if line.startswith('C:'):
buffer = ''.join(buffer) buffer = ''.join(buffer)
if buffer not in installed: if buffer not in installed:
@ -22,22 +22,22 @@ def fix_installed(layers):
except: except:
continue continue
os.makedirs(os.path.join(layers[-1], 'lib/apk/db'), 0o755, True) 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: with open(os.path.join(layers[-1], 'lib/apk/db/installed'), 'w') as f:
fd.writelines(installed) f.writelines(installed)
def fix_world(layers): def fix_world(layers):
world = [] world = []
for layer in layers[:-1]: for layer in layers[:-1]:
try: try:
with open(os.path.join(layer, 'etc/apk/world'), 'r') as fd: with open(os.path.join(layer, 'etc/apk/world'), 'r') as f:
for line in fd: for line in f:
if line not in world: if line not in world:
world.append(line) world.append(line)
except: except:
continue continue
os.makedirs(os.path.join(layers[-1], 'etc/apk'), 0o755, True) os.makedirs(os.path.join(layers[-1], 'etc/apk'), 0o755, True)
with open(os.path.join(layers[-1], 'etc/apk/world'), 'w') as fd: with open(os.path.join(layers[-1], 'etc/apk/world'), 'w') as f:
fd.writelines(world) f.writelines(world)
fix_installed(sys.argv[1:]) fix_installed(sys.argv[1:])
fix_world(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') self.lxcfile = os.path.join(self.build_dir, 'lxcfile')
def build(self): def build(self):
with open(self.lxcfile, 'r') as fd: with open(self.lxcfile, 'r') as f:
lxcfile = [l.strip() for l in fd.readlines()] lxcfile = [l.strip() for l in f.readlines()]
script = [] script = []
script_eof = None script_eof = None
@ -120,16 +120,16 @@ class LXCImage:
rootfs = 'overlay:{}:{}'.format(':'.join(self.layers[:-1][::-1]), self.layers[-1]) rootfs = 'overlay:{}:{}'.format(':'.join(self.layers[:-1][::-1]), self.layers[-1])
mounts = '\n'.join(self.mounts) mounts = '\n'.join(self.mounts)
env = '\n'.join(self.env) env = '\n'.join(self.env)
with open(os.path.join(LXC_ROOT, self.name, 'config'), 'w') as fd: with open(os.path.join(LXC_ROOT, self.name, 'config'), 'w') as f:
fd.write(CONFIG_TEMPLATE.format(name=self.name, f.write(CONFIG_TEMPLATE.format(name=self.name,
rootfs=rootfs, mounts=mounts, env=env, rootfs=rootfs, mounts=mounts, env=env,
uid=self.uid, gid=self.gid, uid=self.uid, gid=self.gid,
cmd=self.cmd, cwd=self.cwd, halt=self.halt)) cmd=self.cmd, cwd=self.cwd, halt=self.halt))
def run_script(self, script): def run_script(self, script):
sh = os.path.join(self.layers[-1], 'run.sh') sh = os.path.join(self.layers[-1], 'run.sh')
with open(sh, 'w') as fd: with open(sh, 'w') as f:
fd.write('#!/bin/sh\nset -ev\n\n{}\n'.format('\n'.join(script))) f.write('#!/bin/sh\nset -ev\n\n{}\n'.format('\n'.join(script)))
os.chmod(sh, 0o700) os.chmod(sh, 0o700)
subprocess.run(['lxc-execute', '-n', self.name, '--', '/bin/sh', '-lc', '/run.sh'], check=True) subprocess.run(['lxc-execute', '-n', self.name, '--', '/bin/sh', '-lc', '/run.sh'], check=True)
os.unlink(sh) os.unlink(sh)

View File

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