Skip to content

Commit

Permalink
Fix code style.
Browse files Browse the repository at this point in the history
  • Loading branch information
WangYi authored and RoliSoft committed Jan 23, 2019
1 parent f8cf2aa commit 394a0d5
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
print('%s[*]%s Probing the Linux subsystem...' % (Fore.GREEN, Fore.RESET))

basedir, lxpath, bashpath = probe_wsl()
#fix basedir to add LocalState\rootfs
# fix basedir to add LocalState\rootfs
basedir = os.path.join(basedir, 'LocalState')
rootFsDir = os.path.join(basedir, 'rootfs')
rootFsTempDir = os.path.join(basedir, 'rootfs-temp')
rootfsdir = os.path.join(basedir, 'rootfs')
rootfstempdir = os.path.join(basedir, 'rootfs-temp')

print('%s[*]%s Linux subsystem OK.' % (Fore.GREEN, Fore.RESET))

Expand All @@ -70,7 +70,7 @@
else:
homedir = '/home/' + user

homedirFQDN = os.path.join(rootFsDir, homedir.lstrip('/'))
homedirFQDN = os.path.join(rootfsdir, homedir.lstrip('/'))

if len(homedir) == 0 or not os.path.isdir(homedirFQDN):
print('%s[!]%s Failed to get home directory of default user in WSL: Returned path %s%s%s is not valid.' % (Fore.RED, Fore.RESET, Fore.BLUE, homedirFQDN, Fore.RESET))
Expand Down Expand Up @@ -101,7 +101,7 @@

if not isroot:
try:
with open(os.path.join(rootFsDir, 'etc', 'passwd'), newline='\n') as f:
with open(os.path.join(rootfsdir, 'etc', 'passwd'), newline='\n') as f:
for line in f.readlines():
if line.startswith(user + ':'):
etcpasswduser = line.strip()
Expand All @@ -111,7 +111,7 @@
sys.exit(-1)

try:
with open(os.path.join(rootFsDir, 'etc', 'shadow'), newline='\n') as f:
with open(os.path.join(rootfsdir, 'etc', 'shadow'), newline='\n') as f:
for line in f.readlines():
if line.startswith('root:'):
etcshadowroot = line.strip()
Expand All @@ -124,7 +124,7 @@

if not isroot:
try:
with open(os.path.join(rootFsDir, 'etc', 'group'), newline='\n') as f:
with open(os.path.join(rootfsdir, 'etc', 'group'), newline='\n') as f:
for line in f.readlines():
if line.startswith(user + ':'):
etcgroupuser = line.strip()
Expand All @@ -134,7 +134,7 @@
sys.exit(-1)

try:
with open(os.path.join(rootFsDir, 'etc', 'gshadow'), newline='\n') as f:
with open(os.path.join(rootfsdir, 'etc', 'gshadow'), newline='\n') as f:
for line in f.readlines():
if line.startswith(user + ':'):
etcgshadowuser = line.strip()
Expand All @@ -155,21 +155,21 @@
etcshadowroot = parts[1]

# remove old remnants
if os.path.exists(rootFsTempDir):
if os.path.exists(rootfstempdir):
print('%s[*]%s Removing leftover %srootfs-temp%s...' % (Fore.GREEN, Fore.RESET, Fore.BLUE, Fore.RESET))

try:
def retry_rw(operation, name, exc):
os.chmod(name, stat.S_IWRITE)
operation(name)

shutil.rmtree(rootFsTempDir, onerror = retry_rw)
shutil.rmtree(rootfstempdir, onerror = retry_rw)

except Exception:
pass

# ensure it's removed
if os.path.exists(rootFsTempDir):
if os.path.exists(rootfstempdir):
print('%s[*]%s Failed to remove leftover %srootfs-temp%s.' % (Fore.RED, Fore.RESET, Fore.BLUE, Fore.RESET))
sys.exit(-1)
# extract archive
Expand All @@ -182,7 +182,7 @@ def retry_rw(operation, name, exc):

try:
img = PySquashfsImage.SquashFsImage(fname)
path = rootFsTempDir
path = rootfstempdir

hide_cursor()
ntfsea.init()
Expand Down Expand Up @@ -250,7 +250,7 @@ def retry_rw(operation, name, exc):

try:
ntfsea.init()
path = rootFsTempDir
path = rootfstempdir
with tarfile.open(fileobj = fileobj, mode = 'r:*', dereference = True, ignore_zeros = True, errorlevel = 2) as tar:

file = tar.next()
Expand Down Expand Up @@ -341,7 +341,7 @@ def retry_rw(operation, name, exc):

# read label of current distribution

clabel = get_label(rootFsDir)
clabel = get_label(rootfsdir)

