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 aa07205
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 24 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
65 changes: 52 additions & 13 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,21 +223,26 @@ 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() )

**Returns: None**

**name**: The name/source of the library. If the
library is your current project itself, supply
the name you are giving it. If you are NOT
obfuscating it, specify "." instead. Otherwise,
you may specific a remote package name registered
with pip (i.e. the typical way pip is used),
or a local path, or a url (http/git). See
pip documentation for details.
library is your *current project itself* and you
are obfuscating it, be sure to supply the name you
are giving it. If you are NOT obfuscating it,
specify "." instead. If you wish to install a
remote package registered with pip (i.e. the typical way pip
is used), simply supply the name.
If you wish to use a local path, or a specifc url (http/git),
see [PipConfig](#pipconfig) (and perhaps the pip documentation
for details).

**opyConfig**: An (optional) [OpyConfig](#opyconfig) object,
to dictate code obfuscation details using
Expand All @@ -247,7 +252,7 @@ To install a library (via pip), simply invoke installLibrary:
as a importable library, this function is useful
for testing the operations of your library
post-obfuscation/pre-distribution. This will
run obfuscatePyLib with default arguments,
run [obfuscatePyLib](#-library-obfuscation) with default arguments,
install the library, and remove the temporary
obfuscation from the working directory.

Expand All @@ -261,9 +266,40 @@ To install a library (via pip), simply invoke installLibrary:
a specific path such as a temp build directory,
and to request raw .py scripts be placed there.
Note that remote raw pip packages will require an
alternate "vcs url" be supplied to "development"
alternate "vcs url" be supplied to a "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 +315,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 +873,8 @@ Constructor:
, destPath = None
, asSource = False
, incDependencies = True
, isForced= False
, isForced= False
, isCacheUsed = True
, isUpgrade = False
, otherPipArgs = "" )

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

0 comments on commit aa07205

Please sign in to comment.