Skip to content

Commit

Permalink
Added installLibraries() function.
Browse files Browse the repository at this point in the history
  • Loading branch information
BuvinJ committed Jan 17, 2019
1 parent 9a3b557 commit 25c27b1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 15 deletions.
3 changes: 2 additions & 1 deletion distbuilder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
, QT_IFW_VERBOSE_SWITCH

from distbuilder.pip_installer import \
installLibrary \
installLibraries \
, installLibrary \
, uninstallLibrary \
, PipConfig \
, vcsUrl
Expand Down
52 changes: 42 additions & 10 deletions distbuilder/pip_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from distbuilder.util import * # @UnusedWildImport
from distbuilder.opy_library import obfuscatePyLib, OBFUS_DIR_PATH

PIP_PATH = util._pythonScriptsPath( "pip" )
__pipName = "pip"
__p = util._pythonScriptsPath( __pipName )
PIP_PATH = __p if exists( __p ) else __pipName

PIP_INSTALL = "install"
PIP_UNINSTALL = "uninstall"

Expand All @@ -26,7 +29,8 @@ def __init__( self
, destPath = None
, asSource = False
, incDependencies = True
, isForced= False
, isForced = False
, isCacheUsed = True
, isUpgrade = False
, otherPipArgs = "" ) :
self.pipPath = PIP_PATH
Expand All @@ -36,7 +40,8 @@ def __init__( self
self.destPath = destPath
self.asSource = asSource
self.incDependencies = incDependencies
self.isForced = isForced
self.isForced = isForced
self.isCacheUsed = isCacheUsed
self.isUpgrade = isUpgrade
self.otherPipArgs = otherPipArgs # open ended

Expand All @@ -46,20 +51,47 @@ def __str__( self ) :
elif exists( self.source ) :
self.source = '"%s"' % (self.source,)
sourceSpec = ( self.source if self.version is None else
("%s%s%s" % (self.sourceSpec, self.verEquality, self.version)) )
("%s%s%s" % (self.source, self.verEquality, self.version)) )
destSpec = '--target "%s"' % (self.destPath, ) if self.destPath else ""
editableSwitch = "--editable" if self.asSource else ""
forcedSwitch = "--force-reinstall" if self.isForced else ""
upgradeSwitch = "--upgrade" if self.isUpgrade else ""
noCacheSwitch = "" if self.isCacheUsed else "--no-cache-dir"
incDpndncsSwitch = "" if self.incDependencies else "--no-deps"
self.otherPipArgs += " "
tokens = (destSpec, upgradeSwitch, forcedSwitch, incDpndncsSwitch,
self.otherPipArgs, editableSwitch, sourceSpec )
tokens = (destSpec, upgradeSwitch, forcedSwitch, incDpndncsSwitch,
noCacheSwitch, self.otherPipArgs, editableSwitch, sourceSpec )
return ' '.join( (('%s ' * len(tokens)) % tokens).split() )

# -----------------------------------------------------------------------------
def installLibrary( name, opyConfig=None, pipConfig=PipConfig() ):

# -----------------------------------------------------------------------------
def installLibraries( *libs ):

# Note: *libs allows a tuple of arbitrary length to be provided as
# series of arguments (like varargs in C++/Java...)
# Else, a single tuple or list, may be passed
# OR a single item like a string will be converted to a 1 item tuple...
if len(libs)==1: libs=libs[0]
if not isinstance(libs, tuple) and not isinstance(libs, list): libs=(libs,)

for lib in libs:
# each lib item maybe a tuple/list, and then treated as
# the argument list for installLibrary, or it may be
# just a string, representing the name
if isinstance(lib, tuple) or isinstance(lib, list):
try : name = lib[0]
except: name = None
try : opyConfig = lib[1]
except: opyConfig = None
try : pipConfig = lib[2]
except: pipConfig = PipConfig()
installLibrary( name, opyConfig, pipConfig )
elif isinstance(lib, dict): installLibrary( **lib )
else: installLibrary( lib )

def installLibrary( name, opyConfig=None, pipConfig=None ):

if pipConfig is None : pipConfig=PipConfig()

# Set the pip source and working directory.
# Optionally, create obfuscated version of source
if opyConfig is None:
Expand All @@ -68,7 +100,7 @@ def installLibrary( name, opyConfig=None, pipConfig=PipConfig() ):
else:
wrkDir, _ = obfuscatePyLib( name, opyConfig )
pipConfig.source = None

util._system( __PIP_INSTALL_TMPLT %
( pipConfig.pipPath, PIP_INSTALL, str(pipConfig) ), wrkDir )

Expand Down
45 changes: 41 additions & 4 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,11 @@ obfuscatePyLib:
(denoting private) will be still be obfuscated.
This is the default mode for a library.

-------------------------------------------------------
To install a library (via pip), simply invoke installLibrary:
## Library Installation

To install a library (via pip), invoke installLibrary.
Note: that installLibraries (pural) may used to install
multiple libraries in a single call.

installLibrary( name, opyConfig=None, pipConfig=PipConfig() )

Expand Down Expand Up @@ -264,6 +267,37 @@ To install a library (via pip), simply invoke installLibrary:
alternate "vcs url" be supplied to "development"
repository in place of the simple package name.
See [editable-installs](https://pip.pypa.io/en/stable/reference/pip_install/#editable-installs)

installLibraries( *libs )

This function is a convenience wrapper over installLibrary (singluar) function.

**Returns: None**

***libs**: This function is extremely flexible in terms of how
it may be invoked. The libs argment maybe a tuple, a list,
or a series of arguments passed directly to the function
of arbitrary lenth. The arguments or collection may consist of
simple strings (i.e. the library names), or be tuples / dictionaries
themselves. When passing tuples, or dictionaries, they will be
treated as the argument list to installLibrary().

Simple example:

installLibraries( 'six', 'abc' )

Simple example with a version detail:

installLibraries( 'six', 'abc', 'setuptools==40.6.3' )

Complex example:

opyCfg = OpyConfig()
...
pipCfg = PipConfig()
...
myLib = {"name":"MyLib","opyConfig"":opyCfg,"pipConfig":pipCfg }
installLibraries( 'six', myLib )

uninstallLibrary( name )

Expand All @@ -279,6 +313,7 @@ component parts. This is to be used in conjunction
with the PipConfig attribute asSource.
See https://pip.pypa.io/en/stable/reference/pip_install/#vcs-support


## Obfuscation Features

The Opy Library contains an [OpyConfig](#opyconfig) class, which has been extended
Expand Down Expand Up @@ -836,7 +871,8 @@ Constructor:
, destPath = None
, asSource = False
, incDependencies = True
, isForced= False
, isForced= False
, isCacheUsed = True
, isUpgrade = False
, otherPipArgs = "" )

Expand All @@ -849,7 +885,8 @@ Attributes:
destPath
asSource
incDependencies
isForced
isForced
isCacheUsed
isUpgrade
otherPipArgs (open ended argument string)
Expand Down

0 comments on commit 25c27b1

Please sign in to comment.