Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit f3f0ab1

Browse files
David Robertsonanoadragon453
David Robertson
andauthored
Move scripts directory inside synapse, exposing as setuptools entry_points (#12118)
* Two scripts are basically entry_points already * Move and rename scripts/* to synapse/_scripts/*.py * Delete sync_room_to_group.pl * Expose entry points in setup.py * Update linter script and config * Fixup scripts & docs mentioning scripts that moved Co-authored-by: Andrew Morgan <[email protected]>
1 parent 6adb89f commit f3f0ab1

27 files changed

+77
-135
lines changed

Diff for: .ci/scripts/test_export_data_command.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ python -m synapse.app.homeserver --generate-keys -c .ci/sqlite-config.yaml
2121
echo "--- Prepare test database"
2222

2323
# Make sure the SQLite3 database is using the latest schema and has no pending background update.
24-
scripts/update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates
24+
update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates
2525

2626
# Run the export-data command on the sqlite test database
2727
python -m synapse.app.admin_cmd -c .ci/sqlite-config.yaml export-data @anon-20191002_181700-832:localhost:8800 \
@@ -41,7 +41,7 @@ fi
4141

4242
# Port the SQLite databse to postgres so we can check command works against postgres
4343
echo "+++ Port SQLite3 databse to postgres"
44-
scripts/synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
44+
synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
4545

4646
# Run the export-data command on postgres database
4747
python -m synapse.app.admin_cmd -c .ci/postgres-config.yaml export-data @anon-20191002_181700-832:localhost:8800 \

Diff for: .ci/scripts/test_synapse_port_db.sh

+7-5
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,19 @@ python -m synapse.app.homeserver --generate-keys -c .ci/sqlite-config.yaml
2525
echo "--- Prepare test database"
2626

2727
# Make sure the SQLite3 database is using the latest schema and has no pending background update.
28-
scripts/update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates
28+
update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates
2929

3030
# Create the PostgreSQL database.
3131
.ci/scripts/postgres_exec.py "CREATE DATABASE synapse"
3232

3333
echo "+++ Run synapse_port_db against test database"
34-
coverage run scripts/synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
34+
# TODO: this invocation of synapse_port_db (and others below) used to be prepended with `coverage run`,
35+
# but coverage seems unable to find the entrypoints installed by `pip install -e .`.
36+
synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
3537

3638
# We should be able to run twice against the same database.
3739
echo "+++ Run synapse_port_db a second time"
38-
coverage run scripts/synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
40+
synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
3941

4042
#####
4143

@@ -46,12 +48,12 @@ echo "--- Prepare empty SQLite database"
4648
# we do this by deleting the sqlite db, and then doing the same again.
4749
rm .ci/test_db.db
4850

49-
scripts/update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates
51+
update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates
5052

5153
# re-create the PostgreSQL database.
5254
.ci/scripts/postgres_exec.py \
5355
"DROP DATABASE synapse" \
5456
"CREATE DATABASE synapse"
5557

5658
echo "+++ Run synapse_port_db against empty database"
57-
coverage run scripts/synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
59+
synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml

Diff for: .dockerignore

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
# things to include
55
!docker
6-
!scripts
76
!synapse
87
!MANIFEST.in
98
!README.rst

Diff for: MANIFEST.in

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ recursive-include synapse/storage *.txt
1717
recursive-include synapse/storage *.md
1818

1919
recursive-include docs *
20-
recursive-include scripts *
2120
recursive-include scripts-dev *
2221
recursive-include synapse *.pyi
2322
recursive-include tests *.py

Diff for: changelog.d/12118.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Move scripts to Synapse package and expose as setuptools entry points.

Diff for: docker/Dockerfile

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ RUN \
4646
&& rm -rf /var/lib/apt/lists/*
4747

4848
# Copy just what we need to pip install
49-
COPY scripts /synapse/scripts/
5049
COPY MANIFEST.in README.rst setup.py synctl /synapse/
5150
COPY synapse/__init__.py /synapse/synapse/__init__.py
5251
COPY synapse/python_dependencies.py /synapse/synapse/python_dependencies.py

Diff for: docs/development/database_schema.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ same as integers.
158158
There are three separate aspects to this:
159159

160160
* Any new boolean column must be added to the `BOOLEAN_COLUMNS` list in
161-
`scripts/synapse_port_db`. This tells the port script to cast the integer
162-
value from SQLite to a boolean before writing the value to the postgres
163-
database.
161+
`synapse/_scripts/synapse_port_db.py`. This tells the port script to cast
162+
the integer value from SQLite to a boolean before writing the value to the
163+
postgres database.
164164

165165
* Before SQLite 3.23, `TRUE` and `FALSE` were not recognised as constants by
166166
SQLite, and the `IS [NOT] TRUE`/`IS [NOT] FALSE` operators were not

Diff for: docs/usage/administration/admin_api/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ UPDATE users SET admin = 1 WHERE name = '@foo:bar.com';
1212
```
1313

1414
A new server admin user can also be created using the `register_new_matrix_user`
15-
command. This is a script that is located in the `scripts/` directory, or possibly
15+
command. This is a script that is distributed as part of synapse. It is possibly
1616
already on your `$PATH` depending on how Synapse was installed.
1717

1818
Finding your user's `access_token` is client-dependent, but will usually be shown in the client's settings.

Diff for: mypy.ini

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ files =
2323
# https://docs.python.org/3/library/re.html#re.X
2424
exclude = (?x)
2525
^(
26+
|synapse/_scripts/export_signing_key.py
27+
|synapse/_scripts/move_remote_media_to_new_store.py
28+
|synapse/_scripts/synapse_port_db.py
29+
|synapse/_scripts/update_synapse_database.py
2630
|synapse/storage/databases/__init__.py
2731
|synapse/storage/databases/main/__init__.py
2832
|synapse/storage/databases/main/cache.py

Diff for: scripts-dev/generate_sample_config

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ SAMPLE_CONFIG="docs/sample_config.yaml"
1010
SAMPLE_LOG_CONFIG="docs/sample_log_config.yaml"
1111

1212
check() {
13-
diff -u "$SAMPLE_LOG_CONFIG" <(./scripts/generate_log_config) >/dev/null || return 1
13+
diff -u "$SAMPLE_LOG_CONFIG" <(synapse/_scripts/generate_log_config.py) >/dev/null || return 1
1414
}
1515

1616
if [ "$1" == "--check" ]; then
17-
diff -u "$SAMPLE_CONFIG" <(./scripts/generate_config --header-file docs/.sample_config_header.yaml) >/dev/null || {
17+
diff -u "$SAMPLE_CONFIG" <(synapse/_scripts/generate_config.py --header-file docs/.sample_config_header.yaml) >/dev/null || {
1818
echo -e "\e[1m\e[31m$SAMPLE_CONFIG is not up-to-date. Regenerate it with \`scripts-dev/generate_sample_config\`.\e[0m" >&2
1919
exit 1
2020
}
21-
diff -u "$SAMPLE_LOG_CONFIG" <(./scripts/generate_log_config) >/dev/null || {
21+
diff -u "$SAMPLE_LOG_CONFIG" <(synapse/_scripts/generate_log_config.py) >/dev/null || {
2222
echo -e "\e[1m\e[31m$SAMPLE_LOG_CONFIG is not up-to-date. Regenerate it with \`scripts-dev/generate_sample_config\`.\e[0m" >&2
2323
exit 1
2424
}
2525
else
26-
./scripts/generate_config --header-file docs/.sample_config_header.yaml -o "$SAMPLE_CONFIG"
27-
./scripts/generate_log_config -o "$SAMPLE_LOG_CONFIG"
26+
synapse/_scripts/generate_config.py --header-file docs/.sample_config_header.yaml -o "$SAMPLE_CONFIG"
27+
synapse/_scripts/generate_log_config.py -o "$SAMPLE_LOG_CONFIG"
2828
fi

Diff for: scripts-dev/lint.sh

-7
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,6 @@ else
8484
files=(
8585
"synapse" "docker" "tests"
8686
# annoyingly, black doesn't find these so we have to list them
87-
"scripts/export_signing_key"
88-
"scripts/generate_config"
89-
"scripts/generate_log_config"
90-
"scripts/hash_password"
91-
"scripts/register_new_matrix_user"
92-
"scripts/synapse_port_db"
93-
"scripts/update_synapse_database"
9487
"scripts-dev"
9588
"scripts-dev/build_debian_packages"
9689
"scripts-dev/sign_json"

Diff for: scripts-dev/make_full_schema.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ python -m synapse.app.homeserver --generate-keys -c "$SQLITE_CONFIG"
147147

148148
# Make sure the SQLite3 database is using the latest schema and has no pending background update.
149149
echo "Running db background jobs..."
150-
scripts/update_synapse_database --database-config --run-background-updates "$SQLITE_CONFIG"
150+
synapse/_scripts/update_synapse_database.py --database-config --run-background-updates "$SQLITE_CONFIG"
151151

152152
# Create the PostgreSQL database.
153153
echo "Creating postgres database..."
@@ -156,10 +156,10 @@ createdb --lc-collate=C --lc-ctype=C --template=template0 "$POSTGRES_DB_NAME"
156156
echo "Copying data from SQLite3 to Postgres with synapse_port_db..."
157157
if [ -z "$COVERAGE" ]; then
158158
# No coverage needed
159-
scripts/synapse_port_db --sqlite-database "$SQLITE_DB" --postgres-config "$POSTGRES_CONFIG"
159+
synapse/_scripts/synapse_port_db.py --sqlite-database "$SQLITE_DB" --postgres-config "$POSTGRES_CONFIG"
160160
else
161161
# Coverage desired
162-
coverage run scripts/synapse_port_db --sqlite-database "$SQLITE_DB" --postgres-config "$POSTGRES_CONFIG"
162+
coverage run synapse/_scripts/synapse_port_db.py --sqlite-database "$SQLITE_DB" --postgres-config "$POSTGRES_CONFIG"
163163
fi
164164

165165
# Delete schema_version, applied_schema_deltas and applied_module_schemas tables

Diff for: scripts/register_new_matrix_user

-19
This file was deleted.

Diff for: scripts/synapse_review_recent_signups

-19
This file was deleted.

Diff for: scripts/sync_room_to_group.pl

-45
This file was deleted.

Diff for: setup.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1616
# See the License for the specific language governing permissions and
1717
# limitations under the License.
18-
import glob
1918
import os
2019
from typing import Any, Dict
2120

@@ -153,8 +152,19 @@ def exec_file(path_segments):
153152
python_requires="~=3.7",
154153
entry_points={
155154
"console_scripts": [
155+
# Application
156156
"synapse_homeserver = synapse.app.homeserver:main",
157157
"synapse_worker = synapse.app.generic_worker:main",
158+
# Scripts
159+
"export_signing_key = synapse._scripts.export_signing_key:main",
160+
"generate_config = synapse._scripts.generate_config:main",
161+
"generate_log_config = synapse._scripts.generate_log_config:main",
162+
"generate_signing_key = synapse._scripts.generate_signing_key:main",
163+
"hash_password = synapse._scripts.hash_password:main",
164+
"register_new_matrix_user = synapse._scripts.register_new_matrix_user:main",
165+
"synapse_port_db = synapse._scripts.synapse_port_db:main",
166+
"synapse_review_recent_signups = synapse._scripts.review_recent_signups:main",
167+
"update_synapse_database = synapse._scripts.update_synapse_database:main",
158168
]
159169
},
160170
classifiers=[
@@ -167,6 +177,6 @@ def exec_file(path_segments):
167177
"Programming Language :: Python :: 3.9",
168178
"Programming Language :: Python :: 3.10",
169179
],
170-
scripts=["synctl"] + glob.glob("scripts/*"),
180+
scripts=["synctl"],
171181
cmdclass={"test": TestCommand},
172182
)

Diff for: snap/snapcraft.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ apps:
2020
generate-config:
2121
command: generate_config
2222
generate-signing-key:
23-
command: generate_signing_key.py
23+
command: generate_signing_key
2424
register-new-matrix-user:
2525
command: register_new_matrix_user
2626
plugs: [network]

Diff for: scripts/export_signing_key renamed to synapse/_scripts/export_signing_key.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def format_for_config(public_key: nacl.signing.VerifyKey, expiry_ts: int):
5050
)
5151

5252

53-
if __name__ == "__main__":
53+
def main():
5454
parser = argparse.ArgumentParser()
5555

5656
parser.add_argument(
@@ -85,7 +85,6 @@ def format_for_config(public_key: nacl.signing.VerifyKey, expiry_ts: int):
8585
else format_plain
8686
)
8787

88-
keys = []
8988
for file in args.key_file:
9089
try:
9190
res = read_signing_keys(file)
@@ -98,3 +97,7 @@ def format_for_config(public_key: nacl.signing.VerifyKey, expiry_ts: int):
9897
res = []
9998
for key in res:
10099
formatter(get_verify_key(key))
100+
101+
102+
if __name__ == "__main__":
103+
main()

Diff for: scripts/generate_config renamed to synapse/_scripts/generate_config.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
from synapse.config.homeserver import HomeServerConfig
88

9-
if __name__ == "__main__":
9+
10+
def main():
1011
parser = argparse.ArgumentParser()
1112
parser.add_argument(
1213
"--config-dir",
@@ -76,3 +77,7 @@
7677
shutil.copyfileobj(args.header_file, args.output_file)
7778

7879
args.output_file.write(conf)
80+
81+
82+
if __name__ == "__main__":
83+
main()

Diff for: scripts/generate_log_config renamed to synapse/_scripts/generate_log_config.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
from synapse.config.logger import DEFAULT_LOG_CONFIG
2121

22-
if __name__ == "__main__":
22+
23+
def main():
2324
parser = argparse.ArgumentParser()
2425

2526
parser.add_argument(
@@ -42,3 +43,7 @@
4243
out = args.output_file
4344
out.write(DEFAULT_LOG_CONFIG.substitute(log_file=args.log_file))
4445
out.flush()
46+
47+
48+
if __name__ == "__main__":
49+
main()

Diff for: scripts/generate_signing_key.py renamed to synapse/_scripts/generate_signing_key.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
from synapse.util.stringutils import random_string
2121

22-
if __name__ == "__main__":
22+
23+
def main():
2324
parser = argparse.ArgumentParser()
2425

2526
parser.add_argument(
@@ -34,3 +35,7 @@
3435
key_id = "a_" + random_string(4)
3536
key = (generate_signing_key(key_id),)
3637
write_signing_keys(args.output_file, key)
38+
39+
40+
if __name__ == "__main__":
41+
main()

0 commit comments

Comments
 (0)