def import_upstream(tarball_path, patches_path, version, name, replace): # Check for a url and download it url = urlparse(tarball_path) if url.scheme: # Some scheme like http, https, or file... tmp_dir = tempfile.mkdtemp() try: info("Fetching file from url: '{0}'".format(tarball_path)) req = load_url_to_file_handle(tarball_path) tarball_path = os.path.join(tmp_dir, os.path.basename(url.path)) with open(tarball_path, 'wb') as f: chunk_size = 16 * 1024 while True: chunk = req.read(chunk_size) if not chunk: break f.write(chunk) return import_upstream(tarball_path, patches_path, version, name, replace) finally: shutil.rmtree(tmp_dir) # If there is not tarball at the given path, fail if not os.path.exists(tarball_path): error("Specified archive does not exists: '{0}'".format(tarball_path), exit=True) # If either version or name are not provided, guess from archive name if not version or not name: # Parse tarball name tarball_file = os.path.basename(tarball_path) ending = None if tarball_file.endswith('.tar.gz'): ending = '.tar.gz' elif tarball_file.endswith('.zip'): ending = '.zip' else: error("Cannot detect type of archive: '{0}'" .format(tarball_file), exit=True) tarball_file = tarball_file[:-len(ending)] split_tarball_file = tarball_file.split('-') if len(split_tarball_file) < 2 and not version or len(split_tarball_file) < 1: error("Cannot detect name and/or version from archive: '{0}'" .format(tarball_file), exit=True) if not name and len(split_tarball_file) == 1: name = split_tarball_file[0] elif not name and len(split_tarball_file) == 1: name = '-'.join(split_tarball_file[:-1]) if not version and len(split_tarball_file) < 2: error("Cannot detect version from archive: '{0}'" .format(tarball_file) + " and the version was not spcified.", exit=True) version = version if version else split_tarball_file[-1] # Check if the patches_path (if given) exists patches_path_dict = None if patches_path: patches_path_dict = ls_tree(BLOOM_CONFIG_BRANCH, patches_path) if not patches_path_dict: error("Given patches path '{0}' does not exist in bloom branch." .format(patches_path), exit=True) # Do version checking version_check(version) # Check for existing tags upstream_tag = 'upstream/{0}'.format(version) if tag_exists(upstream_tag): if not replace: error("Tag '{0}' already exists, use --replace to override it." .format(upstream_tag), exit=True) warning("Removing tag: '{0}'".format(upstream_tag)) delete_tag(upstream_tag) if not get_git_clone_state(): delete_remote_tag(upstream_tag) name_tag = '{0}/{1}'.format(name or 'upstream', version) if name_tag != upstream_tag and tag_exists(name_tag): if not replace: error("Tag '{0}' already exists, use --replace to override it." .format(name_tag), exit=True) warning("Removing tag: '{0}'".format(name_tag)) delete_tag(name_tag) if not get_git_clone_state(): delete_remote_tag(name_tag) # If there is not upstream branch, create one if not branch_exists('upstream'): info("Creating upstream branch.") create_branch('upstream', orphaned=True) else: track_branches(['upstream']) # Import the given tarball info("Importing archive into upstream branch...") import_tarball(tarball_path, 'upstream', version, name) # Handle patches_path if patches_path: import_patches(patches_path, patches_path_dict, 'upstream', version) # Create tags with inbranch('upstream'): info("Creating tag: '{0}'".format(upstream_tag)) create_tag(upstream_tag) if name_tag != upstream_tag: info("Creating tag: '{0}'".format(name_tag)) create_tag(name_tag)
def import_upstream(tarball_path, patches_path, version, name, replace): # If there is not tarball at the given path, fail if not os.path.exists(tarball_path): error("Specified archive does not exists: '{0}'".format(tarball_path), exit=True) # If either version or name are not provided, guess from archive name if not version or not name: # Parse tarball name tarball_file = os.path.basename(tarball_path) ending = None if tarball_file.endswith(".tar.gz"): ending = ".tar.gz" elif tarball_file.endswith(".zip"): ending = ".zip" else: error("Cannot detect type of archive: '{0}'".format(tarball_file), exit=True) tarball_file = tarball_file[: -len(ending)] split_tarball_file = tarball_file.split("-") if len(split_tarball_file) < 2 and not version or len(split_tarball_file) < 1: error("Cannot detect name and/or version from archive: '{0}'".format(tarball_file), exit=True) if not name and len(split_tarball_file) == 1: name = split_tarball_file[0] elif not name and len(split_tarball_file) == 1: name = "-".join(split_tarball_file[:-1]) if not version and len(split_tarball_file) < 2: error( "Cannot detect version from archive: '{0}'".format(tarball_file) + " and the version was not spcified.", exit=True, ) version = version if version else split_tarball_file[-1] # Check if the patches_path (if given) exists patches_path_dict = None if patches_path: patches_path_dict = ls_tree("bloom", patches_path) if not patches_path_dict: error("Given patches path '{0}' does not exist in bloom branch.".format(patches_path), exit=True) # Do version checking version_check(version) # Check for existing tags upstream_tag = "upstream/{0}".format(version) if tag_exists(upstream_tag): if not replace: error("Tag '{0}' already exists, use --replace to override it.".format(upstream_tag), exit=True) warning("Removing tag: '{0}'".format(upstream_tag)) delete_tag(upstream_tag) if not get_git_clone_state(): delete_remote_tag(upstream_tag) name_tag = "{0}/{1}".format(name or "upstream", version) if name_tag != upstream_tag and tag_exists(name_tag): if not replace: error("Tag '{0}' already exists, use --replace to override it.".format(name_tag), exit=True) warning("Removing tag: '{0}'".format(name_tag)) delete_tag(name_tag) if not get_git_clone_state(): delete_remote_tag(name_tag) # If there is not upstream branch, create one if not branch_exists("upstream"): info("Creating upstream branch.") create_branch("upstream", orphaned=True) else: track_branches(["upstream"]) # Import the given tarball info("Importing archive into upstream branch...") import_tarball(tarball_path, "upstream", version, name) # Handle patches_path if patches_path: import_patches(patches_path, patches_path_dict, "upstream", version) # Create tags with inbranch("upstream"): info("Creating tag: '{0}'".format(upstream_tag)) create_tag(upstream_tag) if name_tag != upstream_tag: info("Creating tag: '{0}'".format(name_tag)) create_tag(name_tag)