|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +__author__ = ['Brandon Amos <http://github.com/bamos>'] |
| 4 | +__date__ = '2015.03.27' |
| 5 | + |
| 6 | +""" |
| 7 | +Generates the README for |
| 8 | +[bamos/python-scripts](https://github.com/bamos/python-scripts). |
| 9 | +Script descriptions are obtained by parsing the docstrings. |
| 10 | +""" |
| 11 | + |
| 12 | +import ast |
| 13 | +import glob |
| 14 | +import os |
| 15 | +from jinja2 import Template |
| 16 | +from subprocess import Popen,PIPE |
| 17 | + |
| 18 | +readme = Template(""" |
| 19 | +This is a collection of short Python scripts I have added to my |
| 20 | +`PATH` variable to run from anywhere. |
| 21 | +None are currently available in [pip][pip], |
| 22 | +but I will add them if enough people are interested. |
| 23 | +
|
| 24 | +To add these to your `PATH`, clone the repo and add the following |
| 25 | +to your `bashrc` or `zshrc`, replacing `<python-scripts>` |
| 26 | +with the location of the cloned repository. |
| 27 | +Furthermore, see my [dotfiles][dotfiles] repo for my |
| 28 | +complete Mac and Linux system configurations. |
| 29 | +
|
| 30 | +```Bash |
| 31 | +# Add additional directories to the path. |
| 32 | +pathadd() { |
| 33 | + [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]] && PATH="${PATH:+"$PATH:"}$1" |
| 34 | +} |
| 35 | +
|
| 36 | +pathadd <python-scripts>/python2.7 |
| 37 | +pathadd <python-scripts>/python3 |
| 38 | +``` |
| 39 | +
|
| 40 | +[pip]: http://pip.readthedocs.org/en/latest/ |
| 41 | +[dotfiles]: https://github.com/bamos/dotfiles |
| 42 | +
|
| 43 | +# Script Descriptions |
| 44 | +{{descriptions}} |
| 45 | +
|
| 46 | +# Similar Projects |
| 47 | +There are many potpourri Python script repositories on GitHub. |
| 48 | +The following list shows a short sampling of projects, |
| 49 | +and I'm happy to merge pull requests of other projects. |
| 50 | +
|
| 51 | +{{similar_projects}} |
| 52 | +""") |
| 53 | + |
| 54 | +def get_docstr(filename): |
| 55 | + print(" + get_docstr({})".format(filename)) |
| 56 | + with open(filename) as f: |
| 57 | + script = ast.parse(f.read()) |
| 58 | + try: |
| 59 | + authors,date,desc = map(lambda x: ast.literal_eval(x.value), |
| 60 | + script.body[0:3]) |
| 61 | + except: |
| 62 | + print(" + Error reading (author,date,desc).") |
| 63 | + raise |
| 64 | + return """ |
| 65 | +## {} |
| 66 | ++ Authors: {} |
| 67 | ++ Date Created: {} |
| 68 | +
|
| 69 | +{} |
| 70 | +""".format(filename, ", ".join(authors), date, desc) |
| 71 | + |
| 72 | +def get_descriptions(): |
| 73 | + print("# Getting project descriptions") |
| 74 | + return ("\n".join(map(get_docstr, |
| 75 | + ['generate-readme.py']+glob.glob("python*/*.py")))) |
| 76 | + |
| 77 | +def get_similar_projects(): |
| 78 | + print("# Getting similar projects") |
| 79 | + projs =['gpambrozio/PythonScripts', |
| 80 | + 'ClarkGoble/Scripts', |
| 81 | + 'gpambrozio/PythonScripts', |
| 82 | + 'realpython/python-scripts', |
| 83 | + 'averagesecurityguy/Python-Examples', |
| 84 | + 'computermacgyver/twitter-python'] |
| 85 | + cmd = ['./python3/github-repo-summary.py'] + projs |
| 86 | + p = Popen(cmd, stdout=PIPE) |
| 87 | + out,err = p.communicate() |
| 88 | + return out.decode() |
| 89 | + |
| 90 | +if __name__=='__main__': |
| 91 | + # cd into the script directory. |
| 92 | + abspath = os.path.abspath(__file__) |
| 93 | + dname = os.path.dirname(abspath) |
| 94 | + os.chdir(dname) |
| 95 | + |
| 96 | + with open("README.md","w") as f: |
| 97 | + f.write(readme.render( |
| 98 | + descriptions=get_descriptions(), |
| 99 | + similar_projects=get_similar_projects() |
| 100 | + )) |
0 commit comments