-
Notifications
You must be signed in to change notification settings - Fork 106
/
generate-readme.py
executable file
·132 lines (108 loc) · 4.26 KB
/
generate-readme.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
__author__ = ['[Brandon Amos](http://bamos.github.io)']
__date__ = '2015.03.27'
"""
Generates the README for
[bamos/python-scripts](https://github.com/bamos/python-scripts)
so that the README and scripts contain synchronized documentation.
Script descriptions are obtained by parsing the docstrings
and inserted directly into the README as markdown.
"""
import ast
import glob
import os
from jinja2 import Template
from subprocess import Popen, PIPE
readme = Template("""
[![Build Status](https://travis-ci.org/bamos/python-scripts.svg)](https://travis-ci.org/bamos/python-scripts)
[![Dependency Status](https://gemnasium.com/bamos/python-scripts.svg)](https://gemnasium.com/bamos/python-scripts)
This is a collection of short Python scripts I use in Linux.
Portions of this README are automatically generated with
[generate-readme.py](https://github.com/bamos/python-scripts#generate-readmepy)
to insert the script-level comments as descriptions below.
# Adding to your PATH
I have these added to my `PATH`
[variable](https://wiki.archlinux.org/index.php/Environment_variables)
to run from anywhere.
Clone the repo and add the following
to your `bashrc` or `zshrc`, replacing `<python-scripts>`
with the location of the cloned repository.
See my [dotfiles](https://github.com/bamos/dotfiles)
repo for my complete Mac and Linux system configurations.
```Bash
# Add additional directories to the path.
pathadd() {
[ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]] && PATH="${PATH:+"$PATH:"}$1"
}
pathadd <python-scripts>/python2.7
pathadd <python-scripts>/python3
```
# Dependencies
These scripts are written in Python 3 except when external
libraries don't support Python 3.
Dependencies for Python 2 and 3 for all scripts are
included in `requirements-{2,3}.txt` and can be installed
using `pip` with `pip3 install -r requirements-3.txt`.
# Travis CI
Continuous integration is provided by Travis CI
[here](https://travis-ci.org/bamos/python-scripts).
[.travis.yml](https://github.com/bamos/python-scripts/blob/master/.travis.yml)
calls
[.travis-script-2.sh](https://github.com/bamos/python-scripts/blob/master/.travis-script-2.sh)
and
[.travis-script-3.sh](https://github.com/bamos/python-scripts/blob/master/.travis-script-3.sh)
to ensure `requirements-{2,3}.txt` have all of the Python 2 and Python 3 scripts
and that there are no careless errors that cause the scripts to
not execute the help message.
[pep8](https://github.com/jcrocholl/pep8) will fail the build
if pep8 styling conventions aren't met.
# Scripts
The subheadings link to the script contents on GitHub.
{{descriptions}}
# Similar Projects
There are many potpourri Python script repositories on GitHub.
The following list shows a short sampling of projects,
and I'm happy to merge pull requests of other projects.
{{similar_projects}}
""")
def get_docstr(filename):
print(" + get_docstr({})".format(filename))
with open(filename) as f:
script = ast.parse(f.read())
try:
authors, date, desc = map(lambda x: ast.literal_eval(x.value),
script.body[0:3])
except:
print(" + Error reading (author, date, desc).")
raise
return """
## [{}](https://github.com/bamos/python-scripts/blob/master/{})
+ Authors: {}
+ Created: {}
{}
""".format(filename, filename, ", ".join(authors), date, desc)
def get_descriptions():
print("# Getting project descriptions")
return ("\n".join(map(get_docstr,
['generate-readme.py'] + glob.glob("python*/*.py"))))
def get_similar_projects():
print("# Getting similar projects")
projs = ['ClarkGoble/Scripts',
'gpambrozio/PythonScripts',
'realpython/python-scripts',
'averagesecurityguy/Python-Examples',
'computermacgyver/twitter-python']
cmd = ['./python3/github-repo-summary.py'] + projs
p = Popen(cmd, stdout=PIPE)
out, err = p.communicate()
return out.decode()
if __name__ == '__main__':
# cd into the script directory.
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
with open("README.md", "w") as f:
f.write(readme.render(
descriptions=get_descriptions(),
similar_projects=get_similar_projects()
))