def parse_new_posts(self, filename_list): for filename in filename_list: post_content = self.database.get_item('posts', filename)['content'] if post_content: post_tmp = Post(filename=filename) post_tmp.parse_from_db(post_content) dir_name = os.path.join(settings.PUBLISH_DIR, post_tmp.url.strip("/\\")) file_path = os.path.join(dir_name, 'index.html') if os.path.exists(file_path): os.remove(file_path) else: logger.warning("Field to remove file: '{0}".format(post_tmp.title)) post_tmp = Post(filename=filename) file_content = open(os.path.join(filename),'r')\ .read().decode('utf8') if not post_tmp.check_illegal(file_content, filename=filename): # If the post_content is not illegal, pass it. logger.warning("This post doesn't have a correct format: '{0}".format(filename)) continue else: post_tmp.parse() self.posts.append(post_tmp) self.new_posts.append(post_tmp) post_dict = post_tmp.__dict__.copy() post_dict['pub_time'] = time.mktime( post_dict['pub_time'].timetuple()) post_dict.pop('config', None) last_mod_time = os.path.getmtime( os.path.join(filename)) post_dict_in_db = { 'last_mod_time': last_mod_time, 'content': post_dict} self.database.set_item('posts', filename, post_dict_in_db)
def parse_new_pages(self, filename_list): for filename in filename_list: try: page_content = self.database.get_item('pages', filename)['content'] page_tmp = Page(filename=filename) page_tmp.parse_from_db(page_content) dname = os.path.join(settings.PUBLISH_DIR, page_tmp.url.strip("/\\")) filepath = os.path.join(dname, 'index.html') if os.path.exists(filepath): os.remove(filepath) else: logger.warning("Field to remove file: '{0}".format( page_tmp.title)) except Exception, e: pass page_tmp = Page(filename) page_tmp.save( open(os.path.join(filename), 'r').read().decode('utf8')) self.pages.append(page_tmp) self.new_pages.append(page_tmp) page_dict = page_tmp.__dict__.copy() page_dict['pub_time'] = time.mktime( page_dict['pub_time'].timetuple()) page_dict.pop('config', None) last_mod_time = os.path.getmtime(os.path.join(filename)) page_dict_in_db = { 'last_mod_time': last_mod_time, 'content': page_dict } self.database.set_item('pages', filename, page_dict_in_db)
def load(self, source_type): if not source_type in self.db: self.db[source_type] = {} logger.warning('New key "{0}" created.'.format(source_type)) return {} else: return self.db[source_type]
def parse_new_pages(self, filename_list): for filename in filename_list: try: page_content = self.database.get_item('pages', filename)['content'] page_tmp = Page(filename=filename) page_tmp.parse_from_db(page_content) dname = os.path.join(settings.PUBLISH_DIR, page_tmp.url.strip("/\\")) filepath = os.path.join(dname, 'index.html') if os.path.exists(filepath): os.remove(filepath) else: logger.warning("Field to remove file: '{0}".format(page_tmp.title)) except Exception, e: pass page_tmp = Page(filename) page_tmp.save( open(os.path.join(filename), 'r').read().decode('utf8')) self.pages.append(page_tmp) self.new_pages.append(page_tmp) page_dict = page_tmp.__dict__.copy() page_dict['pub_time'] = time.mktime( page_dict['pub_time'].timetuple()) page_dict.pop('config', None) last_mod_time = os.path.getmtime( os.path.join(filename)) page_dict_in_db = { 'last_mod_time': last_mod_time, 'content': page_dict} self.database.set_item('pages', filename, page_dict_in_db)
def parse_removed_static_files(self, filenames, static_type): for filename in filenames: relativ_path = os.path.relpath(filename, getattr(settings, static_type.upper() + '_dir'.upper())) output_path = os.path.join(settings.PUBLISH_DIR, relativ_path) self.database.remove_item('static', filename) try: os.remove(output_path) except Exception, e: logger.warning("Field to remove static file: '{0}".format(output_path))
def remove_item(self, table, key): """ Remove the entry directly from the database based on key and table name. :param table: Name of the table acquired. :param key: Name of the key in the indicated table. :value value: Value to be set in the table. :return: The corresponding value stored in the table. """ if key in self.raw_db[table]: del self[table][key] else: logger.warning("Failed to remove from database: {0}, TYPE: {1}".format(key, table))
def parse_removed_posts(self, filename_list): for filename in filename_list: post_content = self.database.get_item('posts', filename)['content'] post_tmp = Post(filename=filename) post_tmp.parse_from_db(post_content) self.database.remove_item('posts', filename) self.removed_posts.append(post_tmp) dname = os.path.join(settings.PUBLISH_DIR, post_tmp.url.strip("/\\")) filepath = os.path.join(dname, 'index.html') try: os.remove(filepath) except Exception, e: logger.warning("Field to remove file: '{0}".format(post_tmp.title))
def init_git_repo(tar_dir): """ Creates & init empty repo in `tar_dir'. Also it creates `source', `master' branches and commit initial changes into it. """ try: tar_dir = os.path.realpath(tar_dir) repo = git.Repo.init(path=tar_dir) repo.git.commit(allow_empty=True, m="Created master") repo.git.checkout(orphan="source") repo.git.add(tar_dir) repo.git.commit(tar_dir, m="initial commit") logger.info("Git repository initialised") except git.GitCommandError, gitErr: logger.warning(gitErr)
def parse_removed_pages(self, filename_list): for filename in filename_list: page_content = self.database.get_item('pages', filename)['content'] page_tmp = Page(filename) page_tmp.parse_from_db(page_content) self.database.remove_item('pages', filename) self.removed_pages.append(page_tmp) dname = os.path.join(settings.PUBLISH_DIR, page_tmp.url.strip("/\\")) filepath = os.path.join(dname, 'index.html') try: os.remove(filepath) except: logger.warning("Field to remove file: '{0}".format( page_tmp.title))
def remove_dir(path): if not os.path.isdir(path): return # remove empty sub-folders files = os.listdir(path) for f in files: full_path = os.path.join(path, f) if os.path.isdir(full_path): remove_dir(full_path) # if folder empty, delete it files = os.listdir(path) if len(files) == 0: logger.warning("Empty folder removed: '{0}".format(path)) os.rmdir(path)
def setup_github_pages(): try: repo = git.Repo(settings.BASE_DIR) committer = git.Actor.committer(repo.config_reader()) author = committer.name + " <" + committer.email + ">" repo.git.checkout("master") copy_dir(settings.PUBLISH_DIR, settings.BASE_DIR) repo.git.add(".") repo.git.reset("_sites") #forget `_sites' folder repo.git.reset("db.json") #forget `db.json' too. message = "Site Updated at %s" %\ datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S %Z") if not repo.remotes: print "Enter the read/write url for your repository" print "(For example, '[email protected]:your_username/your_username.github.io.git)" repo_url = raw_input("Repository url: ") try: repo.create_remote("origin", repo_url) except git.GitCommandError, giterr: logger.warning(giterr) repo.git.commit(m=message, author=author) #Push `source' as well as `master' repo.git.push("origin", all=True)
def parse_new_posts(self, filename_list): for filename in filename_list: post_content = self.database.get_item('posts', filename)['content'] if post_content: post_tmp = Post(filename=filename) post_tmp.parse_from_db(post_content) dir_name = os.path.join(settings.PUBLISH_DIR, post_tmp.url.strip("/\\")) file_path = os.path.join(dir_name, 'index.html') if os.path.exists(file_path): os.remove(file_path) else: logger.warning("Field to remove file: '{0}".format( post_tmp.title)) post_tmp = Post(filename=filename) file_content = open(os.path.join(filename),'r')\ .read().decode('utf8') if not post_tmp.check_illegal(file_content, filename=filename): # If the post_content is not illegal, pass it. logger.warning( "This post doesn't have a correct format: '{0}".format( filename)) continue else: post_tmp.parse() self.posts.append(post_tmp) self.new_posts.append(post_tmp) post_dict = post_tmp.__dict__.copy() post_dict['pub_time'] = time.mktime( post_dict['pub_time'].timetuple()) post_dict.pop('config', None) last_mod_time = os.path.getmtime(os.path.join(filename)) post_dict_in_db = { 'last_mod_time': last_mod_time, 'content': post_dict } self.database.set_item('posts', filename, post_dict_in_db)
def setup_github_pages(): try: repo = git.Repo(settings.BASE_DIR) committer = git.Actor.committer(repo.config_reader()) author = committer.name + " <" + committer.email + ">" repo.git.checkout("master") copy_dir(settings.PUBLISH_DIR, settings.BASE_DIR) repo.git.add(".") repo.git.reset("_sites") #forget `_sites' folder repo.git.reset("db.json") #forget `db.json' too. message = "Site Updated at %s" %\ datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S %Z") if not repo.remotes: print "Enter the read/write url for your repository" print "(For example, '[email protected]:your_username/your_username.github.io.git)" repo_url = raw_input("Repository url: ") try: repo.create_remote("origin", repo_url) except git.GitCommandError, giterr: logger.warning(giterr) repo.git.commit(m=message, author=author) #Push `source' as well as `master' repo.git.push("origin", all=True) except Exception, ex: logger.warning(ex) else: logger.success("GitHub pages deployed") finally: #Go back to `source' repo.git.checkout("source")
def remove_item(self, source_type, filename): if filename in self.db[source_type]: del self.db[source_type][filename] else: logger.warning("Failed to remove from database: {0}, TYPE: {1}".format(filename, source_type))