def test_pdu_len(self): enc = bser.dumps(1) self.assertEquals(len(enc), bser.pdu_len(enc)) # try a bigger one; prove that we get the correct length # even though we receive just a portion of the complete # data enc = bser.dumps([1, 2, 3, "hello there, much larger"]) self.assertEquals(len(enc), bser.pdu_len(enc[0:7]))
def test_bserInput(self): sockname = self.getSockPath() watchman_cmd = bser.dumps(['get-sockname']) cli_cmd = [ 'watchman', '--sockname={0}'.format(sockname), '--logfile=/BOGUS', '--statefile=/BOGUS', '--no-spawn', '--no-local', '-j', ] proc = subprocess.Popen(cli_cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE) stdout, stderr = proc.communicate(input=watchman_cmd) self.assertEqual(proc.poll(), 0, stderr) # the response should be bser to match our input result = bser.loads(stdout) result_sockname = result['sockname'] if compat.PYTHON3: result_sockname = encoding.decode_local(result_sockname) self.assertEqual(result_sockname, sockname, binascii.hexlify(stdout).decode('ascii'))
def test_bserInput(self): sockname = self.getSockPath() watchman_cmd = bser.dumps(["get-sockname"]) cli_cmd = [ "watchman", "--sockname={0}".format(sockname), "--logfile=/BOGUS", "--statefile=/BOGUS", "--no-spawn", "--no-local", "-j", ] proc = subprocess.Popen( cli_cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE, ) stdout, stderr = proc.communicate(input=watchman_cmd) self.assertEqual(proc.poll(), 0, stderr) # the response should be bser to match our input result = bser.loads(stdout) result_sockname = result["sockname"] if compat.PYTHON3: result_sockname = encoding.decode_local(result_sockname) self.assertEqual(result_sockname, sockname, binascii.hexlify(stdout).decode("ascii"))
def test_bserInput(self): sockname = self.getSockPath() watchman_cmd = bser.dumps(['get-sockname']) cli_cmd = [ 'watchman', '--sockname={}'.format(sockname), '--logfile=/BOGUS', '--statefile=/BOGUS', '--no-spawn', '--no-local', '-j', ] proc = subprocess.Popen(cli_cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE) stdout, stderr = proc.communicate(input=watchman_cmd) self.assertEqual(proc.poll(), 0, stderr) # the response should be bser to match our input result = bser.loads(stdout) if PY2: self.assertEqual(result[b'sockname'], sockname, stdout.encode('hex')) if PY3: self.assertEqual(result[b'sockname'], sockname, binascii.hexlify(stdout))
def test_bserInput(self): sockpath = self.getSockPath() watchman_cmd = bser.dumps(["get-sockname"]) cli_cmd = [ os.environ.get("WATCHMAN_BINARY", "watchman"), "--unix-listener-path={0}".format(sockpath.unix_domain), "--named-pipe-path={0}".format(sockpath.named_pipe), "--logfile=/BOGUS", "--statefile=/BOGUS", "--no-spawn", "--no-local", "-j", ] proc = subprocess.Popen( cli_cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE, ) stdout, stderr = proc.communicate(input=watchman_cmd) self.assertEqual(proc.poll(), 0, stderr) # the response should be bser to match our input result = bser.loads(stdout) result_sockname = result["unix_domain"] if compat.PYTHON3: result_sockname = encoding.decode_local(result_sockname) self.assertEqual( result_sockname, sockpath.unix_domain, binascii.hexlify(stdout).decode("ascii"), )
def munged(self, val, munged): enc = bser.dumps(val) if isinstance(val, unicode): print "# %s --> %s" % (val.encode('utf-8'), enc.encode('hex')) else: print "# %s --> %s" % (val, enc.encode('hex')) dec = bser.loads(enc) self.assertEquals(munged, dec)
def encode_result(values, diagnostics): result = {"values": values} if diagnostics: encoded_diagnostics = [] for d in diagnostics: encoded_diagnostics.append({"message": d.message, "level": d.level}) result["diagnostics"] = encoded_diagnostics return bser.dumps(result)
def test_bserInput(self): sockname = self.getSockPath() watchman_cmd = bser.dumps(["get-sockname"]) cli_cmd = ["watchman", "--sockname={}".format(sockname), "--no-spawn", "--no-local", "-j"] proc = subprocess.Popen(cli_cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE) stdout, stderr = proc.communicate(input=watchman_cmd) self.assertEqual(proc.poll(), 0, stderr) # the response should be bser to match our input result = bser.loads(stdout) self.assertEqual(result["sockname"], sockname, stdout.encode("hex"))
def encode_result(values, diagnostics): result = {'values': values} if diagnostics: encoded_diagnostics = [] for d in diagnostics: encoded_diagnostics.append({ 'message': d.message, 'level': d.level, }) result['diagnostics'] = encoded_diagnostics return bser.dumps(result)
def encode_result(values, diagnostics, profile): result = {'values': values} if diagnostics: encoded_diagnostics = [] for d in diagnostics: encoded_diagnostics.append({ 'message': d.message, 'level': d.level, }) result['diagnostics'] = encoded_diagnostics if profile is not None: result['profile'] = profile return bser.dumps(result)
def test_bserInput(self): sockname = self.getSockPath() watchman_cmd = bser.dumps(['get-sockname']) cli_cmd = [ 'watchman', '--sockname={}'.format(sockname), '--no-spawn', '--no-local', '-j', ] proc = subprocess.Popen(cli_cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE) stdout, stderr = proc.communicate(input=watchman_cmd) self.assertEqual(proc.poll(), 0, stderr) # the response should be bser to match our input result = bser.loads(stdout) self.assertEqual(result['sockname'], sockname, stdout.encode('hex'))
def roundtrip(self, val): enc = bser.dumps(val) print "# %s --> %s" % (val, enc.encode('hex')) dec = bser.loads(enc) self.assertEquals(val, dec)
def main(): # Our parent expects to read JSON from our stdout, so if anyone # uses print, buck will complain with a helpful "but I wanted an # array!" message and quit. Redirect stdout to stderr so that # doesn't happen. Actually dup2 the file handle so that writing # to file descriptor 1, os.system, and so on work as expected too. to_parent = os.fdopen(os.dup(sys.stdout.fileno()), 'a') os.dup2(sys.stderr.fileno(), sys.stdout.fileno()) parser = optparse.OptionParser() parser.add_option('--project_root', action='store', type='string', dest='project_root') parser.add_option('--build_file_name', action='store', type='string', dest="build_file_name") parser.add_option( '--allow_empty_globs', action='store_true', dest='allow_empty_globs', help= 'Tells the parser not to raise an error when glob returns no results.') parser.add_option( '--use_watchman_glob', action='store_true', dest='use_watchman_glob', help= 'Invokes `watchman query` to get lists of files instead of globbing in-process.' ) parser.add_option( '--watchman_watch_root', action='store', type='string', dest='watchman_watch_root', help= 'Path to root of watchman watch as returned by `watchman watch-project`.' ) parser.add_option( '--watchman_project_prefix', action='store', type='string', dest='watchman_project_prefix', help='Relative project prefix as returned by `watchman watch-project`.' ) parser.add_option( '--watchman_query_timeout_ms', action='store', type='int', dest='watchman_query_timeout_ms', help= 'Maximum time in milliseconds to wait for watchman query to respond.') parser.add_option('--include', action='append', dest='include') (options, args) = parser.parse_args() # Even though project_root is absolute path, it may not be concise. For # example, it might be like "C:\project\.\rule". # # Under cygwin, the project root will be invoked from buck as C:\path, but # the cygwin python uses UNIX-style paths. They can be converted using # cygpath, which is necessary because abspath will treat C:\path as a # relative path. options.project_root = cygwin_adjusted_path(options.project_root) project_root = os.path.abspath(options.project_root) watchman_client = None watchman_error = None output_format = 'JSON' output_encode = lambda val: json.dumps(val, sort_keys=True) if options.use_watchman_glob: import pywatchman client_args = {} if options.watchman_query_timeout_ms is not None: # pywatchman expects a timeout as a nonnegative floating-point # value in seconds. client_args['timeout'] = max( 0.0, options.watchman_query_timeout_ms / 1000.0) watchman_client = pywatchman.client(**client_args) watchman_error = pywatchman.WatchmanError try: import pywatchman.bser as bser except ImportError, e: import pywatchman.pybser as bser output_format = 'BSER' output_encode = lambda val: bser.dumps(val)
async def send(self, *args): cmd = bser.dumps(*args, version=2, capabilities=0) await self.transport.write(cmd)
def main(): # Our parent expects to read JSON from our stdout, so if anyone # uses print, buck will complain with a helpful "but I wanted an # array!" message and quit. Redirect stdout to stderr so that # doesn't happen. Actually dup2 the file handle so that writing # to file descriptor 1, os.system, and so on work as expected too. to_parent = os.fdopen(os.dup(sys.stdout.fileno()), 'a') os.dup2(sys.stderr.fileno(), sys.stdout.fileno()) parser = optparse.OptionParser() parser.add_option( '--project_root', action='store', type='string', dest='project_root') parser.add_option( '--build_file_name', action='store', type='string', dest="build_file_name") parser.add_option( '--allow_empty_globs', action='store_true', dest='allow_empty_globs', help='Tells the parser not to raise an error when glob returns no results.') parser.add_option( '--use_watchman_glob', action='store_true', dest='use_watchman_glob', help='Invokes `watchman query` to get lists of files instead of globbing in-process.') parser.add_option( '--watchman_watch_root', action='store', type='string', dest='watchman_watch_root', help='Path to root of watchman watch as returned by `watchman watch-project`.') parser.add_option( '--watchman_project_prefix', action='store', type='string', dest='watchman_project_prefix', help='Relative project prefix as returned by `watchman watch-project`.') parser.add_option( '--watchman_query_timeout_ms', action='store', type='int', dest='watchman_query_timeout_ms', help='Maximum time in milliseconds to wait for watchman query to respond.') parser.add_option( '--include', action='append', dest='include') (options, args) = parser.parse_args() # Even though project_root is absolute path, it may not be concise. For # example, it might be like "C:\project\.\rule". # # Under cygwin, the project root will be invoked from buck as C:\path, but # the cygwin python uses UNIX-style paths. They can be converted using # cygpath, which is necessary because abspath will treat C:\path as a # relative path. options.project_root = cygwin_adjusted_path(options.project_root) project_root = os.path.abspath(options.project_root) watchman_client = None watchman_error = None output_format = 'JSON' output_encode = lambda val: json.dumps(val, sort_keys=True) if options.use_watchman_glob: import pywatchman client_args = {} if options.watchman_query_timeout_ms is not None: # pywatchman expects a timeout as a nonnegative floating-point # value in seconds. client_args['timeout'] = max(0.0, options.watchman_query_timeout_ms / 1000.0) watchman_client = pywatchman.client(**client_args) watchman_error = pywatchman.WatchmanError try: import pywatchman.bser as bser except ImportError, e: import pywatchman.pybser as bser output_format = 'BSER' output_encode = lambda val: bser.dumps(val)
def main(): # Our parent expects to read BSER from our stdout, so if anyone # uses print, buck will complain with a helpful "but I wanted an # array!" message and quit. Redirect stdout to stderr so that # doesn't happen. Actually dup2 the file handle so that writing # to file descriptor 1, os.system, and so on work as expected too. to_parent = os.fdopen(os.dup(sys.stdout.fileno()), "a") os.dup2(sys.stderr.fileno(), sys.stdout.fileno()) parser = optparse.OptionParser() parser.add_option("--project_root", action="store", type="string", dest="project_root") parser.add_option("--build_file_name", action="store", type="string", dest="build_file_name") parser.add_option( "--allow_empty_globs", action="store_true", dest="allow_empty_globs", help="Tells the parser not to raise an error when glob returns no results.", ) parser.add_option( "--use_watchman_glob", action="store_true", dest="use_watchman_glob", help="Invokes `watchman query` to get lists of files instead of globbing in-process.", ) parser.add_option( "--watchman_watch_root", action="store", type="string", dest="watchman_watch_root", help="Path to root of watchman watch as returned by `watchman watch-project`.", ) parser.add_option( "--watchman_project_prefix", action="store", type="string", dest="watchman_project_prefix", help="Relative project prefix as returned by `watchman watch-project`.", ) parser.add_option( "--watchman_query_timeout_ms", action="store", type="int", dest="watchman_query_timeout_ms", help="Maximum time in milliseconds to wait for watchman query to respond.", ) parser.add_option("--include", action="append", dest="include") (options, args) = parser.parse_args() # Even though project_root is absolute path, it may not be concise. For # example, it might be like "C:\project\.\rule". # # Under cygwin, the project root will be invoked from buck as C:\path, but # the cygwin python uses UNIX-style paths. They can be converted using # cygpath, which is necessary because abspath will treat C:\path as a # relative path. options.project_root = cygwin_adjusted_path(options.project_root) project_root = os.path.abspath(options.project_root) watchman_client = None watchman_error = None if options.use_watchman_glob: import pywatchman client_args = {} if options.watchman_query_timeout_ms is not None: # pywatchman expects a timeout as a nonnegative floating-point # value in seconds. client_args["timeout"] = max(0.0, options.watchman_query_timeout_ms / 1000.0) watchman_client = pywatchman.client(**client_args) watchman_error = pywatchman.WatchmanError buildFileProcessor = BuildFileProcessor( project_root, options.watchman_watch_root, options.watchman_project_prefix, options.build_file_name, options.allow_empty_globs, watchman_client, watchman_error, implicit_includes=options.include or [], ) buildFileProcessor.install_builtins(__builtin__.__dict__) for build_file in args: build_file = cygwin_adjusted_path(build_file) values = buildFileProcessor.process(build_file) to_parent.write(bser.dumps(values)) to_parent.flush() # "for ... in sys.stdin" in Python 2.x hangs until stdin is closed. for build_file in iter(sys.stdin.readline, ""): build_file = cygwin_adjusted_path(build_file) values = buildFileProcessor.process(build_file.rstrip()) to_parent.write(bser.dumps(values)) to_parent.flush() # Python tries to flush/close stdout when it quits, and if there's a dead # pipe on the other end, it will spit some warnings to stderr. This breaks # tests sometimes. Prevent that by explicitly catching the error. try: to_parent.close() except IOError: pass