Skip to content

Commit

Permalink
update script to handle all path types
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Jan 31, 2024
1 parent 9c162fd commit bdbd7cf
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 45 deletions.
67 changes: 34 additions & 33 deletions scripts/migrate_to_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,54 +84,55 @@

# Write the new content to the markdown file.
md_file = str(path).replace('.rst', '.md')
with open(md_file, 'w') as fid:
fid.write('\n'.join(new_lines))
# with open(md_file, 'w') as fid:
# fid.write('\n'.join(new_lines))

# Handle links in other files.
# We accept relative path links or links to master
# (https://github.com/mongodb/specifications/blob/master/source/...)
# and rewrite them to use appropriate md links.
# If the link is malformed we ignore and print an error.
curr = path
target = path.name
while curr.parent.name != "source":
target = f"{curr.parent.name}/{target}"
curr = curr.parent
pattern = re.compile(f'(<.*{target}[>#])')
pattern2 = re.compile(f'(/source/{target}[>#])')
for p in Path("source").rglob("*.rst"):
rel_pattern = re.compile(f'(\.\.\S*/{path.name})')
md_pattern = re.compile(f'(\(http\S*/{path.name})')
rst_pattern = re.compile(f'(<http\S*/{path.name})')
abs_pattern = re.compile(f'(/source/\S*/{path.name})')
for p in Path("source").rglob("*"):
if p.suffix not in ['.rst', '.md']:
continue
found = False
with p.open() as fid:
lines = fid.readlines()
new_lines = []
relpath = os.path.relpath(md_file, start=p.parent)
for line in lines:
match = re.search(pattern, line)
if not match:
match = re.search(pattern2, line)
if match:
found = True
new_name = path.name.replace('.rst', '.md')
line = line.replace(path.name, new_name)
new_lines.append(line)
continue
matchstr = match.groups()[0]
if matchstr[0].startswith('https'):
substr = match.string
if not substr.startswith('<https://github.com/mongodb/specifications/blob/master/source/'):
print('*** Error in link: ', substr, p)
new_lines.append(line)
continue
found = True
relpath = os.path.relpath(path.parent.parent, start=p.parent)
new_name = path.name.replace('.rst', '.md')
new_substr = f'<{relpath}/{path.parent.name}/{new_name}{matchstr[-1]}'
new_lines.append(line.replace(matchstr, new_substr))
new_line = line
if re.search(rel_pattern, line):
matchstr = re.search(rel_pattern, line).groups()[0]
new_line = line.replace(matchstr, relpath)
elif re.search(md_pattern, line):
matchstr = re.search(md_pattern, line).groups()[0]
if not matchstr.startswith('(https://github.com/mongodb/specifications/blob/master/source'):
print('*** Error in link: ', matchstr, p)
else:
new_line = line.replace(matchstr, f'({relpath}')
elif re.search(rst_pattern, line):
matchstr = re.search(rst_pattern, line).groups()[0]
if not matchstr.startswith('<https://github.com/mongodb/specifications/blob/master/source'):
print('*** Error in link: ', matchstr, p)
else:
new_line = line.replace(matchstr, f'<{relpath}')
elif re.search(abs_pattern, line):
matchstr = re.search(abs_pattern, line).groups()[0]
new_line = line.replace(matchstr, relpath)

if new_line != line:
found = True
new_lines.append(new_line)

if found:
with p.open('w') as fid:
fid.writelines(new_lines)
print(f'Update link(s) in {p.name}')

print(f'Update link(s) in {p}')


print('Created markdown file:')
Expand Down
2 changes: 1 addition & 1 deletion source/client-side-encryption/tests/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ Repeat the steps from the "Via bypassAutoEncryption" test, replacing "bypassAuto
9. Deadlock Tests
~~~~~~~~~~~~~~~~~

.. _Connection Monitoring and Pooling: /source/connection-monitoring-and-pooling/connection-monitoring-and-pooling.rst
.. _Connection Monitoring and Pooling: ../../connection-monitoring-and-pooling/connection-monitoring-and-pooling.md

The following tests only apply to drivers that have implemented a connection pool (see the `Connection Monitoring and Pooling`_ specification).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ Each YAML file has the following keys:
All Unit Tests have some of the following fields:

