scrapli / scrapli_cfg

Network device Configuration Management with scrapli
https://scrapli.github.io/scrapli_cfg/
MIT License
32 stars 4 forks source link

Add count to regex match, so only first occurance is matched #41

Closed WillIrvine closed 2 years ago

WillIrvine commented 2 years ago

PR to limit the header pattern match in the XE driver to the first occurrence.

Justification:

The "S" flag in the pattern match is very slow in python regex.

OUTPUT_HEADER_PATTERN = re.compile( pattern=r".*(?=(version \d+\.\d+))", flags=re.I | re.S, )

Default behavior in Scrapli_Cfg is to run this regex pattern on the entire config file to remove any text before the actual configuration.

This pattern will only be seen once in a valid configuration file. This PR limits the match to the first occurrence, so that we don't have to regex the entire file, significantly speeding up the processing of the new config.

On my test config (>18000 lines), this speed up the processing from 2 hours, to less than a second, and still performed the required task of removing anything above the start of the configuration

From what i can see, the Async driver references this one, so we don't need to adjust anything there, and the other Cisco drivers don't use the "S" flag so won't have the same processing issue.

Let me know if there is anything i have missed or if you have any other contributing requirements.

Thanks

carlmontanari commented 2 years ago

LGTM, thanks a bunch @WillIrvine !