code_junkie

How to use Popen in Windows to invoke an external .py script and wait for its completion

Nov 12th, 2011
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. #**********************************************************************
  2. # Description:
  3. # Zips the contents of a folder.
  4. # Parameters:
  5. # 0 - Input folder.
  6. # 1 - Output zip file. It is assumed that the user added the .zip
  7. # extension.
  8. #**********************************************************************
  9.  
  10. # Import modules and create the geoprocessor
  11. #
  12. import sys, zipfile, arcgisscripting, os, traceback
  13. gp = arcgisscripting.create()
  14.  
  15. # Function for zipping files. If keep is true, the folder, along with
  16. # all its contents, will be written to the zip file. If false, only
  17. # the contents of the input folder will be written to the zip file -
  18. # the input folder name will not appear in the zip file.
  19. #
  20. def zipws(path, zip, keep):
  21. path = os.path.normpath(path)
  22. # os.walk visits every subdirectory, returning a 3-tuple
  23. # of directory name, subdirectories in it, and filenames
  24. # in it.
  25. #
  26. for (dirpath, dirnames, filenames) in os.walk(path):
  27. # Iterate over every filename
  28. #
  29. for file in filenames:
  30. # Ignore .lock files
  31. #
  32. if not file.endswith('.lock'):
  33. gp.AddMessage("Adding %s..." % os.path.join(path, dirpath, file))
  34. try:
  35. if keep:
  36. zip.write(os.path.join(dirpath, file),
  37. os.path.join(os.path.basename(path),
  38. os.path.join(dirpath, file)[len(path)+len(os.sep):]))
  39. else:
  40. zip.write(os.path.join(dirpath, file),
  41. os.path.join(dirpath[len(path):], file))
  42.  
  43. except Exception, e:
  44. gp.AddWarning(" Error adding %s: %s" % (file, e))
  45.  
  46. return None
  47.  
  48. if __name__ == '__main__':
  49. try:
  50. # Get the tool parameter values
  51. #
  52. infolder = gp.GetParameterAsText(0)
  53. outfile = gp.GetParameterAsText(1)
  54.  
  55. # Create the zip file for writing compressed data. In some rare
  56. # instances, the ZIP_DEFLATED constant may be unavailable and
  57. # the ZIP_STORED constant is used instead. When ZIP_STORED is
  58. # used, the zip file does not contain compressed data, resulting
  59. # in large zip files.
  60. #
  61. try:
  62. zip = zipfile.ZipFile(outfile, 'w', zipfile.ZIP_DEFLATED)
  63. zipws(infolder, zip, True)
  64. zip.close()
  65. except RuntimeError:
  66. # Delete zip file if exists
  67. #
  68. if os.path.exists(outfile):
  69. os.unlink(outfile)
  70. zip = zipfile.ZipFile(outfile, 'w', zipfile.ZIP_STORED)
  71. zipws(infolder, zip, True)
  72. zip.close()
  73. gp.AddWarning(" Unable to compress zip file contents.")
  74.  
  75. gp.AddMessage("Zip file created successfully")
  76.  
  77. except:
  78. # Return any python specific errors as well as any errors from the geoprocessor
  79. #
  80. tb = sys.exc_info()[2]
  81. tbinfo = traceback.format_tb(tb)[0]
  82. pymsg = "PYTHON ERRORS:nTraceback Info:n" + tbinfo +
  83. "nError Info:n " + str(sys.exc_type) +
  84. ": " + str(sys.exc_value) + "n"
  85. gp.AddError(pymsg)
  86.  
  87. msgs = "GP ERRORS:n" + gp.GetMessages(2) + "n"
  88. gp.AddError(msgs)
Add Comment
Please, Sign In to add comment