12
from urllib import urlretrieve
13
from urlparse import urlsplit
14
from optparse import OptionParser
16
from zope.component import getUtility
18
from canonical.launchpad.scripts.productreleasefinder.hose import Hose
19
from canonical.launchpad.scripts.productreleasefinder.filter import (
21
from canonical.launchpad.interfaces import IProductSet, IProductReleaseSet
22
from canonical.librarian.interfaces import IFileUploadClient
23
from canonical.launchpad.validators.version import sane_version
13
from canonical.config import config
24
14
from canonical.lp import initZopeless
25
from canonical.config import config
26
15
from canonical.launchpad.scripts import (execute_zcml_for_scripts,
27
16
logger, logger_options)
29
from hct.util import path
17
from canonical.launchpad.scripts.productreleasefinder.finder import (
33
22
# Parse command-line arguments
34
parser = OptionParser()
23
parser = optparse.OptionParser()
35
24
logger_options(parser)
36
(options, args) = parser.parse_args()
25
(options, args) = parser.parse_args(argv[1:])
39
log = logger(options, "productreleasefinder")
27
execute_zcml_for_scripts()
40
28
ztm = initZopeless(dbuser=config.productreleasefinder.dbuser,
41
29
implicitBegin=False)
43
cache = Cache(config.productreleasefinder.cache_path, log_parent=log)
45
for product_name, filters in get_filters(ztm):
46
hose = Hose(filters, cache)
47
for series_name, url in hose:
48
if series_name is not None:
49
new_release(ztm, product_name, series_name, url)
51
log.warning("File in %s found that matched no glob: %s",
57
"""Build the list of products and filters.
59
Returns a list of (product_name, filters) for each product in the database,
60
where the filter keys are series names.
65
products = getUtility(IProductSet)
66
for product in products:
69
for series in product.serieslist:
70
if series.releasefileglob is None or series.releasefileglob == "":
73
releasefileglob = series.releasefileglob
75
if series.releaseroot is None or series.releaseroot == "":
76
if product.releaseroot is None or product.releaseroot == "":
79
releaseroot = product.releaseroot
81
releaseroot = series.releaseroot
83
filters.append(FilterPattern(series.name,
84
releaseroot, releasefileglob))
89
log.info("%s has %d series with information", product.name,
92
todo.append((product.name, filters))
97
def new_release(ztm, product_name, series_name, url):
98
"""Create a new ProductRelease.
100
Downloads the file and creates a new ProductRelease associated with
103
filename = urlsplit(url)[2]
104
slash = filename.rfind("/")
106
filename = filename[slash + 1:]
107
log.debug("Filename portion is %s", filename)
109
version = path.split_version(path.name(filename))[1]
110
log.debug("Version is %s", version)
112
log.error("Unable to parse version from %s", url)
115
if not sane_version(version):
116
log.error("Version number '%s' for '%s' is not sane", version, url)
119
(mimetype, encoding) = mimetypes.guess_type(url)
120
log.debug("Mime Type is %s", mimetype)
122
mimetype = "application/octet-stream"
124
log.debug("Downloading %s", url)
126
(local, headers) = urlretrieve(url)
127
stat = os.stat(local)
129
log.error("Download of %s failed, can't create release")
132
log.error("Unable to stat downloaded file, can't create release")
135
open_file = open(local, "r")
140
product = getUtility(IProductSet)[product_name]
141
series = product.getSeries(series_name)
142
log.info("Creating new release %s for %s %s",
143
version, product.name, series.name)
145
release = getUtility(IProductReleaseSet).\
146
new(version, series, product.owner)
147
log.debug("Created ProductRelease %d", release.id)
149
alias_id = getUtility(IFileUploadClient).\
150
addFile(filename, stat.st_size, open_file, mimetype)
151
log.debug("Created LibraryFileAlias %d", alias_id)
153
release.addFileAlias(alias_id)
31
log = logger(options, "productreleasefinder")
33
prf = ProductReleaseFinder(ztm, log)
162
36
if __name__ == "__main__":
163
execute_zcml_for_scripts()
37
sys.exit(main(sys.argv))