• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

codesearch/H28-May-2019-90,56289,486

doc/H28-May-2019-181131

.gitignoreH A D28-May-201977 65

.style.yapfH A D28-May-201948 43

.travis.ymlH A D28-May-2019410 1914

CONTRIBUTING.mdH A D28-May-20192.3 KiB5941

LICENSEH A D28-May-20191.5 KiB2827

README.mdH A D28-May-20195.7 KiB147116

__init__.pyH A D28-May-2019232 81

ccsH A D28-May-2019231 92

format_code.shH A D28-May-2019697 237

refresh_cached_responses.shH A D28-May-20191.9 KiB7251

run_tests.shH A D28-May-2019907 4422

setup.cfgH A D28-May-201944 43

setup.pyH A D28-May-2019537 199

README.md

1Chromium CodeSearch Library
2===========================
3
4The `codesearch` Python library provides an interface for talking to the
5Chromium CodeSearch backend at https://cs.chromium.org/
6
7The primary entry point into the library is the `CodeSearch` class. Various
8message classes you are likely to encounter are defined in `messages.py`.
9
10A quick example:
11
12``` python
13'''
14
15Step 1 is to import codesearch.
16>>> import codesearch
17
18The following examples are written out as Python doctests and are run as a part
19of the run_tests.sh suite. The InstallTestRequestHandler() sets up a test rig
20that is used during testing to ensure that tests are reproducible and can be run
21without a network connection. Feel free to use InstallTestRequestHandler() in
22your own code if you need to test against the CodeSearch library. See
23documentation for details.
24>>> codesearch.InstallTestRequestHandler()
25
26The plugin can optionally work with a local Chromium checkout (see the
27documentation for CodeSearch.__init__()), but for this example, we are going use
28the API without a local checkout. This is indicated by setting the |source_root|
29to '.'.
30>>> cs = codesearch.CodeSearch(source_root='.')
31
32Let's look up a class:
33The SearchForSymbol function searches for a symbol of a specific type. In this
34case we are looking for a class named File. There may be more than one such
35class, so the function returns an array. We only need the first one.
36>>> file_class = cs.SearchForSymbol('FieldTrial$', codesearch.NodeEnumKind.CLASS)[0]
37
38SearchForSymbol returns an XrefNode object. This is a starting point for cross
39reference lookups.
40>>> isinstance(file_class, codesearch.XrefNode)
41True
42
43We can look for references to this class.
44>>> references = file_class.Traverse(codesearch.KytheXrefKind.REFERENCE)
45
46There will be a number of these.
47>>> len(references) > 30
48True
49
50In addition to the above, there are lower level APIs to talk to the unofficial
51endpoints in the https://cs.chromium.org backend. One such API is
52SendRequestToServer.
53
54SendRequestToServer takes a CompoundRequest object ...
55>>> response = cs.SendRequestToServer(codesearch.CompoundRequest(
56...     search_request=[
57...     codesearch.SearchRequest(query='hello world',
58...                              return_line_matches=True,
59...                              lines_context=0,
60...                              max_num_results=10)
61...     ]))
62
63.. and returns a CompoundResponse
64>>> isinstance(response, codesearch.CompoundResponse)
65True
66
67Both CompoundRequest and CompoundResponse are explained in
68codesearch/messages.py. Since our request was a |search_request| which is a list
69of SearchRequest objects, our CompoundResponse object is going to have a
70|search_response| field ...
71>>> hasattr(response, 'search_response')
72True
73
74.. containing a list of SearchResponse objects ...
75>>> isinstance(response.search_response, list)
76True
77
78.. which contains exactly one element ...
79>>> len(response.search_response)
801
81
82.. whose type is SearchResponse.
83>>> isinstance(response.search_response[0], codesearch.SearchResponse)
84True
85
86It should indicate an estimate of how many results exist. This could diverge
87from the number of results included in this response message. If that's the
88case, then |hit_max_results| would be true.
89>>> response.search_response[0].hit_max_results
90True
91
92We can now examine the SearchResult objects to see what the server sent us. The
93fields are explained in message.py.
94>>> lines_left = 10
95>>> for search_result in response.search_response[0].search_result:
96...     assert isinstance(search_result, codesearch.SearchResult)
97...
98...     for match in search_result.match:
99...         print("{}:{}: {}".format(search_result.top_file.file.name,
100...                                  match.line_number,
101...                                  match.line_text.strip()))
102...         lines_left -= 1
103...         if lines_left == 0: break
104...     if lines_left == 0: break
105src/v8/samples/hello-world.cc:40: v8::String::NewFromUtf8(isolate, "'Hello' + ', World!'",
106src/v8/samples/hello-world.cc:34: // Enter the context for compiling and running the hello world script.
107src/gin/shell/hello_world.js:5: log("Hello World");
108src/v8/test/fuzzer/parser/hello-world:1: console.log('hello world');
109src/native_client/tests/hello_world/hello_world.c:13: void hello_world(void) {
110src/native_client/tests/hello_world/hello_world.c:14: printf("Hello, World!\n");
111src/native_client/tests/hello_world/hello_world.c:18: hello_world();
112src/tools/gn/tutorial/hello_world.cc:8: printf("Hello, world.\n");
113infra/go/src/go.chromium.org/luci/grpc/prpc/e2etest/helloworld_test.proto:19: service Hello {
114infra/go/src/go.chromium.org/luci/grpc/prpc/e2etest/helloworld_test.proto:15: message HelloReply {
115
116Note that in the example above:
117*  search_result.match was available because |return_line_matches| was set to
118   True when making the server request. Otherwise the server response will not
119   contain line matches.
120*  The line matches will have multiple lines of context. This was suppressed in
121   the example by setting |lines_context| to 0.
122'''
123```
124
125In addition, the library also includes facilities for maintaining an ephemeral
126or persistent cache in order to minimize generated network traffic.
127
128**Note**: The library uses an unsupported interface to talk to the backend. If
129you are using the library and it suddenly stops working, file an issue and/or
130monitor the project on GitHub for updates.
131
132**Note**: This is not an official Google product.
133
134Support
135-------
136
137Feel free to submit issues on GitHub and/or contact the authors. This project is
138not officially supported by Google or the Chromium project.
139
140Contributing
141------------
142
143See [CONTRIBUTING](./CONTRIBUTING.md) for details on contributing.
144
145[![Build Status](https://travis-ci.org/chromium/codesearch-py.svg?branch=master)](https://travis-ci.org/chromium/codesearch-py)
146
147