Package: projenv
Overviewprojenv provides mechanism for project to manage parameters for programs in
hierarchical way.
Environment is a special type of dictionary holding parameters used by parts.
There are multiple levels and types of environment files playing part in program run.Environment nodeEnvironment evaluation process walks the project tree from top to program location.
In each node it looks for set of files that defines the environment parameters.
By default, environment is derived from two type of files as each node:NumberNameDescription1.envpackage.xmlenvironment parameters for the package.2.envoverride.xmlpersonal overrides for package environment. It
may include re-define parameters with override
ride=True.Project can alter this default behavior by defining.envconfig.xmlat its root
with specific value for envnodes.<environ><envnodes>.envproject,.envpackage,.envoverride</envnodes></environ>Environment node must also have __init__.py.  Each folder in the project hierarchy must have __init__.py file; this is since the search for parent folder stops when a folder is found not to have __init__.py file.Important: without __init__.py in the packages that have .envpackage.xml and .envoverride.xml, Environ will fail to load.Program InterfaceWithin programs there are three types of access points to the
environment variables. To get projenv dictionary, program can
perform the following command:Loading environment variables from project structureUpdating environment variable in programAccessing environment variablesLoading environment variablesimportprojenvenv=projenv.Environ()When Program evaluates environment, it starts with root location going down the tree up to and including package environment of Program location.Environ class __init__ has the following signatureEnviron(self, osenv=True, trace_env=None, logclass=None, logger=None)NameDescriptionDefault ValuesosenvIf set, load os environ.Truetrace_envList of environment variables to traceNonelogclassIf provided the string will be used for trace naming.NoneloggerIf set to True and logclass=None, use Python
getChild to set trace name.NoneWithin derivative articles environment can be updated with environment variable as follows.Updating environment variablesenv.updates([EnvVar(name='REJ_ALLOWED',cast='integer',value=0,input=True),EnvVar(name='OUT_FILE',value='${VAR_LOC}/summary.csv',cast='path',input=True),EnvVar(name='RATE',override='True',cast='integer',value=5,input=True)])If input is set to Truethe variable update will be ignored if the variable is defined in parent environment. If variable is not defined in parent environment, it will be defined and set to value from the command.If input is set to Falseupdate will overwrite variable value if variable exists, if variable is not defined it will define it.Overrideflags environment variable as changeable by derivative program articles.Accessing environment variablesimportprojenvenv=projenv.Environ()env.updates([EnvVar(name='REJ_ALLOWED',cast='integer',value=0,input=True),EnvVar(name='OUT_FILE',value='${VAR_LOC}/summary.csv',cast='path',input=True),EnvVar(name='RATE',override='True',cast='integer',value=5,input=True)])ofile=env['OUT_FILE']rate=env.get('RATE')In the first case(ofile variable), direct access, KeyError exception may be sent if variable name does not exist.
In the second example(rate variable), None value will be returned if not found.Environment TreeEnvironment files are evaluated in hierarchical way.  The project tree and its packages are treated as nodes in a tree.
Each node can be evaluated and have its own representation of the environment.Single Project Environment TreeAt each node, environment is evaluated in the sequence or envnodes configuration parameter.
By default this means:First .envpackage.xml, if available, is read and set.Next, .envoverride.xml overrides, if available, is read and set.As shown below, this behavior could be changed to support different
environment node structure. For example, to support legacy projects using older
version of projenv, the following configuration .envconfig.xml can be used:<environ><envnodes>.projectenv,packageenv,personalenv</envnodes></environ>The following figure shows a possible use of default configuration.The structure below
shows example environment tree in a project.  When the above command is engaged in
Program A, it would include environment setting of Project and Package A locations.
Program AB will include Program A, Package A and Package AB accordingly.ProjectenvpackageenvoverrideProgram APackage AevpackageenvoverridePackage ABenvpackageenvoverrideProgram ABThe structure below shows example of an environment file. Core environment is tagged under
< environ>. Environ mechanism would look for this tag. Once found, it would evaluate its
content as environ- ment directive.<environment><environ><varname='AC_WS_LOC'value='${HOME}/sand/myproject'export='True'/><varname='AC_ENV_NAME'value='test'export='True'/><varname='AC_VAR_BASE'value='${HOME}/var/data/'export='True'/><varname='AC_LOG_LEVEL'value='DEBUG'export='True'/><varname='AC_LOG_STDOUT'value='True'override='True'export='True'cast='boolean'/><varname='AC_LOG_STDOUT_LEVEL'value='INFO'override='True'export='True'/><varname='AC_LOG_STDERR'value='True'override='True'export='True'cast='boolean'/><varname='AC_LOG_STDERR_LEVEL'value='CRITICAL'override='True'export='True'/></environ></environment>Note: <environment> tag is to provide enclosure to environ. Environ mechanism is not
depending on its existent per se.  However, some kind on enclosure is required;  <environ>
can not be in top level of the XML.Example of Multiple Project Environment TreeAt each import, environment is evaluated in the following sequence:First get the node representation of imported path.Evaluate it recursively (loading imports).Finally, insert the resulted imported map instead of the import directive (flat).Project A: /Users/me/projs/proja/.envpackage.xml<environment><environ><varname='FILE_LOC'value='/Users/me/tmp/'export='True'/><varname='FILE_NAME'value='aname'export='True'/><varname='FILE_PATH'value='${FILE_LOC}${FILE_NAME}'export='True'/></environ></environment>Project B: /Users/me/projs/projb/.envpackage.xml’<environment><environ><importname='proja'path='/Users/me/projs/proja/.projectenv.xml'/><varname='FILE_NAME'value='bname'export='True'/></environ></environment>The example above shows import project directive within project B’s environment.  In project B’s context, FILE_PATH variable will result with
the value /Users/me/tmp/bname.Recursiveinclusion of environments (recursive import statement) would cause evaluation of environment variables to be loaded recursively.
Consideration is given to overrides in post import environments.Note: import must be set as full path for the installation of the included project. It is therefore best practice to populate real path
only in .envoverride.xml and not in .envpackage.xml.Best PracticesSo many options, so what should one do?Naming ParametersPrefixyour parameters with an identifier. Specifically if your projects would
need to cooperate (import their environment). We have all parameters us ’AC ’ as prefix. We
also define ’AC PROJ PREFIX’ that can be used in program to construct parameter name.We recommend following UNIX convention for environment variables. Use upper-case letters
separated with underscore. We use this style in all of this document listings.Drivers and Derivatives, for the sake of this discussion we define three types of parameters:
1. standalone is a parameter that is not dependent on another and is not used by another parameter.
2. driver is a parameter that other parameters defined by it.
3. derivative is a parameter that includes a driver in its definitions.A parameter can be both a driver and derivative.
Use drivers and derivative parameter definition in such a way that users may personalize the
behavior of the system. For example, developers may want to change their own directory structure to
fit their own tools..envprojectDot (.) envproject, although not default in envnodes configuration, good practice to use. It
is usually contains parameters that are good for the all projects. You can look at is as your
standard parameters to all projects that you produce. In the following listing locations are
defined as derivatives of AC VAR BASE. This is useful since users of this project can override
that parameter to change to their own structure.<environment><environ><varname=’AC_PROJ_PREFIX’value=’AC_’export=’True’override=’True’/><varname=’AC_VAR_BASE’value=’/var/accord/data/’override=’True’export=’True’/><varname=’AC_ENV_NAME’value=’.’override=’True’export=’True’/><varname=’AC_VAR_LOC’value=’${AC_VAR_BASE}${AC_ENV_NAME}/’override=’True’export=’True’/><varname=’AC_LOG_LOC’value=’${AC_VAR_LOC}/log/’override=’True’export=’True’/><varname=’AC_REJ_LOC’value=’${AC_VAR_LOC}/rej/’override=’True’export=’True’/><varname=’AC_RUN_LOC’value=’${AC_VAR_LOC}/run/’override=’True’export=’True’/><varname=’AC_IN_LOC’value=’${AC_VAR_LOC}/in/’override=’True’export=’True’/><varname=’AC_OUT_LOC’value=’${AC_VAR_LOC}/out/’override=’True’export=’True’/></environ></environment>.envpackageDot envpackage includes definitions for that are specific to the project or the package.
Usually this is kept for things like RPC PORT or maybe MAIL SEND SMTP..envoverrideDot envoverride provides means to personalize an environment. Users can override .envpackage default parameters settings.It is recommended to exclude envoverride from your code repository
(e.g., add envoverride.xml to .gitignore). Otherwise, users may override each other
personalizations.Utilitiesprojenvcontains a few utilities to help initialize and use project or package environments.mkprojenvpackage.pymkprojenvpackage.pypredominately creates .envpackage.xml parameter set.  It also creates .envoverride.xml parameter set unless instructed otherwise.Finally, it creates project folders as defined in the parameter set, if choose to do so.ExampleIn this example , only var folder is being override (line 7)1$mkdirteste2$cdteste3$touch__init__.py4$mkprojenvpackage.py5Enterprojectname(nameoffolderthatwillbecreatedforproject)[teste]:6Enterprojectversion[]:7Enterlocationforproject's var folder [/var/data/teste]: /var/acrisel/data/teste8Varspacelocation/var/acrisel/data/testedon't exist, create? [Yes]:9Enterprojectprefix[TESTE_]:10Enterenvironmentname[.]:11Successfullycreatedprojectenv/Users/tester/teste/.envpackage.xml12Successfullycreatedprojectenv/Users/tester/teste/.envoverride.xml13createprojectfolders?[Yes]:14Creatingprojectfolders:15TESTE_PROJ_LOC:Alreadyexists/Users/tester/teste/16TESTE_PROJ_API_LOC:Created/Users/tester/teste/api/17TESTE_PROJ_DB_LOC:Created/Users/tester/teste/db/18TESTE_PROJ_SRC_LOC:Created/Users/tester/teste/src/19TESTE_PROJ_FMT_LOC:Created/Users/tester/teste/fmt/20TESTE_PROJ_RES_LOC:Created/Users/tester/teste/res/21TESTE_PROJ_CFG_LOC:Created/Users/tester/teste/cfg/22TESTE_PROJ_DATA_LOC:Created/Users/tester/teste/data/23TESTE_VAR_LOC:Created/var/acrisel/data/teste/./24TESTE_LOG_LOC:Created/var/acrisel/data/teste/./log/25TESTE_REJ_LOC:Created/var/acrisel/data/teste/./rej/26TESTE_RUN_LOC:Created/var/acrisel/data/teste/./run/27TESTE_IN_LOC:Created/var/acrisel/data/teste/./in/28TESTE_OUT_LOC:Created/var/acrisel/data/teste/./out/29TESTE_DB_LOC:Created/var/acrisel/data/teste/./db/30TESTE_LKUP_LOC:Created/var/acrisel/data/teste/./lkup/31TESTE_TMP_LOC:Created/var/acrisel/data/teste/./tmp/Command line options$mkprojenvpackage.py-husage:mkprojenvpackage.py[-h][-tPATH][-pPATH][-nNAME][-dPATH][-wPATH][-xNAME][-eNAME][-vVERSION][--no-override][--force-defaults]optionalarguments:-h,--helpshowthishelpmessageandexit-tPATH,--templatesPATHlocationoftemplates.defaultstoprojenvbuilt-intemplates-pPATH,--projectPATHpathtoproject.defaultstocurrentworkingdirectory-nNAME,--nameNAMEnametoproject.defaultfordirectorynameofprojectlocation-dPATH,--dataPATHlocationofdataspacewheredataprojectsarecreated,default/var/data/(nameofPROJECT)-wPATH,--workPATHlocationofworkspacewhereprocessingin-workdataiscreated,defaultDATAspace-xNAME,--prefixNAMEprojectprefixusedforenvironmentparametersnames.defaultstoprojectname.-eNAME,--environmentNAMEnametobeusetopersonalizevararea.Recommendedtousewhenmultipleusersusethesamevararea-vVERSION,--versionVERSIONversionforthisprojectenvironment.Useifneedtobindenvironmentversiontoproject.--no-overrideskipscreationof.envoverride.xml--force-defaultsforcestheuseofdefault.thisoptionskipsinteractionmkprojenvoverride.pyExampleIn this example, tester only had .envpackage.xml file in project directory.  In building overrides, tester profides environment name (line 2).  as a result, var area is personalized with tester name (as oppose to previous example line 10)1$mkprojenvoverride.py2Enterenvironmentname[.]:tester3createprojectfolders?[Yes]:4Creatingprojectfolders:5TESTE_OUT_LOC:Created/var/acrisel/data/teste/tester/out/6TESTE_LKUP_LOC:Created/var/acrisel/data/teste/tester/lkup/7TESTE_PROJ_CFG_LOC:Created/Users/tester/teste/cfg/8TESTE_TMP_LOC:Created/var/acrisel/data/teste/tester/tmp/9TESTE_DB_LOC:Created/var/acrisel/data/teste/tester/db/10TESTE_PROJ_SRC_LOC:Created/Users/tester/teste/src/11TESTE_PROJ_DATA_LOC:Created/Users/tester/teste/data/12TESTE_PROJ_RES_LOC:Created/Users/tester/teste/res/13TESTE_PROJ_FMT_LOC:Created/Users/tester/teste/fmt/14TESTE_REJ_LOC:Created/var/acrisel/data/teste/tester/rej/15TESTE_LOG_LOC:Created/var/acrisel/data/teste/tester/log/16TESTE_PROJ_LOC:Alreadyexists/Users/tester/teste/17TESTE_VAR_LOC:Alreadyexists/var/acrisel/data/teste/tester/18TESTE_PROJ_API_LOC:Created/Users/tester/teste/api/19TESTE_RUN_LOC:Created/var/acrisel/data/teste/tester/run/20TESTE_IN_LOC:Created/var/acrisel/data/teste/tester/in/21TESTE_PROJ_DB_LOC:Created/Users/tester/teste/db/Command line options$mkprojenvoverride.py-husage:mkprojenvoverride.py[-h][-tPATH][-pPATH][-eNAME][--force-defaults]optionalarguments:-h,--helpshowthishelpmessageandexit-tPATH,--templatesPATHlocationoftemplates.defaultstoprojenvbuilt-intemplates-pPATH,--projectPATHpathtoproject.defaultstocurrentworkingdirectory-eNAME,--environmentNAMEnametobeusetopersonalizevararea.Recommendedtousewhenmultipleusersusethesamevararea--force-defaultsforcestheuseofdefault.thisoptionskipsinteractionmkprojenvdirs.pyCreates project folders accordingExample1$mkprojenvdirs.py2createprojectfolders?[Yes]:3Creatingprojectfolders:4TESTE_PROJ_LOC:Alreadyexists/Users/tester/teste/5TESTE_PROJ_API_LOC:Created/Users/tester/teste/api/6TESTE_PROJ_DB_LOC:Created/Users/tester/teste/db/7TESTE_PROJ_SRC_LOC:Created/Users/tester/teste/src/8TESTE_PROJ_FMT_LOC:Created/Users/tester/teste/fmt/9TESTE_PROJ_RES_LOC:Created/Users/tester/teste/res/10TESTE_PROJ_CFG_LOC:Created/Users/tester/teste/cfg/11TESTE_PROJ_DATA_LOC:Created/Users/tester/teste/data/12TESTE_VAR_LOC:Alreadyexists/var/acrisel/data/teste/tester/13TESTE_LOG_LOC:Alreadyexists/var/acrisel/data/teste/tester/log/14TESTE_REJ_LOC:Alreadyexists/var/acrisel/data/teste/tester/rej/15TESTE_RUN_LOC:Alreadyexists/var/acrisel/data/teste/tester/run/16TESTE_IN_LOC:Alreadyexists/var/acrisel/data/teste/tester/in/17TESTE_OUT_LOC:Alreadyexists/var/acrisel/data/teste/tester/out/18TESTE_DB_LOC:Alreadyexists/var/acrisel/data/teste/tester/db/19TESTE_LKUP_LOC:Alreadyexists/var/acrisel/data/teste/tester/lkup/20TESTE_TMP_LOC:Alreadyexists/var/acrisel/data/teste/tester/tmp/Command line options$mkprojenvdirs.py-husage:mkprojenvdirs.py[-h][-pPATH]optionalarguments:-h,--helpshowthishelpmessageandexit-pPATH,--projectPATHpathtoproject.defaultstocurrentworkingdirectoryexport_projev.py and .shexport_projenv.pyprovide a function and command line access to get projects’ environment parameters in appropriate shell format.
it sh partner,export_projenv.shcan be source to export environment parameters in current shell.source export_projenv.sh [project].export_projenv.shusesexport_projev.pyto produce exportable construct for UNIX shell.Example$export_projenv.py--exclude-osexportPROJENV_PREFIX="TESTE_"exportTESTE_PROJ_NAME="teste"exportTESTE_PROJ_VER=""exportTESTE_PROJ_LOC="/Users/tester/teste/"exportTESTE_PROJ_API_LOC="/Users/tester/teste/api/"exportTESTE_PROJ_DB_LOC="/Users/tester/teste/db/"exportTESTE_PROJ_SRC_LOC="/Users/tester/teste/src/"exportTESTE_PROJ_FMT_LOC="/Users/tester/teste/fmt/"exportTESTE_PROJ_RES_LOC="/Users/tester/teste/res/"exportTESTE_PROJ_CFG_LOC="/Users/tester/teste/cfg/"exportTESTE_PROJ_DATA_LOC="/Users/tester/teste/data/"exportTESTE_ENCODING="ascii"exportTESTE_BYTEORDER="network"exportTESTE_ENV_NAME="tester"exportTESTE_VAR_LOC="/var/acrisel/data/teste/tester/"exportTESTE_LOG_LOC="/var/acrisel/data/teste/tester/log/"exportTESTE_REJ_LOC="/var/acrisel/data/teste/tester/rej/"exportTESTE_RUN_LOC="/var/acrisel/data/teste/tester/run/"exportTESTE_IN_LOC="/var/acrisel/data/teste/tester/in/"exportTESTE_OUT_LOC="/var/acrisel/data/teste/tester/out/"exportTESTE_DB_LOC="/var/acrisel/data/teste/tester/db/"exportTESTE_LKUP_LOC="/var/acrisel/data/teste/tester/lkup/"exportTESTE_TMP_LOC="/var/acrisel/data/teste/tester/tmp/"exportTESTE_LOG_LEVEL="DEBUG"exportTESTE_LOG_STDOUT="False"exportTESTE_LOG_STDOUT_LEVEL="INFO"exportTESTE_LOG_STDERR="True"exportTESTE_LOG_STDERR_LEVEL="WARNING"Command line options$export_projenv.py-husage:export_projenv.py[-h][-pPATH][--shell{sh,ksh,bash}][--exclude-os]optionalarguments:-h,--helpshowthishelpmessageandexit-pPATH,--projectPATHpathtoproject.defaultstocurrentworkingdirectory--shell{sh,ksh,bash}bindtheshelltobecompatiblewith--exclude-osdoesnotaddOSenvironHook to Virtualenv and virtualenvwrapperIt is highly recommended to use virtualenv and virtualenvwrapper in conjunction to projenv.To make this convenient, projenv has a hook into virtualenv:addproj2virtualenv [path]>.Multiple projects mat be using the same virtualenv.  Once project is added to virtualenv, to get access to project features,setproj <name>should be used.Once project is selected bysetproj,cdsand <tag>can be used where tag is navigate to project sandbox base location parameter.  Similarly,cdvar <tag>can be used to navigate to var base location parameters.ExampleHere we assume projenv templates were used to create a project.  Hence the following environment variables are used.  If project name is example, the following environment parameter will be part of the environment:EXAMPLE_PROJ_LOC
EXAMPLE_PROJ_API_LOC
EXAMPLE_VAR_LOC
EXAMPLE_LOG_LOCaftersetproj example, we can use the following navigational commands:cdproj: will relocate to EXAMPLE_PROJ_LOCcdproj apiwill relocate to EXAMPLE_PROJ_API_LOCcdvarwill relocate to EXAMPLE_VAR_LOCcdvar logwill relocate to EXAMPLE_LOG_LOCInstallation, validation and example programHow to install, validate installation and use the package?InstallationTo install run following command: pip install projenvValidationtest.py in github link below perform unit test cases to check projenv.ExampleSee example of the program using projenv on
Githubhttps://github.com/Acrisel/projenv/blob/master/environ/example/exampleBackwards compatibilityDue the changes in naming of node base files, projects using previous version can do one of the following steps.Change node files name to fit the new naming convention.Add.envconfig.xmlwith proper envnodes definition as follows:<environ><envnodes>.projectenv.xml,packageenv.xml,personalenv.xml</envnodes></environ>Also, each folder in the project hierarchy need to have __init__.py file; See environment node section above for more details.Additional resourcesDocumentation is in the “docs” directory and online at the design and use of projenv.exampleandtestsdirectory shows ways to use projenv.Environ . Both directories are available to view and download as part of source code
on GitHub.GitHub_linkDocs are updated rigorously. If you find any problems in the docs, or think they
should be clarified in any way, please take 30 seconds to fill out a ticket in
github or send us email atsupport@acrisel.comTo get more help or to provide suggestions you can send as email to:arnon@acrisel.comuri@acrisel.com