def serve(opts): """ Run a light-weight HTTP server hosting previously mirrored resources """ resources = _load(opts.resources, opts.output_dir) opts.output_dir = resources.output_dir # allow resources.yaml to set default output_dir if not os.path.exists(opts.output_dir): sys.stderr.write( "Resources dir '{}' not found. Did you fetch?\n".format( opts.output_dir)) return 1 backend.PyPIResource.build_pypi_indexes(opts.output_dir) os.chdir(opts.output_dir) HTTPServer.allow_reuse_address = True httpd = HTTPServer((opts.host, opts.port), SimpleHTTPRequestHandler) if opts.ssl_cert: httpd.socket = ssl.wrap_socket(httpd.socket, certfile=opts.ssl_cert, server_side=True) print("Serving at: http{}://{}:{}/".format('s' if opts.ssl_cert else '', socket.gethostname(), opts.port)) httpd.serve_forever()
def resource_path(opts): """ Return the full path to a named resource. """ resources = _load(opts.resources, opts.output_dir) if opts.resource_name not in resources: sys.stderr.write('Invalid resource name: {}\n'.format(opts.resource_name)) return 1 print(resources[opts.resource_name].destination)
def resource_spec(opts): """ Return the spec (URL, package spec, file, etc) for a named resource. """ resources = _load(opts.resources, opts.output_dir) if opts.resource_name not in resources: sys.stderr.write('Invalid resource name: {}\n'.format(opts.resource_name)) return 1 print(resources[opts.resource_name].spec)
def resource_spec(opts): """ Return the spec (URL, package spec, file, etc) for a named resource. """ resources = _load(opts.resources, opts.output_dir) if opts.resource_name not in resources: sys.stderr.write('Invalid resource name: {}\n'.format( opts.resource_name)) return 1 print(resources[opts.resource_name].spec)
def resource_path(opts): """ Return the full path to a named resource. """ resources = _load(opts.resources, opts.output_dir) if opts.resource_name not in resources: sys.stderr.write('Invalid resource name: {}\n'.format( opts.resource_name)) return 1 print(resources[opts.resource_name].destination)
def fetch(opts): """ Create a local mirror of one or more resources. """ resources = _load(opts.resources, opts.output_dir) if opts.all: opts.resource_names = ALL reporthook = None if opts.quiet else lambda name: print('Fetching {}...'.format(name)) if opts.verbose: backend.VERBOSE = True _fetch(resources, opts.resource_names, opts.mirror_url, opts.force, reporthook) return verify(opts)
def fetch(opts): """ Create a local mirror of one or more resources. """ resources = _load(opts.resources, opts.output_dir) if opts.all: opts.resource_names = ALL reporthook = None if opts.quiet else lambda name: print('Fetching {}...'. format(name)) if opts.verbose: backend.VERBOSE = True _fetch(resources, opts.resource_names, opts.mirror_url, opts.force, reporthook) return verify(opts)
def verify(opts): """ Verify that one or more resources were downloaded successfully. """ resources = _load(opts.resources, opts.output_dir) if opts.all: opts.resource_names = ALL invalid = _invalid(resources, opts.resource_names) if not invalid: if not opts.quiet: print("All resources successfully downloaded") return 0 else: if not opts.quiet: print("Invalid or missing resources: {}".format(', '.join(invalid))) return 1
def verify(opts): """ Verify that one or more resources were downloaded successfully. """ resources = _load(opts.resources, opts.output_dir) if opts.all: opts.resource_names = ALL invalid = _invalid(resources, opts.resource_names) if not invalid: if not opts.quiet: print("All resources successfully downloaded") return 0 else: if not opts.quiet: print("Invalid or missing resources: {}".format( ', '.join(invalid))) return 1
def install(opts): """ Install one or more resources. """ resources = _load(opts.resources, opts.output_dir) if opts.all: opts.resource_names = ALL success = _install(resources, opts.resource_names, opts.mirror_url, opts.destination, opts.skip_top_level) if success: if not opts.quiet: print("All resources successfully installed") return 0 else: if not opts.quiet: invalid = _invalid(resources, opts.resource_names) print("Unable to install some resources: {}".format(', '.join(invalid))) return 1
def install(opts): """ Install one or more resources. """ resources = _load(opts.resources, opts.output_dir) if opts.all: opts.resource_names = ALL success = _install(resources, opts.resource_names, opts.mirror_url, opts.destination, opts.skip_top_level) if success: if not opts.quiet: print("All resources successfully installed") return 0 else: if not opts.quiet: invalid = _invalid(resources, opts.resource_names) print("Unable to install some resources: {}".format( ', '.join(invalid))) return 1
def serve(opts): """ Run a light-weight HTTP server hosting previously mirrored resources """ resources = _load(opts.resources, opts.output_dir) opts.output_dir = resources.output_dir # allow resources.yaml to set default output_dir if not os.path.exists(opts.output_dir): sys.stderr.write("Resources dir '{}' not found. Did you fetch?\n".format(opts.output_dir)) return 1 backend.PyPIResource.build_pypi_indexes(opts.output_dir) os.chdir(opts.output_dir) HTTPServer.allow_reuse_address = True httpd = HTTPServer((opts.host, opts.port), SimpleHTTPRequestHandler) if opts.ssl_cert: httpd.socket = ssl.wrap_socket(httpd.socket, certfile=opts.ssl_cert, server_side=True) print("Serving at: http{}://{}:{}/".format( 's' if opts.ssl_cert else '', socket.gethostname(), opts.port)) httpd.serve_forever()
def test_load_defaults(self): resources = jujuresources._load(os.path.join(self.test_data, 'res-defaults.yaml')) self.assertItemsEqual(resources._required, set(['foo', 'bar'])) self.assertItemsEqual(resources.keys(), ['foo', 'bar', 'qux']) self.assertEqual(resources['foo'].url, 'http://foo.com/foo.tgz') self.assertEqual(resources['foo'].hash, 'deadbeef1') self.assertEqual(resources['foo'].hash_type, 'nonce') self.assertEqual(resources['foo'].filename, 'foo.tgz') self.assertEqual(resources['foo'].destination, 'resources/foo/foo.tgz') self.assertEqual(resources['bar'].url, 'http://bar.com/bar.tgz') self.assertEqual(resources['bar'].hash, 'deadbeef2') self.assertEqual(resources['bar'].hash_type, 'nonce') self.assertEqual(resources['bar'].filename, 'BAR') self.assertEqual(resources['bar'].destination, 'custom/bar') self.assertEqual(resources['qux'].url, 'http://qux.com/qux.tgz') self.assertEqual(resources['qux'].hash, 'deadbeef3') self.assertEqual(resources['qux'].hash_type, 'nonce') self.assertEqual(resources['qux'].filename, 'qux.tgz') self.assertEqual(resources['qux'].destination, 'resources/qux/qux.tgz')
def test_load_defaults(self): resources = jujuresources._load( os.path.join(self.test_data, 'res-defaults.yaml')) self.assertItemsEqual(resources._required, set(['foo', 'bar'])) self.assertItemsEqual(resources.keys(), ['foo', 'bar', 'qux']) self.assertEqual(resources['foo'].url, 'http://foo.com/foo.tgz') self.assertEqual(resources['foo'].hash, 'deadbeef1') self.assertEqual(resources['foo'].hash_type, 'nonce') self.assertEqual(resources['foo'].filename, 'foo.tgz') self.assertEqual(resources['foo'].destination, 'resources/foo/foo.tgz') self.assertEqual(resources['bar'].url, 'http://bar.com/bar.tgz') self.assertEqual(resources['bar'].hash, 'deadbeef2') self.assertEqual(resources['bar'].hash_type, 'nonce') self.assertEqual(resources['bar'].filename, 'BAR') self.assertEqual(resources['bar'].destination, 'custom/bar') self.assertEqual(resources['qux'].url, 'http://qux.com/qux.tgz') self.assertEqual(resources['qux'].hash, 'deadbeef3') self.assertEqual(resources['qux'].hash_type, 'nonce') self.assertEqual(resources['qux'].filename, 'qux.tgz') self.assertEqual(resources['qux'].destination, 'resources/qux/qux.tgz')
def test_load_options(self): resfile = os.path.join(self.test_data, 'res-options.yaml') resources = jujuresources._load(resfile) self.assertEqual(resources['foo'].destination, 'custom/foo/foo.tgz') resources = jujuresources._load(resfile, 'different') self.assertEqual(resources['foo'].destination, 'different/foo/foo.tgz')