if not clabel:
clabel = 'ubuntu_trusty'
Expand All @@ -352,7 +352,7 @@ def retry_rw(operation, name, exc):
print('%s[*]%s Backing up current %srootfs%s to %srootfs_%s%s...' % (Fore.GREEN, Fore.RESET, Fore.BLUE, Fore.RESET, Fore.BLUE, clabel, Fore.RESET))

try:
subprocess.check_output(['cmd', '/C', 'move', path_trans(rootFsDir), path_trans(os.path.join(basedir, 'rootfs_' + clabel))])
subprocess.check_output(['cmd', '/C', 'move', path_trans(rootfsdir), path_trans(os.path.join(basedir, 'rootfs_' + clabel))])

except subprocess.CalledProcessError as err:
print('%s[!]%s Failed to backup current %srootfs%s: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, Fore.RESET, err))
Expand All @@ -363,14 +363,14 @@ def retry_rw(operation, name, exc):
time.sleep(4)

try:
subprocess.check_output(['cmd', '/C', 'move', path_trans(rootFsTempDir), path_trans(rootFsDir)])
subprocess.check_output(['cmd', '/C', 'move', path_trans(rootfstempdir), path_trans(rootfsdir)])

except subprocess.CalledProcessError as err:
print('%s[!]%s Failed to switch to new %srootfs%s: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, Fore.RESET, err))
print('%s[*]%s Rolling back to old %srootfs%s...' % (Fore.YELLOW, Fore.RESET, Fore.BLUE, Fore.RESET))

try:
subprocess.check_output(['cmd', '/C', 'move', path_trans(os.path.join(basedir, 'rootfs_' + clabel)), path_trans(rootFsDir)])
subprocess.check_output(['cmd', '/C', 'move', path_trans(os.path.join(basedir, 'rootfs_' + clabel)), path_trans(rootfsdir)])

except subprocess.CalledProcessError as err:
print('%s[!]%s Failed to roll back to old %srootfs%s: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, Fore.RESET, err))
Expand All @@ -381,7 +381,7 @@ def retry_rw(operation, name, exc):
# save label

try:
with open(os.path.join(rootFsDir, '.switch_label'), 'w') as f:
with open(os.path.join(rootfsdir, '.switch_label'), 'w') as f:
f.write(label + '\n')

except OSError as err:
Expand All @@ -392,7 +392,7 @@ def retry_rw(operation, name, exc):

if not isroot:
try:
with open(os.path.join(rootFsDir, 'etc', 'passwd'), 'a', newline='\n') as f:
with open(os.path.join(rootfsdir, 'etc', 'passwd'), 'a', newline='\n') as f:
f.write(etcpasswduser + '\n')
#sudo not installed via image
#with open(os.path.join(rootFsDir, 'etc', 'sudoers'), 'a', newline='\n') as f:
Expand All @@ -404,7 +404,7 @@ def retry_rw(operation, name, exc):
try:
shadows = []

with open(os.path.join(rootFsDir, 'etc', 'shadow'), 'r+', newline='\n') as f:
with open(os.path.join(rootfsdir, 'etc', 'shadow'), 'r+', newline='\n') as f:
shadows = f.readlines()

if etcshadowroot:
Expand All @@ -429,14 +429,14 @@ def retry_rw(operation, name, exc):

if not isroot:
try:
with open(os.path.join(rootFsDir, 'etc', 'group'), 'a', newline='\n') as f:
with open(os.path.join(rootfsdir, 'etc', 'group'), 'a', newline='\n') as f:
f.write(etcgroupuser + '\n')

except OSError as err:
print('%s[!]%s Failed to open file %s/etc/group%s for writing: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, Fore.RESET, err))

try:
with open(os.path.join(rootFsDir, 'etc', 'gshadow'), 'a', newline='\n') as f:
with open(os.path.join(rootfsdir, 'etc', 'gshadow'), 'a', newline='\n') as f:
f.write(etcgshadowuser + '\n')

except OSError as err:
Expand Down Expand Up @@ -470,7 +470,7 @@ def retry_rw(operation, name, exc):
sys.exit(-1)

homedir = '/root'
homedirFQDN = os.path.join(rootFsDir, homedir.lstrip('/'))
homedirFQDN = os.path.join(rootfsdir, homedir.lstrip('/'))

if not os.path.isdir(homedirFQDN):
print('%s[!]%s Failed to get home directory of default user in WSL: Returned path %s%s%s is not valid.' % (Fore.RED, Fore.RESET, Fore.BLUE, homedirFQDN, Fore.RESET))
Expand Down

0 comments on commit 394a0d5

Please sign in to comment.