cheat-fork-echo/cheat/sheets.py
Chris Lane e391cf2f01 Issue #172
Previously, `cheat` would exit if run by `root`. The rationale was:

If `cheat` was run by an unprivileged user (`chris`, for example), the
`$DEFAULT_CHEAT_DIR` would default to `/home/chris/.cheat`. If cheat was
run via `sudo`, however, the `$DEFAULT_CHEAT_DIR` would suddenly be
`/root/.cheat`.

Presuming that those individual user cheat dirs actually contained cheat
sheets, this could cause confusion, because cheat sheets accessible to
one user (`chris`) would not be accessible to the other (`root`). Thus,
cheatsheets would appear and disappear, depending on which user was
running `cheat`. (This would only be an issue, of course, for cheat
sheets that existed within the respective users' cheat dirs. System-wide
cheat sheets would be unaffected.)

`cheat` was thus programmed to gracefully fail when run as `root` to
prevent that possible confusion.

However, I'm backing away from that reasoning, because:

1. It's causing a headache for real users who'd like to run `cheat` as
root

2. Other venerable programs (`vim`, etc.) suffer from the same problem,
but nobody seems to mind enough to do anything about it. Thus, I suppose
the laissez-faire approach is essentially the sanctioned "solution" to
this problem.

3. Users sufficently troubled by this confusion may rememdy the problem
manually by setting environment variables (`$DEFAULT_CHEAT_DIR`, etc.)

Thus, `cheat` no longer complains if run by `root`.

Version-bumped to 2.1.0.
2014-08-06 21:56:19 -04:00

101 lines
3 KiB
Python

from cheat import cheatsheets
from cheat.utils import *
import os
# @kludge: it breaks the functional paradigm to a degree, but declaring this
# var here (versus within get()) gives us a "poor man's" memoization on the
# call to get(). This, in turn, spares us from having to call out to the
# filesystem more than once.
cheats = {}
def default_path():
""" Returns the default cheatsheet path """
# determine the default cheatsheet dir
default_sheets_dir = os.environ.get('DEFAULT_CHEAT_DIR') or os.path.join(os.path.expanduser('~'), '.cheat')
# create the DEFAULT_CHEAT_DIR if it does not exist
if not os.path.isdir(default_sheets_dir):
try:
# @kludge: unclear on why this is necessary
os.umask(0000)
os.mkdir(default_sheets_dir)
except OSError:
die('Could not create DEFAULT_CHEAT_DIR')
# assert that the DEFAULT_CHEAT_DIR is readable and writable
if not os.access(default_sheets_dir, os.R_OK):
die('The DEFAULT_CHEAT_DIR (' + default_sheets_dir +') is not readable.')
if not os.access(default_sheets_dir, os.W_OK):
die('The DEFAULT_CHEAT_DIR (' + default_sheets_dir +') is not writeable.')
# return the default dir
return default_sheets_dir
def get():
""" Assembles a dictionary of cheatsheets as name => file-path """
# if we've already reached out to the filesystem, just return the result
# from memory
if cheats:
return cheats
# otherwise, scan the filesystem
for cheat_dir in reversed(paths()):
cheats.update(
dict([
(cheat, os.path.join(cheat_dir, cheat))
for cheat in os.listdir(cheat_dir)
if not cheat.startswith('.')
and not cheat.startswith('__')
])
)
return cheats
def paths():
""" Assembles a list of directories containing cheatsheets """
sheet_paths = [
default_path(),
cheatsheets.sheets_dir()[0],
]
# merge the CHEATPATH paths into the sheet_paths
if 'CHEATPATH' in os.environ and os.environ['CHEATPATH']:
for path in os.environ['CHEATPATH'].split(os.pathsep):
if os.path.isdir(path):
sheet_paths.append(path)
if not sheet_paths:
die('The DEFAULT_CHEAT_DIR dir does not exist or the CHEATPATH is not set.')
return sheet_paths
def list():
""" Lists the available cheatsheets """
sheet_list = ''
pad_length = max([len(x) for x in get().keys()]) + 4
for sheet in sorted(get().items()):
sheet_list += sheet[0].ljust(pad_length) + sheet[1] + "\n"
return sheet_list
def search(term):
""" Searches all cheatsheets for the specified term """
result = ''
for cheatsheet in sorted(get().items()):
match = ''
for line in open(cheatsheet[1]):
if term in line:
match += ' ' + line
if not match == '':
result += cheatsheet[0] + ":\n" + match + "\n"
return result