The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / tools / install.py @ de32b389

History | View | Annotate | Download (4.81 KB)

1
#!/usr/bin/env python
2

    
3
import errno
4
import json
5
import os
6
import re
7
import shutil
8
import sys
9

    
10
# set at init time
11
dst_dir = None
12
node_prefix = None # dst_dir without DESTDIR prefix
13
target_defaults = None
14
variables = None
15

    
16
def abspath(*args):
17
  path = os.path.join(*args)
18
  return os.path.abspath(path)
19

    
20
def load_config():
21
  s = open('config.gypi').read()
22
  s = re.sub(r'#.*?\n', '', s) # strip comments
23
  s = re.sub(r'\'', '"', s) # convert quotes
24
  return json.loads(s)
25

    
26
def try_unlink(path):
27
  try:
28
    os.unlink(path)
29
  except OSError, e:
30
    if e.errno != errno.ENOENT: raise
31

    
32
def try_symlink(source_path, link_path):
33
  print 'symlinking %s -> %s' % (source_path, link_path)
34
  try_unlink(link_path)
35
  os.symlink(source_path, link_path)
36

    
37
def try_mkdir_r(path):
38
  try:
39
    os.makedirs(path)
40
  except OSError, e:
41
    if e.errno != errno.EEXIST: raise
42

    
43
def try_rmdir_r(path):
44
  path = abspath(path)
45
  while path.startswith(dst_dir):
46
    try:
47
      os.rmdir(path)
48
    except OSError, e:
49
      if e.errno == errno.ENOTEMPTY: return
50
      if e.errno == errno.ENOENT: return
51
      raise
52
    path = abspath(path, '..')
53

    
54
def mkpaths(path, dst):
55
  if dst.endswith('/'):
56
    target_path = abspath(dst_dir, dst, os.path.basename(path))
57
  else:
58
    target_path = abspath(dst_dir, dst)
59
  return path, target_path
60

    
61
def try_copy(path, dst):
62
  source_path, target_path = mkpaths(path, dst)
63
  print 'installing %s' % target_path
64
  try_mkdir_r(os.path.dirname(target_path))
65
  return shutil.copy2(source_path, target_path)
66

    
67
def try_remove(path, dst):
68
  source_path, target_path = mkpaths(path, dst)
69
  print 'removing %s' % target_path
70
  try_unlink(target_path)
71
  try_rmdir_r(os.path.dirname(target_path))
72

    
73
def install(paths, dst): map(lambda path: try_copy(path, dst), paths)
74
def uninstall(paths, dst): map(lambda path: try_remove(path, dst), paths)
75

    
76
def update_shebang(path, shebang):
77
  print 'updating shebang of %s' % path
78
  s = open(path, 'r').read()
79
  s = re.sub(r'#!.*\n', '#!' + shebang + '\n', s)
80
  open(path, 'w').write(s)
81

    
82
def npm_files(action):
83
  target_path = 'lib/node_modules/npm/'
84

    
85
  # don't install npm if the target path is a symlink, it probably means
86
  # that a dev version of npm is installed there
87
  if os.path.islink(abspath(dst_dir, target_path)): return
88

    
89
  # npm has a *lot* of files and it'd be a pain to maintain a fixed list here
90
  # so we walk its source directory instead...
91
  for dirname, subdirs, basenames in os.walk('deps/npm', topdown=True):
92
    subdirs[:] = filter('test'.__ne__, subdirs) # skip test suites
93
    paths = [os.path.join(dirname, basename) for basename in basenames]
94
    action(paths, target_path + dirname[9:] + '/')
95

    
96
  # create/remove symlink
97
  link_path = abspath(dst_dir, 'bin/npm')
98
  if action == uninstall:
99
    action([link_path], 'bin/npm')
100
  elif action == install:
101
    try_symlink('../lib/node_modules/npm/bin/npm-cli.js', link_path)
102
    update_shebang(link_path, node_prefix + '/bin/node')
103
  else:
104
    assert(0) # unhandled action type
105

    
106
def files(action):
107
  action(['deps/uv/include/uv.h',
108
          'deps/v8/include/v8-debug.h',
109
          'deps/v8/include/v8-preparser.h',
110
          'deps/v8/include/v8-profiler.h',
111
          'deps/v8/include/v8-testing.h',
112
          'deps/v8/include/v8.h',
113
          'deps/v8/include/v8stdint.h',
114
          'src/eio-emul.h',
115
          'src/ev-emul.h',
116
          'src/node.h',
117
          'src/node_buffer.h',
118
          'src/node_object_wrap.h',
119
          'src/node_version.h'],
120
          'include/node/')
121
  action(['deps/uv/include/uv-private/eio.h',
122
          'deps/uv/include/uv-private/ev.h',
123
          'deps/uv/include/uv-private/ngx-queue.h',
124
          'deps/uv/include/uv-private/tree.h',
125
          'deps/uv/include/uv-private/uv-unix.h',
126
          'deps/uv/include/uv-private/uv-win.h'],
127
          'include/node/uv-private/')
128
  action(['doc/node.1'], 'share/man/man1/')
129
  action(['out/Release/node'], 'bin/node')
130

    
131
  # install unconditionally, checking if the platform supports dtrace doesn't
132
  # work when cross-compiling and besides, there's at least one linux flavor
133
  # with dtrace support now (oracle's "unbreakable" linux)
134
  action(['src/node.d'], 'lib/dtrace/')
135

    
136
  if variables.get('node_install_npm'): npm_files(action)
137

    
138
def run(args):
139
  global dst_dir, node_prefix, target_defaults, variables
140

    
141
  # chdir to the project's top-level directory
142
  os.chdir(abspath(os.path.dirname(__file__), '..'))
143

    
144
  conf = load_config()
145
  variables = conf['variables']
146
  target_defaults = conf['target_defaults']
147

    
148
  # argv[2] is a custom install prefix for packagers (think DESTDIR)
149
  dst_dir = node_prefix = variables.get('node_prefix', '/usr/local')
150
  if len(args) > 2: dst_dir = abspath(args[2] + '/' + dst_dir)
151

    
152
  cmd = args[1] if len(args) > 1 else 'install'
153
  if cmd == 'install': return files(install)
154
  if cmd == 'uninstall': return files(uninstall)
155
  raise RuntimeError('Bad command: %s\n' % cmd)
156

    
157
if __name__ == '__main__':
158
  run(sys.argv[:])