-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathcommon.py
714 lines (583 loc) · 25.4 KB
/
common.py
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# Copyright 2014 The Oppia Authors. All Rights Reserved.
#
# 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.
"""Common utility functions and classes used by multiple Python scripts."""
from __future__ import absolute_import # pylint: disable=import-only-modules
from __future__ import unicode_literals # pylint: disable=import-only-modules
import contextlib
import getpass
import os
import platform
import re
import shutil
import socket
import subprocess
import sys
import time
import constants
import feconf
import python_utils
AFFIRMATIVE_CONFIRMATIONS = ['y', 'ye', 'yes']
CURRENT_PYTHON_BIN = sys.executable
# Versions of libraries used in devflow.
COVERAGE_VERSION = '5.3'
ESPRIMA_VERSION = '4.0.1'
ISORT_VERSION = '4.3.21'
PYCODESTYLE_VERSION = '2.6.0'
PSUTIL_VERSION = '5.7.3'
PYLINT_VERSION = '1.9.5'
PYLINT_QUOTES_VERSION = '0.1.8'
PYGITHUB_VERSION = '1.45'
WEBTEST_VERSION = '2.0.35'
PIP_TOOLS_VERSION = '5.4.0'
GRPCIO_VERSION = '1.0.0'
ENUM_VERSION = '1.1.10'
PROTOBUF_VERSION = '3.13.0'
SETUPTOOLS_VERSION = '36.6.0'
# Node version.
NODE_VERSION = '14.15.0'
# NB: Please ensure that the version is consistent with the version in .yarnrc.
YARN_VERSION = '1.22.10'
# Versions of libraries used in backend.
PILLOW_VERSION = '6.2.2'
# Buf version.
BUF_VERSION = '0.29.0'
# Protoc is the compiler for protobuf files and the version must be same as
# the version of protobuf library being used.
PROTOC_VERSION = PROTOBUF_VERSION
# We use redis 6.0.5 instead of the latest stable build of redis (6.0.6) because
# there is a `make test` bug in redis 6.0.6 where the solution has not been
# released. This is explained in this issue:
# https://github.com/redis/redis/issues/7540.
# IMPORTANT STEPS FOR DEVELOPERS TO UPGRADE REDIS:
# 1. Download the new version of the redis cli.
# 2. Extract the cli in the folder that it was downloaded, most likely
# Downloads/.
# 3. Change directories into the folder you extracted, titled
# redis-<new version>/ and change into that directory:
# cd redis-<new version>/
# 4. From the top level of the redis-<new version> directory,
# run `make test`.
# 5. All of the tests should pass with an [ok] status with no error codes. The
# final output should be 'All tests pass'.
# 6. Be sure to leave a note in the PR description to confirm that you have read
# this message, and that all of the `make test` tests pass before you commit
# the upgrade to develop.
# 7. If any tests fail, DO NOT upgrade to this newer version of the redis cli.
REDIS_CLI_VERSION = '6.0.5'
RELEASE_BRANCH_NAME_PREFIX = 'release-'
CURR_DIR = os.path.abspath(os.getcwd())
OPPIA_TOOLS_DIR = os.path.join(CURR_DIR, os.pardir, 'oppia_tools')
OPPIA_TOOLS_DIR_ABS_PATH = os.path.abspath(OPPIA_TOOLS_DIR)
THIRD_PARTY_DIR = os.path.join(CURR_DIR, 'third_party')
THIRD_PARTY_PYTHON_LIBS_DIR = os.path.join(THIRD_PARTY_DIR, 'python_libs')
GOOGLE_CLOUD_SDK_HOME = os.path.join(
OPPIA_TOOLS_DIR_ABS_PATH, 'google-cloud-sdk-304.0.0', 'google-cloud-sdk')
GOOGLE_APP_ENGINE_SDK_HOME = os.path.join(
GOOGLE_CLOUD_SDK_HOME, 'platform', 'google_appengine')
GOOGLE_CLOUD_SDK_BIN = os.path.join(GOOGLE_CLOUD_SDK_HOME, 'bin')
GCLOUD_PATH = os.path.join(GOOGLE_CLOUD_SDK_BIN, 'gcloud')
NODE_PATH = os.path.join(OPPIA_TOOLS_DIR, 'node-%s' % NODE_VERSION)
PYLINT_PATH = os.path.join(OPPIA_TOOLS_DIR, 'pylint-%s' % PYLINT_VERSION)
PYCODESTYLE_PATH = os.path.join(
OPPIA_TOOLS_DIR, 'pycodestyle-%s' % PYCODESTYLE_VERSION)
PYLINT_QUOTES_PATH = os.path.join(
OPPIA_TOOLS_DIR, 'pylint-quotes-%s' % PYLINT_QUOTES_VERSION)
NODE_MODULES_PATH = os.path.join(CURR_DIR, 'node_modules')
FRONTEND_DIR = os.path.join(CURR_DIR, 'core', 'templates')
YARN_PATH = os.path.join(OPPIA_TOOLS_DIR, 'yarn-%s' % YARN_VERSION)
OS_NAME = platform.system()
ARCHITECTURE = platform.machine()
PSUTIL_DIR = os.path.join(OPPIA_TOOLS_DIR, 'psutil-%s' % PSUTIL_VERSION)
REDIS_SERVER_PATH = os.path.join(
OPPIA_TOOLS_DIR, 'redis-cli-%s' % REDIS_CLI_VERSION,
'src', 'redis-server')
REDIS_CLI_PATH = os.path.join(
OPPIA_TOOLS_DIR, 'redis-cli-%s' % REDIS_CLI_VERSION,
'src', 'redis-cli')
RELEASE_BRANCH_REGEX = r'release-(\d+\.\d+\.\d+)$'
RELEASE_MAINTENANCE_BRANCH_REGEX = r'release-maintenance-(\d+\.\d+\.\d+)$'
HOTFIX_BRANCH_REGEX = r'release-(\d+\.\d+\.\d+)-hotfix-[1-9]+$'
TEST_BRANCH_REGEX = r'test-[A-Za-z0-9-]*$'
USER_PREFERENCES = {'open_new_tab_in_browser': None}
FECONF_PATH = os.path.join('feconf.py')
CONSTANTS_FILE_PATH = os.path.join('assets', 'constants.ts')
MAX_WAIT_TIME_FOR_PORT_TO_OPEN_SECS = 1000
REDIS_CONF_PATH = os.path.join('redis.conf')
# Path for the dump file the redis server autogenerates. It contains data
# used by the Redis server.
REDIS_DUMP_PATH = os.path.join(CURR_DIR, 'dump.rdb')
# The requirements.txt file is auto-generated and contains a deterministic list
# of all libraries and versions that should exist in the
# 'third_party/python_libs' directory.
# NOTE: Developers should NOT modify this file.
COMPILED_REQUIREMENTS_FILE_PATH = os.path.join(CURR_DIR, 'requirements.txt')
# The precompiled requirements file is the one that developers should be
# modifying. It is the file that we use to recompile the
# "requirements.txt" file so that all installations using "requirements.txt"
# will be identical.
REQUIREMENTS_FILE_PATH = os.path.join(CURR_DIR, 'requirements.in')
def is_windows_os():
"""Check if the running system is Windows."""
return OS_NAME == 'Windows'
def is_mac_os():
"""Check if the running system is MacOS."""
return OS_NAME == 'Darwin'
def is_linux_os():
"""Check if the running system is Linux."""
return OS_NAME == 'Linux'
def is_x64_architecture():
"""Check if the architecture is on X64."""
# https://docs.python.org/2/library/platform.html#platform.architecture
return sys.maxsize > 2**32
NODE_BIN_PATH = os.path.join(
NODE_PATH, '' if is_windows_os() else 'bin', 'node')
# Add path for node which is required by the node_modules.
os.environ['PATH'] = os.pathsep.join([
os.path.dirname(NODE_BIN_PATH), os.path.join(YARN_PATH, 'bin'),
os.environ['PATH']])
def run_cmd(cmd_tokens):
"""Runs the command and returns the output.
Raises subprocess.CalledProcessError upon failure.
Args:
cmd_tokens: list(str). The list of command tokens to execute.
Returns:
str. The output of the command.
"""
return subprocess.check_output(cmd_tokens).strip()
def ensure_directory_exists(d):
"""Creates the given directory if it does not already exist."""
if not os.path.exists(d):
os.makedirs(d)
def require_cwd_to_be_oppia(allow_deploy_dir=False):
"""Ensures that the current working directory ends in 'oppia'.
If allow_deploy_dir is True, this also allows the cwd to be a directory
called 'deploy-*' which is a sibling of the oppia/ directory.
"""
is_oppia_dir = os.getcwd().endswith('oppia')
current_dirname = os.path.basename(os.path.normpath(os.getcwd()))
is_deploy_dir = (
current_dirname.startswith('deploy-') and
os.path.isdir(os.path.join(os.getcwd(), os.pardir, 'oppia')))
if is_oppia_dir or (allow_deploy_dir and is_deploy_dir):
return
raise Exception('Please run this script from the oppia/ directory.')
def open_new_tab_in_browser_if_possible(url):
"""Opens the given URL in a new browser tab, if possible."""
if USER_PREFERENCES['open_new_tab_in_browser'] is None:
python_utils.PRINT(
'\nDo you want the url to be opened in the browser? '
'Confirm by entering y/ye/yes.')
USER_PREFERENCES['open_new_tab_in_browser'] = python_utils.INPUT()
if USER_PREFERENCES['open_new_tab_in_browser'] not in ['y', 'ye', 'yes']:
python_utils.PRINT(
'Please open the following link in browser: %s' % url)
return
browser_cmds = ['chromium-browser', 'google-chrome', 'firefox']
for cmd in browser_cmds:
if subprocess.call(['which', cmd]) == 0:
subprocess.check_call([cmd, url])
return
python_utils.PRINT(
'******************************************************************')
python_utils.PRINT(
'WARNING: Unable to open browser. Please manually open the following')
python_utils.PRINT('URL in a browser window, then press Enter to confirm.')
python_utils.PRINT('')
python_utils.PRINT(' %s' % url)
python_utils.PRINT('')
python_utils.PRINT(
'NOTE: To get rid of this message, open scripts/common.py and fix')
python_utils.PRINT(
'the function open_new_tab_in_browser_if_possible() to work on your')
python_utils.PRINT('system.')
python_utils.INPUT()
def get_remote_alias(remote_url):
"""Finds the correct alias for the given remote repository URL."""
git_remote_output = subprocess.check_output(
['git', 'remote', '-v']).split('\n')
remote_alias = None
for line in git_remote_output:
if remote_url in line:
remote_alias = line.split()[0]
if remote_alias is None:
raise Exception(
'ERROR: There is no existing remote alias for the %s repo.'
% remote_url)
return remote_alias
def verify_local_repo_is_clean():
"""Checks that the local Git repo is clean."""
git_status_output = subprocess.check_output(
['git', 'status']).strip().split('\n')
branch_is_clean_message_1 = 'nothing to commit, working directory clean'
branch_is_clean_message_2 = 'nothing to commit, working tree clean'
if (
not branch_is_clean_message_1 in git_status_output and
not branch_is_clean_message_2 in git_status_output):
raise Exception(
'ERROR: This script should be run from a clean branch.')
def get_current_branch_name():
"""Get the current branch name.
Returns:
str. The name of current branch.
"""
git_status_output = subprocess.check_output(
['git', 'status']).strip().split('\n')
branch_message_prefix = 'On branch '
git_status_first_line = git_status_output[0]
assert git_status_first_line.startswith(branch_message_prefix)
return git_status_first_line[len(branch_message_prefix):]
def get_current_release_version_number(release_branch_name):
"""Gets the release version given a release branch name.
Args:
release_branch_name: str. The name of release branch.
Returns:
str. The version of release.
"""
release_match = re.match(RELEASE_BRANCH_REGEX, release_branch_name)
release_maintenance_match = re.match(
RELEASE_MAINTENANCE_BRANCH_REGEX, release_branch_name)
hotfix_match = re.match(
HOTFIX_BRANCH_REGEX, release_branch_name)
if release_match:
return release_match.group(1)
elif release_maintenance_match:
return release_maintenance_match.group(1)
elif hotfix_match:
return hotfix_match.group(1)
else:
raise Exception('Invalid branch name: %s.' % release_branch_name)
def is_current_branch_a_hotfix_branch():
"""Checks if the current branch is a hotfix branch.
Returns:
bool. Whether the current branch is hotfix branch.
"""
current_branch_name = get_current_branch_name()
return bool(
re.match(HOTFIX_BRANCH_REGEX, current_branch_name))
def is_current_branch_a_release_branch():
"""Returns whether the current branch is a release branch.
Returns:
bool. Whether the current branch is a release branch.
"""
current_branch_name = get_current_branch_name()
release_match = bool(re.match(RELEASE_BRANCH_REGEX, current_branch_name))
release_maintenance_match = bool(
re.match(RELEASE_MAINTENANCE_BRANCH_REGEX, current_branch_name))
hotfix_match = bool(
re.match(HOTFIX_BRANCH_REGEX, current_branch_name))
return release_match or release_maintenance_match or hotfix_match
def is_current_branch_a_test_branch():
"""Returns whether the current branch is a test branch for deployment.
Returns:
bool. Whether the current branch is a test branch for deployment.
"""
current_branch_name = get_current_branch_name()
return bool(re.match(TEST_BRANCH_REGEX, current_branch_name))
def verify_current_branch_name(expected_branch_name):
"""Checks that the user is on the expected branch."""
if get_current_branch_name() != expected_branch_name:
raise Exception(
'ERROR: This script can only be run from the "%s" branch.' %
expected_branch_name)
def is_port_open(port):
"""Checks if a process is listening to the port.
Args:
port: int. The port number.
Returns:
bool. True if port is open else False.
"""
with contextlib.closing(
socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
return bool(not s.connect_ex(('localhost', port)))
def recursive_chown(path, uid, gid):
"""Changes the owner and group id of all files in a path to the numeric
uid and gid.
Args:
path: str. The path for which owner id and group id need to be setup.
uid: int. Owner ID to be set.
gid: int. Group ID to be set.
"""
os.chown(path, uid, gid)
for root, directories, filenames in os.walk(path):
for directory in directories:
os.chown(os.path.join(root, directory), uid, gid)
for filename in filenames:
os.chown(os.path.join(root, filename), uid, gid)
def recursive_chmod(path, mode):
"""Changes the mode of path to the passed numeric mode.
Args:
path: str. The path for which mode would be set.
mode: int. The mode to be set.
"""
os.chmod(path, mode)
for root, directories, filenames in os.walk(path):
for directory in directories:
os.chmod(os.path.join(root, directory), mode)
for filename in filenames:
os.chmod(os.path.join(root, filename), mode)
def print_each_string_after_two_new_lines(strings):
"""Prints the given strings, separating adjacent strings with two newlines.
Args:
strings: list(str). The strings to print.
"""
for string in strings:
python_utils.PRINT('%s\n' % string)
def install_npm_library(library_name, version, path):
"""Installs the npm library after ensuring its not already installed.
Args:
library_name: str. The library name.
version: str. The library version.
path: str. The installation path for the library.
"""
python_utils.PRINT(
'Checking whether %s is installed in %s' % (library_name, path))
if not os.path.exists(os.path.join(NODE_MODULES_PATH, library_name)):
python_utils.PRINT('Installing %s' % library_name)
subprocess.check_call([
'yarn', 'add', '%s@%s' % (library_name, version)])
def ask_user_to_confirm(message):
"""Asks user to perform a task and confirm once they are done.
Args:
message: str. The message which specifies the task user has
to do.
"""
while True:
python_utils.PRINT(
'******************************************************')
python_utils.PRINT(message)
python_utils.PRINT('Confirm once you are done by entering y/ye/yes.\n')
answer = python_utils.INPUT().lower()
if answer in AFFIRMATIVE_CONFIRMATIONS:
return
def get_personal_access_token():
""""Returns the personal access token for the GitHub id of user.
Returns:
str. The personal access token for the GitHub id of user.
Raises:
Exception. Personal access token is None.
"""
personal_access_token = getpass.getpass(
prompt=(
'Please provide personal access token for your github ID. '
'You can create one at https://github.com/settings/tokens: '))
if personal_access_token is None:
raise Exception(
'No personal access token provided, please set up a personal '
'access token at https://github.com/settings/tokens and re-run '
'the script')
return personal_access_token
def check_blocking_bug_issue_count(repo):
"""Checks the number of unresolved blocking bugs.
Args:
repo: github.Repository.Repository. The PyGithub object for the repo.
Raises:
Exception. Number of unresolved blocking bugs is not zero.
Exception. The blocking bug milestone is closed.
"""
blocking_bugs_milestone = repo.get_milestone(
number=constants.release_constants.BLOCKING_BUG_MILESTONE_NUMBER)
if blocking_bugs_milestone.state == 'closed':
raise Exception('The blocking bug milestone is closed.')
if blocking_bugs_milestone.open_issues:
open_new_tab_in_browser_if_possible(
'https://github.com/oppia/oppia/issues?q=is%3Aopen+'
'is%3Aissue+milestone%3A%22Blocking+bugs%22')
raise Exception(
'There are %s unresolved blocking bugs. Please ensure '
'that they are resolved before release summary generation.' % (
blocking_bugs_milestone.open_issues))
def check_prs_for_current_release_are_released(repo):
"""Checks that all pull requests for current release have a
'PR: released' label.
Args:
repo: github.Repository.Repository. The PyGithub object for the repo.
Raises:
Exception. Some pull requests for current release do not have a
"PR: released" label.
"""
current_release_label = repo.get_label(
constants.release_constants.LABEL_FOR_CURRENT_RELEASE_PRS)
current_release_prs = repo.get_issues(
state='all', labels=[current_release_label])
for pr in current_release_prs:
label_names = [label.name for label in pr.labels]
if constants.release_constants.LABEL_FOR_RELEASED_PRS not in (
label_names):
open_new_tab_in_browser_if_possible(
'https://github.com/oppia/oppia/pulls?utf8=%E2%9C%93&q=is%3Apr'
'+label%3A%22PR%3A+for+current+release%22+')
raise Exception(
'There are PRs for current release which do not have '
'a \'PR: released\' label. Please ensure that they are '
'released before release summary generation.')
def kill_processes_based_on_regex(pattern):
"""Kill any processes whose command line matches the provided regex.
Args:
pattern: str. Pattern for searching processes.
"""
regex = re.compile(pattern)
if PSUTIL_DIR not in sys.path:
sys.path.insert(1, PSUTIL_DIR)
import psutil
for process in psutil.process_iter():
try:
cmdline = ' '.join(process.cmdline())
if regex.match(cmdline) and process.is_running():
python_utils.PRINT('Killing %s ...' % cmdline)
process.kill()
# Possible exception raised by psutil includes: AccessDenied,
# NoSuchProcess, ZombieProcess, TimeoutExpired. We can safely ignore
# those ones and continue.
# https://psutil.readthedocs.io/en/latest/#exceptions
except psutil.Error:
continue
def convert_to_posixpath(file_path):
"""Converts a Windows style filepath to posixpath format. If the operating
system is not Windows, this function does nothing.
Args:
file_path: str. The path to be converted.
Returns:
str. Returns a posixpath version of the file path.
"""
if not is_windows_os():
return file_path
return file_path.replace('\\', '/')
def create_readme(dir_path, readme_content):
"""Creates a readme in a given dir path with the specified
readme content.
Args:
dir_path: str. The path of the dir where the README is to
be created.
readme_content: str. The content to be written in the README.
"""
with python_utils.open_file(os.path.join(dir_path, 'README.md'), 'w') as f:
f.write(readme_content)
def inplace_replace_file(filename, regex_pattern, replacement_string):
"""Replace the file content in-place with regex pattern. The pattern is used
to replace the file's content line by line.
Note:
This function should only be used with files that are processed line by
line.
Args:
filename: str. The name of the file to be changed.
regex_pattern: str. The pattern to check.
replacement_string: str. The content to be replaced.
"""
backup_filename = '%s.bak' % filename
shutil.copyfile(filename, backup_filename)
new_contents = []
try:
regex = re.compile(regex_pattern)
with python_utils.open_file(backup_filename, 'r') as f:
for line in f:
new_contents.append(regex.sub(replacement_string, line))
with python_utils.open_file(filename, 'w') as f:
for line in new_contents:
f.write(line)
os.remove(backup_filename)
except Exception:
# Restore the content if there was en error.
os.remove(filename)
shutil.move(backup_filename, filename)
raise
def wait_for_port_to_be_open(port_number):
"""Wait until the port is open and exit if port isn't open after
1000 seconds.
Args:
port_number: int. The port number to wait.
"""
waited_seconds = 0
while (not is_port_open(port_number)
and waited_seconds < MAX_WAIT_TIME_FOR_PORT_TO_OPEN_SECS):
time.sleep(1)
waited_seconds += 1
if (waited_seconds == MAX_WAIT_TIME_FOR_PORT_TO_OPEN_SECS
and not is_port_open(port_number)):
python_utils.PRINT(
'Failed to start server on port %s, exiting ...' %
port_number)
sys.exit(1)
def start_redis_server():
"""Start the redis server with the daemonize argument to prevent
the redis-server from exiting on its own.
"""
if is_windows_os():
raise Exception(
'The redis command line interface is not installed because your '
'machine is on the Windows operating system. The redis server '
'cannot start.')
# Check if a redis dump file currently exists. This file contains residual
# data from a previous run of the redis server. If it exists, removes the
# dump file so that the redis server starts with a clean slate.
if os.path.exists(REDIS_DUMP_PATH):
os.remove(REDIS_DUMP_PATH)
# Redis-cli is only required in a development environment.
python_utils.PRINT('Starting Redis development server.')
# Start the redis local development server. Redis doesn't run on
# Windows machines.
subprocess.call([
REDIS_SERVER_PATH, REDIS_CONF_PATH,
'--daemonize', 'yes'
])
wait_for_port_to_be_open(feconf.REDISPORT)
def stop_redis_server():
"""Stops the redis server by shutting it down."""
if is_windows_os():
raise Exception(
'The redis command line interface is not installed because your '
'machine is on the Windows operating system. There is no redis '
'server to shutdown.')
python_utils.PRINT('Cleaning up the redis_servers.')
# Shutdown the redis server before exiting.
subprocess.call([REDIS_CLI_PATH, 'shutdown'])
def fix_third_party_imports():
"""Sets up up the environment variables and corrects the system paths so
that the backend tests and imports work correctly.
"""
# These environmental variables are required to allow Google Cloud Tasks to
# operate in a local development environment without connecting to the
# internet. These environment variables allow Cloud APIs to be instantiated.
os.environ['CLOUDSDK_CORE_PROJECT'] = 'dummy-cloudsdk-project-id'
os.environ['APPLICATION_ID'] = 'dummy-cloudsdk-project-id'
# The devappserver function fixes the system path by adding certain google
# appengine libraries that we need in oppia to the python system path. The
# Google Cloud SDK comes with certain packages preinstalled including
# webapp2, jinja2, and pyyaml so this function makes sure that those
# libraries are installed.
import dev_appserver
dev_appserver.fix_sys_path()
# In the process of migrating Oppia from Python 2 to Python 3, we are using
# both google app engine apis that are contained in the Google Cloud SDK
# folder, and also google cloud apis that are installed in our
# 'third_party/python_libs' directory. Therefore, there is a confusion of
# where the google module is located and which google module to import from.
# The following code ensures that the google module that python looks at
# imports from the 'third_party/python_libs' folder so that the imports are
# correct.
if 'google' in sys.modules:
google_path = os.path.join(THIRD_PARTY_PYTHON_LIBS_DIR, 'google')
google_module = sys.modules['google']
google_module.__path__ = [google_path]
google_module.__file__ = os.path.join(google_path, '__init__.py')
sys.path.insert(1, THIRD_PARTY_PYTHON_LIBS_DIR)
class CD(python_utils.OBJECT):
"""Context manager for changing the current working directory."""
def __init__(self, new_path):
self.new_path = new_path
self.saved_path = None
def __enter__(self):
self.saved_path = os.getcwd()
os.chdir(self.new_path)
def __exit__(self, etype, value, traceback):
os.chdir(self.saved_path)