def clone()

in tsqa/environment.py [0:0]


    def clone(self, layout=None):
        """
        Clone the given layout to this environment's prefix
        """
        # First, make the prefix directory.
        if self.layout is None:
            self.layout = Layout(tempfile.mkdtemp(
                prefix=os.environ.get('TSQA_LAYOUT_PREFIX', 'tsqa.env.'),
                dir=os.environ.get('TSQA_LAYOUT_DIR', None)),
            )
        else:
            os.makedirs(self.layout.prefix)
        os.chmod(self.layout.prefix, 0777)  # Make the tmp dir readable by all

        # TODO: handle sock files (copytree doesn't like them!)
        # copy all files from old layout to new one
        for item in os.listdir(layout.prefix):
            src_path = os.path.join(layout.prefix, item)
            dst_path = os.path.join(self.layout.prefix, item)
            # if its the bindir, lets symlink in everything
            if item == layout.suffixes['bindir']:
                os.makedirs(dst_path)  # make the dest dir
                for bin_item in os.listdir(src_path):
                     os.symlink(
                        os.path.join(src_path, bin_item),
                        os.path.join(dst_path, bin_item),
                     )

            elif os.path.islink(src_path):
                linkto = os.readlink(src_path)
                os.symlink(linkto, dst_path)

            elif os.path.isdir(src_path):
                shutil.copytree(
                    src_path,
                    dst_path,
                    symlinks=True,
                    ignore=None,
                )

            elif os.path.isfile(src_path):
                shutil.copyfile(src_path, dst_path)

        # make sure that all suffixes in new layout exist
        for name in self.layout.suffixes:
            dirname = getattr(self.layout, name)
            if not os.path.exists(dirname):
                os.makedirs(dirname, 0777)
            else:
                os.chmod(dirname, 0777)

        http_server_port = tsqa.utils.bind_unused_port()[1]
        manager_mgmt_port = tsqa.utils.bind_unused_port()[1]
        admin_port = tsqa.utils.bind_unused_port()[1]

        self.hostports = [
            ('127.0.0.1', http_server_port),
            ('127.0.0.1', manager_mgmt_port),
            ('127.0.0.1', admin_port),
        ]

        # TODO: clear out all configs!
        # truncate remap.config
        open(os.path.join(self.layout.sysconfdir, 'remap.config'), 'w').truncate()
        open(os.path.join(self.layout.sysconfdir, 'records.config'), 'w').truncate()

        # overwrite a few things that need to be changed to have a unique env
        records = tsqa.configs.RecordsConfig(os.path.join(self.layout.sysconfdir, 'records.config'))
        records['CONFIG'] = {
            'proxy.config.config_dir': self.layout.sysconfdir,
            'proxy.config.body_factory.template_sets_dir': os.path.join(self.layout.sysconfdir, 'body_factory'),
            'proxy.config.plugin.plugin_dir': self.layout.plugindir,
            'proxy.config.bin_path': self.layout.bindir,
            'proxy.config.log.logfile_dir': self.layout.logdir,
            'proxy.config.local_state_dir': self.layout.runtimedir,
            'proxy.config.http.server_ports': str(http_server_port),  # your own listen port
            'proxy.config.process_manager.mgmt_port': manager_mgmt_port,  # your own listen port
            'proxy.config.admin.synthetic_port': admin_port,
            'proxy.config.admin.autoconf_port': admin_port,  # support pre ATS 6.x
            'proxy.config.admin.user_id': '#-1',

            # a bunch of debug options
            'proxy.config.diags.show_location': 1,
            'proxy.config.diags.output.diag': 'OL',
            'proxy.config.diags.output.debug': 'OL',
            'proxy.config.diags.output.status': 'OL',
            'proxy.config.diags.output.note': 'OL',
            'proxy.config.diags.output.warning': 'OL',
            'proxy.config.diags.output.error': 'OL',
            'proxy.config.diags.output.fatal': 'OL',
            'proxy.config.diags.output.alert': 'OL',
            'proxy.config.diags.output.emergency': 'OL',

            # set the process_server timeouts to 0 (faster startup)
            'proxy.config.lm.pserver_timeout_secs': 0,
            'proxy.config.lm.pserver_timeout_msecs': 0,
        }
        records.write()

        os.chmod(os.path.join(os.path.dirname(self.layout.runtimedir)), 0777)
        os.chmod(os.path.join(self.layout.runtimedir), 0777)

        # write out a convenience script to
        with open(os.path.join(self.layout.prefix, 'run'), 'w') as runscript:
            runscript.write('#! /usr/bin/env sh\n\n')
            runscript.write('# run PROGRAM [ARGS ...]\n')
            runscript.write('# Run a Traffic Server program in this environment\n\n')
            for k, v in self.shell_env.iteritems():
                runscript.write('{0}="{1}"\n'.format(k, v))
                runscript.write('export {0}\n\n'.format(k))
            runscript.write('exec "$@"\n')

        os.chmod(os.path.join(self.layout.prefix, 'run'), 0755)