- `poolOptions`: If present, connection pool options to use when creating a pool; both
[standard ConnectionPoolOptions](https://github.com/mongodb/specifications/blob/master/source/connection-monitoring-and-pooling/connection-monitoring-and-pooling.rst#connection-pool-options-1)
and the following test-specific options are allowed:
[standard ConnectionPoolOptions](../../connection-monitoring-and-pooling.md#connection-pool-options-1) and the
following test-specific options are allowed:
- `backgroundThreadIntervalMS`: A time interval between the end of a
[Background Thread Run](https://github.com/mongodb/specifications/blob/master/source/connection-monitoring-and-pooling/connection-monitoring-and-pooling.rst#background-thread)
and the beginning of the next Run. If a Connection Pool does not implement a Background Thread, the Test Runner MUST
ignore the option. If the option is not specified, an implementation is free to use any value it finds reasonable.
[Background Thread Run](../../connection-monitoring-and-pooling.md#background-thread) and the beginning of the next
Run. If a Connection Pool does not implement a Background Thread, the Test Runner MUST ignore the option. If the
option is not specified, an implementation is free to use any value it finds reasonable.

Possible values (0 is not allowed):

Expand Down
2 changes: 1 addition & 1 deletion source/connections-survive-step-down/tests/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ methodology is in contrast to the one adopted by the SDAM spec tests that rely e
server communication.


.. _CMAP: /source/connection-monitoring-and-pooling/connection-monitoring-and-pooling.rst
.. _CMAP: ../../connection-monitoring-and-pooling/connection-monitoring-and-pooling.md
.. _PoolClearedEvent: /source/connection-monitoring-and-pooling/connection-monitoring-and-pooling.md#events
.. _serverStatus: https://www.mongodb.com/docs/manual/reference/command/serverStatus
.. _connections.totalCreated: https://www.mongodb.com/docs/manual/reference/command/serverStatus/#serverstatus.connections.totalCreated
2 changes: 1 addition & 1 deletion source/retryable-reads/retryable-reads.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ SocketException 9001

- a `PoolClearedError`_

.. _PoolClearedError: ../connection-monitoring-and-pooling/connection-monitoring-and-pooling.rst#connection-pool-errors
.. _PoolClearedError: ../connection-monitoring-and-pooling/connection-monitoring-and-pooling.md#connection-pool-errors

- Any of the above retryable errors that occur during a connection handshake (including the
authentication step). For example, a network error or ShutdownInProgress error
Expand Down
2 changes: 1 addition & 1 deletion source/retryable-writes/retryable-writes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ The RetryableWriteError label might be added to an error in a variety of ways:
the MongoClient performing the operation has the retryWrites configuration
option set to true.

.. _PoolClearedError: ../connection-monitoring-and-pooling/connection-monitoring-and-pooling.rst#connection-pool-errors
.. _PoolClearedError: ../connection-monitoring-and-pooling/connection-monitoring-and-pooling.md#connection-pool-errors

- For server versions 4.4 and newer, the server will add a RetryableWriteError
label to errors or server responses that it considers retryable before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2557,7 +2557,7 @@ Changelog
.. _scanning order: server-monitoring.rst#scanning-order
.. _clients update the topology from each handshake: server-monitoring.rst#clients-update-the-topology-from-each-handshake
.. _single-threaded monitoring: server-monitoring.rst#single-threaded-monitoring
.. _Connection Monitoring and Pooling spec: /source/connection-monitoring-and-pooling/connection-monitoring-and-pooling.rst
.. _CMAP spec: /source/connection-monitoring-and-pooling/connection-monitoring-and-pooling.rst
.. _Connection Monitoring and Pooling spec: ../connection-monitoring-and-pooling/connection-monitoring-and-pooling.md
.. _CMAP spec: ../connection-monitoring-and-pooling/connection-monitoring-and-pooling.md
.. _Authentication spec: /source/auth/auth.rst
.. _Server Monitoring (Measuring RTT): server-monitoring.rst#measuring-rtt
2 changes: 1 addition & 1 deletion source/server-selection/server-selection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2024,7 +2024,7 @@ References
.. _idleWritePeriodMS: https://github.com/mongodb/specifications/blob/master/source/max-staleness/max-staleness.rst#idlewriteperiodms
.. _Driver Authentication: https://github.com/mongodb/specifications/blob/master/source/auth
.. _maxConnecting: /source/connection-monitoring-and-pooling/connection-monitoring-and-pooling.md#connection-pool
.. _Connection Monitoring and Pooling: /source/connection-monitoring-and-pooling/connection-monitoring-and-pooling.rst
.. _Connection Monitoring and Pooling: ../connection-monitoring-and-pooling/connection-monitoring-and-pooling.md
.. _Global Command Argument: /source/message/OP_MSG.rst#global-command-arguments

Changelog
Expand Down

0 comments on commit bdbd7cf

Please sign in to comment.