def __repr__(self): # Calculate a hash that represents most data about the post m = hashlib.md5() # source_path modification date (to avoid reading it) m.update(utils.unicode_str(os.stat(self.source_path).st_mtime).encode('utf-8')) m.update(utils.unicode_str(json.dumps(self.meta, cls=utils.CustomEncoder, sort_keys=True)).encode('utf-8')) return '<Post: {0} {1}>'.format(self.source_path, m.hexdigest())
def __repr__(self): # Calculate a hash that represents most data about the post m = hashlib.md5() # source_path modification date (to avoid reading it) m.update(utils.unicode_str(os.stat(self.source_path).st_mtime).encode('utf-8')) clean_meta = {} for k, v in self.meta.items(): sub_meta = {} clean_meta[k] = sub_meta for kk, vv in v.items(): if vv: sub_meta[kk] = vv m.update(utils.unicode_str(json.dumps(clean_meta, cls=utils.CustomEncoder, sort_keys=True)).encode('utf-8')) return '<Post: {0!r} {1}>'.format(self.source_path, m.hexdigest())
def __repr__(self): """Provide a representation of the post object.""" # Calculate a hash that represents most data about the post m = hashlib.md5() # source_path modification date (to avoid reading it) m.update(utils.unicode_str(os.stat(self.source_path).st_mtime).encode('utf-8')) clean_meta = {} for k, v in self.meta.items(): sub_meta = {} clean_meta[k] = sub_meta for kk, vv in v.items(): if vv: sub_meta[kk] = vv m.update(utils.unicode_str(json.dumps(clean_meta, cls=utils.CustomEncoder, sort_keys=True)).encode('utf-8')) return '<Post: {0!r} {1}>'.format(self.source_path, m.hexdigest())
def _commit_and_push(self): """Commit all the files and push.""" source = self.site.config['GITHUB_SOURCE_BRANCH'] deploy = self.site.config['GITHUB_DEPLOY_BRANCH'] remote = self.site.config['GITHUB_REMOTE_NAME'] source_commit = uni_check_output(['git', 'rev-parse', source]) commit_message = ( 'Nikola auto commit.\n\n' 'Source commit: %s' 'Nikola version: %s' % (source_commit, __version__) ) output_folder = self.site.config['OUTPUT_FOLDER'] command = ['ghp-import', '-n', '-m', commit_message, '-p', '-r', remote, '-b', deploy, output_folder] self.logger.info("==> {0}".format(command)) try: subprocess.check_call(command) except subprocess.CalledProcessError as e: self.logger.error( 'Failed GitHub deployment — command {0} ' 'returned {1}'.format(e.cmd, e.returncode) ) return e.returncode self.logger.info("Successful deployment") # Store timestamp of successful deployment timestamp_path = os.path.join(self.site.config["CACHE_FOLDER"], "lastdeploy") new_deploy = datetime.utcnow() makedirs(self.site.config["CACHE_FOLDER"]) with io.open(timestamp_path, "w+", encoding="utf8") as outf: outf.write(unicode_str(new_deploy.isoformat()))
def _commit_and_push(self): """ Commit all the files and push. """ source = self.site.config['GITHUB_SOURCE_BRANCH'] deploy = self.site.config['GITHUB_DEPLOY_BRANCH'] remote = self.site.config['GITHUB_REMOTE_NAME'] source_commit = uni_check_output(['git', 'rev-parse', source]) commit_message = ( 'Nikola auto commit.\n\n' 'Source commit: %s' 'Nikola version: %s' % (source_commit, __version__) ) output_folder = self.site.config['OUTPUT_FOLDER'] command = ['ghp-import', '-n', '-m', commit_message, '-p', '-r', remote, '-b', deploy, output_folder] self.logger.info("==> {0}".format(command)) try: subprocess.check_call(command) except subprocess.CalledProcessError as e: self.logger.error( 'Failed GitHub deployment — command {0} ' 'returned {1}'.format(e.cmd, e.returncode) ) sys.exit(e.returncode) self.logger.info("Successful deployment") # Store timestamp of successful deployment timestamp_path = os.path.join(self.site.config["CACHE_FOLDER"], "lastdeploy") new_deploy = datetime.utcnow() makedirs(self.site.config["CACHE_FOLDER"]) with io.open(timestamp_path, "w+", encoding="utf8") as outf: outf.write(unicode_str(new_deploy.isoformat()))
def write_header_line(fd, header_field, header_content): """Write comment header line.""" if header_content is None: return header_content = unicode_str(header_content).replace('\n', ' ') line = '.. ' + header_field + ': ' + header_content + '\n' fd.write(line.encode('utf8'))
def write_header_line(fd, header_field, header_content): """Write comment header line.""" if header_content is None: return header_content = unicode_str(header_content).replace("\n", " ") line = ".. " + header_field + ": " + header_content + "\n" fd.write(line.encode("utf8"))
def _execute(self, command, args): self.logger = get_logger('deploy', self.site.loghandlers) # Get last successful deploy date timestamp_path = os.path.join(self.site.config['CACHE_FOLDER'], 'lastdeploy') if self.site.config['COMMENT_SYSTEM_ID'] == 'nikolademo': self.logger.warn( "\nWARNING WARNING WARNING WARNING\n" "You are deploying using the nikolademo Disqus account.\n" "That means you will not be able to moderate the comments in your own site.\n" "And is probably not what you want to do.\n" "Think about it for 5 seconds, I'll wait :-)\n\n") time.sleep(5) deploy_drafts = self.site.config.get('DEPLOY_DRAFTS', True) deploy_future = self.site.config.get('DEPLOY_FUTURE', False) undeployed_posts = [] if not (deploy_drafts and deploy_future): # Remove drafts and future posts out_dir = self.site.config['OUTPUT_FOLDER'] self.site.scan_posts() for post in self.site.timeline: if (not deploy_drafts and post.is_draft) or \ (not deploy_future and post.publish_later): remove_file(os.path.join(out_dir, post.destination_path())) remove_file(os.path.join(out_dir, post.source_path)) undeployed_posts.append(post) for command in self.site.config['DEPLOY_COMMANDS']: self.logger.info("==> {0}".format(command)) try: subprocess.check_call(command, shell=True) except subprocess.CalledProcessError as e: self.logger.error('Failed deployment — command {0} ' 'returned {1}'.format(e.cmd, e.returncode)) sys.exit(e.returncode) self.logger.info("Successful deployment") try: with io.open(timestamp_path, 'r', encoding='utf8') as inf: last_deploy = datetime.strptime(inf.read().strip(), "%Y-%m-%dT%H:%M:%S.%f") clean = False except (IOError, Exception) as e: self.logger.debug("Problem when reading `{0}`: {1}".format( timestamp_path, e)) last_deploy = datetime(1970, 1, 1) clean = True new_deploy = datetime.utcnow() self._emit_deploy_event(last_deploy, new_deploy, clean, undeployed_posts) # Store timestamp of successful deployment with io.open(timestamp_path, 'w+', encoding='utf8') as outf: outf.write(unicode_str(new_deploy.isoformat()))
def _commit_and_push(self, commit_first_line): """Commit all the files and push.""" source = self.site.config['GITHUB_SOURCE_BRANCH'] deploy = self.site.config['GITHUB_DEPLOY_BRANCH'] remote = self.site.config['GITHUB_REMOTE_NAME'] autocommit = self.site.config['GITHUB_COMMIT_SOURCE'] try: if autocommit: commit_message = ('{0}\n\n' 'Nikola version: {1}'.format( commit_first_line, __version__)) e = self._run_command(['git', 'checkout', source], True) if e != 0: self._run_command(['git', 'checkout', '-b', source]) self._run_command(['git', 'add', '.']) # Figure out if there is anything to commit e = self._run_command(['git', 'diff-index', '--quiet', 'HEAD'], True) if e != 0: self._run_command(['git', 'commit', '-am', commit_message]) else: self.logger.notice('Nothing to commit to source branch.') source_commit = uni_check_output(['git', 'rev-parse', source]) commit_message = ('{0}\n\n' 'Source commit: {1}' 'Nikola version: {2}'.format( commit_first_line, source_commit, __version__)) output_folder = self.site.config['OUTPUT_FOLDER'] command = [ 'ghp-import', '-n', '-m', commit_message, '-p', '-r', remote, '-b', deploy, output_folder ] self._run_command(command) if autocommit: self._run_command(['git', 'push', '-u', remote, source]) except SystemError as e: return e.args[0] self.logger.info("Successful deployment") # Store timestamp of successful deployment timestamp_path = os.path.join(self.site.config["CACHE_FOLDER"], "lastdeploy") new_deploy = datetime.utcnow() makedirs(self.site.config["CACHE_FOLDER"]) with io.open(timestamp_path, "w+", encoding="utf8") as outf: outf.write(unicode_str(new_deploy.isoformat()))
def _execute(self, command, args): self.logger = get_logger('deploy', self.site.loghandlers) # Get last successful deploy date timestamp_path = os.path.join(self.site.config['CACHE_FOLDER'], 'lastdeploy') if self.site.config['COMMENT_SYSTEM_ID'] == 'nikolademo': self.logger.warn("\nWARNING WARNING WARNING WARNING\n" "You are deploying using the nikolademo Disqus account.\n" "That means you will not be able to moderate the comments in your own site.\n" "And is probably not what you want to do.\n" "Think about it for 5 seconds, I'll wait :-)\n\n") time.sleep(5) deploy_drafts = self.site.config.get('DEPLOY_DRAFTS', True) deploy_future = self.site.config.get('DEPLOY_FUTURE', False) undeployed_posts = [] if not (deploy_drafts and deploy_future): # Remove drafts and future posts out_dir = self.site.config['OUTPUT_FOLDER'] self.site.scan_posts() for post in self.site.timeline: if (not deploy_drafts and post.is_draft) or \ (not deploy_future and post.publish_later): remove_file(os.path.join(out_dir, post.destination_path())) remove_file(os.path.join(out_dir, post.source_path)) undeployed_posts.append(post) for command in self.site.config['DEPLOY_COMMANDS']: self.logger.info("==> {0}".format(command)) try: subprocess.check_call(command, shell=True) except subprocess.CalledProcessError as e: self.logger.error('Failed deployment — command {0} ' 'returned {1}'.format(e.cmd, e.returncode)) sys.exit(e.returncode) self.logger.info("Successful deployment") try: with io.open(timestamp_path, 'r', encoding='utf8') as inf: last_deploy = datetime.strptime(inf.read().strip(), "%Y-%m-%dT%H:%M:%S.%f") clean = False except (IOError, Exception) as e: self.logger.debug("Problem when reading `{0}`: {1}".format(timestamp_path, e)) last_deploy = datetime(1970, 1, 1) clean = True new_deploy = datetime.utcnow() self._emit_deploy_event(last_deploy, new_deploy, clean, undeployed_posts) # Store timestamp of successful deployment with io.open(timestamp_path, 'w+', encoding='utf8') as outf: outf.write(unicode_str(new_deploy.isoformat()))
def _commit_and_push(self, commit_first_line): """Commit all the files and push.""" source = self.site.config['GITHUB_SOURCE_BRANCH'] deploy = self.site.config['GITHUB_DEPLOY_BRANCH'] remote = self.site.config['GITHUB_REMOTE_NAME'] autocommit = self.site.config['GITHUB_COMMIT_SOURCE'] try: if autocommit: commit_message = ( '{0}\n\n' 'Nikola version: {1}'.format(commit_first_line, __version__) ) e = self._run_command(['git', 'checkout', source], True) if e != 0: self._run_command(['git', 'checkout', '-b', source]) self._run_command(['git', 'add', '.']) # Figure out if there is anything to commit e = self._run_command(['git', 'diff-index', '--quiet', 'HEAD'], True) if e != 0: self._run_command(['git', 'commit', '-am', commit_message]) else: self.logger.notice('Nothing to commit to source branch.') source_commit = uni_check_output(['git', 'rev-parse', source]) commit_message = ( '{0}\n\n' 'Source commit: {1}' 'Nikola version: {2}'.format(commit_first_line, source_commit, __version__) ) output_folder = self.site.config['OUTPUT_FOLDER'] command = ['ghp-import', '-n', '-m', commit_message, '-p', '-r', remote, '-b', deploy, output_folder] self._run_command(command) if autocommit: self._run_command(['git', 'push', '-u', remote, source]) except SystemError as e: return e.args[0] self.logger.info("Successful deployment") # Store timestamp of successful deployment timestamp_path = os.path.join(self.site.config["CACHE_FOLDER"], "lastdeploy") new_deploy = datetime.utcnow() makedirs(self.site.config["CACHE_FOLDER"]) with io.open(timestamp_path, "w+", encoding="utf8") as outf: outf.write(unicode_str(new_deploy.isoformat()))
def get_id(self): """Get user ID.""" return unicode_str(self.uid)
def _execute(self, command, args): self.logger = get_logger("deploy", self.site.loghandlers) # Get last successful deploy date timestamp_path = os.path.join(self.site.config["CACHE_FOLDER"], "lastdeploy") if self.site.config["COMMENT_SYSTEM_ID"] == "nikolademo": self.logger.warn( "\nWARNING WARNING WARNING WARNING\n" "You are deploying using the nikolademo Disqus account.\n" "That means you will not be able to moderate the comments in your own site.\n" "And is probably not what you want to do.\n" "Think about it for 5 seconds, I'll wait :-)\n\n" ) time.sleep(5) deploy_drafts = self.site.config.get("DEPLOY_DRAFTS", True) deploy_future = self.site.config.get("DEPLOY_FUTURE", False) undeployed_posts = [] if not (deploy_drafts and deploy_future): # Remove drafts and future posts out_dir = self.site.config["OUTPUT_FOLDER"] self.site.scan_posts() for post in self.site.timeline: if (not deploy_drafts and post.is_draft) or (not deploy_future and post.publish_later): remove_file(os.path.join(out_dir, post.destination_path())) remove_file(os.path.join(out_dir, post.source_path)) undeployed_posts.append(post) if args: presets = args else: presets = ["default"] # test for preset existence for preset in presets: try: self.site.config["DEPLOY_COMMANDS"][preset] except: self.logger.error("No such preset: {0}".format(preset)) return 255 for preset in presets: self.logger.info("=> preset '{0}'".format(preset)) for command in self.site.config["DEPLOY_COMMANDS"][preset]: self.logger.info("==> {0}".format(command)) try: subprocess.check_call(command, shell=True) except subprocess.CalledProcessError as e: self.logger.error("Failed deployment — command {0} " "returned {1}".format(e.cmd, e.returncode)) return e.returncode self.logger.info("Successful deployment") try: with io.open(timestamp_path, "r", encoding="utf8") as inf: last_deploy = datetime.strptime(inf.read().strip(), "%Y-%m-%dT%H:%M:%S.%f") clean = False except (IOError, Exception) as e: self.logger.debug("Problem when reading `{0}`: {1}".format(timestamp_path, e)) last_deploy = datetime(1970, 1, 1) clean = True new_deploy = datetime.utcnow() self._emit_deploy_event(last_deploy, new_deploy, clean, undeployed_posts) makedirs(self.site.config["CACHE_FOLDER"]) # Store timestamp of successful deployment with io.open(timestamp_path, "w+", encoding="utf8") as outf: outf.write(unicode_str(new_deploy.isoformat()))