|
| 1 | +"""Utility functions used by projects. |
| 2 | +""" |
| 3 | + |
1 | 4 | import subprocess
|
2 | 5 | import os
|
3 | 6 | import fnmatch
|
|
8 | 11 |
|
9 | 12 | from projects.libs.diff_match_patch import diff_match_patch
|
10 | 13 |
|
| 14 | + |
11 | 15 | def find_file(file):
|
| 16 | + """Find matching filenames in the current directory and its subdirectories, |
| 17 | + and return a list of matching filenames. |
| 18 | + """ |
12 | 19 | matches = []
|
13 | 20 | for root, dirnames, filenames in os.walk('.'):
|
14 | 21 | for filename in fnmatch.filter(filenames, file):
|
15 | 22 | matches.append(os.path.join(root, filename))
|
16 | 23 | return matches
|
17 | 24 |
|
| 25 | + |
18 | 26 | def run(*commands):
|
19 | 27 | """
|
20 | 28 | Run one or more commands, and return ``(status, out, err)``.
|
@@ -58,14 +66,20 @@ def diff(txt1, txt2):
|
58 | 66 | patch = dmp.patch_make(txt1, txt2)
|
59 | 67 | return dmp.patch_toText(patch)
|
60 | 68 |
|
| 69 | + |
61 | 70 | def safe_write(filename, contents):
|
| 71 | + """Write ``contents`` to the given ``filename``. If the filename's |
| 72 | + directory does not exist, it is created. Contents are written as UTF-8, |
| 73 | + ignoring any characters that cannot be encoded as UTF-8. |
| 74 | + """ |
62 | 75 | dirname = os.path.dirname(filename)
|
63 | 76 | if not os.path.exists(dirname):
|
64 | 77 | os.makedirs(dirname)
|
65 | 78 | fh = open(filename, 'w')
|
66 | 79 | fh.write(contents.encode('utf-8', 'ignore'))
|
67 | 80 | fh.close()
|
68 | 81 |
|
| 82 | + |
69 | 83 | def sanitize_conf(conf_filename):
|
70 | 84 | """Modify the given ``conf.py`` file from a whitelisted project. For now,
|
71 | 85 | this just adds the RTD template directory to ``templates_path``.
|
@@ -93,3 +107,4 @@ def sanitize_conf(conf_filename):
|
93 | 107 | outfile.write(line)
|
94 | 108 | outfile.close()
|
95 | 109 | return lines_matched
|
| 110 | + |
0 commit comments