def test_same_file_with_error(self):
     # different files
     self.assertFalse(util.same_file("/etc/keytalk/apache.ini", "/etc/keytalk/resept.ini"))
     # different files (though same contents)
     os.system("cp -f /etc/keytalk/apache.ini /tmp/apache.ini")
     self.assertFalse(util.same_file("/etc/keytalk/apache.ini", "/tmp/apache.ini"))
     # directory
     self.assertFalse(util.same_file("/etc/keytalk/", "/etc/keytalk/"))
     # not a file
     self.assertFalse(util.same_file("", ""))
 def test_same_file_with_success(self):
     # same file
     self.assertTrue(util.same_file("/etc/keytalk/apache.ini", "/etc/keytalk/apache.ini"))
     # whitespace
     self.assertTrue(
         util.same_file("/etc/keytalk/apache.ini    ", " /etc/keytalk/apache.ini "))
     # symlink
     os.system("ln -sf /etc/keytalk/apache.ini /tmp/apache-slink.ini")
     self.assertTrue(util.same_file("/etc/keytalk/apache.ini", "/tmp/apache-slink.ini"))
     # hardlink
     os.system("ln -f /etc/keytalk/apache.ini /tmp/apache-hlink.ini")
     self.assertTrue(util.same_file("/etc/keytalk/apache.ini", "/tmp/apache-hlink.ini"))
Пример #3
0
def init():

  global initialized
  if initialized:
     return

  # Create directories
  for dir in [ irpf90_t.irpdir, irpf90_t.mandir ]:
    try:
      wd = os.getcwd()
      os.chdir(dir)
      os.chdir(wd)
    except OSError:
      os.mkdir(dir)

  for dir in command_line.include_dir:
    dir = irpf90_t.irpdir+dir
    try:
      wd = os.getcwd()
      os.chdir(dir)
      os.chdir(wd)
    except OSError:
      os.mkdir(dir)

  # Create makefile
  makefile.create()
  
  # Copy current files in the irpdir
  for dir in ['./']+command_line.include_dir:
    try:
      os.stat(dir)
    except:
      print dir,'not in dir'
      continue
    for filename in os.listdir(dir):
      filename = dir+filename
      if not filename.startswith(".") and not os.path.isdir(filename):
        try:
          file  = open(filename,"r")
        except IOError:
          if command_line.do_warnings:
              print "Warning : Unable to read file %s."%(filename)
        else:
          buffer = file.read()
          file.close()
          if not util.same_file(irpf90_t.irpdir+filename,buffer):
            file = open(irpf90_t.irpdir+filename,"w")
            file.write(buffer)
            file.close()

  initialized = True
def install_apache_ssl_cert(pem_cert_key_path, site, restart_apache=False):
    vhost = site['VHost']
    Logger.info(
        'Installing SSL certificate for virtual host at {VHost}'.format(
            **site))

    server_name = site['ServerName']
    ssl_cert_path = apache_util.get_apache_ssl_cert_path(vhost, server_name)
    ssl_key_path = apache_util.get_apache_ssl_key_path(vhost, server_name)

    certs = util.parse_certs(pem_cert_key_path, Logger)
    if not certs:
        raise Exception(
            "No X.509 certs found in {} received by KeyTalk client".format(
                pem_cert_key_path))
    keys = util.parse_keys(pem_cert_key_path, Logger)
    if not keys:
        raise Exception(
            "No X.509 keys found in {} received by KeyTalk client".format(
                pem_cert_key_path))
    cas = util.parse_cas(Logger)

    if util.same_file(ssl_cert_path, ssl_key_path):
        Logger.debug("Saving SSL certificate with key and {} CAs to {}".format(
            len(cas), ssl_cert_path))
        util.save_to_file('\n'.join(certs + keys + cas), ssl_cert_path)
    else:
        Logger.debug(
            "Saving SSL certificates (serial: {}) and {} CAs to {}".format(
                OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
                                                certs[0]).get_serial_number(),
                len(cas), ssl_cert_path))
        util.save_to_file('\n'.join(certs + cas), ssl_cert_path)
        Logger.debug("Saving SSL key to " + ssl_key_path)
        util.save_to_file('\n'.join(keys), ssl_key_path)

    # ask Apache to gracefully reload key material
    if restart_apache:
        reload_apache()
