summaryrefslogtreecommitdiffstats
path: root/o3d/plugin/version_info.py
blob: 23d4accf83feb27441d7a539efbf6b4226fe9798 (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/python2.4
# Copyright 2009 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This script does substitution on a list of files for
# version-specific information relating to the plugin.

import os.path
import re
import sys

script_dir = os.path.join(os.path.dirname(sys.argv[0]))
gflags_dir = os.path.normpath(
  os.path.join(script_dir, '..', 'third_party', 'gflags', 'python'))
sys.path.append(gflags_dir)

import gflags

FLAGS = gflags.FLAGS
gflags.DEFINE_boolean('kill_switch', False,
                      'Generate version numbers for kill switch binary.')

gflags.DEFINE_boolean('description', False,
                      'Print out the plugin description and exit.')

gflags.DEFINE_boolean('version', False,
                      'Print out the plugin version and exit.')

gflags.DEFINE_boolean('commaversion', False,
                      'Print out the plugin version with commas and exit.')

gflags.DEFINE_string('set_name', '',
                     'Sets the plugin name to use.')

gflags.DEFINE_string('set_npapi_filename', '',
                     'Sets the plugin NPAPI filename to use.')

gflags.DEFINE_string('set_npapi_mimetype', '',
                     'Sets the plugin NPAPI mimetype to use.')

gflags.DEFINE_string('set_activex_hostcontrol_clsid', '',
                     'Sets the ActiveX HostControl\'s CLSID to use.')

gflags.DEFINE_string('set_activex_typelib_clsid', '',
                     'Sets the ActiveX TypeLib\'s CLSID to use.')

gflags.DEFINE_string('set_activex_hostcontrol_name', '',
                     'Sets the ActiveX HostControl\'s name to use.')

gflags.DEFINE_string('set_activex_typelib_name', '',
                     'Sets the ActiveX TypeLib\'s name to use.')

def GetDotVersion(version):
  return '%d.%d.%d.%d' % version

def GetCommaVersion(version):
  return '%d,%d,%d,%d' % version

def DoReplace(in_filename, out_filename, replacements):
  '''Replace the version number in the given filename with the replacements.'''
  if not os.path.exists(in_filename):
    raise Exception(r'''Input template file %s doesn't exist.''' % in_filename)
  input_file = open(in_filename, 'r')
  input = input_file.read()
  input_file.close()
  for (source, target) in replacements:
    input = re.sub(source, target, input)

  output_file = open(out_filename, 'wb')
  output_file.write(input)
  output_file.close()

def main(argv):
  try:
    files = FLAGS(argv)  # Parse flags
  except gflags.FlagsError, e:
    print '%s.\nUsage: %s [<options>] [<input_file> <output_file>]\n%s' % \
          (e, sys.argv[0], FLAGS)
    sys.exit(1)

  # Strip off argv[0]
  files = files[1:]

  # Get version string from o3d_version.py
  o3d_version_vars = {}
  if FLAGS.kill_switch:
    execfile(os.path.join(script_dir, '..', 'installer', 'win',
                          'o3d_kill_version.py'), o3d_version_vars)
  else:
    execfile(os.path.join(script_dir, '..', 'installer', 'win',
                          'o3d_version.py'), o3d_version_vars)

  plugin_version = o3d_version_vars['plugin_version']
  sdk_version = o3d_version_vars['sdk_version']

  # This name is used by Javascript to find the plugin therefore it must
  # not change. If you change this you must change the name in
  # samples/o3djs/util.js but be aware, changing the name
  # will break all apps that use o3d on the web.
  O3D_PLUGIN_NAME = FLAGS.set_name
  O3D_PLUGIN_VERSION = GetDotVersion(plugin_version)
  O3D_PLUGIN_VERSION_COMMAS = GetCommaVersion(plugin_version)
  O3D_SDK_VERSION = GetDotVersion(sdk_version)
  O3D_SDK_VERSION_COMMAS = GetCommaVersion(sdk_version)
  O3D_PLUGIN_DESCRIPTION = '%s version:%s' % (O3D_PLUGIN_NAME,
                                              O3D_PLUGIN_VERSION)
  O3D_PLUGIN_NPAPI_FILENAME = FLAGS.set_npapi_filename
  O3D_PLUGIN_NPAPI_MIMETYPE = FLAGS.set_npapi_mimetype
  O3D_PLUGIN_ACTIVEX_HOSTCONTROL_CLSID = FLAGS.set_activex_hostcontrol_clsid
  O3D_PLUGIN_ACTIVEX_TYPELIB_CLSID = FLAGS.set_activex_typelib_clsid
  O3D_PLUGIN_ACTIVEX_HOSTCONTROL_NAME = FLAGS.set_activex_hostcontrol_name
  O3D_PLUGIN_ACTIVEX_TYPELIB_NAME = FLAGS.set_activex_typelib_name

  if FLAGS.description:
    print '%s' % O3D_PLUGIN_DESCRIPTION
    sys.exit(0)

  if FLAGS.version:
    print '%s' % O3D_PLUGIN_VERSION
    sys.exit(0)

  if FLAGS.commaversion:
    print '%s' % O3D_PLUGIN_VERSION_COMMAS
    sys.exit(0)

  plugin_replace_strings = [
      ('@@@PluginName@@@', O3D_PLUGIN_NAME),
      ('@@@ProductVersionCommas@@@', O3D_PLUGIN_VERSION_COMMAS),
      ('@@@ProductVersion@@@', O3D_PLUGIN_VERSION),
      ('@@@PluginDescription@@@', O3D_PLUGIN_DESCRIPTION),
      ('@@@PluginNpapiFilename@@@', O3D_PLUGIN_NPAPI_FILENAME),
      ('@@@PluginNpapiMimeType@@@', O3D_PLUGIN_NPAPI_MIMETYPE),
      ('@@@PluginActiveXHostControlClsid@@@',
           O3D_PLUGIN_ACTIVEX_HOSTCONTROL_CLSID),
      ('@@@PluginActiveXTypeLibClsid@@@', O3D_PLUGIN_ACTIVEX_TYPELIB_CLSID),
      ('@@@PluginActiveXHostControlName@@@',
           O3D_PLUGIN_ACTIVEX_HOSTCONTROL_NAME),
      ('@@@PluginActiveXTypeLibName@@@', O3D_PLUGIN_ACTIVEX_TYPELIB_NAME),
  ]

  if len(files) == 2:
    DoReplace(files[0], files[1], plugin_replace_strings)
  elif len(files) > 0:
    raise Exception(r'You must supply and input and output filename for '
                    r'replacement.')

if __name__ == '__main__':
  main(sys.argv)