use of aQute.bnd.service.repository.ResourceRepository in project bnd by bndtools.
the class RepoCommand method _copy.
public void _copy(CopyOptions options) throws Exception {
List<String> args = options._arguments();
String srcName = args.remove(0);
String dstName = args.remove(0);
RepositoryPlugin source = workspace.getRepository(srcName);
RepositoryPlugin dest = workspace.getRepository(dstName);
if (source == null) {
bnd.error("No such source repository: %s, available repos %s", srcName, workspace.getRepositories());
}
if (dest == null) {
bnd.error("No such destination repository: %s, available repos %s", dstName, workspace.getRepositories());
} else if (!dest.canWrite())
bnd.error("Destination repository cannot write: %s", dest);
if (!bnd.isOk() || source == null || dest == null) {
return;
}
logger.debug("src = {} -> {}", srcName, source);
logger.debug("dst = {} -> {}", dstName, dest);
@SuppressWarnings("unused")
class Spec {
DownloadBlocker src;
DownloadBlocker dst;
String bsn;
Version version;
public byte[] digest;
}
List<Spec> sources = new ArrayList<Spec>();
for (String bsn : source.list(null)) {
for (Version version : source.versions(bsn)) {
logger.debug("src: {} {}", bsn, version);
Spec spec = new Spec();
spec.bsn = bsn;
spec.version = version;
spec.src = new DownloadBlocker(bnd);
File src = source.get(bsn, version, null, spec.src);
if (src == null) {
bnd.error("No such entry: %s-%s", bsn, version);
} else {
spec.dst = findMatchingVersion(dest, bsn, version);
sources.add(spec);
}
}
}
for (Spec spec : sources) {
String reason = spec.src.getReason();
if (reason != null) {
bnd.error("Failed to find %s because: %s", spec.src.getFile(), reason);
}
File src = spec.src.getFile();
if (!src.isFile()) {
bnd.error("Not a valid file %s", spec.src.getFile());
}
spec.digest = SHA1.digest(src).digest();
}
//
// See if we can prune the list by diffing
//
ResourceRepository resources = null;
if (dest instanceof ResourceRepository)
resources = (ResourceRepository) dest;
nextFile: for (Iterator<Spec> i = sources.iterator(); i.hasNext(); ) {
Spec spec = i.next();
if (resources != null) {
ResourceDescriptor rd = resources.getResourceDescriptor(spec.digest);
if (rd != null)
// Already exists
continue nextFile;
}
// TODO Diff
}
if (!bnd.isOk())
return;
for (Spec spec : sources) {
File src = spec.src.getFile();
if (!options.dry()) {
try (InputStream fin = IO.stream(src)) {
PutResult put = dest.put(fin, null);
if (put.digest != null) {
if (!Arrays.equals(spec.digest, put.digest)) {
bnd.error("Digest error in upload %s", src);
}
}
} catch (Exception e) {
bnd.exception(e, "Exception %s in upload %s", e, src);
}
}
}
for (String bsn : source.list(null)) {
for (Version version : source.versions(bsn)) {
System.out.println(bsn + ";version=" + version);
}
}
}
Aggregations