def AddRecordedUserStories(self, user_stories, upload_to_cloud_storage=False): if not user_stories: os.remove(self.temp_target_wpr_file_path) return (target_wpr_file, target_wpr_file_path) = self._NextWprFileName() for user_story in user_stories: self._SetWprFileForUserStory(user_story.display_name, target_wpr_file) shutil.move(self.temp_target_wpr_file_path, target_wpr_file_path) # Update the hash file. target_wpr_file_hash = cloud_storage.CalculateHash(target_wpr_file_path) with open(target_wpr_file_path + '.sha1', 'wb') as f: f.write(target_wpr_file_hash) f.flush() self._WriteToFile() self._DeleteAbandonedWprFiles() # Upload to cloud storage if upload_to_cloud_storage: if not self._bucket: logging.warning('UserStorySet must have bucket specified to upload ' 'user stories to cloud storage.') return try: cloud_storage.Insert(self._bucket, target_wpr_file_hash, target_wpr_file_path) except cloud_storage.CloudStorageError, e: logging.warning('Failed to upload wpr file %s to cloud storage. ' 'Error:%s' % target_wpr_file_path, e)
def assertCorrectHashFile(self, file_path): old_ch = cloud_storage.CalculateHash cloud_storage.CalculateHash = self.overrides.cloud_storage.CalculateHash try: self.assertTrue(os.path.exists(file_path + '.sha1')) with open(file_path + '.sha1', 'rb') as f: self.assertEquals(cloud_storage.CalculateHash(file_path), f.read()) finally: cloud_storage.CalculateHash = old_ch
def _SyncFilesToCloud(input_api, output_api): """Searches for .sha1 files and uploads them to Cloud Storage. It validates all the hashes and skips upload if not necessary. """ cloud_storage = LoadSupport(input_api) results = [] for hash_path, file_hash in _GetFilesNotInCloud(input_api): file_path, _ = os.path.splitext(hash_path) if not re.match('^([A-Za-z0-9]{40})$', file_hash): results.append( output_api.PresubmitError( 'Hash file does not contain a valid SHA-1 hash: %s' % hash_path)) continue if not os.path.exists(file_path): results.append( output_api.PresubmitError( 'Hash file exists, but file not found: %s' % hash_path)) continue if cloud_storage.CalculateHash(file_path) != file_hash: results.append( output_api.PresubmitError( 'Hash file does not match file\'s actual hash: %s' % hash_path)) continue try: bucket_aliases_string = ', '.join(cloud_storage.BUCKET_ALIASES) bucket_input = raw_input( 'Uploading to Cloud Storage: %s\n' 'Which bucket should this go in? (%s) ' % (file_path, bucket_aliases_string)).lower() bucket = cloud_storage.BUCKET_ALIASES.get(bucket_input, None) if not bucket: results.append( output_api.PresubmitError( '"%s" was not one of %s' % (bucket_input, bucket_aliases_string))) return results cloud_storage.Insert(bucket, file_hash, file_path) results.append( output_api.PresubmitNotifyResult( 'Uploaded file to Cloud Storage: %s' % file_path)) except cloud_storage.CloudStorageError, e: results.append( output_api.PresubmitError( 'Unable to upload to Cloud Storage: %s\n\n%s' % (file_path, e)))
def AddRecordedPages(self, pages): if not pages: os.remove(self.temp_target_wpr_file_path) return (target_wpr_file, target_wpr_file_path) = self._NextWprFileName() for page in pages: self._SetWprFileForPage(page.display_name, target_wpr_file) shutil.move(self.temp_target_wpr_file_path, target_wpr_file_path) # Update the hash file. with open(target_wpr_file_path + '.sha1', 'wb') as f: f.write(cloud_storage.CalculateHash(target_wpr_file_path)) f.flush() self._WriteToFile() self._DeleteAbandonedWprFiles()
def assertCorrectHashFile(self, file_path): self.assertTrue(os.path.exists(file_path + '.sha1')) with open(file_path + '.sha1', 'rb') as f: self.assertEquals(cloud_storage.CalculateHash(file_path), f.read())