Skip to content

Commit

Permalink
Improve cli arg parsing and help messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
timthelion committed Apr 19, 2014
1 parent 9eb64ee commit d455555
Show file tree
Hide file tree
Showing 16 changed files with 266 additions and 236 deletions.
23 changes: 10 additions & 13 deletions logic/subuserCommands/clean
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
# This file should be compatible with both Python 2 and 3.
# If it is not, please file a bug report.
import sys
import optparse
import subuserlib.registry
import subuserlib.permissions
import subuserlib.subprocessExtras
import subuserlib.commandLineArguments

####################################################
def printHelp():
print(""" Clean your system of unneeded images/libaries/executable-less programs. If a libarary has no programs depending on it, uninstall that libarary. Run this command with:
$ subuser clean
""")
def parseCliArgs():
usage = "usage: subuser %prog"
description = """ Clean your system of unneeded images/libaries/executable-less programs. If a libarary has no programs depending on it, uninstall that libarary.
"""
parser = optparse.OptionParser(usage=usage,description=description,formatter=subuserlib.commandLineArguments.HelpFormatterThatDoesntReformatDescription())
return parser.parse_args()

def gatherUnneededLibs():
""" Returns a list of unneeded libaries. """
Expand Down Expand Up @@ -41,11 +44,5 @@ def performUninstalls(programsToUninstall):

#################################################################################################

# This command takes no arguments.
# If the user passed it one or more arguments,
# They cleary don't know what they're doing,
# and need to see the help message.
if len(sys.argv) > 1:
printHelp()
else:
cleanUnneededLibs()
parseCliArgs()
cleanUnneededLibs()
18 changes: 13 additions & 5 deletions logic/subuserCommands/current-utc-time
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#!/usr/bin/env python
# This file should be compatible with both Python 2 and 3.
# If it is not, please file a bug report.
import sys
import sys,optparse

import subuserlib.installTime
import subuserlib.commandLineArguments

def parseCliArgs():
usage = "usage: subuser %prog"
description = """ Display the current time in properly formated utc. Useful for setting the last-update-time attribute in permissions.json
"""
parser = optparse.OptionParser(usage=usage,description=description,formatter=subuserlib.commandLineArguments.HelpFormatterThatDoesntReformatDescription())
return parser.parse_args()

if len(sys.argv) == 1:
print(subuserlib.installTime.currentTimeString())
parseCliArgs()
print(subuserlib.installTime.currentTimeString())

else:
print(""" Display the current time in properly formated utc. Useful for setting the last-update-time attribute in permissions.json """)
20 changes: 10 additions & 10 deletions logic/subuserCommands/describe
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#!/usr/bin/env python
# This file should be compatible with both Python 2 and 3.
# If it is not, please file a bug report.
import sys
import optparse
import subuserlib.describe
import subuserlib.commandLineArguments

