-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
241 lines (172 loc) · 5.8 KB
/
index.py
File metadata and controls
241 lines (172 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python3
"""
Copyright 2020 Mike Pawlowski
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# Pylint Rule Overrides
# Modules
import sys
import os
import logging
import argparse
import github
from lib.constants import constants
from lib.classes.query_processor import QueryProcessor
from lib.utils import logger_util
# Authorship
# pylint: disable=unused-variable
__author__ = "Mike Pawlowski"
__copyright__ = "Copyright 2020 Mike Pawlowski."
__license__ = "Apache 2.0"
__version__ = "1.0.1"
__maintainer__ = "Mike Pawlowski"
__email__ = "TODO"
__status__ = "Production"
# pylint: enable=unused-variable
# Globals
MIN_VERSION_PYTHON = (3, 7)
ARGUMENT_PARSER_DESCRIPTION = \
"A Python application that searches for code in GitHub repositories based on a user-defined queries."
ARGUMENT_PARSER_EPILOG = \
"=== Documentation ===\n" \
"\n" \
"https://docs.github.com/en/github/searching-for-information-on-github/searching-code\n" \
"\n" \
"=== Environment Variables ===\n" \
"\n" \
"GITHUB_ACCESS_TOKEN : A personal access token with the \"repo\" scope selected.\n" \
"\n" \
"=== Examples ===\n" \
"\n" \
"export GITHUB_ACCESS_TOKEN=cabfe35410755fbb6c281e92902ed122144886c5\n" \
"\n" \
"python index.py -q \"org:dap path:/ filename:package.json request\"\n" \
"python index.py -q \"org:dap filename:deploy.properties k8s_yp_prod_eu_de_STAGE\"\n" \
"python index.py -q \"org:dap org:data-platform path:/ filename:package.json lodash\"\n" \
"\n" \
"\n"
DEFAULT_LOGGER = logging.getLogger("index")
# Functions ------------------------------------------------------------------->
def _validate_python_version(logger=DEFAULT_LOGGER):
"""
Validates whether the minimum Python interpreter version is satisfied
"""
if sys.version_info < MIN_VERSION_PYTHON:
logger.error("Python version %s.%s or later is required.", MIN_VERSION_PYTHON[0], MIN_VERSION_PYTHON[1])
return False
logger_util.log_trace(
logger,
"Detected Python version: %s.%s.%s",
sys.version_info.major,
sys.version_info.minor,
sys.version_info.micro)
return True
def _validate_env_vars(logger=DEFAULT_LOGGER):
"""
Validate required environment variables
"""
if not constants.ENV_VAR_GITHUB_ACCESS_TOKEN in os.environ:
logger.error("Environment variable not defined: %s.", constants.ENV_VAR_GITHUB_ACCESS_TOKEN)
return False
return True
def _get_parsed_args():
"""
Parse command-line arguments
"""
parser = argparse.ArgumentParser(
description=ARGUMENT_PARSER_DESCRIPTION,
epilog=ARGUMENT_PARSER_EPILOG,
formatter_class=argparse.RawTextHelpFormatter)
# Required
parser.add_argument(
"-q",
"--query",
required=True,
help="A user-defined GitHub query.")
# Optional
parser.add_argument(
"-o",
"--hostname",
choices=[
constants.GITHUB_ENTERPRISE_IBM_API_DOMAIN,
constants.GITHUB_PUBLIC_API_DOMAIN
],
default=constants.GITHUB_ENTERPRISE_IBM_API_DOMAIN,
help="The target GitHub API domain. "
"Default: {0}.".format(constants.GITHUB_ENTERPRISE_IBM_API_DOMAIN))
parser.add_argument(
"-d",
"--debug",
action="store_true",
help="Enable debug mode. "
"Default: False.")
args = parser.parse_args()
return args
def _display_parsed_args(args, logger=DEFAULT_LOGGER):
"""
Display command-line argument values
"""
separator = "\n"
string_buffer = (
"Parsed Command-line Arguments:",
"- GitHub Query: \"{0}\".".format(args.query),
"- GitHub API Domain: {0}.".format(args.hostname),
"- Debug Mode: {0}.".format(args.debug)
)
content = separator.join(string_buffer)
logger.info(content)
def _fatal_exit(logger=DEFAULT_LOGGER):
"""
Exit script with fatal status
"""
status = 1
logger.critical("Fatal error encountered. Exit script status: %d.", status)
sys.exit(status)
def _main(logger=DEFAULT_LOGGER):
"""
The main function.
"""
status = False
# Logging
status = logger_util.init_logging_subsystem(logger)
if status is False:
# Should never happen
print("Failed to initialize the logging subsystem.")
sys.exit(1)
# Command-line arguments
args = _get_parsed_args()
# Script banner
logger.info("[-- GitHub Search -------------------------------------------------------------".upper())
# Python version
status = _validate_python_version()
if status is False:
_fatal_exit()
# Environment variables
status = _validate_env_vars()
if status is False:
_fatal_exit()
# Display command-line argument values
_display_parsed_args(args)
# Enable debug mode logging
if args.debug:
github.enable_console_debug_logging()
# Process the GitHub query
query_processor = QueryProcessor(
hostname=args.hostname,
query=args.query)
status = query_processor.run()
if status is False:
_fatal_exit()
# Exit process
sys.exit(0)
# Main ------------------------------------------------------------------------>
if __name__ == "__main__":
_main()