This repository has been archived by the owner on May 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
base.py
205 lines (168 loc) · 5.95 KB
/
base.py
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
##############################################################################
#
# Copyright (c) 2008 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""base classes
"""
__docformat__ = 'ReStructuredText'
import logging
import optparse
import os
import shutil
import subprocess
import sys
import stat
import urllib2
import xmlrpclib
LOGGER = logging.Logger('build')
formatter = logging.Formatter('%(levelname)s - %(message)s')
is_win32 = sys.platform == 'win32'
BUILD_SECTION = 'build'
class Command(object):
def __init__(self, cwd=None, captureOutput=True, exitOnError=True):
self.cwd = cwd
self.captureOutput = captureOutput
self.exitOnError = exitOnError
def do(self, cmd):
LOGGER.debug('Command: ' + cmd)
if self.captureOutput:
stdout = stderr = subprocess.PIPE
else:
stdout = stderr = None
p = subprocess.Popen(
cmd, stdout=stdout, stderr=stderr,
shell=True, cwd=self.cwd)
stdout, stderr = p.communicate()
if stdout is None:
stdout = "See output above"
if stderr is None:
stderr = "See output above"
if p.returncode != 0:
LOGGER.error(u'An error occurred while running command: %s' %cmd)
LOGGER.error('Error Output: \n%s' % stderr)
if self.exitOnError:
sys.exit(p.returncode)
else:
raise OSError(p.returncode)
LOGGER.debug('Output: \n%s' % stdout)
return stdout
class Git(object):
def __init__(self, exitOnError=True):
self.cmd = self.commandKlass(exitOnError=exitOnError)
def clone(self, url, folder):
command = 'git clone %s %s' % (url, folder)
return self.cmd.do(command)
def checkout(self, branch):
command = 'git checkout %s' % branch
return self.cmd.do(command)
class SVN(object):
user = None
passwd = None
forceAuth = False
#hook to enable testing
commandKlass = Command
#TODO: spaces in urls+folder names???
def __init__(self, user=None, passwd=None,
forceAuth=False, exitOnError=True):
self.user = user
self.passwd = passwd
self.forceAuth = forceAuth
self.cmd = self.commandKlass(exitOnError=exitOnError)
def _addAuth(self, command):
auth = ''
if self.user:
auth = '--username %s --password %s' % (self.user, self.passwd)
if self.forceAuth:
auth += ' --no-auth-cache'
command = command.replace('##__auth__##', auth)
return command
def info(self, url):
command = 'svn info --non-interactive ##__auth__## --xml %s' % url
command = self._addAuth(command)
return self.cmd.do(command)
def ls(self, url):
command = 'svn ls --non-interactive ##__auth__## --xml %s' % url
command = self._addAuth(command)
return self.cmd.do(command)
def cp(self, fromurl, tourl, comment):
command = 'svn cp --non-interactive ##__auth__## -m "%s" %s %s' %(
comment, fromurl, tourl)
command = self._addAuth(command)
self.cmd.do(command)
def co(self, url, folder):
command = 'svn co --non-interactive ##__auth__## %s %s' % (url, folder)
command = self._addAuth(command)
self.cmd.do(command)
def ci(self, folder, comment):
command = 'svn ci --non-interactive ##__auth__## -m "%s" %s' % (
comment, folder)
command = self._addAuth(command)
self.cmd.do(command)
class PYPI(object):
def __init__(self):
self.proxy = xmlrpclib.ServerProxy('http://pypi.python.org/pypi')
def list_packages(self):
pass
def package_releases(self, package_name, show_hidden=False):
return self.proxy.package_releases(package_name, show_hidden)
def release_urls(self, package_name, version):
pass
def release_data(self, package_name, version):
pass
def search(self, spec, operator=None):
pass
def changelog(self, since):
pass
class URLGetter(object):
def get(self, url):
req = urllib2.Request(url)
return urllib2.urlopen(req).read()
def getInput(prompt, default, useDefaults):
if useDefaults:
return default
defaultStr = ''
if default:
defaultStr = ' [' + default + ']'
value = raw_input(prompt + defaultStr + ': ')
if not value:
return default
return value
def checkRO(function, path, excinfo):
if (function == os.remove
and excinfo[0] == WindowsError
and excinfo[1].winerror == 5):
#Access is denied
#because it's a readonly file
os.chmod(path, stat.S_IWRITE)
os.remove(path)
def rmtree(dirname):
if is_win32:
shutil.rmtree(dirname, ignore_errors=False, onerror=checkRO)
else:
shutil.rmtree(dirname)
parser = optparse.OptionParser()
parser.add_option(
"-q", "--quiet", action="store_true",
dest="quiet", default=False,
help="When specified, no messages are displayed.")
parser.add_option(
"-v", "--verbose", action="store_true",
dest="verbose", default=False,
help="When specified, debug information is displayed.")
parser.add_option(
"-d", "--dryrun", action="store_true",
dest="dryrun", default=False,
help="When specified, no upload is done.")
parser.add_option(
"-s", "--status", action="store_true",
dest="status", default=False,
help="When specified, detailed status is output at the end.")