1# -*- coding: utf-8 -*-
2# Copyright 2010-2018, Google Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9#     * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11#     * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15#     * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31"""Creates versioned files and information files of the files.
32
33  When the version is "1.2.3.4", the following command creates these four files.
34    /PATH/TO/file1-1.2.3.4.ext      : Copied from /PATH/TO/file1.ext
35    /PATH/TO/file1-1.2.3.4.info.ext : File information about file1.ext
36    /PATH/TO/file2-1.2.3.4          : Copied from /PATH/TO/file2
37    /PATH/TO/file2-1.2.3.4.info     : File information about file2
38
39  % python versioning_files.py --version_file mozc_version.txt \
40      --configuration Release /PATH/TO/file1.ext /PATH/TO/file2
41
42  If configuration is "Debug" these file names will be
43    "/PATH/TO/file1-Debug-1.2.3.4.ext"
44    "/PATH/TO/file1-Debug-1.2.3.4.info.ext"
45    "/PATH/TO/file2-Debug-1.2.3.4"
46    "/PATH/TO/file2-Debug-1.2.3.4.info"
47"""
48
49__author__ = "horo"
50
51import base64
52import hashlib
53import logging
54import optparse
55import os
56import shutil
57
58from build_tools import mozc_version
59
60
61def _GetSha1Digest(file_path):
62  """Returns the sha1 hash of the file."""
63  sha = hashlib.sha1()
64  with open(file_path, 'rb') as f:
65    data = f.read()
66    sha.update(data)
67  return sha.digest()
68
69
70def _GetBuildId():
71  """Returns the build ID or an empty string."""
72  envvar_list = []
73
74  for envvar in envvar_list:
75    # If the value is empty, it should be skipped.
76    if os.environ.get(envvar):
77      return os.environ[envvar]
78  return ''
79
80
81def _VersioningFile(version_string, is_debug, file_path):
82  """Creates a versioned file and a information file of the file."""
83  build_id = _GetBuildId()
84  file_path_base, ext = os.path.splitext(file_path)
85  if is_debug:
86    file_path_base += '-Debug'
87  new_file_path = '%s-%s%s' % (file_path_base, version_string, ext)
88  shutil.copyfile(file_path, new_file_path)
89  package = os.path.basename(new_file_path)
90  file_size = os.path.getsize(new_file_path)
91  sha1_digest = _GetSha1Digest(new_file_path)
92  sha1_hash = base64.b64encode(sha1_digest)
93  sha1_hash_hex = sha1_digest.encode('hex')
94  with open('%s.info' % new_file_path, 'w') as output:
95    output.write('package\t%s\n' % package)
96    output.write('build_id\t%s\n' % build_id)
97    output.write('version\t%s\n' % version_string)
98    output.write('size\t%s\n' % file_size)
99    output.write('sha1_hash\t%s\n' % sha1_hash)
100    output.write('sha1_hash_hex\t%s\n' % sha1_hash_hex)
101
102
103def VersioningFiles(version_string, is_debug, file_paths):
104  """Creates versioned files and information files of the files."""
105  for file_path in file_paths:
106    _VersioningFile(version_string, is_debug, file_path)
107
108
109def main():
110  """The main function."""
111  parser = optparse.OptionParser()
112  parser.add_option('--version_file', dest='version_file')
113  parser.add_option('--configuration', dest='configuration')
114
115  (options, args) = parser.parse_args()
116  if options.version_file is None:
117    logging.error('--version_file is not specified.')
118    exit(-1)
119  if options.configuration is None:
120    logging.error('--configuration is not specified.')
121    exit(-1)
122  version = mozc_version.MozcVersion(options.version_file)
123  version_string = version.GetVersionString()
124  is_debug = (options.configuration == 'Debug')
125  VersioningFiles(version_string, is_debug, args)
126
127if __name__ == '__main__':
128  main()
129