def _locked_refresh_study_ids(self): d = create_id2study_info(self.study_dir, self.name) rc_dict = diagnose_repo_study_id_convention(self.path) self.filepath_for_study_id_fn = rc_dict['fp_fn'] self.id_alias_list_fn = rc_dict['id2alias_list'] if rc_dict['convention'] != 'simple': a = {} for k, v in d.items(): alias_list = self.id_alias_list_fn(k) for alias in alias_list: a[alias] = v d = a self.has_aliases = True else: self.has_aliases = False self._study_index = d
def refresh_study_index(shard, initializing=False): from peyotl.phylesystem.helper import create_id2study_info, \ diagnose_repo_study_id_convention d = create_id2study_info(shard.doc_dir, shard.name) rc_dict = diagnose_repo_study_id_convention(shard.path) shard.filepath_for_doc_id_fn = rc_dict['fp_fn'] shard.id_alias_list_fn = rc_dict['id2alias_list'] if rc_dict['convention'] != 'simple': a = {} for k, v in d.items(): alias_list = shard.id_alias_list_fn(k) for alias in alias_list: a[alias] = v d = a shard.has_aliases = True if initializing: shard.infer_study_prefix() else: shard.has_aliases = False shard.study_index = d
def __init__(self, name, path, repo_nexml2json=None, git_ssh=None, pkey=None, git_action_class=GitAction, push_mirror_repo_path=None, new_study_prefix=None, infrastructure_commit_author='OpenTree API <*****@*****.**>', **kwargs): self._index_lock = Lock() PhylesystemShardBase.__init__(self, name) self._infrastructure_commit_author = infrastructure_commit_author self._study_counter_lock = Lock() self._master_branch_repo_lock = Lock() self._new_study_prefix = new_study_prefix self._ga_class = git_action_class self.git_ssh = git_ssh self.pkey = pkey path = os.path.abspath(path) dot_git = os.path.join(path, '.git') study_dir = os.path.join(path, 'study') if not os.path.isdir(path): raise ValueError('"{p}" is not a directory'.format(p=path)) if not os.path.isdir(dot_git): raise ValueError('"{p}" is not a directory'.format(p=dot_git)) if not os.path.isdir(study_dir): raise ValueError('"{p}" is not a directory'.format(p=study_dir)) self.path = path self._id_minting_file = os.path.join(path, 'next_study_id.json') if self._new_study_prefix is None: prefix_file = os.path.join(path, 'new_study_prefix') if os.path.exists(prefix_file): pre_content = open(prefix_file, 'r').read().strip() valid_pat = re.compile('^[a-zA-Z0-9]+_$') if len(pre_content) != 3 or not valid_pat.match(pre_content): raise ValueError('Expecting prefix in new_study_prefix file to be two '\ 'letters followed by an underscore') self._new_study_prefix = pre_content else: self._new_study_prefix = 'ot_' # ot_ is the default if there is no file d = create_id2study_info(study_dir, name) rc_dict = diagnose_repo_study_id_convention(path) self.filepath_for_study_id_fn = rc_dict['fp_fn'] self.filepath_for_global_resource_fn = lambda frag: os.path.join(path, frag) self.id_alias_list_fn = rc_dict['id2alias_list'] if rc_dict['convention'] != 'simple': a = {} for k, v in d.items(): alias_list = self.id_alias_list_fn(k) for alias in alias_list: a[alias] = v d = a self.has_aliases = True self.inferred_study_prefix = True self.infer_study_prefix() else: self.inferred_study_prefix = False self.study_dir = study_dir with self._index_lock: self._locked_refresh_study_ids() self.parent_path = os.path.split(path)[0] + '/' self.git_dir = dot_git self.push_mirror_repo_path = push_mirror_repo_path if repo_nexml2json is None: try: repo_nexml2json = get_config_setting_kwargs(None, 'phylesystem', 'repo_nexml2json', **kwargs) except: pass if repo_nexml2json == None: repo_nexml2json = self.diagnose_repo_nexml2json() self.repo_nexml2json = repo_nexml2json self._next_study_id = None self._study_counter_lock = None self._known_prefixes = None
def __init__(self, name, path, repo_nexml2json=None, git_ssh=None, pkey=None, git_action_class=GitAction, push_mirror_repo_path=None, new_study_prefix=None, infrastructure_commit_author='OpenTree API <*****@*****.**>', **kwargs): self._index_lock = Lock() PhylesystemShardBase.__init__(self, name) self._infrastructure_commit_author = infrastructure_commit_author self._study_counter_lock = Lock() self._master_branch_repo_lock = Lock() self._new_study_prefix = new_study_prefix self._ga_class = git_action_class self.git_ssh = git_ssh self.pkey = pkey path = os.path.abspath(path) dot_git = os.path.join(path, '.git') study_dir = os.path.join(path, 'study') if not os.path.isdir(path): raise NotAPhylesystemShardError('"{p}" is not a directory'.format(p=path)) if not os.path.isdir(dot_git): raise NotAPhylesystemShardError('"{p}" is not a directory'.format(p=dot_git)) if not os.path.isdir(study_dir): raise NotAPhylesystemShardError('"{p}" is not a directory'.format(p=study_dir)) self.path = path self._id_minting_file = os.path.join(path, 'next_study_id.json') if self._new_study_prefix is None: prefix_file = os.path.join(path, 'new_study_prefix') if os.path.exists(prefix_file): pre_content = open(prefix_file, 'r').read().strip() valid_pat = re.compile('^[a-zA-Z0-9]+_$') if len(pre_content) != 3 or not valid_pat.match(pre_content): raise NotAPhylesystemShardError('Expecting prefix in new_study_prefix file to be two '\ 'letters followed by an underscore') self._new_study_prefix = pre_content else: self._new_study_prefix = 'ot_' # ot_ is the default if there is no file d = create_id2study_info(study_dir, name) rc_dict = diagnose_repo_study_id_convention(path) self.filepath_for_study_id_fn = rc_dict['fp_fn'] self.filepath_for_global_resource_fn = lambda frag: os.path.join(path, frag) self.id_alias_list_fn = rc_dict['id2alias_list'] if rc_dict['convention'] != 'simple': a = {} for k, v in d.items(): alias_list = self.id_alias_list_fn(k) for alias in alias_list: a[alias] = v d = a self.has_aliases = True self.inferred_study_prefix = True self.infer_study_prefix() else: self.inferred_study_prefix = False self.study_dir = study_dir with self._index_lock: self._locked_refresh_study_ids() self.parent_path = os.path.split(path)[0] + '/' self.git_dir = dot_git self.push_mirror_repo_path = push_mirror_repo_path if repo_nexml2json is None: try: repo_nexml2json = get_config_setting_kwargs(None, 'phylesystem', 'repo_nexml2json', **kwargs) except: pass if repo_nexml2json == None: repo_nexml2json = self.diagnose_repo_nexml2json() max_file_size = kwargs.get('max_file_size') if max_file_size is None: max_file_size = get_config_setting_kwargs(None, 'phylesystem', 'max_file_size', default=None, **kwargs) if max_file_size is not None: try: max_file_size = int(max_file_size) except: m = 'Configuration-base value of max_file_size was "{}". Expecting and integer.' m = m.format(max_file_size) raise RuntimeError(m) self.max_file_size = max_file_size self.repo_nexml2json = repo_nexml2json self._next_study_id = None self._study_counter_lock = None self._known_prefixes = None