<s> import base64 <EOL> import getopt <EOL> import httplib <EOL> import json <EOL> import re <EOL> import os <EOL> import os . path <EOL> import sys <EOL> import StringIO <EOL> import urlparse <EOL> import xml . dom . minidom <EOL> import zipfile <EOL> import getpass <EOL> from ApigeePlatformTools import httptools , deploytools <EOL> def printUsage ( ) : <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> def run ( ) : <EOL> ApigeeURL = '<STR_LIT>' <EOL> Username = None <EOL> Password = None <EOL> Directory = None <EOL> MainScript = None <EOL> Organization = None <EOL> Environment = None <EOL> Name = None <EOL> BasePath = '<STR_LIT:/>' <EOL> ShouldDeploy = True <EOL> ZipFile = None <EOL> VirtualHost = '<STR_LIT:default>' <EOL> Options = '<STR_LIT>' <EOL> opts = getopt . getopt ( sys . argv [ <NUM_LIT:2> : ] , Options ) [ <NUM_LIT:0> ] <EOL> for o in opts : <EOL> if o [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> Organization = o [ <NUM_LIT:1> ] <EOL> elif o [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> Environment = o [ <NUM_LIT:1> ] <EOL> elif o [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> Name = o [ <NUM_LIT:1> ] <EOL> elif o [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> Directory = o [ <NUM_LIT:1> ] <EOL> elif o [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> MainScript = o [ <NUM_LIT:1> ] <EOL> elif o [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> Username = o [ <NUM_LIT:1> ] <EOL> elif o [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> Password = o [ <NUM_LIT:1> ] <EOL> elif o [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> BasePath = o [ <NUM_LIT:1> ] <EOL> elif o [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> ApigeeURL = o [ <NUM_LIT:1> ] <EOL> elif o [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> ZipFile = o [ <NUM_LIT:1> ] <EOL> elif o [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> VirtualHost = o [ <NUM_LIT:1> ] <EOL> elif o [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> ShouldDeploy = False <EOL> elif o [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> printUsage ( ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> BadUsage = False <EOL> if not Username : <EOL> BadUsage = True <EOL> print '<STR_LIT>' <EOL> if not Directory : <EOL> BadUsage = True <EOL> print '<STR_LIT>' <EOL> if not Environment : <EOL> BadUsage = True <EOL> print '<STR_LIT>' <EOL> if not Name : <EOL> BadUsage = True <EOL> print '<STR_LIT>' <EOL> if not Organization : <EOL> BadUsage = True <EOL> print '<STR_LIT>' <EOL> if not MainScript : <EOL> BadUsage = True <EOL> print '<STR_LIT>' <EOL> if not BadUsage and not Password : <EOL> Password = getpass . getpass ( ) <EOL> if not BadUsage and not Password : <EOL> BadUsage = True <EOL> print '<STR_LIT>' <EOL> if BadUsage : <EOL> printUsage ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> httptools . setup ( ApigeeURL , Username , Password ) <EOL> def makeApplication ( ) : <EOL> return '<STR_LIT>' % Name <EOL> def makeProxy ( ) : <EOL> return '<STR_LIT>' % ( BasePath , VirtualHost ) <EOL> def makeTarget ( ) : <EOL> return '<STR_LIT>' % MainScript <EOL> def pathContainsDot ( p ) : <EOL> c = re . compile ( '<STR_LIT>' ) <EOL> for pc in p . split ( '<STR_LIT:/>' ) : <EOL> if c . match ( pc ) != None : <EOL> return True <EOL> return False <EOL> def zipDirectory ( dir , pfx ) : <EOL> ret = StringIO . StringIO ( ) <EOL> tzip = zipfile . ZipFile ( ret , '<STR_LIT:w>' ) <EOL> dirList = os . walk ( dir ) <EOL> for dirEntry in dirList : <EOL> for fileEntry in dirEntry [ <NUM_LIT:2> ] : <EOL> if not fileEntry . endswith ( '<STR_LIT>' ) : <EOL> fn = os . path . join ( dirEntry [ <NUM_LIT:0> ] , fileEntry ) <EOL> en = os . path . join ( pfx , os . path . relpath ( dirEntry [ <NUM_LIT:0> ] , dir ) , fileEntry ) <EOL> if ( os . path . isfile ( fn ) ) : <EOL> tzip . write ( fn , en ) <EOL> tzip . close ( ) <EOL> return ret . getvalue ( ) <EOL> tf = StringIO . StringIO ( ) <EOL> zipout = zipfile . ZipFile ( tf , '<STR_LIT:w>' ) <EOL> zipout . writestr ( '<STR_LIT>' % Name , makeApplication ( ) ) <EOL> zipout . writestr ( '<STR_LIT>' , makeProxy ( ) ) <EOL> zipout . writestr ( '<STR_LIT>' , makeTarget ( ) ) <EOL> for topName in os . listdir ( Directory ) : <EOL> if not pathContainsDot ( topName ) : <EOL> fn = os . path . join ( Directory , topName ) <EOL> if ( os . path . isdir ( fn ) ) : <EOL> contents = zipDirectory ( fn , topName ) <EOL> en = '<STR_LIT>' % topName <EOL> zipout . writestr ( en , contents ) <EOL> else : <EOL> en = '<STR_LIT>' % topName <EOL> zipout . write ( fn , en ) <EOL> zipout . close ( ) <EOL> if ( ZipFile != None ) : <EOL> tzf = open ( ZipFile , '<STR_LIT:w>' ) <EOL> tzf . write ( tf . getvalue ( ) ) <EOL> tzf . close ( ) <EOL> revision = deploytools . importBundle ( Organization , Name , tf . getvalue ( ) ) <EOL> if ( revision < <NUM_LIT:0> ) : <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> print '<STR_LIT>' % revision <EOL> if ShouldDeploy : <EOL> status = deploytools . deployWithoutConflict ( Organization , Environment , Name , '<STR_LIT:/>' , revision ) <EOL> if status == False : <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> response = httptools . httpCall ( '<STR_LIT:GET>' , <EOL> '<STR_LIT>' % ( Organization , Name ) ) <EOL> deps = deploytools . parseAppDeployments ( Organization , response , Name ) <EOL> deploytools . printDeployments ( deps ) </s>
