def set_long_path_support(): global_git_config_path = os.path.normpath(expanduser("~/.gitconfig")) with git.GitConfigParser(global_git_config_path, read_only=False) as git_globalconfig: if 'core' not in git_globalconfig.sections(): git_globalconfig.add_section('core') git_globalconfig.set('core', 'longpaths', 'true')
def get_edkrepo_global_data_directory(): global_data_dir = None if sys.platform == "win32": shell32 = oledll.shell32 SHGetFolderPath = shell32.SHGetFolderPathW SHGetFolderPath.argtypes = [ c_void_p, c_uint32, c_void_p, c_uint32, c_wchar_p ] CSIDL_COMMON_APPDATA = 0x0023 SHGFP_TYPE_CURRENT = 0 MAX_PATH = 260 common_appdata = create_unicode_buffer(MAX_PATH) SHGetFolderPath(None, CSIDL_COMMON_APPDATA, None, SHGFP_TYPE_CURRENT, common_appdata) global_data_dir = os.path.join(common_appdata.value, "edkrepo") elif sys.platform == "darwin" or sys.platform.startswith( "linux") or os.name == "posix": global_data_dir = expanduser("~/.edkrepo") if not os.path.isdir(global_data_dir): if not os.path.exists(os.path.dirname(global_data_dir)): raise EdkrepoGlobalDataDirectoryNotFoundException( humble.GLOBAL_DATA_DIR_NOT_FOUND.format( os.path.dirname(global_data_dir))) os.mkdir(global_data_dir) return global_data_dir
def init_color_console(force_color_output): config = git.GitConfigParser(os.path.normpath(expanduser("~/.gitconfig"))) config_color = config.get("color", "ui", fallback="auto") strip = not sys.stdout.isatty() convert = sys.stdout.isatty() if force_color_output or config_color == "always": strip = False elif config_color == "false": strip = True convert = False if os.name == 'posix': # Never convert on Linux. Setting it to False seems to cause problems. convert = None colorama.init(strip=strip, convert=convert, autoreset=True) return strip, convert
def write_conditional_include(workspace_path, repo_sources, included_configs): gitconfigpath = os.path.normpath(expanduser("~/.gitconfig")) for source in repo_sources: for included_config in included_configs: if included_config[0] == source.remote_name: gitdir = str(os.path.normpath(os.path.join(workspace_path, source.root))) gitdir = get_actual_path(gitdir) gitdir = gitdir.replace('\\', '/') if sys.platform == "win32": gitdir = '/{}'.format(gitdir) path = '/{}'.format(included_config[1]) else: path = included_config[1] path = path.replace('\\', '/') section = 'includeIf "gitdir:{}/"'.format(gitdir) with git.GitConfigParser(gitconfigpath, read_only=False) as gitglobalconfig: gitglobalconfig.add_section(section) gitglobalconfig.set(section, 'path', path)
def update_repo_commit_template(workspace_dir, repo, repo_info, config, global_manifest_directory): # Open the local manifest and get any templates manifest = edk_manifest.ManifestXml(os.path.join(workspace_dir, 'repo', 'Manifest.xml')) templates = manifest.commit_templates #Check for the presence of a globally defined commit template global_template_in_use = False global_gitconfig_path = os.path.normpath(expanduser("~/.gitconfig")) with git.GitConfigParser(global_gitconfig_path, read_only=False) as gitglobalconfig: if gitglobalconfig.has_option(section='commit', option='template'): gitglobalconfig.get_value(section='commit', option='template') global_template_in_use = True print(COMMIT_TEMPLATE_CUSTOM_VALUE.format(repo_info.remote_name)) # Apply the template based on current manifest with repo.config_writer() as cw: if not global_template_in_use: if cw.has_option(section='commit', option='template'): current_template = cw.get_value(section='commit', option='template').replace('"', '') if not current_template.startswith(os.path.normpath(global_manifest_directory).replace('\\', '/')): if os.path.isfile(current_template): print(COMMIT_TEMPLATE_CUSTOM_VALUE.format(repo_info.remote_name)) return else: print(COMMIT_TEMPLATE_NOT_FOUND.format(current_template)) print(COMMIT_TEMPLATE_RESETTING_VALUE) if repo_info.remote_name in templates: template_path = os.path.normpath(os.path.join(global_manifest_directory, templates[repo_info.remote_name])) if not os.path.isfile(template_path): print(COMMIT_TEMPLATE_NOT_FOUND.format(template_path)) return template_path = template_path.replace('\\', '/') # Convert to git approved path cw.set_value(section='commit', option='template', value='"{}"'.format(template_path)) else: if cw.has_option(section='commit', option='template'): cw.remove_option(section='commit', option='template') else: if cw.has_option(section='commit', option='template'): cw.remove_option(section='commit', option='template')
def clean_git_globalconfig(): global_gitconfig_path = os.path.normpath(expanduser("~/.gitconfig")) with git.GitConfigParser(global_gitconfig_path, read_only=False) as git_globalconfig: includeif_regex = re.compile('^includeIf "gitdir:(/.+)/"$') for section in git_globalconfig.sections(): data = includeif_regex.match(section) if data: gitrepo_path = data.group(1) gitconfig_path = git_globalconfig.get(section, 'path') if sys.platform == "win32": gitrepo_path = gitrepo_path[1:] gitconfig_path = gitconfig_path[1:] gitrepo_path = os.path.normpath(gitrepo_path) gitconfig_path = os.path.normpath(gitconfig_path) (repo_manifest_path, _) = os.path.split(gitconfig_path) repo_manifest_path = os.path.join(repo_manifest_path, "Manifest.xml") if not os.path.isdir(gitrepo_path) and not os.path.isfile( gitconfig_path): if not os.path.isfile(repo_manifest_path): git_globalconfig.remove_section(section)
def __check_submodule_config(self, workspace_path, manifest, repo_sources): gitconfigpath = os.path.normpath(expanduser("~/.gitconfig")) gitglobalconfig = git.GitConfigParser(gitconfigpath, read_only=False) try: local_manifest_dir = os.path.join(workspace_path, "repo") includeif_regex = re.compile('^includeIf "gitdir:(/.+)/"$') rewrite_everything = False #Generate list of .gitconfig files that should be present in the workspace included_configs = [] for remote in manifest.remotes: included_config_name = os.path.join( local_manifest_dir, INCLUDED_FILE_NAME.format(remote.name)) included_config_name = get_actual_path(included_config_name) remote_alts = [ submodule for submodule in manifest.submodule_alternate_remotes if submodule.remote_name == remote.name ] if remote_alts: included_configs.append( (remote.name, included_config_name)) for source in repo_sources: for included_config in included_configs: #If the current repository has a .gitconfig (aka it has a submodule) if included_config[0] == source.remote_name: if not os.path.isfile(included_config[1]): rewrite_everything = True gitdir = str( os.path.normpath( os.path.join(workspace_path, source.root))) gitdir = get_actual_path(gitdir) gitdir = gitdir.replace('\\', '/') if sys.platform == "win32": gitdir = '/{}'.format(gitdir) #Make sure the .gitconfig file is referenced by the global git config found_include = False for section in gitglobalconfig.sections(): data = includeif_regex.match(section) if data: if data.group(1) == gitdir: found_include = True break if not found_include: #If the .gitconfig file is missing from the global git config, add it. if sys.platform == "win32": path = '/{}'.format(included_config[1]) else: path = included_config[1] path = path.replace('\\', '/') section = 'includeIf "gitdir:{}/"'.format(gitdir) gitglobalconfig.add_section(section) gitglobalconfig.set(section, 'path', path) gitglobalconfig.release() gitglobalconfig = git.GitConfigParser( gitconfigpath, read_only=False) if rewrite_everything: #If one or more of the .gitconfig files are missing, re-generate all the .gitconfig files remove_included_config(manifest.remotes, manifest.submodule_alternate_remotes, local_manifest_dir) write_included_config(manifest.remotes, manifest.submodule_alternate_remotes, local_manifest_dir) finally: gitglobalconfig.release()