def create(name): host = name2addr(name) cmd = ['redis-server', '--bind', host, '--port', str(MDS_PORT)] popen(cmd) srv = redis.StrictRedis(host=host, port=MDS_PORT) if not _check_server(srv): raise Exception('failed to create') srv.flushall() _log('create, name=%s' % name)
def _start_container(name): pid = ipc.get_pid(name) if not pid: log('failed to start container') raise Exception('failed to start container') cfg = os.path.join(PATH_LXC_LIB, name, 'config') cmd = ['lxc-start', '-F', '-P', PATH_LXC_LIB, '-p', _get_path_run(name), '-n', name, '-f', cfg, '--share-ipc', str(pid), '-o', _get_log_path(name), '-c', '/dev/null'] if SHOW_CMD: _log('%s' % ' '.join(cmd)) popen(cmd) os.system('lxc-wait -n %s -s "RUNNING"' % name) os.system('lxc-device add -n %s /dev/ckpt' % name)
def tmpdir(cmd, files, filename, code): filename = os.path.split(filename)[1] d = tempfile.mkdtemp() for f in files: try: os.makedirs(os.path.split(f)[0]) except: pass target = os.path.join(d, f) if os.path.split(target)[1] == filename: # source file hasn't been saved since change, so update it from our live buffer f = open(target, 'wb') f.write(code) f.close() else: shutil.copyfile(f, target) os.chdir(d) out = popen(cmd) if out: out = out.communicate() out = (out[0] or '') + '\n' + (out[1] or '') # filter results from build to just this filename # no guarantee all languages are as nice about this as Go # may need to improve later or just defer to communicate() out = '\n'.join([ line for line in out.split('\n') if filename in line.split(':', 1)[0] ]) else: out = '' shutil.rmtree(d, True) return out
def _start(name): _log('start, name=%s' % name) path = _get_path_run(name) kill_by_file(KLNK_NAME, path) binding = get_binding(name) umount(binding) mkdirs(binding) mountpoint = get_mountpoint(name) umount(mountpoint) mkdirs(mountpoint) cmd = [_get_path_cmd(), name2addr(name), mountpoint] # _log(' '.join(cmd)) pid = popen(cmd) if not chkpid(pid): raise Exception("failed to start ipc") path = _get_path_run(name) save_pid(path, pid) path = os.path.join(mountpoint, 'test') if not _touch(path): raise Exception('failed to start ipc') bind(mountpoint, binding) path = os.path.join(binding, 'test') if not _touch(path): raise Exception('failed to start ipc')
def _start(name): _log('start, name=%s' % name) path = _get_path_run(name) kill_by_file(KLNK_NAME, path) binding = get_binding(name) umount(binding) mkdirs(binding) mountpoint = get_mountpoint(name) umount(mountpoint) mkdirs(mountpoint) cmd = [_get_path_cmd(), name2addr(name), mountpoint] # _log(' '.join(cmd)) pid = popen(cmd) if not chkpid(pid): raise Exception("failed to start ipc") path = _get_path_run(name) save_pid(path, pid) path = os.path.join(mountpoint, 'test') if not _touch(path): raise Exception('failed to start ipc') bind(mountpoint, binding) path = os.path.join(binding, 'test') if not _touch(path): raise Exception('failed to start ipc')
def _start_container(name): pid = ipc.get_pid(name) if not pid: log('failed to start container') raise Exception('failed to start container') cfg = os.path.join(PATH_LXC_LIB, name, 'config') cmd = [ 'lxc-start', '-F', '-P', PATH_LXC_LIB, '-p', _get_path_run(name), '-n', name, '-f', cfg, '--share-ipc', str(pid), '-o', _get_log_path(name), '-c', '/dev/null' ] if SHOW_CMD: _log('%s' % ' '.join(cmd)) popen(cmd) os.system('lxc-wait -n %s -s "RUNNING"' % name) os.system('lxc-device add -n %s /dev/ckpt' % name)
def start(name, key): bridge = _get_bridge(key) path = _get_path_run(name) kill_by_file(EDGE_NAME, path) cmd = [EDGE_NAME, '-r', '-d', name, '-a', '0.0.0.0', '-s', NETMASK, '-c', name, '-k', key, '-l', bridge] pid = popen(cmd) if not chkpid(pid): log('failed to start edge node') raise Exception('failed to start edge node') save_pid(path, pid) cmd = ['dhclient', '-q', name] popen(cmd) ret = chkaddr(name) if not ret: log('failed to start edge node, invalid address') raise Exception('failed to start edge node, invalid address') return ret
def initialize(self): self._clear() args = [self._path, '-s', str(OUTPUT_MAX), '-p', str(BRIDGE_PORT), '-l', str(ADAPTER_PORT), '-e', str(CHANNEL_EVENT_PORT)] self._log('initialize, cmd=%s' % ' '.join(args)) pid = popen(*args) if not chkpid(pid): raise Exception(log_get(self, 'failed to initialize')) return pid
def tmpfile(cmd, code, suffix=''): if isinstance(cmd, basestring): cmd = cmd, f = tempfile.NamedTemporaryFile(suffix=suffix) f.write(code) f.flush() cmd = tuple(cmd) + (f.name,) out = popen(cmd) if out: out = out.communicate() return (out[0] or '') + (out[1] or '') else: return ''
def _start_lbfs(name, create=False): mnt = _get_lbfs_mnt(name) path = _get_path_run(name) home = _get_path_home(name) _log('start lbfs, name=%s, home=%s, mnt=%s' % (name, home, mnt)) kill_by_file(LBFS_NAME, path) umount(mnt) mkdirs(mnt) if create: mkdirs(home) cmd = [_get_path_cmd(), name, home, mnt] # _log(' '.join(cmd)) pid = popen(cmd) if not chkpid(pid): log('failed to start lbfs (name=%s)' % name) raise Exception('failed to start lbfs') save_pid(path, pid)
def _start(name, create=False): path = _get_path_run(name) kill_by_file(VMAP_NAME, path) vmap = os.path.join(PATH_INIT, VMAP_NAME) home = _get_path_home(name) if create: mkdirs(home) mountpoint = get_mountpoint(name) _log('start, home=%s, mnt=%s' % (home, mountpoint)) umount(mountpoint) mkdirs(mountpoint) cmd = [vmap, home, mountpoint] pid = popen(cmd) if not chkpid(pid): log('failed to start ckpt') raise Exception('failed to start ckpt') path = _get_path_run(name) save_pid(path, pid)
def _start(name, create=False): path = _get_path_run(name) kill_by_file(VMAP_NAME, path) vmap = os.path.join(PATH_INIT, VMAP_NAME) home = _get_path_home(name) if create: mkdirs(home) mountpoint = get_mountpoint(name) _log('start, home=%s, mnt=%s' % (home, mountpoint)) umount(mountpoint) mkdirs(mountpoint) cmd = [vmap, home, mountpoint] pid = popen(cmd) if not chkpid(pid): log('failed to start ckpt') raise Exception('failed to start ckpt') path = _get_path_run(name) save_pid(path, pid)
def create(self, addr, key, bridge): self._clear() args = [self._path, '-a', addr, '-k', key, '-b', bridge, '-s', str(OUTPUT_MAX), '-p', str(BRIDGE_PORT), '-w', str(TIMEOUT_PUT), '-t', str(TIMEOUT_SEND), '-l', str(ADAPTER_PORT), '-c', str(CONDUCTOR_PORT), '-e', str(CHANNEL_EVENT_PORT)] self._log('create, cmd=%s' % ' '.join(args)) pid = popen(*args) if not chkpid(pid): raise Exception(log_get(self, 'failed to create')) return pid
def communicate(cmd, stdin=None, timeout=None, **popen_args): p = popen(cmd, **popen_args) if isinstance(p, subprocess.Popen): timer = None if timeout is not None: kill = lambda: p.kill() timer = Timer(timeout, kill) timer.start() out = p.communicate(stdin) if timer is not None: timer.cancel() return (out[0] or '') + (out[1] or '') elif isinstance(p, basestring): return p else: return ''
def spawn(view, edit, indent, cmd, sel): q = Queue.Queue() def fold(region): regions = view.get_regions(region) for region in regions: lines = view.split_by_newlines(region) if len(lines) > 24: lines = lines[1:-24] try: area = lines.pop() except IndexError: return for sub in lines: area = area.cover(sub) view.unfold(area) view.fold(area) def merge(region): if q.empty(): return regions = view.get_regions(region) if not regions: return pos = view.line(regions[0].end() - 1) restore_sel = [] for sel in view.sel(): if pos.end() in (sel.a, sel.b): restore_sel.append(sel) view.sel().subtract(sel) edit = view.begin_edit() try: start = time.time() lines = [] while time.time() - start < 0.05 and len(lines) < 200: try: lines.append(q.get(False)) q.task_done() except Queue.Empty: break if not lines: return insert(view, edit, pos, '\n'.join(lines), indent + INDENTATION) fold(region) except: print traceback.format_exc() finally: for sel in restore_sel: view.sel().add(sel) view.end_edit(edit) def poll(p, region, fd): while p.poll() is None: line = fd.readline().decode('utf-8') line = backspace_re.sub('', line) if line: q.put(line.rstrip('\r\n')) # if the process wasn't terminated if p.returncode >= 0: out = fd.read() if out: q.put(out.rstrip('\r\n')) sublime.set_timeout(make_callback(merge, region), 100) def out(p, region): last = 0 while p.poll() is None: since = time.time() - last if since > 0.05 or since > 0.01 and q.qsize() < 10: last = time.time() sublime.set_timeout(make_callback(merge, region), 10) else: time.sleep(max(0.1 - since, 0.1)) if p.returncode not in (-9, -15): del commands[region] while not q.empty(): sublime.set_timeout(make_callback(merge, region), 10) time.sleep(0.05) sublime.set_timeout(make_callback(view.erase_regions, region), 150) def stderr(p, region): poll(p, region, p.stderr) def stdout(p, region): poll(p, region, p.stdout) p = popen(cmd, return_error=True) if isinstance(p, subprocess.Popen): region = 'xiki sub %i' % p.pid line = view.full_line(sel.b) spread = sublime.Region(line.a, line.b) commands[region] = p view.add_regions(region, [spread], 'keyword', '', sublime.DRAW_OUTLINED) thread.start_new_thread(stdout, (p, region)) thread.start_new_thread(stderr, (p, region)) thread.start_new_thread(out, (p, region)) else: insert(view, edit, sel, 'Error: ' + p, indent + INDENTATION)
def extract_path(cmd, delim=':'): path = popen(cmd, os.environ).communicate()[0] path = path.split('__SUBL__', 1)[1].strip('\r\n') return ':'.join(path.split(delim))
def spawn(view, edit, indent, cmd, sel): local_commands = commands[view.id()] q = Queue.Queue() def fold(region): regions = view.get_regions(region) for region in regions: lines = view.split_by_newlines(region) if len(lines) > 24: lines = lines[1:-24] try: area = lines.pop() except IndexError: return for sub in lines: area = area.cover(sub) view.unfold(area) view.fold(area) def merge(region): if q.empty(): return regions = view.get_regions(region) if not regions: return pos = view.line(regions[0].end() - 1) restore_sel = [] for sel in view.sel(): if pos.end() in (sel.a, sel.b): restore_sel.append(sel) view.sel().subtract(sel) edit = view.begin_edit() try: start = time.time() lines = [] while time.time() - start < 0.05 and len(lines) < 200: try: lines.append(q.get(False)) q.task_done() except Queue.Empty: break if not lines: return lines = backspace_re.sub('', '\n'.join(lines)) insert(view, edit, pos, lines, indent + INDENTATION) fold(region) except: print traceback.format_exc() finally: for sel in restore_sel: view.sel().add(sel) view.end_edit(edit) def poll(p, region, fd): while p.poll() is None: line = fd.readline().decode('utf-8') if line: q.put(line.rstrip('\r\n')) # if the process wasn't terminated if p.returncode >= 0: out = fd.read() if out: q.put(out.rstrip('\r\n')) sublime.set_timeout(make_callback(merge, region), 100) def out(p, region): last = 0 while p.poll() is None: since = time.time() - last if since > 0.05 or since > 0.01 and q.qsize() < 10: last = time.time() sublime.set_timeout(make_callback(merge, region), 10) else: time.sleep(max(0.1 - since, 0.1)) if p.returncode not in (-9, -15): del local_commands[region] while not q.empty(): sublime.set_timeout(make_callback(merge, region), 10) time.sleep(0.05) sublime.set_timeout(make_callback(view.erase_regions, region), 150) def stderr(p, region): poll(p, region, p.stderr) def stdout(p, region): poll(p, region, p.stdout) p = popen(cmd, return_error=True) if isinstance(p, subprocess.Popen): region = 'xiki sub %i' % p.pid line = view.full_line(sel.b) spread = sublime.Region(line.a, line.b) local_commands[region] = p view.add_regions(region, [spread], 'keyword', '', sublime.DRAW_OUTLINED) thread.start_new_thread(stdout, (p, region)) thread.start_new_thread(stderr, (p, region)) thread.start_new_thread(out, (p, region)) else: insert(view, edit, sel, 'Error: ' + p, indent + INDENTATION)