~juju-qa/juju-ci-tools/trunk

« back to all changes in this revision

Viewing changes to upload_jenkins_job.py

  • Committer: Seman
  • Date: 2016-05-03 00:01:23 UTC
  • mto: This revision was merged to the branch mainline in revision 1402.
  • Revision ID: seman.said@canonical.com-20160503000123-h24cg4kk8e8tftyk
Upadated upload_jenkins_job to filter artifacts file extension.

Show diffs side-by-side

added added

removed removed

Lines of Context:
175
175
    Uploads the result of a Jenkins job to S3.
176
176
    """
177
177
 
178
 
    def __init__(self, s3, jenkins_build, unique_id=None, no_prefixes=False):
 
178
    def __init__(self, s3, jenkins_build, unique_id=None, no_prefixes=False,
 
179
                 artifact_file_ext=None):
179
180
        self.s3 = s3
180
181
        self.jenkins_build = jenkins_build
181
182
        self.unique_id = unique_id
182
183
        self.no_prefixes = no_prefixes
 
184
        self.artifact_file_ext = artifact_file_ext
183
185
 
184
186
    @classmethod
185
187
    def factory(cls, credentials, jenkins_job, build_number, bucket,
186
 
                directory, unique_id=None, no_prefixes=False):
 
188
                directory, unique_id=None, no_prefixes=False,
 
189
                artifact_file_ext=None):
187
190
        """
188
191
        Creates S3Uploader.
189
192
        :param credentials: Jenkins credential
191
194
        :param build_number: Jenkins build number
192
195
        :param bucket: S3 bucket name
193
196
        :param directory: S3 directory name
 
197
        :param artifact_file_ext: List of artifact file extentions. If set,
 
198
        only artifact with these ejections will be uploaded.
194
199
        :rtype: S3Uploader
195
200
        """
196
201
        s3 = S3.factory(bucket, directory)
199
204
            credentials=credentials, job_name=jenkins_job,
200
205
            build_number=build_number)
201
206
        return cls(s3, jenkins_build,
202
 
                   unique_id=unique_id, no_prefixes=no_prefixes)
 
207
                   unique_id=unique_id, no_prefixes=no_prefixes,
 
208
                   artifact_file_ext=artifact_file_ext)
203
209
 
204
210
    def upload(self):
205
211
        """Uploads Jenkins job results, console logs and artifacts to S3.
280
286
 
281
287
    def upload_artifacts(self):
282
288
        for filename, content in self.jenkins_build.artifacts():
 
289
            if self.artifact_file_ext:
 
290
                if os.path.splitext(filename)[1] not in self.artifact_file_ext:
 
291
                    continue
283
292
            filename = self._create_filename(filename)
284
293
            headers = self.make_headers(filename)
285
294
            self.s3.store(filename, content, headers=headers)
329
338
    parser.add_argument(
330
339
        '--no-prefixes', action='store_true', default=False,
331
340
        help='Do not add prefixes to file names; the s3_directory is unique.')
 
341
    parser.add_argument(
 
342
        '--artifact-file-ext', nargs='+',
 
343
        help='Artifacts include file extentions. If set, only files with '
 
344
             'these extentions will be uploaded.')
332
345
    add_credential_args(parser)
333
346
    args = parser.parse_args(argv)
334
347
    args.all = False
350
363
    uploader = S3Uploader.factory(
351
364
        cred, args.jenkins_job, args.build_number, args.s3_bucket,
352
365
        args.s3_directory, unique_id=args.unique_id,
353
 
        no_prefixes=args.no_prefixes)
 
366
        no_prefixes=args.no_prefixes, artifact_file_ext=args.artifact_file_ext)
354
367
    if args.build_number:
355
368
        print('Uploading build number {:d}.'.format(args.build_number))
356
369
        uploader.upload()