~ubuntu-branches/ubuntu/quantal/enigmail/quantal-security

« back to all changes in this revision

Viewing changes to config/nsinstall.py

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2013-09-13 16:02:15 UTC
  • mfrom: (0.12.16)
  • Revision ID: package-import@ubuntu.com-20130913160215-u3g8nmwa0pdwagwc
Tags: 2:1.5.2-0ubuntu0.12.10.1
* New upstream release v1.5.2 for Thunderbird 24

* Build enigmail using a stripped down Thunderbird 17 build system, as it's
  now quite difficult to build the way we were doing previously, with the
  latest Firefox build system
* Add debian/patches/no_libxpcom.patch - Don't link against libxpcom, as it
  doesn't exist anymore (but exists in the build system)
* Add debian/patches/use_sdk.patch - Use the SDK version of xpt.py and
  friends
* Drop debian/patches/ipc-pipe_rename.diff (not needed anymore)
* Drop debian/patches/makefile_depth.diff (not needed anymore)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# This Source Code Form is subject to the terms of the Mozilla Public
2
 
# License, v. 2.0. If a copy of the MPL was not distributed with this
3
 
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
 
 
5
 
# This is a partial python port of nsinstall.
6
 
# It's intended to be used when there's no natively compile nsinstall
7
 
# available, and doesn't intend to be fully equivalent.
8
 
# Its major use is for l10n repackaging on systems that don't have
9
 
# a full build environment set up.
10
 
# The basic limitation is, it doesn't even try to link and ignores
11
 
# all related options.
12
 
 
13
 
from optparse import OptionParser
14
 
import os
15
 
import os.path
16
 
import sys
17
 
import shutil
18
 
import stat
19
 
 
20
 
def _nsinstall_internal(argv):
21
 
  usage = "usage: %prog [options] arg1 [arg2 ...] target-directory"
22
 
  p = OptionParser(usage=usage)
23
 
 
24
 
  p.add_option('-D', action="store_true",
25
 
               help="Create a single directory only")
26
 
  p.add_option('-t', action="store_true",
27
 
               help="Preserve time stamp")
28
 
  p.add_option('-m', action="store",
29
 
               help="Set mode", metavar="mode")
30
 
  p.add_option('-d', action="store_true",
31
 
               help="Create directories in target")
32
 
  p.add_option('-R', action="store_true",
33
 
               help="Use relative symbolic links (ignored)")
34
 
  p.add_option('-l', action="store_true",
35
 
               help="Create link (ignored)")
36
 
  p.add_option('-L', action="store", metavar="linkprefix",
37
 
               help="Link prefix (ignored)")
38
 
  p.add_option('-X', action="append", metavar="file",
39
 
               help="Ignore a file when installing a directory recursively.")
40
 
 
41
 
  # The remaining arguments are not used in our tree, thus they're not
42
 
  # implented.
43
 
  def BadArg(option, opt, value, parser):
44
 
    parser.error('option not supported: %s' % opt)
45
 
    
46
 
  p.add_option('-C', action="callback", metavar="CWD",
47
 
               callback=BadArg,
48
 
               help="NOT SUPPORTED")
49
 
  p.add_option('-o', action="callback", callback=BadArg,
50
 
               help="Set owner (NOT SUPPORTED)", metavar="owner")
51
 
  p.add_option('-g', action="callback", callback=BadArg,
52
 
               help="Set group (NOT SUPPORTED)", metavar="group")
53
 
 
54
 
  (options, args) = p.parse_args(argv)
55
 
 
56
 
  if options.m:
57
 
    # mode is specified
58
 
    try:
59
 
      options.m = int(options.m, 8)
60
 
    except:
61
 
      sys.stderr.write('nsinstall: ' + options.m + ' is not a valid mode\n')
62
 
      return 1
63
 
 
64
 
  # just create one directory?
65
 
  def maybe_create_dir(dir, mode, try_again):
66
 
    dir = os.path.abspath(dir)
67
 
    if os.path.exists(dir):
68
 
      if not os.path.isdir(dir):
69
 
        print >> sys.stderr, ('nsinstall: %s is not a directory' % dir)
70
 
        return 1
71
 
      if mode:
72
 
        os.chmod(dir, mode)
73
 
      return 0
74
 
 
75
 
    try:
76
 
      if mode:
77
 
        os.makedirs(dir, mode)
78
 
      else:
79
 
        os.makedirs(dir)
80
 
    except Exception, e:
81
 
      # We might have hit EEXIST due to a race condition (see bug 463411) -- try again once
82
 
      if try_again:
83
 
        return maybe_create_dir(dir, mode, False)
