Skip to content

Instantly share code, notes, and snippets.

@shacker
Created November 22, 2017 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shacker/37f6783138faa016829afe54d012837b to your computer and use it in GitHub Desktop.
Save shacker/37f6783138faa016829afe54d012837b to your computer and use it in GitHub Desktop.
Convert output of django-extensions' `show_urls` to a python list of "simple" URLs
import re
from django.core.management import call_command
from test_plus.test import TestCase
class SmokeTestAllViews(BaseUserTestCase):
def test_all_view_respones(self):
"""
Extract all URLs and do a quick sanity check for accessibility.
Are all responding with 200 or 302 or 404 where expected? Are expected authorizations enforced?
Clean up the output of django-extensions' `show_urls` command. A bit tricky because it's ANSI-encoded
for terminal display, so clean that up and filter out the /admin URLs.
On each remaining line, strip out everything after first tab char.
"""
# Write output to a file, then read it back in
tempfile = '/tmp/show_urls.txt'
with open(tempfile, 'w+') as f:
call_command('show_urls', stdout=f)
f.close()
with open(tempfile) as f:
content = f.readlines()
# Strip out the ansi chars
ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
url_list = []
[url_list.append(ansi_escape.sub('', line)) for line in content] # Strip ANSI chars
url_list = [line.split('\t')[0] for line in url_list] # Output is tab-sep; keep data before first tab per line
url_list = list(filter(lambda x: not x.startswith("/admin"), url_list)) # Filter out all the /admin URLs
for el in url_list:
print(el)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment