Skip to content

Commit f381ef1

Browse files
committed
Use version specified in .node-version if exists
It's pretty common to have a file `.node-version` in a repo to specify the version of node to use. This will pick up the version specified in this file and use that as the default when `Config` is initialised.
1 parent 21d6a78 commit f381ef1

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

nodeenv.py

+38-28
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,19 @@
3434
from ConfigParser import SafeConfigParser as ConfigParser
3535
# noinspection PyCompatibility
3636
import urllib2
37+
3738
iteritems = operator.methodcaller('iteritems')
3839
import httplib
40+
3941
IncompleteRead = httplib.IncompleteRead
4042
except ImportError: # pragma: no cover (py3 only)
4143
from configparser import ConfigParser
4244
# noinspection PyUnresolvedReferences
4345
import urllib.request as urllib2
46+
4447
iteritems = operator.methodcaller('items')
4548
import http
49+
4650
IncompleteRead = http.client.IncompleteRead
4751

4852
from pkg_resources import parse_version
@@ -59,6 +63,7 @@
5963

6064
ignore_ssl_certs = False
6165

66+
6267
# ---------------------------------------------------------
6368
# Utils
6469

@@ -69,19 +74,19 @@ def to_utf8(text):
6974
if not text or is_PY3:
7075
return text
7176

72-
try: # unicode or pure ascii
77+
try: # unicode or pure ascii
7378
return text.encode("utf8")
7479
except UnicodeDecodeError:
75-
try: # successful UTF-8 decode means it's pretty sure UTF-8
80+
try: # successful UTF-8 decode means it's pretty sure UTF-8
7681
text.decode("utf8")
7782
return text
7883
except UnicodeDecodeError:
79-
try: # get desperate; and yes, this has a western hemisphere bias
84+
try: # get desperate; and yes, this has a western hemisphere bias
8085
return text.decode("cp1252").encode("utf8")
8186
except UnicodeDecodeError:
8287
pass
8388

84-
return text # return unchanged, hope for the best
89+
return text # return unchanged, hope for the best
8590

8691

8792
class Config(object):
@@ -120,7 +125,7 @@ def _load(cls, configfiles, verbose=False):
120125

121126
for attr, val in iteritems(vars(cls)):
122127
if attr.startswith('_') or not \
123-
ini_file.has_option(section, attr):
128+
ini_file.has_option(section, attr):
124129
continue
125130

126131
if isinstance(val, bool):
@@ -133,6 +138,10 @@ def _load(cls, configfiles, verbose=False):
133138
os.path.basename(configfile), attr, val))
134139
setattr(cls, attr, val)
135140

141+
if os.path.exists(".node-version"):
142+
with open(".node-version", "r") as v_file:
143+
setattr(cls, "node", v_file.readlines(1)[0].strip())
144+
136145
@classmethod
137146
def _dump(cls):
138147
"""
@@ -190,6 +199,7 @@ def emit(self, record):
190199
fs = "%s" if getattr(record, "continued", False) else "%s\n"
191200
self.stream.write(fs % to_utf8(msg))
192201
self.flush()
202+
193203
logging.StreamHandler.emit = emit
194204

195205
# create console handler and set level to debug
@@ -223,11 +233,11 @@ def parse_args(check=True):
223233
parser.add_option(
224234
'-n', '--node', dest='node', metavar='NODE_VER', default=Config.node,
225235
help='The node.js version to use, e.g., '
226-
'--node=0.4.3 will use the node-v0.4.3 '
227-
'to create the new environment. '
228-
'The default is last stable version (`latest`). '
229-
'Use `lts` to use the latest LTS release. '
230-
'Use `system` to use system-wide node.')
236+
'--node=0.4.3 will use the node-v0.4.3 '
237+
'to create the new environment. '
238+
'The default is the version specified in `.node-version` if exists, or last stable version (`latest`). '
239+
'Use `lts` to use the latest LTS release. '
240+
'Use `system` to use system-wide node.')
231241

232242
parser.add_option(
233243
'--mirror',
@@ -238,12 +248,12 @@ def parse_args(check=True):
238248
parser.add_option(
239249
'-j', '--jobs', dest='jobs', default=Config.jobs,
240250
help='Sets number of parallel commands at node.js compilation. '
241-
'The default is 2 jobs.')
251+
'The default is 2 jobs.')
242252

243253
parser.add_option(
244254
'--load-average', dest='load_average',
245255
help='Sets maximum load average for executing parallel commands '
246-
'at node.js compilation.')
256+
'at node.js compilation.')
247257

248258
parser.add_option(
249259
'--without-ssl', dest='without_ssl',
@@ -284,7 +294,7 @@ def parse_args(check=True):
284294
parser.add_option(
285295
'-C', '--config-file', dest='config_file', default=None,
286296
help="Load a different file than '~/.nodeenvrc'. "
287-
"Pass an empty string for no config (use built-in defaults).")
297+
"Pass an empty string for no config (use built-in defaults).")
288298

289299
parser.add_option(
290300
'-r', '--requirements',
@@ -309,16 +319,16 @@ def parse_args(check=True):
309319
'--with-npm', dest='with_npm',
310320
action='store_true', default=Config.with_npm,
311321
help='Build without installing npm into the new virtual environment. '
312-
'Required for node.js < 0.6.3. By default, the npm included with '
313-
'node.js is used. Under Windows, this defaults to true.')
322+
'Required for node.js < 0.6.3. By default, the npm included with '
323+
'node.js is used. Under Windows, this defaults to true.')
314324

315325
parser.add_option(
316326
'--npm', dest='npm',
317327
metavar='NPM_VER', default=Config.npm,
318328
help='The npm version to use, e.g., '
319-
'--npm=0.3.18 will use the npm-0.3.18.tgz '
320-
'tarball to install. '
321-
'The default is last available version (`latest`).')
329+
'--npm=0.3.18 will use the npm-0.3.18.tgz '
330+
'tarball to install. '
331+
'The default is last available version (`latest`).')
322332

323333
parser.add_option(
324334
'--no-npm-clean', dest='no_npm_clean',
@@ -516,21 +526,21 @@ def is_x86_64_musl():
516526

517527
def get_node_bin_url(version):
518528
archmap = {
519-
'x86': 'x86', # Windows Vista 32
520-
'i686': 'x86',
529+
'x86': 'x86', # Windows Vista 32
530+
'i686': 'x86',
521531
'x86_64': 'x64', # Linux Ubuntu 64
522-
'amd64': 'x64', # FreeBSD 64bits
523-
'AMD64': 'x64', # Windows Server 2012 R2 (x64)
524-
'armv6l': 'armv6l', # arm
532+
'amd64': 'x64', # FreeBSD 64bits
533+
'AMD64': 'x64', # Windows Server 2012 R2 (x64)
534+
'armv6l': 'armv6l', # arm
525535
'armv7l': 'armv7l',
526536
'armv8l': 'armv7l',
527537
'aarch64': 'arm64',
528538
'arm64': 'arm64',
529539
'arm64/v8': 'arm64',
530540
'armv8': 'arm64',
531541
'armv8.4': 'arm64',
532-
'ppc64le': 'ppc64le', # Power PC
533-
's390x': 's390x', # IBM S390x
542+
'ppc64le': 'ppc64le', # Power PC
543+
's390x': 's390x', # IBM S390x
534544
}
535545
sysinfo = {
536546
'system': platform.system().lower(),
@@ -585,8 +595,8 @@ def download_node_src(node_url, src_dir, opt):
585595

586596
with ctx as archive:
587597
node_ver = re.escape(opt.node)
588-
rexp_string = r"node-v%s[^/]*/(README\.md|CHANGELOG\.md|LICENSE)"\
589-
% node_ver
598+
rexp_string = r"node-v%s[^/]*/(README\.md|CHANGELOG\.md|LICENSE)" \
599+
% node_ver
590600
extract_list = [
591601
member
592602
for member in members(archive)
@@ -605,6 +615,7 @@ def urlopen(url):
605615
return urllib2.urlopen(req, context=context)
606616
return urllib2.urlopen(req)
607617

618+
608619
# ---------------------------------------------------------
609620
# Virtual environment functions
610621

@@ -1334,7 +1345,6 @@ def main():
13341345
fi
13351346
"""
13361347

1337-
13381348
ACTIVATE_FISH = """
13391349
13401350
# This file must be used with "source bin/activate.fish" *from fish*

0 commit comments

Comments
 (0)