From 831f21f555a1abf7cf2b21160ecaaec63848abd7 Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Wed, 1 Nov 2017 22:14:35 +0200 Subject: [PATCH 1/4] sync: properly collect and check data items Fixes #282 Signed-off-by: Ruslan Kuprieiev --- dvc/command/data_sync.py | 2 +- dvc/data_cloud.py | 66 ++++++++++++++++------------------------ dvc/path/data_item.py | 5 --- dvc/path/factory.py | 4 +-- 4 files changed, 29 insertions(+), 48 deletions(-) diff --git a/dvc/command/data_sync.py b/dvc/command/data_sync.py index 8513b74de..71f060877 100644 --- a/dvc/command/data_sync.py +++ b/dvc/command/data_sync.py @@ -38,7 +38,7 @@ class CmdDataStatus(CmdBase): cloud.STATUS_NEW : 'new file:', } - Logger.info('\t{}\t{}'.format(prefix_map[ret], target.resolved_cache.dvc)) + Logger.info('\t{}\t{}'.format(prefix_map[ret], target.data.dvc)) def run(self): with DvcLock(self.is_locker, self.git): diff --git a/dvc/data_cloud.py b/dvc/data_cloud.py index 25ae03a26..357fa786d 100644 --- a/dvc/data_cloud.py +++ b/dvc/data_cloud.py @@ -166,7 +166,7 @@ class DataCloudBase(object): def pull(self, item): """ Generic method for pulling data from the cloud """ - fname = item.resolved_cache.dvc + fname = item.cache.dvc key_name = self.cache_file_key(fname) return self._import(self.storage_bucket, key_name, item) @@ -183,7 +183,7 @@ class DataCloudBase(object): """ item = self._cloud_settings.path_factory.data_item(fname) - if os.path.isfile(item.resolved_cache.dvc): + if os.path.isfile(item.cache.dvc): return self.push(item) else: return self.pull(item) @@ -211,8 +211,8 @@ class DataCloudLOCAL(DataCloudBase): Driver for local storage. """ def push(self, item): - Logger.debug('sync to cloud ' + item.resolved_cache.dvc + " " + self.storage_path) - copyfile(item.resolved_cache.dvc, self.storage_path) + Logger.debug('sync to cloud ' + item.cache.dvc + " " + self.storage_path) + copyfile(item.cache.dvc, self.storage_path) return item def _import(self, bucket, i, item): @@ -229,19 +229,19 @@ class DataCloudLOCAL(DataCloudBase): return item def pull(self, item): - Logger.debug('sync from cloud ' + self.storage_path + " " + item.resolved_cache.dvc) + Logger.debug('sync from cloud ' + self.storage_path + " " + item.cache.dvc) return self._import(None, self.storage_path, item) def remove(self, item): - Logger.debug('rm from cloud ' + item.resolved_cache.dvc) - os.remove(item.resolved_cache.dvc) + Logger.debug('rm from cloud ' + item.cache.dvc) + os.remove(item.cache.dvc) def import_data(self, path, item): Logger.debug('import from cloud ' + path + " " + item.data.dvc) return self._import(None, path, item) def _status(self, data_item): - local = data_item.resolved_cache.relative + local = data_item.cache.relative remote = '{}/{}'.format(self.storage_path, os.path.basename(local)) remote_exists = os.path.exists(remote) @@ -607,13 +607,13 @@ class DataCloudAWS(DataCloudBase): def push(self, data_item): """ push, aws version """ - aws_key = self.cache_file_key(data_item.resolved_cache.dvc) + aws_key = self.cache_file_key(data_item.cache.dvc) bucket = self._get_bucket_aws(self.storage_bucket) key = bucket.get_key(aws_key) if key: Logger.debug('File already uploaded to the cloud. Checksum validation...') - if self._cmp_checksum(key, data_item.resolved_cache.dvc): + if self._cmp_checksum(key, data_item.cache.dvc): Logger.debug('File checksum matches. No uploading is needed.') return data_item @@ -622,25 +622,25 @@ class DataCloudAWS(DataCloudBase): key = bucket.new_key(aws_key) try: - self._push_multipart(key, data_item.resolved_cache.relative) + self._push_multipart(key, data_item.cache.relative) except Exception as exc: - Logger.error('Failed to upload "{}": {}'.format(data_item.resolved_cache.relative, exc)) + Logger.error('Failed to upload "{}": {}'.format(data_item.cache.relative, exc)) return None - progress.finish_target(os.path.basename(data_item.resolved_cache.relative)) + progress.finish_target(os.path.basename(data_item.cache.relative)) return data_item def _status(self, data_item): - aws_key = self.cache_file_key(data_item.resolved_cache.dvc) + aws_key = self.cache_file_key(data_item.cache.dvc) bucket = self._get_bucket_aws(self.storage_bucket) key = bucket.get_key(aws_key) remote_exists = key is not None - local_exists = os.path.exists(data_item.resolved_cache.relative) + local_exists = os.path.exists(data_item.cache.relative) diff = None if remote_exists and local_exists: - diff = self._cmp_checksum(key, data_item.resolved_cache.dvc) + diff = self._cmp_checksum(key, data_item.cache.dvc) return (local_exists, remote_exists, diff) @@ -744,12 +744,12 @@ class DataCloudGCP(DataCloudBase): """ push, gcp version """ bucket = self._get_bucket_gc(self.storage_bucket) - blob_name = self.cache_file_key(data_item.resolved_cache.dvc) - name = os.path.basename(data_item.resolved_cache.dvc) + blob_name = self.cache_file_key(data_item.cache.dvc) + name = os.path.basename(data_item.cache.dvc) blob = bucket.get_blob(blob_name) if blob is not None and blob.exists(): - if self._cmp_checksum(blob, data_item.resolved_cache.dvc): + if self._cmp_checksum(blob, data_item.cache.dvc): Logger.debug('checksum %s matches. Skipping upload' % data_item.cache.relative) return data_item Logger.debug('checksum %s mismatch. re-uploading' % data_item.cache.relative) @@ -758,10 +758,10 @@ class DataCloudGCP(DataCloudBase): progress.update_target(name, 0, None) blob = bucket.blob(blob_name) - blob.upload_from_filename(data_item.resolved_cache.relative) + blob.upload_from_filename(data_item.cache.relative) progress.finish_target(name) - Logger.debug('uploading %s completed' % data_item.resolved_cache.relative) + Logger.debug('uploading %s completed' % data_item.cache.relative) return data_item @@ -769,14 +769,14 @@ class DataCloudGCP(DataCloudBase): """ status, gcp version """ bucket = self._get_bucket_gc(self.storage_bucket) - blob_name = self.cache_file_key(data_item.resolved_cache.dvc) + blob_name = self.cache_file_key(data_item.cache.dvc) blob = bucket.get_blob(blob_name) remote_exists = blob is not None and blob.exists() - local_exists = os.path.exists(data_item.resolved_cache.relative) + local_exists = os.path.exists(data_item.cache.relative) diff = None if remote_exists and local_exists: - diff = self._cmp_checksum(blob, data_item.resolved_cache.dvc) + diff = self._cmp_checksum(blob, data_item.cache.dvc) return (local_exists, remote_exists, diff) @@ -867,29 +867,15 @@ class DataCloud(object): self._cloud.sanity_check() - def _collect_dir(self, directory): - """ - Collect data items in a specified directory tree. - """ - targets = [] - - for root, dirs, files in os.walk(directory): - for fname in files: - path = os.path.join(root, fname) - item = self._settings.path_factory.data_item(path) - targets.append(item) - - return targets - def _collect_target(self, target): """ Collect target as a file or directory. """ if self._settings.path_factory.is_data_item(target): - item = self._settings.path_factory.data_item(target) + item = self._settings.path_factory.existing_data_item(target) return [item] elif os.path.isdir(target): - return self._collect_dir(target) + return self._settings.path_factory.all_existing_data_items(target) Logger.warn('Target "{}" does not exist'.format(target)) diff --git a/dvc/path/data_item.py b/dvc/path/data_item.py index 6a40c0ff1..ccfba0c2c 100644 --- a/dvc/path/data_item.py +++ b/dvc/path/data_item.py @@ -92,11 +92,6 @@ class DataItem(object): cache_file = os.path.join(cache_dir, file_name) return Path(cache_file, self._git) - @cached_property - def resolved_cache(self): - resolved_cache = os.path.realpath(self._data.relative) - return Path(resolved_cache, self._git) - @cached_property def state_dir_abs(self): return os.path.join(self._git.git_dir_abs, self._config.state_dir) diff --git a/dvc/path/factory.py b/dvc/path/factory.py index 9dc3164fb..f79ddd0f6 100644 --- a/dvc/path/factory.py +++ b/dvc/path/factory.py @@ -49,11 +49,11 @@ class PathFactory(object): return result, externally_created_files - def all_existing_data_items(self): + def all_existing_data_items(self, subdir='.'): items = [] states = [] - for root, dirs, files in os.walk(self._config.state_dir): + for root, dirs, files in os.walk(os.path.join(self._config.state_dir, subdir)): for fname in files: path = os.path.join(root, fname) From 8fac23f93d086e48c5c8c355436509cd8b171ba2 Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Wed, 1 Nov 2017 23:50:43 +0200 Subject: [PATCH 2/4] functest: fix test-sync Signed-off-by: Ruslan Kuprieiev --- functest/test-sync.sh | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/functest/test-sync.sh b/functest/test-sync.sh index 389a1bcf6..aaa3bf53d 100755 --- a/functest/test-sync.sh +++ b/functest/test-sync.sh @@ -14,26 +14,18 @@ function test_sync() { dvc_info "Checking status" dvc status data/xml - dvc_info "Modifying data" - echo "123456" >> data/xml/Tags.xml - - dvc_info "Checking status" - dvc status data/xml - - dvc_info "Pulling data" - dvc pull data/xml - - dvc_info "Checking status" - dvc status data/xml - dvc_info "Removing all cache" - rm -rf .cache/xml + rm -rf .dvc/cache/* + rm -rf data/xml/*.xml dvc_info "Checking status" dvc status data/xml dvc_info "Pulling data" - dvc pull data/xml + dvc pull -v data/xml + + dvc_info "Checking status" + dvc status data/xml } function test_aws() { @@ -41,9 +33,9 @@ function test_aws() { dvc_info "Setting up AWS cloud" dvc_clean_cloud_aws - dvc config global.cloud AWS dvc config aws.storagepath $TEST_REPO_S3 dvc config aws.region $TEST_REPO_REGION + dvc config global.cloud AWS test_sync @@ -55,9 +47,9 @@ function test_gcp() { dvc_info "Setting up GCP cloud" dvc_clean_cloud_gcp - dvc config global.cloud GCP dvc config gcp.storagepath $TEST_REPO_GCP dvc config gcp.projectname $TEST_REPO_GCP_PROJECT + dvc config global.cloud GCP test_sync From 7e90522d3f32b2e8669c1f73d93b970ad74d33a5 Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Thu, 2 Nov 2017 00:42:58 +0200 Subject: [PATCH 3/4] cloud: gcp: decode base64 when comparing files Otherwise comparison with string fails on Python3, as base64 returns bytes. Signed-off-by: Ruslan Kuprieiev --- dvc/data_cloud.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dvc/data_cloud.py b/dvc/data_cloud.py index 357fa786d..8a5752d2e 100644 --- a/dvc/data_cloud.py +++ b/dvc/data_cloud.py @@ -694,7 +694,8 @@ class DataCloudGCP(DataCloudBase): """ Verify local and remote checksums. """ - b64_encoded_md5 = base64.b64encode(file_md5(fname)[1]) + md5 = file_md5(fname)[1] + b64_encoded_md5 = base64.b64encode(md5).decode() if md5 else None if blob.md5_hash == b64_encoded_md5: return True @@ -726,7 +727,6 @@ class DataCloudGCP(DataCloudBase): try: blob.download_to_filename(tmp_file) - data_item.import_cache(tmp_file) except Exception as exc: Logger.error('Failed to download "{}": {}'.format(key, exc)) return None From c022aa89d5def210d0fd30a9f332053a9c0677d8 Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Thu, 2 Nov 2017 01:39:39 +0200 Subject: [PATCH 4/4] functest: use small data to test basic functionality Fixes #283 Signed-off-by: Ruslan Kuprieiev --- functest/common.sh | 12 ++++++------ functest/test-checkout.sh | 6 ++---- functest/test-import.sh | 6 +++--- functest/test-merge.sh | 20 ++++++++++---------- functest/test-repro-code.sh | 34 ++++++++++++++++++++-------------- functest/test-sync.sh | 15 ++++++++------- 6 files changed, 49 insertions(+), 44 deletions(-) diff --git a/functest/common.sh b/functest/common.sh index 694d038d5..daead7842 100644 --- a/functest/common.sh +++ b/functest/common.sh @@ -8,13 +8,13 @@ TEST_REPO=$SDIR/myrepo CODE_CACHE=$CACHE_DIR/code DATA_CACHE=$CACHE_DIR/data -DATA="Badges.xml Tags.xml PostLinks.xml" -CODE="xmltotsv.py" +DATA="foo bar" +CODE="code.sh" REGION='us-west-2' BASE_DIR_S3=https://s3-$REGION.amazonaws.com/dataversioncontrol/functests -DATA_S3=$BASE_DIR_S3/stackoverflow_raw_small -CODE_S3=$BASE_DIR_S3/code +DATA_S3=$BASE_DIR_S3/dummy_data +CODE_S3=$BASE_DIR_S3/dummy_code TEST_REPO_S3='dvc-test/myrepo' TEST_REPO_REGION='us-east-2' @@ -114,8 +114,8 @@ function dvc_create_repo() { dvc init - mkdir -p data/xml - dvc import $DATA_CACHE/* data/xml + mkdir data + dvc import $DATA_CACHE/* data } function dvc_clean_cloud_aws() { diff --git a/functest/test-checkout.sh b/functest/test-checkout.sh index 4609cc202..a6efddea9 100755 --- a/functest/test-checkout.sh +++ b/functest/test-checkout.sh @@ -4,11 +4,9 @@ set -e source common.sh -ITEM=data/xml/Badges.xml - dvc_create_repo -rm -f $ITEM +rm -f data/foo dvc checkout -dvc_check_files $ITEM +dvc_check_files data/foo dvc_pass diff --git a/functest/test-import.sh b/functest/test-import.sh index 75ab62857..bc277499b 100755 --- a/functest/test-import.sh +++ b/functest/test-import.sh @@ -7,17 +7,17 @@ source common.sh dvc_create_repo dvc_info "Import from local file" -dvc import $DATA_CACHE/Tags.xml data/local +dvc import $DATA_CACHE/foo data/local dvc_check_files "data/local" dvc_info "Import from http url" -dvc import $DATA_S3/Tags.xml data/http +dvc import $DATA_S3/foo data/http dvc_check_files "data/http" if [[ "$TRAVIS_PULL_REQUEST" == "false" && "$TRAVIS_SECURE_ENV_VARS" == "true" ]]; then dvc_info "Import from s3 url" dvc config aws.region $REGION - dvc import s3://dataversioncontrol/functests/stackoverflow_raw_small/Tags.xml data/s3 + dvc import s3://dataversioncontrol/functests/dummy_data/foo data/s3 dvc_check_files "data/s3" fi diff --git a/functest/test-merge.sh b/functest/test-merge.sh index 2afbe4153..0a9e1a949 100755 --- a/functest/test-merge.sh +++ b/functest/test-merge.sh @@ -7,8 +7,8 @@ source common.sh dvc_create_repo # Adding changes to the first branch -git checkout -b input_50K -echo -e "#!/bin/bash\nset -x\nset -e\ncp \$1 \$2" > code/code1.sh +git checkout -b foo +cp code/code.sh code/code1.sh chmod +x code/code1.sh cp code/code1.sh code/code2.sh cp code/code1.sh code/code3.sh @@ -16,7 +16,7 @@ cp code/code1.sh code/code4.sh git add code git commit -s -m"add code" -dvc import https://s3-us-west-2.amazonaws.com/dvc-share/so/50K/Posts.xml.tgz data/data +dvc import $DATA_CACHE/foo data/data ORIG_DATA=$(dvc_md5 data/data) @@ -29,19 +29,19 @@ dvc config Global.Target data/data4 git commit -am 'Set Target' # Create new branch to work on small data set -git checkout -b input_10K +git checkout -b bar dvc remove -c -l data/data -dvc import https://s3-us-west-2.amazonaws.com/dvc-share/so/10K/Posts.xml.tgz data/data +dvc import $DATA_CACHE/bar data/data dvc repro -echo -e "#!/bin/bash\n\n\n\ncp \$1 \$2" > code/code1.sh +echo -e "\n\n\n\n" >> code/code1.sh git commit -am"change code1.sh" dvc repro -# Merge input_10K into input_50K -git checkout input_50K -git merge input_10K -# Notice we're running 'dvc merge' on input_50K branch. This +# Merge bar into foo +git checkout foo +git merge bar +# Notice we're running 'dvc merge' on foo branch. This # way dvc knows to favor data files from current branch. dvc merge diff --git a/functest/test-repro-code.sh b/functest/test-repro-code.sh index ffdbd85b0..618912972 100755 --- a/functest/test-repro-code.sh +++ b/functest/test-repro-code.sh @@ -6,27 +6,33 @@ source common.sh dvc_create_repo -mkdir data/tsv - -dvc_info 'Convert Budget.xml to tsv' -dvc run --code code/xmltotsv.py python code/xmltotsv.py --extract row/@Id,row/@UserId,row/@Name,row/@Class,row/@TagBased,row/@Date data/xml/Badges.xml data/tsv/Badges.tsv - -dvc_info 'Modify code' -echo " " >> code/xmltotsv.py -git commit -am 'Change code' - -dvc_info 'Reproduce tsv convertion code' -dvc repro data/tsv/Badges.tsv +dvc_info 'Copy foo into foo1' +dvc run bash code/code.sh data/foo data/foo1 dvc_info 'Modify code' -echo " " >> code/xmltotsv.py +echo " " >> code/code.sh git commit -am 'Change code' + +dvc_info 'Reproduce foo1' +dvc repro data/foo1 +dvc_check_files data/foo1 +if [ "$(cat data/foo1)" != "foo" ]; then + dvc_fail +fi + +dvc_info 'Modify foo' +dvc remove data/foo +dvc import $DATA_CACHE/bar data/foo dvc_info 'Set default target' -dvc config global.target data/tsv/Badges.tsv +dvc config global.target data/foo1 git commit -am 'Set default target' -dvc_info 'Reproduce tsv convertion code as default target' +dvc_info 'Reproduce foo1 as default target' dvc repro +dvc_check_files data/foo1 +if [ "$(cat data/foo1)" != "bar" ]; then + dvc_fail +fi dvc_pass diff --git a/functest/test-sync.sh b/functest/test-sync.sh index aaa3bf53d..d9e3ebfa3 100755 --- a/functest/test-sync.sh +++ b/functest/test-sync.sh @@ -6,26 +6,27 @@ source common.sh function test_sync() { dvc_info "Checking status" - dvc status data/xml + dvc status data/ | grep "new file" || dvc_fail dvc_info "Pushing data" - dvc push data/xml + dvc push data/ dvc_info "Checking status" - dvc status data/xml + dvc status data/ dvc_info "Removing all cache" rm -rf .dvc/cache/* - rm -rf data/xml/*.xml + rm -rf data/foo data/bar dvc_info "Checking status" - dvc status data/xml + dvc status data/ | grep "deleted" || dvc_fail dvc_info "Pulling data" - dvc pull -v data/xml + dvc pull data/ + dvc_check_files data/foo data/bar dvc_info "Checking status" - dvc status data/xml + dvc status data/ } function test_aws() {