84
 
      print >> sys.stderr, ("nsinstall: failed to create directory %s: %s" % (dir, e))
85
 
      return 1
86
 
    else:
87
 
      return 0
88
 
 
89
 
  if options.X:
90
 
    options.X = [os.path.abspath(p) for p in options.X]
91
 
 
92
 
  if options.D:
93
 
    return maybe_create_dir(args[0], options.m, True)
94
 
 
95
 
  # nsinstall arg1 [...] directory
96
 
  if len(args) < 2:
97
 
    p.error('not enough arguments')
98
 
 
99
 
  def copy_all_entries(entries, target):
100
 
    for e in entries:
101
 
      e = os.path.abspath(e)
102
 
      if options.X and e in options.X:
103
 
        continue
104
 
 
105
 
      dest = os.path.join(target, os.path.basename(e))
106
 
      dest = os.path.abspath(dest)
107
 
      handleTarget(e, dest)
108
 
      if options.m:
109
 
        os.chmod(dest, options.m)
110
 
 
111
 
  # set up handler
112
 
  if options.d:
113
 
    # we're supposed to create directories
114
 
    def handleTarget(srcpath, targetpath):
115
 
      # target directory was already created, just use mkdir
116
 
      os.mkdir(targetpath)
117
 
  else:
118
 
    # we're supposed to copy files
119
 
    def handleTarget(srcpath, targetpath):
120
 
      if os.path.isdir(srcpath):
121
 
        if not os.path.exists(targetpath):
122
 
          os.mkdir(targetpath)
123
 
        entries = [os.path.join(srcpath, e) for e in os.listdir(srcpath)]
124
 
        copy_all_entries(entries, targetpath)
125
 
        # options.t is not relevant for directories
126
 
        if options.m:
127
 
          os.chmod(targetpath, options.m)
128
 
      else:
129
 
        if os.path.exists(targetpath):
130
 
          # On Windows, read-only files can't be deleted
131
 
          os.chmod(targetpath, stat.S_IWUSR)
132
 
          os.remove(targetpath)
133
 
        if options.t:
134
 
          shutil.copy2(srcpath, targetpath)
135
 
        else:
136
 
          shutil.copy(srcpath, targetpath)
137
 
 
138
 
  # the last argument is the target directory
139
 
  target = args.pop()
140
 
  # ensure target directory (importantly, we do not apply a mode to the directory
141
 
  # because we want to copy files into it and the mode might be read-only)
142
 
  rv = maybe_create_dir(target, None, True)
143
 
  if rv != 0:
144
 
    return rv
145
 
 
146
 
  copy_all_entries(args, target)
147
 
  return 0
148
 
 
149
 
# nsinstall as a native command is always UTF-8
150
 
def nsinstall(argv):
151
 
  return _nsinstall_internal([unicode(arg, "utf-8") for arg in argv])
152
 
 
153
 
if __name__ == '__main__':
154
 
  # sys.argv corrupts characters outside the system code page on Windows
155
 
  # <http://bugs.python.org/issue2128>. Use ctypes instead. This is also
156
 
  # useful because switching to Unicode strings makes python use the wide
157
 
  # Windows APIs, which is what we want here since the wide APIs normally do a
158
 
  # better job at handling long paths and such.
159
 
  if sys.platform == "win32":
160
 
    import ctypes
161
 
    from ctypes import wintypes
162
 
    GetCommandLine = ctypes.windll.kernel32.GetCommandLineW
163
 
    GetCommandLine.argtypes = []
164
 
    GetCommandLine.restype = wintypes.LPWSTR
165
 
 
166
 
    CommandLineToArgv = ctypes.windll.shell32.CommandLineToArgvW
167
 
    CommandLineToArgv.argtypes = [wintypes.LPWSTR, ctypes.POINTER(ctypes.c_int)]
168
 
    CommandLineToArgv.restype = ctypes.POINTER(wintypes.LPWSTR)
169
 
 
170
 
    argc = ctypes.c_int(0)
171
 
    argv_arr = CommandLineToArgv(GetCommandLine(), ctypes.byref(argc))
172
 
    # The first argv will be "python", the second will be the .py file
173
 
    argv = argv_arr[1:argc.value]
174
 
  else:
175
 
    # For consistency, do it on Unix as well
176
 
    if sys.stdin.encoding is not None:
177
 
      argv = [unicode(arg, sys.stdin.encoding) for arg in sys.argv]
178
 
    else:
179
 
      argv = [unicode(arg) for arg in sys.argv]
180
 
 
181
 
  sys.exit(_nsinstall_internal(argv[1:]))