Skip to content

Instantly share code, notes, and snippets.

@dersteppenwolf
Forked from cwalv/gist:9191377
Created November 12, 2015 14:53
Show Gist options
  • Save dersteppenwolf/f1b5965387234e109061 to your computer and use it in GitHub Desktop.
Save dersteppenwolf/f1b5965387234e109061 to your computer and use it in GitHub Desktop.
gpServiceDeploy.py
#!/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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment