Added Linux functions for building UFDs

This commit is contained in:
2Shirt 2020-01-06 20:27:59 -07:00
parent 8f31e5bd67
commit c135d686df
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
2 changed files with 44 additions and 30 deletions

View file

@ -71,31 +71,6 @@ def find_first_partition(dev_path):
return part_path
def get_user_home(user):
"""Get path to user's home dir, returns str."""
home_dir = None
cmd = ['getent', 'passwd', user]
result = run_program(cmd, encoding='utf-8', errors='ignore', check=False)
try:
home_dir = result.stdout.split(':')[5]
except Exception:
# Just use HOME from ENV (or '/root' if that fails)
home_dir = os.environ.get('HOME', '/root')
return home_dir
def get_user_name():
"""Get real user name, returns str."""
user = None
if 'SUDO_USER' in os.environ:
user = os.environ.get('SUDO_USER', 'Unknown')
else:
user = os.environ.get('USER', 'Unknown')
return user
def hide_items(ufd_dev, items):
"""Set FAT32 hidden flag for items."""
# pylint: disable=invalid-name
@ -231,11 +206,6 @@ def remove_arch():
shutil.rmtree(find_path('/mnt/UFD/arch'))
def running_as_root():
"""Check if running with effective UID of 0, returns bool."""
return os.geteuid() == 0
def show_selections(args, sources, ufd_dev, ufd_sources):
"""Show selections including non-specified options."""

View file

@ -2,6 +2,7 @@
# vim: sts=2 sw=2 ts=2
import logging
import os
import pathlib
import re
import subprocess
@ -17,6 +18,44 @@ UUID_CORESTORAGE = '53746f72-6167-11aa-aa11-00306543ecac'
# Functions
def get_user_home(user):
"""Get path to user's home dir, returns pathlib.Path obj."""
home = None
# Get path from user details
cmd = ['getent', 'passwd', user]
proc = run_program(cmd, check=False)
try:
home = proc.stdout.split(':')[5]
except IndexError:
# Try using environment variable
home = os.environ.get('HOME')
# Raise exception if necessary
if not home:
raise RuntimeError(f'Failed to find home for: {user}')
# Done
return pathlib.Path(home)
def get_user_name():
"""Get real user name, returns str."""
user = None
# Query environment
user = os.environ.get('SUDO_USER')
if not user:
user = os.environ.get('USER')
# Raise exception if necessary
if not user:
raise RuntimeError('Failed to determine user')
# Done
return user
def make_temp_file():
"""Make temporary file, returns pathlib.Path() obj."""
proc = run_program(['mktemp'], check=False)
@ -111,6 +150,11 @@ def mount_volumes(device_path=None, read_write=False, scan_corestorage=False):
return report
def running_as_root():
"""Check if running with effective UID of 0, returns bool."""
return os.geteuid() == 0
def scan_corestorage_container(container, timeout=300):
"""Scan CoreStorage container for inner volumes, returns list."""
# TODO: Test Scanning CoreStorage containers