Created
February 24, 2014 16:17
-
-
Save cwalv/9191377 to your computer and use it in GitHub Desktop.
Revisions
-
cwalv created this gist
Feb 24, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,102 @@ #!/usr/bin/env python ''' This script creates an .sd from a .pyt .. It assumes that all tools can be called with no parameters (i.e., all parameters are "optional". Not ideal for all services, but works fine for services that arn't called from ArcMap anyway. If you are calling your service from ArcMap, publish the traditional way .. ''' import argparse import cgi import logging import re import sys import os.path as p # TODO: make these available as args from the command line .. pytfn = '/path/to/pytfile.pyt' serviceName = 'service name' uglyDescriptionWrapper = '<DIV STYLE="text-align:Left;"><DIV><P><SPAN>{}</SPAN></P></DIV></DIV>' def addParameterDescriptions(pytXmlFn, description='-'): pat = r'<param (.+?)/>' repl = r'<param \1><dialogReference>{}</dialogReference></param>'.format( cgi.escape(uglyDescriptionWrapper.format(description))) with open(pytXmlFn, 'rb+') as fobj: pytXml = fobj.read() fobj.seek(0) fobj.truncate() fobj.write(re.sub(pat, repl, pytXml, flags=re.DOTALL)) def addToolDescription(pytXmlFn, description='-'): pat = r'(<parameters/>|</parameters>).*</tool>' repl = r'\1<summary>{}</summary></tool>'.format( cgi.escape(uglyDescriptionWrapper.format(description))) with open(pytXmlFn, 'rb+') as fobj: pytXml = fobj.read() fobj.seek(0) fobj.truncate() fobj.write(re.sub(pat, repl, pytXml, flags=re.DOTALL)) def createServiceDescriptionForPyt(pytfn, serviceName=None): logging.basicConfig(level=logging.DEBUG) logging.getLogger().setLevel(logging.DEBUG) pytDir, pytName = p.split(pytfn) if not pytName.endswith('.pyt'): raise ValueError('pytfn ({}) must end with .pyt'.format(pytfn)) pytName = pytName[:-4] # truncate .pyt # we do this from within the function because it takes forever .. # an alternative would be something like hg's demandload() logging.info('importing arcpy ..') import arcpy logging.info('importing toolbox ..') stdout, stderr = sys.stdout, sys.stderr try: m = arcpy.ImportToolbox(pytfn) finally: sys.stdout, sys.stderr = stdout, stderr toolNames = m.__all__ logging.info('adding parameter/tool descriptions ..') for toolName in toolNames: logging.info(' .. adding description for {}'.format(toolName)) xmlfn = p.join(pytDir, '{}.{}.pyt.xml'.format(pytName, toolName)) addParameterDescriptions(xmlfn) addToolDescription(xmlfn) logging.info('collecting results objects ..') results = [] for toolName in toolNames: tool = getattr(m, toolName) results.append(tool()) gpsdraftFn = p.join(pytDir, '{}.sddraft'.format(pytName)) if serviceName is None: serviceName = pytName arcpy.CreateGPSDDraft(results, gpsdraftFn, serviceName, showMessages='Info') arcpy.StageService_server(gpsdraftFn) if __name__ == '__main__': createServiceDescriptionForPyt(pytfn, serviceName)