Пример #5
0
def init():

  global initialized
  if initialized:
     return

  # Create directories
  for dir in [ irpf90_t.irpdir, irpf90_t.mandir ]:
    try:
      wd = os.getcwd()
      os.chdir(dir)
      os.chdir(wd)
    except OSError:
      os.mkdir(dir)

  # Create makefile
  makefile.create()
  
  # Copy current files in the irpdir
  for filename in os.listdir(os.getcwd()):
    if not filename[0].startswith(".") and not os.path.isdir(filename):
      try:
        file  = open(filename,"r")
      except IOError:
        print "Warning : Unable to read file %s."%(filename)
      else:
        buffer = file.readlines()
        file.close()
        if not util.same_file(irpf90_t.irpdir+filename,buffer):
          file  = open(filename,"r")
          buffer = file.read()
          file.close()
          file = open(irpf90_t.irpdir+filename,"w")
          file.write(buffer)
          file.close()

  initialized = True
Пример #6
0
def create():

    txt = """
module irp_stack_mod
  integer, parameter            :: STACKMAX=1000
  character*(128),allocatable   :: irp_stack(:,:)
  double precision,allocatable  :: irp_cpu(:,:)
  integer,allocatable           :: stack_index(:)
  logical                       :: alloc = .False.
  integer                       :: nthread
  character*(128)               :: white = ''
end module

subroutine irp_enter(irp_where)
 use irp_stack_mod
 integer       :: ithread
 character*(*) :: irp_where
"""
    if not do_openmp:
        txt += """
   ithread = 0
"""
    else:
        txt += """
 integer, external :: omp_get_thread_num
 integer, external :: omp_get_num_threads
 ithread = omp_get_thread_num()
"""

    txt += "$1"

    if do_memory:
        txt += """
 if (.not.alloc) then
"""
        if do_openmp:
            txt += """
 !$OMP PARALLEL
 !$OMP SINGLE
 nthread = omp_get_num_threads()
 !$OMP END SINGLE
 !$OMP END PARALLEL
"""
        else:
            txt += """
 nthread = 1
 """
        txt += """
   print *, 'Allocating irp_stack(',STACKMAX,',',0:nthread,')'
   print *, 'Allocating irp_cpu(',STACKMAX,',',0:nthread,')'
   print *, 'Allocating stack_index(',0:nthread,')'
 endif"""
    txt += """
$2
end subroutine

subroutine irp_enter_f(irp_where)
 use irp_stack_mod
 integer       :: ithread
 character*(*) :: irp_where
 """
    if do_openmp:
        txt += """
 integer, external :: omp_get_thread_num
 integer, external :: omp_get_num_threads
 ithread = omp_get_thread_num()
"""
    else:
        txt += """
  ithread = 0
"""
    txt += """
$1
"""
    if do_memory:
        txt += """
 if (.not.alloc) then
"""
        if do_openmp:
            txt += """
 !$OMP PARALLEL
 !$OMP SINGLE
  nthread = omp_get_num_threads()
 !$OMP END SINGLE
 !$OMP END PARALLEL
"""
        else:
            txt += """
  nthread = 1
"""
        txt += """
  print *, 'Allocating irp_stack(',STACKMAX,',',0:nthread,')'
  print *, 'Allocating irp_cpu(',STACKMAX,',',0:nthread,')'
  print *, 'Allocating stack_index(',0:nthread,')'
 endif
"""
    txt += """
$2
end subroutine

subroutine irp_leave (irp_where)
 use irp_stack_mod
  character*(*) :: irp_where
  integer       :: ithread
  double precision :: cpu
"""
    if do_openmp:
        txt += """
 integer, external :: omp_get_thread_num
 ithread = omp_get_thread_num()
 """
    else:
        txt += """
 ithread = 0
 """
    txt += """
$3
$4
end subroutine
"""

    # $1
    if do_debug:
        s = """
 if (.not.alloc) then
 """
        if do_openmp:
            s += """
 !$OMP PARALLEL
 !$OMP SINGLE
 nthread = omp_get_num_threads()
 !$OMP END SINGLE
 !$OMP END PARALLEL
 !$OMP CRITICAL
 if (.not.alloc) then
   allocate(irp_stack(0:STACKMAX,0:nthread))
   allocate(irp_cpu(0:STACKMAX,0:nthread))
   allocate(stack_index(0:nthread))
   stack_index = 0
   alloc = .True.
 endif
 !$OMP END CRITICAL
 endif
 stack_index(ithread) = min(stack_index(ithread)+1,STACKMAX)
 irp_stack(stack_index(ithread),ithread) = irp_where"""
        else:
            s += """
 nthread = 1
 if (.not.alloc) then
   allocate(irp_stack(0:STACKMAX,1))
   allocate(irp_cpu(0:STACKMAX,1))
   allocate(stack_index(2))
   stack_index = 0
   alloc = .True.
 endif
 endif
 stack_index(1) = min(stack_index(1)+1,STACKMAX)
 irp_stack(stack_index(1),1) = irp_where"""
        if do_memory:
            txt += """
  print *, 'Allocating irp_stack(',STACKMAX,','0:nthread,')'
  print *, 'Allocating irp_cpu(',STACKMAX,','0:nthread,')'
  print *, 'Allocating stack_index(',0:nthread,')'"""
    else:
        s = ""
    txt = txt.replace("$1", s)

    # $2
    if do_debug:
        txt = txt.replace(
            "$2", """
  print *, ithread, ':', white(1:stack_index(ithread))//'-> ', trim(irp_where)
  call cpu_time(irp_cpu(stack_index(ithread),ithread))""")
    else:
        txt = txt.replace("$2", "")

    # $3
    if do_debug:
        txt = txt.replace(
            "$3", """
  call cpu_time(cpu)
  print *, ithread, ':', white(1:stack_index(ithread))//'<- ', &
    trim(irp_stack(stack_index(ithread),ithread)), &
    cpu-irp_cpu(stack_index(ithread),ithread)""")
    else:
        txt = txt.replace("$3", "")

    # $4
    if do_debug:
        txt = txt.replace(
            "$4", """
  stack_index(ithread) = max(0,stack_index(ithread)-1)""")
    else:
        txt = txt.replace("$4", "")

    txt += """
subroutine irp_trace
 use irp_stack_mod
 integer :: ithread
 integer :: i
"""
    if do_openmp:
        txt += """
!$ integer, external :: omp_get_thread_num
!$ ithread = omp_get_thread_num()
"""
    else:
        txt += """
 ithread = 0
"""
    txt += """
 if (.not.alloc) return
 print *, 'Stack trace: ', ithread
 print *, '-------------------------'
 do i=1,stack_index(ithread)
  print *, trim(irp_stack(i,ithread))
 enddo
 print *, '-------------------------'
end subroutine
"""

    txt = txt.split('\n')
    txt = map(lambda x: x + "\n", txt)
    if not util.same_file(FILENAME, txt):
        file = open(FILENAME, 'w')
        file.writelines(txt)
        file.close()
