Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0eef3088078de5b23721ac63939661e0f790031a (plain) (blame)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""Copyright 2011 Google Inc. All Rights Reserved.

All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html

Contributors:
  Google, Inc. - initial API and implementation

@author: Mark R Russell
"""


import datetime
import logging
import os
import re
import subprocess
import util

global eclipse_archive_dir
eclipse_archive_dir = None
global eclipse_archive_file
eclipse_archive_file = None
global eclipse_dir
eclipse_dir = None

eclipse_archive_dir = os.path.join(os.sep + 'home', 'data', 'httpd',
                                   'download.eclipse.org', 'eclipse',
                                   'downloads', 'drops')

log = logging.getLogger('releng.eclipse')


def _FindEclipseArchive(eclipse_version):
  global eclipse_archive_file
  log.debug('findEclipseArchive(' + eclipse_version + ')')
  log.info('searching for Eclipse in ' + eclipse_archive_dir)
  if eclipse_archive_file is None:
    try:
      files = os.listdir(eclipse_archive_dir)
    except OSError as e:
      log.error('could not read files in ' + eclipse_archive_dir)
      raise e

    found_eclipse_dirs = []
    for f in files:
      if f.startswith('R-') or f.startswith('S-'):
        log.debug('found: ' + f)
        found_eclipse_dirs.append(f)
    if not found_eclipse_dirs:
      msg = ('could not find any Eclipse directories'
             ' starting with R or S in {0}').format(eclipse_archive_dir)
      log.error(msg)
      raise OSError(msg)

    saved_ts = datetime.datetime(1970, 1, 1)
    search_dir = None
    search_term = '[RS]-([0-9]\.[0-9](\.[0-9]|M[0-9]|RC[0-9])?)-(.+)(.*)'
    search = re.compile(search_term)
    for d in found_eclipse_dirs:
      results = search.search(d)
      util.DisplayMatch(results)
      version = results.group(1)
      timestamp = results.group(3)
      if version.find(eclipse_version) >= 0:
        eclipse_ts = datetime.datetime.strptime(timestamp, '%Y%m%d%H%M')
        if eclipse_ts > saved_ts:
          saved_ts = eclipse_ts
          search_dir = os.path.join(eclipse_archive_dir, d)

    if not search_dir:
      raise Exception('can not find eclipse {0} in {1}'.format(eclipse_version, found_eclipse_dirs))

    log.debug('search_dir = ' + search_dir)
    if search_dir is None:
      msg = 'could not find any Eclipse directories with ' + eclipse_version
      log.error(msg)
      raise OSError(msg)

    try:
      files = os.listdir(search_dir)
    except OSError as e:
      log.error('could not read files in ' + search_dir)
      raise e

    eclipse_archive_file = None
    search_term = eclipse_version + '(M[0-9]|RC[0-9]|\.[0-3])?-linux-gtk.tar.gz'
    search = re.compile(search_term)

    log.debug('search_term = ' + search_term)
    for f in files:
      if search.search(f) is not None:
        eclipse_archive_file = os.path.join(search_dir, f)
        break

    if eclipse_archive_file is None:
      msg = ('could not find any Eclipse archives'
             ' with {0} in {1}').format(search_term, search_dir)
      log.error(msg)
      raise OSError(msg)

  log.info('found Eclipse archive ' + eclipse_archive_file)
  return eclipse_archive_file


def _GetEclipse(version, directory):
  if eclipse_archive_file is None:
    eclipse_archive = _FindEclipseArchive(version)
    util.Unarchive(eclipse_archive, directory)
  return os.path.join(directory, 'eclipse')


def _FindPlugin(eclipse_home, search):
  log.debug('_FindPlugin(' + search + ')')
  plugins = os.path.join(eclipse_home, 'plugins')
  try:
    files = os.listdir(plugins)
  except OSError as e:
    log.error('could not read files in ' + eclipse_archive_dir)
    raise e

  for f in files:
    if f.startswith(search):
      return os.path.join(plugins, f)

  raise OSError('could not find plugin starting with ' + search)


def OptimizeSite(eclipse_install_dir, ziped_update_sites_dir, eclipse_version):
  """Call the Eclipse Site optimizer to optimize the update site.

  Args:
    eclipse_install_dir: directory to unarchive Eclipse if it has not
                          been unarchived yet
    ziped_update_sites_dir: the directory where the zipped update sites
                            are stored
    eclipse_version: the version of eclipse to use for optimizing

  Returns:
    the new directory where the optimized zipfiles were written
  """
  log.debug('optimizeSite({0}, {1}, {2}'.format(eclipse_install_dir,
                                                ziped_update_sites_dir,
                                                eclipse_version))
  eclipse_home = _GetEclipse(eclipse_version, eclipse_install_dir)
  try:
    os.mkdir(eclipse_install_dir)
  except OSError as e:
    if e.errno != 17:
      raise e

  launcher = _FindPlugin(eclipse_home, 'org.eclipse.equinox.p2.jarprocessor_')
  try:
    files = os.listdir(ziped_update_sites_dir)
  except OSError as e:
    log.error('could not read files in ' + ziped_update_sites_dir)
    raise e

  out = os.path.join(ziped_update_sites_dir, 'out')
  for f in files:
    full_file = os.path.join(ziped_update_sites_dir, f)
    commands = ['java', '-jar', launcher, '-processAll', '-repack',
                '-outputDir', out, full_file]
    print ' '.join(commands)

    subprocess.check_call(commands)

  return out


def PackSite(eclipse_install_dir, ziped_update_sites_dir, eclipse_version):
  """Call the Eclipse Site pack to generate p200 files for the update site.

  Args:
    eclipse_install_dir: directory to unarchive Eclipse if it has not
                          been unarchived yet
    ziped_update_sites_dir: the directory where the zipped update sites
                            are stored
    eclipse_version: the version of eclipse to use for optimizing

  Returns:
    the new directory where the p200 zipfiles were written
  """
  log.debug('packSite({0}, {1}, {2}'.format(eclipse_install_dir,
                                            ziped_update_sites_dir,
                                            eclipse_version))
  eclipse_home = _GetEclipse(eclipse_version, eclipse_install_dir)
  try:
    os.mkdir(eclipse_install_dir)
  except OSError as e:
    if e.errno != 17:
      raise e

  launcher = _FindPlugin(eclipse_home, 'org.eclipse.equinox.p2.jarprocessor_')
  try:
    files = os.listdir(ziped_update_sites_dir)
  except OSError as e:
    log.error('could not read files in ' + ziped_update_sites_dir)
    raise e

  out = os.path.join(ziped_update_sites_dir, 'out')
  for f in files:
    full_file = os.path.join(ziped_update_sites_dir, f)
    commands = ['java', '-jar', launcher, '-processAll', '-pack',
                '-outputDir', out, full_file]
    print ' '.join(commands)
    subprocess.check_call(commands)

  return out


def PublishSite(eclipse_install_dir, update_sites_dir, eclipse_version):
  """Call the Eclipse Site metadata generator for the update site.

  Args:
    eclipse_install_dir: directory to unarchive Eclipse if it has not
                          been unarchived yet
    update_sites_dir: the directory where the update sites
                            are stored
    eclipse_version: the version of eclipse to use for optimizing

  Returns:
    the new directory where the updated zipfiles were written
  """
  log.debug('publishSite({0}, {1}, {2}'.format(eclipse_install_dir,
                                               update_sites_dir,
                                               eclipse_version))
  eclipse_home = _GetEclipse(eclipse_version, eclipse_install_dir)

  launcher = _FindPlugin(eclipse_home, 'org.eclipse.equinox.launcher_')

  out = os.path.join(eclipse_install_dir, 'out')
  for eclipse_ver in ['3.4', '3.5', '3.6', '3.7', '3.8']:
    full_file = os.path.join(update_sites_dir, eclipse_ver)
    if os.path.exists(full_file):
      os.remove(os.path.join(full_file, 'artifacts.jar'))
      os.remove(os.path.join(full_file, 'content.jar'))
      target = 'file:' + full_file
      commands = ['java', '-jar', launcher, '-application',
                  'org.eclipse.equinox.p2.publisher.UpdateSitePublisher',
                  '-metadataRepository', target,
                  '-metadataRepositoryName',
                  'Eclipse ' + eclipse_ver + ' WindowBuilder Repo',
                  '-artifactRepository', target,
                  '-artifactRepositoryName',
                  'Eclipse ' + eclipse_ver + ' WindowBuilder Repo',
                  '-source', full_file,
                  '-compress', '-publishArtifacts']
      print ' '.join(commands)
      subprocess.check_call(commands)

  return out



def RunAnt(eclipse_install_dir, ant_script, prod_dir, eclipse_version):
  """Run an ANt script under Eclipse.

  Args:
    eclipse_install_dir: directory to unarchive Eclipse if it has not
                          been unarchived yet
    ant_script: the ANt Script to run
    prod_dir: the location of the update sites
    eclipse_version: the version of eclipse to use for optimizing
  """
  log.debug('RunAnt({0}, {1}, {2}, {3}'.format(eclipse_install_dir,
                                               ant_script,
                                               prod_dir,
                                               eclipse_version))
  eclipse_home = _GetEclipse(eclipse_version, eclipse_install_dir)
  try:
    os.mkdir(eclipse_install_dir)
  except OSError as e:
    if e.errno != 17:
      raise e

  launcher = _FindPlugin(eclipse_home, 'org.eclipse.equinox.launcher_')
  try:
    files = os.listdir(prod_dir)
  except OSError as e:
    log.error('could not read files in ' + prod_dir)
    raise e

  for f in files:
    full_file = os.path.join(prod_dir, f)
    if os.path.isdir(full_file):
      commands = ['java', '-jar', launcher, '-application', 
                  'org.eclipse.ant.core.antRunner', '-f', 
                  ant_script, '-Dbuild.repo.dir=' + full_file,
                  'process-artifacts']
      log.info(' '.join(commands))
      subprocess.check_call(commands)

def SetArchiveDir(d):
  global eclipse_archive_dir
  eclipse_archive_dir = d

Back to the top