Skip to content

Commit

Permalink
Replaced builtins.exit() calls to sys.exit()
Browse files Browse the repository at this point in the history
  • Loading branch information
RoliSoft committed Oct 22, 2016
1 parent 83c137a commit 546e399
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
14 changes: 7 additions & 7 deletions get-prebuilt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

if len(sys.argv) < 2:
print('usage: ./get-prebuilt.py image[:tag]')
exit(-1)
sys.exit(-1)

image, tag, fname, label = parse_image_arg(sys.argv[1], False)

Expand All @@ -29,11 +29,11 @@

except urllib.error.HTTPError as err:
print('%s[!]%s Failed to authorization token: %s' % (Fore.RED, Fore.RESET, err))
exit(-1)
sys.exit(-1)

except KeyError as err:
print('%s[!]%s Failed to authorization token: %s' % (Fore.RED, Fore.RESET, err))
exit(-1)
sys.exit(-1)

# get the image manifest

Expand All @@ -51,11 +51,11 @@

if len(manifest['fsLayers']) == 0:
print('%s[!]%s Manifest for image %s%s%s has no layers.' % (Fore.RED, Fore.RESET, Fore.BLUE, image, Fore.RESET))
exit(-1)
sys.exit(-1)

except urllib.error.HTTPError as err:
print('%s[!]%s Failed to fetch manifest info for %s%s%s: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, image, Fore.RESET, err))
exit(-1)
sys.exit(-1)

# download the layers

Expand Down Expand Up @@ -84,11 +84,11 @@
except urllib.error.HTTPError as err:
clear_progress()
print('%s[!]%s Failed to download layer %s%s%s: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, layer['blobSum'], Fore.RESET, err))
exit(-1)
sys.exit(-1)

except OSError as err:
clear_progress()
print('%s[!]%s Failed to open file %s%s%s for writing: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, fname, Fore.RESET, err))
exit(-1)
sys.exit(-1)

print('%s[*]%s Rootfs archive for %s%s%s:%s%s%s saved to %s%s%s.' % (Fore.GREEN, Fore.RESET, Fore.YELLOW, image, Fore.RESET, Fore.YELLOW, tag, Fore.RESET, Fore.GREEN, fname, Fore.RESET))
14 changes: 7 additions & 7 deletions get-source.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

if len(sys.argv) < 2:
print('usage: ./get-source.py image[:tag]')
exit(-1)
sys.exit(-1)

image, tag, fname, label = parse_image_arg(sys.argv[1], False)

Expand Down Expand Up @@ -104,12 +104,12 @@

if not dfurl:
print('%s[!]%s Failed to find tag %s%s%s for image %s%s%s.' % (Fore.RED, Fore.RESET, Fore.BLUE, tag, Fore.RESET, Fore.BLUE, image, Fore.RESET))
exit(-1)
sys.exit(-1)

except urllib.error.HTTPError as err:
print('%s[!]%s Failed to fetch official-images info for %s%s%s: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, image, Fore.RESET, err))
print('%s[!]%s If this is not an official image, try getting it with %sget-prebuilt.py %s%s.' % (Fore.RED, Fore.RESET, Fore.GREEN, sys.argv[1].strip(), Fore.RESET))
exit(-1)
sys.exit(-1)

# process Dockerfile

Expand All @@ -134,11 +134,11 @@

if not tgurl:
print('%s[!]%s Failed to find a suitable rootfs specification in Dockerfile.' % (Fore.RED, Fore.RESET))
exit(-1)
sys.exit(-1)

except urllib.error.HTTPError as err:
print('%s[!]%s Failed to fetch Dockerfile from %s%s%s: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, dfurl, Fore.RESET, err))
exit(-1)
sys.exit(-1)

# download rootfs archive

Expand All @@ -151,11 +151,11 @@
except urllib.error.HTTPError as err:
clear_progress()
print('%s[!]%s Failed to download archive from %s%s%s: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, tgurl, Fore.RESET, err))
exit(-1)
sys.exit(-1)

except OSError as err:
clear_progress()
print('%s[!]%s Failed to open file %s%s%s for writing: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, fname, Fore.RESET, err))
exit(-1)
sys.exit(-1)

print('%s[*]%s Rootfs archive for %s%s%s:%s%s%s saved to %s%s%s.' % (Fore.GREEN, Fore.RESET, Fore.YELLOW, image, Fore.RESET, Fore.YELLOW, tag, Fore.RESET, Fore.GREEN, fname, Fore.RESET))
40 changes: 20 additions & 20 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
if not imgarg:
print('usage: ./install.py [--no-hooks] image[:tag] | tarball | squashfs')
print('\noptions:\n --no-hooks Omits running the hook scripts.')
exit(-1)
sys.exit(-1)

image, tag, fname, label = parse_image_arg(imgarg, True)

Expand All @@ -60,15 +60,15 @@

if not os.path.isfile(out):
print('%s[!]%s Failed to get home directory of default user in WSL: Output file %s%s%s not present.' % (Fore.RED, Fore.RESET, Fore.BLUE, out, Fore.RESET))
exit(-1)
sys.exit(-1)

with open(out) as f:
homedir = f.readline().strip()
homedirw = os.path.join(basedir, homedir.lstrip('/'))

if len(homedir) == 0 or not os.path.isdir(homedirw):
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, homedirw, Fore.RESET))
exit(-1)
sys.exit(-1)

user = f.readline().strip()
isroot = user == 'root'
Expand All @@ -79,15 +79,15 @@

except subprocess.CalledProcessError as err:
print('%s[!]%s Failed to get home directory of default user in WSL: %s' % (Fore.RED, Fore.RESET, err))
exit(-1)
sys.exit(-1)

# check squashfs prerequisites

fext = os.path.splitext(fname)[-1].lower()

if (fext == '.sfs' or fext == '.squashfs') and not havesquashfs:
print('%s[!]%s Module %sPySquashfsImage%s is not available. Install it with %spip3 install PySquashfsImage%s for SquashFS support.' % (Fore.RED, Fore.RESET, Fore.GREEN, Fore.RESET, Fore.GREEN, Fore.RESET))
exit(-1)
sys.exit(-1)

# get /etc/{passwd,shadow,group,gshadow} entries

Expand All @@ -108,7 +108,7 @@

except OSError as err:
print('%s[!]%s Failed to open file %s/etc/passwd%s: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, Fore.RESET, err))
exit(-1)
sys.exit(-1)

try:
with open(os.path.join(basedir, 'rootfs', 'etc', 'shadow'), newline='\n') as f:
Expand All @@ -120,7 +120,7 @@

except OSError as err:
print('%s[!]%s Failed to open file %s/etc/shadow%s: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, Fore.RESET, err))
exit(-1)
sys.exit(-1)

if not isroot:
try:
Expand All @@ -131,7 +131,7 @@

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

try:
with open(os.path.join(basedir, 'rootfs', 'etc', 'gshadow'), newline='\n') as f:
Expand All @@ -141,7 +141,7 @@

except OSError as err:
print('%s[!]%s Failed to open file %s/etc/gshadow%s: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, Fore.RESET, err))
exit(-1)
sys.exit(-1)

if etcshadowroot:
parts = etcshadowroot.split(':')
Expand Down Expand Up @@ -169,7 +169,7 @@ def retry_rw(operation, name, exc):

if os.path.exists(os.path.join(homedirw, 'rootfs-temp')):
print('%s[*]%s Failed to remove leftover %srootfs-temp%s.' % (Fore.RED, Fore.RESET, Fore.BLUE, Fore.RESET))
exit(-1)
sys.exit(-1)

# extract archive

Expand Down Expand Up @@ -258,7 +258,7 @@ def retry_rw(operation, name, exc):
if file is None:
clear_progress()
print('%s[!]%s Failed to extract archive: unable to determine archive type.' % (Fore.RED, Fore.RESET))
exit(-1)
sys.exit(-1)

while file is not None:
try:
Expand Down Expand Up @@ -331,7 +331,7 @@ def retry_rw(operation, name, exc):
except Exception as err:
clear_progress()
print('%s[!]%s Failed to extract archive: %s' % (Fore.RED, Fore.RESET, err))
exit(-1)
sys.exit(-1)

finally:
clear_progress()
Expand All @@ -354,7 +354,7 @@ def retry_rw(operation, name, exc):

except subprocess.CalledProcessError as err:
print('%s[!]%s Failed to backup current %srootfs%s: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, Fore.RESET, err))
exit(-1)
sys.exit(-1)

print('%s[*]%s Switching to new %srootfs%s...' % (Fore.GREEN, Fore.RESET, Fore.BLUE, Fore.RESET))

Expand All @@ -374,7 +374,7 @@ def retry_rw(operation, name, exc):
print('%s[!]%s Failed to roll back to old %srootfs%s: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, Fore.RESET, err))
print('%s[!]%s You are now the proud owner of one broken Linux subsystem! To fix it, run %slxrun /uninstall%s and %slxrun /install%s from the command prompt.' % (Fore.RED, Fore.RESET, Fore.GREEN, Fore.RESET, Fore.GREEN, Fore.RESET))

exit(-1)
sys.exit(-1)

# save label

Expand Down Expand Up @@ -460,7 +460,7 @@ def retry_rw(operation, name, exc):

except subprocess.CalledProcessError as err:
print('%s[!]%s Failed to switch default user in WSL: %s' % (Fore.RED, Fore.RESET, err))
exit(-1)
sys.exit(-1)

try:
subprocess.check_call(['cmd', '/C', lxpath + '\\bash.exe', '-c',
Expand All @@ -469,27 +469,27 @@ def retry_rw(operation, name, exc):

if not os.path.isfile(out):
print('%s[!]%s Failed to get home directory of default user in WSL: Output file %s%s%s not present.' % (Fore.RED, Fore.RESET, Fore.BLUE, out, Fore.RESET))
exit(-1)
sys.exit(-1)

with open(out) as f:
homedir = f.readline().strip()
homedirw = os.path.join(basedir, homedir.lstrip('/'))

if len(homedir) == 0 or not os.path.isdir(homedirw):
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, homedirw, Fore.RESET))
exit(-1)
sys.exit(-1)

user2 = f.readline().strip()

if user2 != 'root':
print('%s[!]%s Failed to switch default user to %sroot%s.' % (Fore.RED, Fore.RESET, Fore.YELLOW, Fore.RESET))
exit(-1)
sys.exit(-1)

os.unlink(out)

except subprocess.CalledProcessError as err:
print('%s[!]%s Failed to get home directory of default user in WSL: %s' % (Fore.RED, Fore.RESET, err))
exit(-1)
sys.exit(-1)

# since we switched to root, switch back to regular user on exit

Expand All @@ -501,7 +501,7 @@ def switch_user_back(user):

except subprocess.CalledProcessError as err:
print('%s[!]%s Failed to switch default user in WSL: %s' % (Fore.RED, Fore.RESET, err))
exit(-1)
sys.exit(-1)

atexit.register(switch_user_back, user)

Expand Down
12 changes: 6 additions & 6 deletions switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
else:
print('You do not seem to have the default distribution installed anymore.\nTo reinstall it, run %slxrun /uninstall%s and %slxrun /install%s from the command prompt.' % (Fore.GREEN, Fore.RESET, Fore.GREEN, Fore.RESET))

exit(-1)
sys.exit(-1)

image, tag, fname, label = parse_image_arg(sys.argv[1], False)

Expand All @@ -71,19 +71,19 @@
if label == clabel:
print('%s[!]%s No %s/.switch_label%s found, and the target rootfs is %subuntu%s:%strusty%s. Cannot continue.' % (Fore.RED, Fore.RESET, Fore.BLUE, Fore.RESET, Fore.YELLOW, Fore.RESET, Fore.YELLOW, Fore.RESET))
print('%s[!]%s To fix this, run %secho some_tag > /.switch_label%s (replacing %ssome_tag%s with something like %sdebian_sid%s) from the current Bash terminal.' % (Fore.RED, Fore.RESET, Fore.GREEN, Fore.RESET, Fore.GREEN, Fore.RESET, Fore.GREEN, Fore.RESET))
exit(-1)
sys.exit(-1)
else:
print('%s[!]%s No %s/.switch_label%s found, assuming current rootfs is %subuntu%s:%strusty%s.' % (Fore.RED, Fore.RESET, Fore.BLUE, Fore.RESET, Fore.YELLOW, Fore.RESET, Fore.YELLOW, Fore.RESET))

# sanity checks, take two

if clabel == label:
print('%s[!]%s The %s%s%s:%s%s%s rootfs is the current installation.' % (Fore.YELLOW, Fore.RESET, Fore.YELLOW, image, Fore.RESET, Fore.YELLOW, tag, Fore.RESET))
exit(-1)
sys.exit(-1)

if not os.path.isdir(os.path.join(basedir, 'rootfs_' + label)):
print('%s[!]%s The %s%s%s:%s%s%s rootfs is not installed.' % (Fore.RED, Fore.RESET, Fore.YELLOW, image, Fore.RESET, Fore.YELLOW, tag, Fore.RESET))
exit(-1)
sys.exit(-1)

# do the switch

Expand All @@ -94,7 +94,7 @@

except subprocess.CalledProcessError as err:
print('%s[!]%s Failed to backup current %srootfs%s: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, Fore.RESET, err))
exit(-1)
sys.exit(-1)

print('%s[*]%s Moving desired %srootfs_%s%s to %srootfs%s...' % (Fore.GREEN, Fore.RESET, Fore.BLUE, label, Fore.RESET, Fore.BLUE, Fore.RESET))

Expand All @@ -112,4 +112,4 @@
print('%s[!]%s Failed to roll back to old %srootfs%s: %s' % (Fore.RED, Fore.RESET, Fore.BLUE, Fore.RESET, err))
print('%s[!]%s You are now the proud owner of one broken Linux subsystem! To fix it, run %slxrun /uninstall%s and %slxrun /install%s from the command prompt.' % (Fore.RED, Fore.RESET, Fore.GREEN, Fore.RESET, Fore.GREEN, Fore.RESET))

exit(-1)
sys.exit(-1)
10 changes: 5 additions & 5 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def parse_image_arg(argv, can_be_file = False):
fname = names[0]
else:
print('%s[!]%s No files found matching %s%s%s.' % (Fore.RED, Fore.RESET, Fore.BLUE, fname, Fore.RESET))
exit(-1)
sys.exit(-1)

else:
fname = 'rootfs_%s_%s' % (image.replace('/', '_'), tag)
Expand All @@ -95,7 +95,7 @@ def parse_image_arg(argv, can_be_file = False):

if not os.path.isfile(fname):
print('%s[!]%s %s%s%s is not an existing file.' % (Fore.RED, Fore.RESET, Fore.BLUE, fname, Fore.RESET))
exit(-1)
sys.exit(-1)

idx = -1

Expand Down Expand Up @@ -134,14 +134,14 @@ def probe_wsl(silent = False):
return None, None

print('%s[!]%s The Linux subsystem is not installed. Please go through the standard installation procedure first.' % (Fore.RED, Fore.RESET))
exit(-1)
sys.exit(-1)

if os.path.exists(os.path.join(basedir, 'temp')):
if silent:
return None, None

print('%s[!]%s The Linux subsystem is currently running. Please kill all instances before continuing.' % (Fore.RED, Fore.RESET))
exit(-1)
sys.exit(-1)

lxpath = ''
lxpaths = [os.path.join(os.getenv('SystemRoot'), 'sysnative'), os.path.join(os.getenv('SystemRoot'), 'System32')]
Expand All @@ -153,7 +153,7 @@ def probe_wsl(silent = False):

if not lxpath and not silent:
print('%s[!]%s Unable to find %slxrun.exe%s in the expected locations.' % (Fore.RED, Fore.RESET, Fore.BLUE, Fore.RESET))
exit(-1)
sys.exit(-1)

return basedir, lxpath

Expand Down

0 comments on commit 546e399

Please sign in to comment.