#!/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 = '
'
def addParameterDescriptions(pytXmlFn, description='-'):
pat = r''
repl = r'{}'.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'(|).*'
repl = r'\1{}'.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)