summaryrefslogtreecommitdiff
path: root/scripts/make_helpdb
blob: f8a91e3348ce59277a1331e5295dbfce446d67d0 (plain) (blame)
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
133
134
#!/usr/bin/env python
#
# Make the help & apropos databases for an IRAF package (adapted from Ureka).
#
# Usage: make_helpdb pkgname
# - The package should already have been defined in extern.pkg, otherwise
#   IRAF can't find it.
#
# Although the generated database files contain some hard-wired paths, in
# practice the databases can be moved and still work without modification,
# so they only need generating once, at compile time.

import sys, os, os.path

# Get the package name argument:
if len(sys.argv) == 2:
    pkgname = sys.argv[1]
else:
    sys.exit('Usage: make_helpdb pkgname')

# Before importing iraf, we must be running in an IRAF home directory with a
# login.cl etc. The user may not have one already, so unless the current
# directory has a login.cl, we create a temporary IRAF directory here. This
# script will be most efficient if run from an existing IRAF directory, to
# avoid setting one up for each invocation/pkg here, but this only adds ~3s.
if not os.access('login.cl', os.R_OK):
    import tempfile, shutil, subprocess, atexit
    tmpdir = tempfile.mkdtemp(prefix='tmp_iraf_')       # makes a dir in /tmp
    def cleantmp():
        # print("Deleting "+tmpdir)
        shutil.rmtree(tmpdir)
    atexit.register(cleantmp)               # clean up temp dir automatically
    os.chdir(tmpdir)
    subprocess.call('echo xterm | mkiraf > /dev/null', shell=True)    # setup
    os.mkdir('pyraf') # else pyraf takes time to generate this & outputs text

# Functions we need for working with IRAF tasks & definitions:
os.environ['PYRAF_NO_DISPLAY'] = 'please'   # Avoid errors about X display
import pyraf.iraffunctions 
import stsci.tools.fileutil
import stsci.tools.irafglobals as irafglobals

# This is how you load iraf packages.
from pyraf import iraf
from iraf import softools


# Find the real directory name where a package lives (this will find
# sub-packages only once the parent package has been loaded into the current
# Python session, eg. with iraf.<pkgname>.run(), but for building help we
# are normally only concerned with top-level packages that are already
# defined when PyRAF/IRAF is started).
#
def pkgname_to_dir(pkgname) :

    # treat iraf$lib as a special case because it doesn't show up in
    # PyRAF's list of packages but its help still needs compiling
    if pkgname == 'iraf':
        s = 'iraf$'
        
    else:
        # here is the object that describes the package
        p =  pyraf.iraffunctions._pkgs[pkgname]

        # get the full package name if IRAF matched an abbreviation
        pkgname = p.getName()

        # here is the name of the .cl file
        s = p.getFilename()

    # turn the iraf-like filename into a normal filename
    # (if it is already a normal filename, it comes through unchanged)
    s = stsci.tools.fileutil.osfn(s)

    # get just the directory part
    s = os.path.dirname(s)

    # these are the full package name & directory where the package is
    return pkgname, s


# We could either 1. make a central apropos database (eg. per variant) or 2.
# make one database per package and set the aproposdb environment variable
# to a list of the individual databases, the same as for helpdb. Pick the
# latter for consistency with helpdb and so we only have to generate each
# database when compiling the corresponding package, rather than regenerating
# a common database with ur-normalize whenever something changes. We then
# append each package's path to aproposdb in extern.pkg, as is standard for
# the help.
def mkapropos(pkgname):

    print("\nMake apropos.db for package %s" % pkgname)

    # Call the STSDAS version of the IRAF mkapropos task (now moved into
    # softools in Ureka's core IRAF) to make the database for this package:
    try:
        iraf.softools.mkapropos(pkglist=pkgname, helpdir='lib/root.hd', \
                                aproposdb=pkgname+'$lib/apropos.db', verbose=0)
    except irafglobals.IrafError:
        sys.exit('make_helpdb: error running mkapropos for '+pkgname)


# Use IRAF to compile the help database for this package:
def mkhelpdb(pkgname) :

    print("\nMake helpdb for package %s" % pkgname)

    # Call mkhelpdb from IRAF's softools:
    try:
        iraf.softools.mkhelpdb(helpdir = pkgname+'$lib/root.hd',
                               helpdb = pkgname + '$lib/helpdb.mip', verbose=0)
    except irafglobals.IrafError:
        sys.exit('make_helpdb: error running mkhelpdb for '+pkgname)



# Check that the pkg$lib directory is actually defined/accessible:
try:
    pkgname, pkgdir = pkgname_to_dir(pkgname)
except:
    sys.exit('make_helpdb: error: path to package %s not found/defined' \
             % pkgname)

pkgdir = pkgdir + '/lib'

if not os.access(pkgdir, os.W_OK):
    sys.exit('make_helpdb: error: path %s not found or isn\'t writeable' \
             % pkgdir)


# Call IRAF to build the databases:
mkhelpdb(pkgname)
mkapropos(pkgname)