This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsetup.py
127 lines (101 loc) · 4.26 KB
/
setup.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
#! /usr/bin/env python2
# Copyright (c) 2012 Victor Terron. All rights reserved.
# Institute of Astrophysics of Andalusia, IAA-CSIC
#
# This file is part of LEMON.
#
# LEMON is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import with_statement
""" Just a shadow of what the actual setup.py will look like, this simple
script, when run, creates the IRAF login script (login.cl file) and the user
parameters directory (uparm) in the directory where LEMON is installed. This
directory is automatically detected as it is assumed to be the same in which
this file is located, so it does not depend on where it is executed from.
"""
import os
import os.path
import errno
import shutil
import subprocess
MKIRAF_BIN = "mkiraf"
TERM_TYPE = "xgterm"
LOGIN_FILE = "login.cl"
UPARM_DIR = "uparm"
PYRAF_CACHE = "pyraf"
# The LEMON configuration file
CONFIG_FILENAME = "~/.lemonrc"
CONFIG_PATH = os.path.expanduser(CONFIG_FILENAME)
def mkiraf(path):
"""Create the IRAF login script and the uparm directory.
This function implements a high-level wrapper around IRAF's mkiraf, which
initializes the IRAF login script and the user parameters directory in the
path given as input. Any existing login script or uparm directory is
silently overwritten, although mkiraf makes a backup of the former by
appending '.OLD' to its name. In this manner, the original login script
'login.cl' becomes 'login.cl.OLD'.
The login script that this function creates chooses 'xgterm' as its
terminal type. It is usually the best choice, being a xterm-like terminal
program written specifically to work with IRAF. Note that, although we are
required to choose a terminal type when mkiraf is run, LEMON never needs
to use it.
"""
os.chdir(path)
# Avoid having to answer the "Initialize uparm? (y|n):" question
if os.path.exists(UPARM_DIR):
shutil.rmtree(UPARM_DIR)
# mkiraf reads the terminal type ("Enter terminal type:") from the user,
# not accepting it as a command line argument. Thus, we need to use a pipe
# to send the terminal type to mkiraf's stdin.
args = [MKIRAF_BIN]
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.communicate(input=TERM_TYPE)
if p.returncode:
msg = "execution of %s failed" % MKIRAF_BIN
raise subprocess.CalledProcessError(p.returncode, args)
with open(LOGIN_FILE, "rt") as fd:
for line in fd:
splitted = line.split()
if len(splitted) == 2:
if splitted[0] == "stty":
if splitted[1] == TERM_TYPE:
break
else:
msg = "terminal type wasn't set correctly"
raise ValueError(msg)
else:
msg = "terminal type not defined in %s" % LOGIN_FILE
raise ValueError(msg)
def mkdir_p(path):
"""Create a directory, give no error if it already exists.
This implements the functionality of Unix `mkdir -p`, creating a
directory but without giving any error if it already exists and
making parent directories as needed.
[URL] http://stackoverflow.com/a/600612/184363
"""
try:
os.mkdir(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
if __name__ == "__main__":
lemon_path = os.path.dirname(os.path.realpath(__file__))
print "Setting up IRAF's %s in %s ..." % (LOGIN_FILE, lemon_path),
mkiraf(lemon_path)
print "done."
print "Creating pyraf/ directory for cache...",
pyraf_cache_path = os.path.join(lemon_path, PYRAF_CACHE)
mkdir_p(pyraf_cache_path)
print "done."