# coding=utf-8 import json import os import socket import sys import shutil import datetime import subprocess import time import ctypes import urllib import urllib2 from imp import reload reload(sys) sys.setdefaultencoding('utf8') def run_command(command): """call shell-command and either return its output or kill it if it doesn't normally exit within timeout seconds and return None""" cmd = command start = datetime.datetime.now() process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() if stderr: return False, "Error:" + stderr.decode() else: return True, stdout.decode() def timeout_command(command, timeout): """call shell-command and either return its output or kill it if it doesn't normally exit within timeout seconds and return None""" cmd = command start = datetime.datetime.now() process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) while process.poll() is None: time.sleep(0.2) now = datetime.datetime.now() if (now - start).seconds > timeout: try: handle = ctypes.windll.kernel32.OpenProcess(1, False, process.pid) ctypes.windll.kernel32.TerminateProcess(handle, 0) except OSError: print(OSError) pass return None return process.stdout.readlines() def create_dir(path): """递归创建目录""" if not os.path.exists(path): os.makedirs(path) def write_event_log(log_type, log_keyno, log_status, log_filelocalpos, log_remark): """调用ERP的WriteEventLog通用接口""" url = 'http://localhost/SG_ERP/sg_webapi/erpsvc/common/erp.common.eventlog.WriteEventLog' log_soft = "python" soft_version = "python2.7" user = "admin" pc_name = socket.gethostname() ipaddr = socket.gethostbyname(pc_name) event_log = [{"log_type": log_type, "log_keyno": log_keyno, "log_status": log_status, "log_soft": log_soft, "softversion": soft_version, "log_filelocalpos": log_filelocalpos, "pcname": pc_name, "ipaddr": ipaddr, "user": user, "log_remark": log_remark}] json_event_log = json.dumps(event_log, ensure_ascii=False) values = {'access_token': "", 'api': "erp.common.eventlog.WriteEventLog", 'eventLog': json_event_log} data = urllib.urlencode(values) req = urllib2.Request(url + '?' + data) response = urllib2.urlopen(req) print(response.read()) def output_map_result(iid, result, filename, maptype): url = "http://localhost/SG_ERP/sg_webapi/erpsvc/common/erp.pro.shchy.outputmapresult" values = {'access_token': "", 'api': "erp.pro.shchy.outputmapresult", 'iid': iid, 'result': result, 'filename': filename, 'maptype': maptype} data = urllib.urlencode(values) req = urllib2.Request(url + '?' + data) print(url + '?' + data) response = urllib2.urlopen(req) print(response.code) print(str(response.read()).encode('gbk')) def get_tag_tablespace(fc_name): """根据feature class截取表空间名""" index1 = fc_name.rfind('\\') index2 = fc_name.rfind('.') tablespace = fc_name[index1 + 1:index2 - index1 - 1] return tablespace