Skip to main content
summaryrefslogtreecommitdiffstats
blob: fc630533b04daea6b796052c028853de8464b5d6 (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
'''
Created on Feb 2, 2012

@author: mrrussell
'''
import logging
import os
import subprocess
import time
import stat
import tempfile
import shutil

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

class SignCode(object):
  '''Class to sign Eclipse update site zips.
  '''
  _sign_method = None


  def __init__(self, signing_method=None):
    """
        Constructor
    """
    if signing_method is None:
      self._sign_method = self._EclipseSign 
    else:
      self._sign_method = signing_method

  def SignZipFiles(self, files_location, selection_method = None):
    """Sign the zip files with Eclipses key.

      The signed files will be in files_location/signed.

    Args:
      files_location: the directory where the ziped update sites are

    Returns:
      the directory where the signed jar file are.
    """

    log.debug('signFiles({0})'.format(files_location))

    signed_location = os.path.join(files_location, 'signed')
    os.makedirs(signed_location)
    signed_files = self._sign_method(self._CollectFiles(files_location,
                                                        selection_method), 
                                     signed_location)
    return signed_location
    
  def _CollectFiles(self, files_location, selection_method=None):
    """Collect the files to process.
    Args:
      files_location: the location of the files to process
      selection_method: the method to select the files
    Returns:
      a list of files to process
    Exception:
      OSError if files_location can not be read
    """
    if selection_method is None:
      selection_method = self._SelectZip
    log.debug('_CollectFiles({0}, selection_method)'.format(files_location))
    try:
      files = os.listdir(files_location)
    except OSError as e:
      log.error('could not read files in ' + files_location)
      raise e

    files_to_sign = []
    for f in files:
      full_file = os.path.join(files_location, f)
      if selection_method(full_file):
        os.chmod(full_file, stat.S_IWRITE | stat.S_IREAD | stat.S_IWGRP |
                 stat.S_IRGRP | stat.S_IWOTH | stat.S_IROTH)
        files_to_sign.append(full_file)
    return files_to_sign

  def _SelectZip(self, file_name):
    """Select a file if it ends with '.zip'
    Args:
      file_name: the name of the file to process
    Returns:
      True if the file_name is a file and it ends in '.zip', otherwise False
    """
    isfile = os.path.isfile(file_name)
    endswithzip = file_name.endswith('.zip')
    return isfile and endswithzip

  def _EclipseSign(self, files_to_sign, sign_location):
    """
    """
    signed_files = []
    log.debug('_EclipseSign({0}, {1}'.format(files_to_sign, sign_location))
    if files_to_sign:
      for zip_path in files_to_sign:
        cmd = ['/usr/local/bin/sign', zip_path, 'now', 'signed']
        if log.debug:
          log.debug(' '.join(cmd))
        subprocess.check_call(cmd)
  
      log.info('all files have been signed')
    else:
      log.warning('no files to process')
    return signed_files

def SelfSign(self, files_to_sign, sign_location):
  """
  """
  base_dir = tempfile.mkdtemp(prefix='unzip-site')
  try:
    
    for zip_file in files_to_sign:
      subprocess.check_call('unzip', )
  finally:
    shutil.rmtree(base_dir)
  return files_to_sign

def MockSign(self, files_to_sign, sign_location):
  """
  """
  for zip_file in files_to_sign:
    shutil.copy2(zip_file, sign_location)
  return files_to_sign

Back to the top