def printHelp():
print("""Show basic information about a program: Whether it is installed, what it's permissions are ect.
def parseCliArgs():
usage = "usage: subuser %prog PROGRAM_NAME(s)"
description = """Show basic information about a program: Whether it is installed, what it's permissions are ect.
Ex:
$ subuser describe firefox
<lots of info>
""")
"""
parser = optparse.OptionParser(usage=usage,description=description,formatter=subuserlib.commandLineArguments.HelpFormatterThatDoesntReformatDescription())
return parser.parse_args()

#################################################################################################
if len(sys.argv) == 1 or {"help","-h","--help"} & set(sys.argv[]):
printHelp()
sys.exit()
#################################################################################################
(options,args) = parseCliArgs()

for program in sys.argv[1:]:
for program in args:
subuserlib.describe.printInfo(program,True)
2 changes: 1 addition & 1 deletion logic/subuserCommands/dry-run
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def getArgsToPassToProgram():
return []

#################################################################################################
if len(sys.argv) == 1 or sys.argv[1] == "help" or sys.argv[1] == "-h" or sys.argv[1] == "--help":
if len(sys.argv) == 1 or {"help","-h","--help"} & set(sys.argv):
printHelp()
sys.exit()
#################################################################################################
Expand Down
41 changes: 15 additions & 26 deletions logic/subuserCommands/install
Original file line number Diff line number Diff line change
@@ -1,47 +1,36 @@
#!/usr/bin/env python
# This file should be compatible with both Python 2 and 3.
# If it is not, please file a bug report.
import sys
import os
import sys,os,optparse

import subuserlib.availablePrograms
import subuserlib.install
import subuserlib.describe
import subuserlib.commandLineArguments

def printHelp():
"""
Display a help message for the install mode of the subuser command.
"""
print("""To install packages list there names:
$ subuser install vim firefox
def parseCliArgs():
usage = "usage: subuser %prog [options] PROGRAM_NAME(s)"
description = """Install a set of subuser programs. To view a list of programs that can be installed try:
Options:
--from-cache build this image using layers from the cache
$ subuser install vim --from-cache
""")
$ subuser list available
"""
parser=optparse.OptionParser(usage=usage,description=description,formatter=subuserlib.commandLineArguments.HelpFormatterThatDoesntReformatDescription())
advancedOptions = subuserlib.commandLineArguments.advancedInstallOptionsGroup(parser)
parser.add_option_group(advancedOptions)
return parser.parse_args()

#################################################################################################
if len(sys.argv) == 1 or sys.argv[1] == "help" or sys.argv[1] == "-h" or sys.argv[1] == "--help":
printHelp()
sys.exit()
#################################################################################################

commandOptionList = ['--from-cache']
userProgramList, userOptionList = subuserlib.commandLineArguments.parseCommandLineArgs(sys.argv[1:], commandOptionList)

# Are we to use layers from the cache when building the docker image: only for Dockerfiles build images
useCache = False
if '--from-cache' in userOptionList:
useCache = True
options,userProgramList = parseCliArgs()

for program in userProgramList:
print(program)
subuserlib.install.installProgramAndDependencies(program, useCache)
subuserlib.install.installProgramAndDependencies(program, options.useCache)

#print for the MAIN program (The user called not the dependency the permission)
#print for the MAIN program ()
print("\n============= INSTALLATION OK =============\n\n")
for program in userProgramList:
#there is too much output: a user will hardly recognize the nice info provided: add some space
#there is too much output: a user will hardly recognize the usefull information provided: add some space
print("\n{0}: has been installed with the following permissions.".format(program))
subuserlib.describe.printInfo(program,False)
print("You can change this program's permissions at any time by editing it's permissions.json file.")
37 changes: 12 additions & 25 deletions logic/subuserCommands/install-from-registry
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#!/usr/bin/env python
# This file should be compatible with both Python 2 and 3.
# If it is not, please file a bug report.
import sys
import sys,optparse
import subuserlib.registry
import subuserlib.dockerImages
import subuserlib.subprocessExtras
import subuserlib.commandLineArguments

####################################################
def printHelp():
print(""" <install-from-registry> command which installs all of the programs listed in installed-programs.json file. Run this command with:
def parseCliArgs():
usage = "usage: subuser %prog [options]"
description = """ Install all of the programs that were installed on a previous system, given that you backed up the installed-programs.json file.
$ subuser install-from-registry
Options:
--from-cache similar to the `install` command: build images using layers from the cache if possible
$ subuser install-from-registry --from-cache
""")
"""
parser = optparse.OptionParser(usage=usage,description=description,formatter=subuserlib.commandLineArguments.HelpFormatterThatDoesntReformatDescription())
advancedInstallOptions = subuserlib.commandLineArguments.advancedInstallOptionsGroup(parser)
parser.add_option_group(advancedInstallOptions)
return parser.parse_args()

def resetRegistry():
""" Go through registry and unregister any program that is not actually installed. """
Expand All @@ -34,20 +35,6 @@ def installFromRegistry(useCache):
subuserlib.subprocessExtras.subprocessCheckedCall(["subuser","install",program])

#################################################################################################
if len(sys.argv) > 1:
if sys.argv[1] == "help" or sys.argv[1] == "-h" or sys.argv[1] == "--help":
printHelp()
sys.exit()
#################################################################################################

# Are we to use layers from the cache when building the docker image: only for Dockerfiles build images
useCache = False
if len(sys.argv) > 1:
if sys.argv[1] == "--from-cache":
useCache = True
else:
print("Invalid argument: see\n")
printHelp()
sys.exit()

installFromRegistry(useCache)
options,arguments=parseCliArgs()
installFromRegistry(options.useCache)
38 changes: 18 additions & 20 deletions logic/subuserCommands/list
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#!/usr/bin/env python
# This file should be compatible with both Python 2 and 3.
# If it is not, please file a bug report.
import sys
import os
import sys,os,optparse

import subuserlib.registry
import subuserlib.paths
import subuserlib.permissions
import subuserlib.availablePrograms
import subuserlib.commandLineArguments

def printHelp():
print("""List subuser-programs:
$ subuser list [available/installed]
OPTIONS:
def parseCliArgs():
usage = "usage: subuser %prog WHAT_TO_LIST [options]"
description = """List subuser-programs. You can use this command to list programs that are:
available
List all subuser-programs
installed
List all subuser-programs which are already installed/registered
--short
output only program names
EXAMPLES:
$ subuser list available --short
$ subuser list installed
""")
"""
parser=optparse.OptionParser(usage=usage,description=description,formatter=subuserlib.commandLineArguments.HelpFormatterThatDoesntReformatDescription())
parser.add_option("--short",dest="short",action="store_true",default=False,help="Only display the names of the programs to be listed, and no other information.")
return parser.parse_args()

def printProgramList(programs):
for program in programs:
Expand All @@ -36,16 +36,14 @@ def printProgramList(programs):
print("\n")

#################################################################################################
if len(sys.argv) == 1 or sys.argv[1] == "help" or sys.argv[1] == "-h" or sys.argv[1] == "--help":
printHelp()
sys.exit()
#################################################################################################

commandOptionList = ['available', 'installed', '--short']
userProgramList, userOptionList = subuserlib.commandLineArguments.parseCommandLineArgs(sys.argv[1:], commandOptionList)
options,args = parseCliArgs()

if len(args)==0:
sys.exit("Nothing to list. Issue this command with the -h argument for help.")

if 'available' in userOptionList:
if '--short' in userOptionList:
if 'available' in args:
if options.short:
print(subuserlib.availablePrograms.getAvailableProgramsText(addNewLine=False, indentSpaces=3))
sys.exit()
else:
Expand All @@ -55,8 +53,8 @@ if 'available' in userOptionList:
availablePrograms = subuserlib.availablePrograms.getAvailablePrograms()
printProgramList(availablePrograms)

if 'installed' in userOptionList:
if '--short' in userOptionList:
if 'installed' in args:
if options.short:
print(subuserlib.registry.getInstalledProgramsText(addNewLine=False, indentSpaces=3))
sys.exit()
else:
Expand Down
32 changes: 11 additions & 21 deletions logic/subuserCommands/mark-as-needing-update
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
#!/usr/bin/env python
# This file should be compatible with both Python 2 and 3.
# If it is not, please file a bug report.
import sys
import optparse
import subuserlib.installTime
import subuserlib.permissions
import subuserlib.availablePrograms
import subuserlib.paths
import subuserlib.commandLineArguments

def printHelp():
print("""
def parseCliArgs():
usage = "usage: subuser %prog PROGRAM_NAME(s)"
description = """
Mark a program as needing to be updated. Note, that this may mess up the formatting of it's permissions.json file.
Usage:
$ subuser mark-as-needing-update program-name
""")
"""
parser=optparse.OptionParser(usage=usage,description=description,formatter=subuserlib.commandLineArguments.HelpFormatterThatDoesntReformatDescription())
return parser.parse_args()

#################################################################################################
if len(sys.argv) == 1 or sys.argv[1] == "help" or sys.argv[1] == "-h" or sys.argv[1] == "--help":
printHelp()
sys.exit()
#################################################################################################

programName = sys.argv[1]
options,programNames = parseCliArgs()

if not subuserlib.availablePrograms.available(programName):
print(programName+ " is not the name of any known program. Cannot mark it as having an update.")
print("\nAvailable programs are: ")
availableProgramsPath = subuserlib.paths.getAvailableProgramsPath()
print(' '.join(sorted([program for program in os.listdir(availableProgramsPath)])))
else:
permissions = subuserlib.permissions.getPermissions(programName)
permissions["last-update-time"] = subuserlib.installTime.currentTimeString()
subuserlib.permissions.setPermissions(programName,permissions)
for program in programNames:
subuserlib.installTime.markProgramAsNeedingUpdate(program)
Loading

0 comments on commit d455555

Please sign in to comment.