def test_hash_file(self): temp_name = tempfile.mkstemp('webports_test')[1] self.addCleanup(os.remove, temp_name) with self.assertRaises(IOError): util.hash_file('/does/not/exist') sha1_empty_string = 'da39a3ee5e6b4b0d3255bfef95601890afd80709' self.assertEqual(util.hash_file(temp_name), sha1_empty_string)
def get_extract_stamp_content(self): patch_file = self.get_patch_file() if os.path.exists(patch_file): patch_sha = util.hash_file(self.get_patch_file()) return 'ARCHIVE_SHA1=%s PATCH_SHA1=%s\n' % (self.SHA1, patch_sha) else: return 'ARCHIVE_SHA1=%s\n' % self.SHA1
def write_index(index_filename, binaries): """Create a package index file from set of binaries on disk. Args: index_filename: The name of the file to write create. binaries: List of (filename, url) pairs containing packages to include. Returns: A PackageIndex object based on the contents of the newly written file. """ # Write index to a temporary file and then rename it, to avoid # leaving a partial index file on disk. tmp_name = index_filename + '.tmp' with open(tmp_name, 'w') as output_file: for i, (filename, url) in enumerate(binaries): sha1 = util.hash_file(filename) if i != 0: output_file.write('\n') output_file.write(extract_pkg_info(filename)) output_file.write('BIN_URL=%s\n' % url) output_file.write('BIN_SIZE=%s\n' % os.path.getsize(filename)) output_file.write('BIN_SHA1=%s\n' % sha1) os.rename(tmp_name, index_filename) return index_from_file(index_filename)
def git_clone(self): """Create a clone of the upstream repo in the build directory. This operation will only require a network connection if the local git mirror is out-of-date.""" stamp_file = self.get_extract_stamp() stamp_content = 'GITURL=%s' % self.URL patch_file = self.get_patch_file() if os.path.exists(patch_file): patch_checksum = util.hash_file(patch_file) stamp_content += ' PATCH=%s' % patch_checksum stamp_content += '\n' dest = self.get_build_location() if os.path.exists(self.get_build_location()): if stamp_contents_match(stamp_file, stamp_content): return raise Error('Upstream archive or patch has changed.\n' + "Please remove existing checkout and try again: '%s'" % dest) util.log_heading('Cloning') # Ensure local mirror is up-to-date git_mirror, git_commit = self.git_clone_to_mirror() # Clone from the local mirror. run_git_cmd(None, ['clone', git_mirror, dest]) run_git_cmd(dest, ['reset', '--hard', git_commit]) # Set the origing to the original URL so it is possible to push directly # from the build tree. run_git_cmd(dest, ['remote', 'set-url', 'origin', self.URL.split('@')[0]]) self.remove_stamps() write_stamp(stamp_file, stamp_content)