Пример #7
0
def create():

  txt = """
module irp_stack_mod
  integer, parameter            :: STACKMAX=1000
  character*(128),allocatable   :: irp_stack(:,:)
  double precision,allocatable  :: irp_cpu(:,:)
  integer,allocatable           :: stack_index(:)
  logical                       :: alloc = .False.
  integer                       :: nthread
  character*(128)               :: white = ''
end module

subroutine irp_enter(irp_where)
 use irp_stack_mod
 integer       :: ithread
 character*(*) :: irp_where
!$ integer, external :: omp_get_thread_num
!$ integer, external :: omp_get_num_threads
 ithread = 0
!$ ithread = omp_get_thread_num()
$1
if (ithread /= 0) then
   print *, 'Error: Provider is called by thread', ithread
   call irp_trace
   stop 1
endif
"""
  if command_line.do_memory:
      txt+="""
 if (.not.alloc) then
 nthread = 1
 !$OMP PARALLEL
 !$OMP SINGLE
 !$ nthread = omp_get_num_threads()
 !$OMP END SINGLE
 !$OMP END PARALLEL
  print *, 'Allocating irp_stack(',STACKMAX,',',nthread,')'
  print *, 'Allocating irp_cpu(',STACKMAX,',',nthread,')'
  print *, 'Allocating stack_index(',nthread,')'
 endif"""
  txt +="""
$2
end subroutine

subroutine irp_enter_f(irp_where)
 use irp_stack_mod
 integer       :: ithread
 character*(*) :: irp_where
!$ integer, external :: omp_get_thread_num
!$ integer, external :: omp_get_num_threads
 ithread = 0
!$ ithread = omp_get_thread_num()
$1
"""
  if command_line.do_memory:
      txt+="""
 if (.not.alloc) then
 !$OMP PARALLEL
 !$OMP SINGLE
 !$ nthread = omp_get_num_threads()
  print *, 'Allocating irp_stack(',STACKMAX,',',nthread,')'
  print *, 'Allocating irp_cpu(',STACKMAX,',',nthread,')'
  print *, 'Allocating stack_index(',nthread,')'
 !$OMP END SINGLE
 !$OMP END PARALLEL
 endif"""
  txt +="""
$2
end subroutine

subroutine irp_leave (irp_where)
 use irp_stack_mod
  character*(*) :: irp_where
  integer       :: ithread
  double precision :: cpu
!$ integer, external :: omp_get_thread_num
 ithread = 0
!$ ithread = omp_get_thread_num()
$3
$4
end subroutine
"""

  # $1
  if do_assert or do_debug:
    txt = txt.replace("$1","""
 if (.not.alloc) then
 !$OMP PARALLEL
 !$OMP SINGLE
 !$ nthread = omp_get_num_threads()
 !$OMP END SINGLE
 !$OMP END PARALLEL
 !$OMP CRITICAL
 if (.not.alloc) then
   allocate(irp_stack(0:STACKMAX,nthread+1))
   allocate(irp_cpu(0:STACKMAX,nthread+1))
   allocate(stack_index(nthread+1))
   stack_index = 0
   alloc = .True.
 endif
 !$OMP END CRITICAL
 endif
 stack_index(ithread+1) = mod(stack_index(ithread+1)+1,STACKMAX)
 irp_stack(stack_index(ithread+1),ithread+1) = irp_where""")
    if command_line.do_memory:
      txt+="""
  print *, 'Allocating irp_stack(',STACKMAX,','nthread,')'
  print *, 'Allocating irp_cpu(',STACKMAX,','nthread,')'
  print *, 'Allocating stack_index(',nthread,')'"""
  else:
    txt = txt.replace("$1","")

  # $2
  if do_debug:
    txt = txt.replace("$2","""
  print *, ithread, ':', white(1:stack_index(ithread+1))//'-> ', trim(irp_where)
  call cpu_time(irp_cpu(stack_index(ithread+1),ithread+1))""")
  else:
    txt = txt.replace("$2","")

  # $3
  if do_debug:
    txt = txt.replace("$3","""
  call cpu_time(cpu)
  print *, ithread, ':', white(1:stack_index(ithread+1))//'<- ', &
    trim(irp_stack(stack_index(ithread+1),ithread+1)), &
    cpu-irp_cpu(stack_index(ithread+1),ithread+1)""")
  else:
    txt = txt.replace("$3","")

  # $4
  if do_debug or do_assert:
    txt = txt.replace("$4","""
  stack_index(ithread+1) = stack_index(ithread+1)-1""")
  else:
    txt = txt.replace("$4","")

  txt += """
subroutine irp_trace
 use irp_stack_mod
 integer :: ithread
 integer :: i
!$ integer, external :: omp_get_thread_num
 ithread = 0
!$ ithread = omp_get_thread_num()
 if (.not.alloc) return
 print *, 'Stack trace: ', ithread
 print *, '-------------------------'
 do i=1,stack_index(ithread+1)
  print *, trim(irp_stack(i,ithread+1))
 enddo
 print *, '-------------------------'
end subroutine
"""

  txt = txt.split('\n')
  txt = map(lambda x: x+"\n",txt)
  if not util.same_file(FILENAME, txt):
    file = open(FILENAME,'w')
    file.writelines(txt)
    file.close()
