# Path: haystack/memory_dumper.py
# class MemoryDumper:
#     ARCHIVE_TYPES = ["dir", "tar", "gztar"]
#     def __init__(self, pid, dest):
#     def make_mappings(self):
#     def dump(self, dest=None):
#     def _dump_all_mappings_winapp(self, destdir):
#     def _dump_all_mappings(self, destdir):
#     def _free_process(self):
#     def _dump_mapping(self, m, tmpdir):
# def dump(pid, outfile):
# def _dump(opt):
# def argparser():
# def main():


import logging
import os
import subprocess
import sys
import shutil
import time
from haystack import memory_dumper



log = logging.getLogger('make.py')


def main():
    print('   **make.py** ', sys.argv[1])
    dumpname = sys.argv[1]
    fn = open('/dev/null')
    app = './%s' % (dumpname[:dumpname.rindex('.')])
    print('   **make.py** app', app)
    try:
        print('   **cleantree** dumpname', dumpname)
        shutil.rmtree(dumpname)
        print('   **cleantree done** dumpname', dumpname)
    except:
        pass
    print('   **open stdout w** ', app + ".stdout")
    out = open(app + ".stdout", 'w')
    # pid1 = subprocess.Popen([app], stdout=fn.fileno())
    print('   **popen process** ', app)
    pid1 = subprocess.Popen([app], bufsize=-1, stdout=out.fileno())
    print("process", pid1.pid, "was launched")
    time.sleep(0.9)
    print("  **end sleep**", pid1.pid)
    if not os.access(app + ".stdout", os.F_OK):
        print(" ** preDUMP ** file %s was not written" % app + ".stdout")
    print('   **DUMP** ', pid1.pid)
    memory_dumper.dump(pid1.pid, dumpname)

# Path: haystack/memory_dumper.py
# class MemoryDumper:
#     ARCHIVE_TYPES = ["dir", "tar", "gztar"]
#     def __init__(self, pid, dest):
#     def make_mappings(self):
#     def dump(self, dest=None):
#     def _dump_all_mappings_winapp(self, destdir):
#     def _dump_all_mappings(self, destdir):
#     def _free_process(self):
#     def _dump_mapping(self, m, tmpdir):
# def dump(pid, outfile):
# def _dump(opt):
# def argparser():
# def main():


# Path: test/src/make.py
import logging
import os
import subprocess
import sys
import shutil
import time
from haystack import memory_dumper




log = logging.getLogger('make.py')


def main():
    print('   **make.py** ', sys.argv[1])
    dumpname = sys.argv[1]
    fn = open('/dev/null')
    app = './%s' % (dumpname[:dumpname.rindex('.')])
    print('   **make.py** app', app)
    try:
        print('   **cleantree** dumpname', dumpname)
        shutil.rmtree(dumpname)
        print('   **cleantree done** dumpname', dumpname)
    except:
        pass
    print('   **open stdout w** ', app + ".stdout")
    out = open(app + ".stdout", 'w')
    # pid1 = subprocess.Popen([app], stdout=fn.fileno())
    print('   **popen process** ', app)
    pid1 = subprocess.Popen([app], bufsize=-1, stdout=out.fileno())
    print("process", pid1.pid, "was launched")
    time.sleep(0.9)
    print("  **end sleep**", pid1.pid)
    if not os.access(app + ".stdout", os.F_OK):
        print(" ** preDUMP ** file %s was not written" % app + ".stdout")
    print('   **DUMP** ', pid1.pid)
    memory_dumper.dump(pid1.pid, dumpname)

    print('   **DUMP terminated** ', pid1.pid)