2012-11-22 2 views
3

Ghostscript 용 Monkey 패치를 만들려면 시스템에서 셸을 사용할 수 없기 때문에 os.popen에서 subsession.popen으로 마이그레이션해야합니다.Ghostscript를 os.popen에서 subsession.popen python으로 변환

나는이 방법으로 시도 :

def mioGhostscript(tile, size, fp): 
    """Render an image using Ghostscript (Unix only)""" 

    # Unpack decoder tile 
    decoder, tile, offset, data = tile[0] 
    length, bbox = data 

    import tempfile, os 

    file = tempfile.mktemp() 

    # Build ghostscript command 
    command = ["gs", 
     "-q",     # quite mode 
     "-g%dx%d" % size,  # set output geometry (pixels) 
     "-dNOPAUSE -dSAFER",  # don't pause between pages, safe mode 
     "-sDEVICE=ppmraw",  # ppm driver 
     "-sOutputFile=%s" % file,# output file 
     "- >/dev/null 2>/dev/null" 
    ] 

    #command = shlex.split(string.join(command)) 
    # push data through ghostscript 

    try: 
     #gs = os.popen(command, "w") 

     args = command#['gs','-dSAFER','-dNOPAUSE','-dBATCH','-sDEVICE=jpeg','-sOutputFile=/home/user/output2.jpg /home/user/downloads/test.pdf'] 
     gs = subprocess.Popen(args, stdout = PIPE, stderr = STDOUT, stdin=PIPE) 
     # adjust for image origin 
     if bbox[0] != 0 or bbox[1] != 0: 
      #gs.write("%d %d translate\n" % (-bbox[0], -bbox[1])) 
      gs.stdin.write("%d %d translate\n" % (-bbox[0], -bbox[1])) 
     fp.seek(offset) 
     while length > 0: 
      s = fp.read(8192) 
      if not s: 
       break 
      length = length - len(s) 
      raise Exception(s) 
      gs.stdin.write(s) 
     gs.communicate()[0] 
     status = gs.stdin.close() 
     #status = gs.close() 
     #if status: 
     # raise IOError("gs failed (status %d)" % status) 
     im = Image.core.open_ppm(file) 
    finally: 
     try: os.unlink(file) 
     except: pass 

    return im 

import PIL 
PIL.EpsImagePlugin.Ghostscript = mioGhostscript 

하지만 난이 역 추적이 :

Traceback (most recent call last): File "/home/web/lib/driver_mod_python.py", line 252, in handler buf = m.__dict__[pard['program']](pard) File "/home/dtwebsite/bin/cms_gest_ordini.py", line 44, in wrapped return func(pard) File "/home/dtwebsite/bin/cms_gest_ordini.py", line 95, in wrapped return func(pard) File "/home/dtwebsite/bin/cms_gest_picking_list.py", line 341, in picking_list tr_modelllo = render_row_picking_list(pard, item, picked=0, plist_allowed=plist_allowed) File "/home/dtwebsite/bin/cms_gest_picking_list.py", line 432, in render_row_picking_list aa = a.tostring() File "/rnd/apps/interpreters/python-2.5.6/lib/python2.5/site-packages/PIL/Image.py", line 532, in tostring self.load() File "/rnd/apps/interpreters/python-2.5.6/lib/python2.5/site-packages/PIL/EpsImagePlugin.py", line 283, in load self.im = Ghostscript(self.tile, self.size, self.fp) File "/home/dtwebsite/bin/cms_gest_picking_list.py", line 64, in mioGhostscript gs.stdin.write(s) IOError: [Errno 32] Broken pipe 

누군가가 제발 도움이 될 수 있습니까?

+1

완전한 추적을 추가하십시오. 그렇지 않으면 그냥 추측입니다. –

+0

찾을 수있는 'subsession.popen'에 대한 유일한 참조는 [여기에 대한 귀하의 질문]입니다 (http://www.google.com/search?&as_epq=subsession + popen). 그것이 무엇인지 설명해 주시겠습니까? – mata

+0

이것은 PIL 모듈 Ghostscripts를위한 원숭이 패치입니다 .. 쉘을 사용하지 않고이 시스템을 재 작성하려고합니다. 왜냐하면 시스템에서 사용할 수 없기 때문입니다 ... – torre87

답변

2

문제가 해결되었습니다. 패키지가 PIL 인 경우 설치 중에 바로 컴파일되지 않았습니다. 그 후 나는 의존성 문제가있었습니다. 그런 다음 명령이보고

import PIL.EpsImagePlugin 
PIL.EpsImagePlugin.Ghostscript = mioGhostscript 

: 나는 다음과 같은 방법으로 그것을 고정 된 코드는 쉘 코드가

"- >/dev/null 2>/dev/null" 

와 파이썬을 시도하기 때문에 내 시스템에서 작동하지 않았다 문자 그대로 - >/dev/null 2>/dev/null이라는 파일을 읽고 존재하지 않습니다.

나는

"-" 

"- >/dev/null 2>/dev/null" 

를 교체하고 프로그램은 이제 stdin에서 읽습니다.

마지막 코드는 다음과 같습니다

def mioGhostscript(tile, size, fp): 
    """Render an image using Ghostscript (Unix only)""" 

    # Unpack decoder tile 
    decoder, tile, offset, data = tile[0] 
    length, bbox = data 

    import tempfile, os 

    file = tempfile.mktemp() 

    # Build ghostscript command 
    command = ["gs", 
     "-q",     # quite mode 
     "-g%dx%d" % size,  # set output geometry (pixels) 
     "-dNOPAUSE -dSAFER",  # don't pause between pages, safe mode 
     "-sDEVICE=ppmraw",  # ppm driver 
     "-sOutputFile=%s" % file,# output file 
     "-" 
    ] 

    #command = shlex.split(string.join(command)) 
    # push data through ghostscript 

    try: 
     #gs = os.popen(command, "w") 

     args = command#['gs','-dSAFER','-dNOPAUSE','-dBATCH','-sDEVICE=jpeg','-sOutputFile=/home/user/output2.jpg /home/user/downloads/test.pdf'] 
     gs = subprocess.Popen(args, stdout = PIPE, stderr = STDOUT, stdin=PIPE) 
     # adjust for image origin 
     if bbox[0] != 0 or bbox[1] != 0: 
      #gs.write("%d %d translate\n" % (-bbox[0], -bbox[1])) 
      gs.stdin.write("%d %d translate\n" % (-bbox[0], -bbox[1])) 
     fp.seek(offset) 
     while length > 0: 
      s = fp.read(8192) 
      if not s: 
       break 
      length = length - len(s) 
      gs.stdin.write(s) 
     gs.communicate()[0] 
     status = gs.stdin.close() 
     #status = gs.close() 
     #if status: 
     # raise IOError("gs failed (status %d)" % status) 
     im = Image.core.open_ppm(file) 
    finally: 
     try: os.unlink(file) 
     except: pass 

    return im 

import PIL.EpsImagePlugin 
PIL.EpsImagePlugin.Ghostscript = mioGhostscript 

나는이 글이 다른 사람을 도울 수 있기를 바랍니다.

관련 문제