Пример #8
0
def create():

  txt = """
module irp_stack_mod
  integer, parameter            :: STACKMAX=1000
  character*(128),allocatable   :: irp_stack(:,:)
  double precision,allocatable  :: irp_cpu(:,:)
  integer,allocatable           :: stack_index(:)
  logical                       :: alloc = .False.
  character*(128)               :: white = ''
end module

subroutine irp_enter(irp_where)
 use irp_stack_mod
 integer       :: ithread
 integer       :: nthread
 character*(*) :: irp_where
$OMP_DECL
!$OMP CRITICAL
 ithread = $OMP_GET_THREAD_NUM
 nthread = $OMP_GET_NUM_THREADS
$1
!$OMP END CRITICAL
"""
  if command_line.do_memory:
      txt+="""
 if (.not.alloc) then
  print *, 'Allocating irp_stack(',STACKMAX,',',nthread,')'
  print *, 'Allocating irp_cpu(',STACKMAX,',',nthread,')'
  print *, 'Allocating stack_index(',nthread,')'
 endif"""
  txt +="""
$2
end subroutine

subroutine irp_leave (irp_where)
 use irp_stack_mod
  character*(*) :: irp_where
  integer       :: ithread
  double precision :: cpu
$OMP_DECL
!$OMP CRITICAL
  ithread = $OMP_GET_THREAD_NUM
$3
$4
!$OMP END CRITICAL
end subroutine
"""

  # $OMP_DECL
  if do_openmp:
    txt = txt.replace("$OMP_DECL","""
  integer :: omp_get_num_threads
  integer :: omp_get_thread_num
""")
    txt = txt.replace("$OMP_GET_NUM_THREADS","omp_get_num_threads()")
    txt = txt.replace("$OMP_GET_THREAD_NUM","omp_get_thread_num()")
  else:
    txt = txt.replace("$OMP_DECL","")
    txt = txt.replace("$OMP_GET_NUM_THREADS","1")
    txt = txt.replace("$OMP_GET_THREAD_NUM","0")

  # $1
  if do_assert or do_debug:
    txt = txt.replace("$1","""
 if (.not.alloc) then
   allocate(irp_stack(STACKMAX,nthread+1))
   allocate(irp_cpu(STACKMAX,nthread+1))
   allocate(stack_index(nthread+1))
   stack_index = 0
   alloc = .True.
 endif
 stack_index(ithread+1) = stack_index(ithread+1)+1
 irp_stack(stack_index(ithread+1),ithread+1) = irp_where""")
    if command_line.do_memory:
      txt+="""
  print *, 'Allocating irp_stack(',STACKMAX,','nthread,')'
  print *, 'Allocating irp_cpu(',STACKMAX,','nthread,')'
  print *, 'Allocating stack_index(',nthread,')'"""
  else:
    txt = txt.replace("$1","")

  # $2
  if do_debug:
    txt = txt.replace("$2","""
  print *, ithread, ':', white(1:stack_index(ithread+1))//'-> ', trim(irp_where)
  call cpu_time(irp_cpu(stack_index(ithread+1),ithread+1))""")
  else:
    txt = txt.replace("$2","")

  # $3
  if do_debug:
    txt = txt.replace("$3","""
  call cpu_time(cpu)
  print *, ithread, ':', white(1:stack_index(ithread+1))//'<- ', &
    trim(irp_stack(stack_index(ithread+1),ithread+1)), &
    cpu-irp_cpu(stack_index(ithread+1),ithread+1)""")
  else:
    txt = txt.replace("$3","")

  # $4
  if do_debug or do_assert:
    txt = txt.replace("$4","""
  stack_index(ithread+1) = stack_index(ithread+1)-1""")
  else:
    txt = txt.replace("$4","")

  if do_debug or do_assert:
    txt += """
subroutine irp_trace
 use irp_stack_mod
 integer :: ithread
 integer :: i
 ithread = 0
 if (.not.alloc) return
 print *, 'Stack trace: ', ithread
 print *, '-------------------------'
 do i=1,stack_index(ithread+1)
  print *, trim(irp_stack(i,ithread+1))
 enddo
 print *, '-------------------------'
end subroutine
"""

  txt = txt.split('\n')
  txt = map(lambda x: x+"\n",txt)
  if not util.same_file(FILENAME, txt):
    file = open(FILENAME,'w')
    file.writelines(txt)
    file.close()
