aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/java/maven/tensorflow-android/update.py
blob: c620564072cb6b5f35415e7c5844bddcdd78cdc7 (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
#  Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
#  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.
"""Fetch android artifacts and update pom properties."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import json
import string
import sys
import urllib2


def get_args():
  """Parse command line args."""
  parser = argparse.ArgumentParser()
  parser.add_argument(
      '--version', required=True, help='Version for the artifact.')
  parser.add_argument(
      '--dir',
      required=True,
      help='Directory where the pom and aar artifact will be written.')
  parser.add_argument(
      '--template', required=True, help='Path to pom template file.')
  return parser.parse_args()


def get_json(url):
  """Load the contents of the URL as a json object."""
  return json.load(urllib2.urlopen(url))


def get_commit_id(build_info):
  """Fetch the git commit id from the build info json object."""
  release_commit_id = build_info.get('build_commit_id')
  if release_commit_id:
    return release_commit_id
  actions = build_info.get('actions')
  build_data = next(
      a for a in actions
      if a.get('_class') == 'hudson.plugins.git.util.BuildData')
  if not build_data:
    raise ValueError('Missing BuildData: %s' % build_info)
  revision_info = build_data.get('lastBuiltRevision')
  if not revision_info:
    raise ValueError('Missing lastBuiltRevision: %s' % build_info)
  return revision_info.get('SHA1')


def get_aar_url(build_info):
  """Given the json build info, find the URL to the tensorflow.aar artifact."""
  base_url = build_info.get('url')
  if not base_url:
    raise ValueError('Missing url: %s' % build_info)
  build_class = build_info.get('_class')
  if (build_class == 'hudson.model.FreeStyleBuild' or
      build_class == 'hudson.matrix.MatrixRun'):
    aar_info = next(
        a for a in build_info.get('artifacts')
        if a.get('fileName') == 'tensorflow.aar')
    if not aar_info:
      raise ValueError('Missing aar artifact: %s' % build_info)
    return '%s/artifact/%s' % (base_url, aar_info.get('relativePath'))

  raise ValueError('Unknown build_type %s' % build_info)


def read_template(path):
  with open(path) as f:
    return string.Template(f.read())


def main():
  args = get_args()

  release_prefix = 'https://storage.googleapis.com/tensorflow/libtensorflow'
  info_url = '%s/android_buildinfo-%s.json' % (release_prefix, args.version)
  aar_url = '%s/tensorflow-%s.aar' % (release_prefix, args.version)
  build_type = 'release-android'

  # Retrieve build information
  build_info = get_json(info_url)

  # Check all required build info is present
  build_commit_id = get_commit_id(build_info)
  if not build_commit_id:
    raise ValueError('Missing commit id: %s' % build_info)

  # Write the pom file updated with build attributes.
  template = read_template(args.template)
  with open('%s/pom-android.xml' % args.dir, 'w') as f:
    f.write(
        template.substitute({
            'build_commit_id': build_commit_id,
            'build_type': build_type,
            'version': args.version
        }))

  # Retrieve the aar location if needed.
  if not aar_url:
    aar_url = get_aar_url(build_info)

  # And download the aar to the desired location.
  with open('%s/tensorflow.aar' % args.dir, 'w') as f:
    aar = urllib2.urlopen(aar_url)
    f.write(aar.read())


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