Пример #9
0
def create():

  txt = """
module irp_stack_mod
  integer, parameter            :: STACKMAX=1000
  character*(128),allocatable   :: irp_stack(:,:)
  double precision,allocatable  :: irp_cpu(:,:)
  integer,allocatable           :: stack_index(:)
  logical                       :: alloc = .False.
  integer                       :: nthread
  character*(128)               :: white = ''
end module

subroutine irp_enter(irp_where)
 use irp_stack_mod
 integer       :: ithread
 character*(*) :: irp_where
"""
  if not do_openmp:
    txt += """
   ithread = 0
"""
  else:
    txt += """
 integer, external :: omp_get_thread_num
 integer, external :: omp_get_num_threads
 ithread = omp_get_thread_num()
"""

  txt += "$1"

  if do_memory:
     txt+="""
 if (.not.alloc) then
"""
     if do_openmp:
       txt += """
 !$OMP PARALLEL
 !$OMP SINGLE
 nthread = omp_get_num_threads()
 !$OMP END SINGLE
 !$OMP END PARALLEL
"""
     else:
       txt += """
 nthread = 1
 """
     txt += """
   print *, 'Allocating irp_stack(',STACKMAX,',',0:nthread,')'
   print *, 'Allocating irp_cpu(',STACKMAX,',',0:nthread,')'
   print *, 'Allocating stack_index(',0:nthread,')'
 endif"""
  txt +="""
$2
end subroutine

subroutine irp_enter_f(irp_where)
 use irp_stack_mod
 integer       :: ithread
 character*(*) :: irp_where
 """
  if do_openmp:
    txt += """
 integer, external :: omp_get_thread_num
 integer, external :: omp_get_num_threads
 ithread = omp_get_thread_num()
"""
  else:
    txt += """
  ithread = 0
"""
  txt += """
$1
"""
  if do_memory:
    txt+="""
 if (.not.alloc) then
"""
    if do_openmp:
      txt += """
 !$OMP PARALLEL
 !$OMP SINGLE
  nthread = omp_get_num_threads()
 !$OMP END SINGLE
 !$OMP END PARALLEL
"""
    else:
      txt += """
  nthread = 1
"""
    txt +="""
  print *, 'Allocating irp_stack(',STACKMAX,',',0:nthread,')'
  print *, 'Allocating irp_cpu(',STACKMAX,',',0:nthread,')'
  print *, 'Allocating stack_index(',0:nthread,')'
 endif
"""
  txt += """
$2
end subroutine

subroutine irp_leave (irp_where)
 use irp_stack_mod
  character*(*) :: irp_where
  integer       :: ithread
  double precision :: cpu
"""
  if do_openmp:
    txt += """
 integer, external :: omp_get_thread_num
 ithread = omp_get_thread_num()
 """
  else:
    txt += """
 ithread = 0
 """
  txt += """
$3
$4
end subroutine
"""

  # $1
  if do_debug:
    s = """
 if (.not.alloc) then
 """
    if do_openmp:
      s += """
 !$OMP PARALLEL
 !$OMP SINGLE
 nthread = omp_get_num_threads()
 !$OMP END SINGLE
 !$OMP END PARALLEL
 !$OMP CRITICAL
 if (.not.alloc) then
   allocate(irp_stack(0:STACKMAX,0:nthread))
   allocate(irp_cpu(0:STACKMAX,0:nthread))
   allocate(stack_index(0:nthread))
   stack_index = 0
   alloc = .True.
 endif
 !$OMP END CRITICAL
 endif
 stack_index(ithread) = min(stack_index(ithread)+1,STACKMAX)
 irp_stack(stack_index(ithread),ithread) = irp_where"""
    else:
      s += """
 nthread = 1
 if (.not.alloc) then
   allocate(irp_stack(0:STACKMAX,1))
   allocate(irp_cpu(0:STACKMAX,1))
   allocate(stack_index(2))
   stack_index = 0
   alloc = .True.
 endif
 endif
 stack_index(1) = min(stack_index(1)+1,STACKMAX)
 irp_stack(stack_index(1),1) = irp_where"""
    if do_memory:
      txt+="""
  print *, 'Allocating irp_stack(',STACKMAX,','0:nthread,')'
  print *, 'Allocating irp_cpu(',STACKMAX,','0:nthread,')'
  print *, 'Allocating stack_index(',0:nthread,')'"""
  else:
    s = ""
  txt = txt.replace("$1",s)

  # $2
  if do_debug:
    txt = txt.replace("$2","""
  print *, ithread, ':', white(1:stack_index(ithread))//'-> ', trim(irp_where)
  call cpu_time(irp_cpu(stack_index(ithread),ithread))""")
  else:
    txt = txt.replace("$2","")

  # $3
  if do_debug:
    txt = txt.replace("$3","""
  call cpu_time(cpu)
  print *, ithread, ':', white(1:stack_index(ithread))//'<- ', &
    trim(irp_stack(stack_index(ithread),ithread)), &
    cpu-irp_cpu(stack_index(ithread),ithread)""")
  else:
    txt = txt.replace("$3","")

  # $4
  if do_debug:
    txt = txt.replace("$4","""
  stack_index(ithread) = max(0,stack_index(ithread)-1)""")
  else:
    txt = txt.replace("$4","")

  txt += """
subroutine irp_trace
 use irp_stack_mod
 integer :: ithread
 integer :: i
"""
  if do_openmp:
    txt += """
!$ integer, external :: omp_get_thread_num
!$ ithread = omp_get_thread_num()
"""
  else:
    txt += """
 ithread = 0
"""
  txt += """
 if (.not.alloc) return
 print *, 'Stack trace: ', ithread
 print *, '-------------------------'
 do i=1,stack_index(ithread)
  print *, trim(irp_stack(i,ithread))
 enddo
 print *, '-------------------------'
end subroutine
"""

  txt = txt.split('\n')
  txt = map(lambda x: x+"\n",txt)
  if not util.same_file(FILENAME, txt):
    file = open(FILENAME,'w')
    file.writelines(txt)
    file.close()